控制器
在Kubernetes(簡稱K8s)中,控制器是負責管理和維護集群中資源狀態(tài)的組件??刂破鞅O(jiān)視集群中的對象,并根據(jù)它們的預期狀態(tài)來采取行動,以確保系統(tǒng)的期望狀態(tài)與實際狀態(tài)保持一致。
對于自主式pod來說,刪除pod之后pod就直接消失了,如果因為一些誤操作或pod錯誤退出,就不會自動恢復,這個時候就需要使用k8s的控制器,使用控制器創(chuàng)建的pod可以進行故障的恢復與自愈,并且也可以做資源調(diào)度、配置管理等內(nèi)容
ReplicaSet控制器
ReplicaSet是Kubernetes中的一種控制器,用于確保一組Pod副本的運行。它定義了所需的Pod副本數(shù)量,并監(jiān)控它們的運行狀態(tài),以確保始終有指定數(shù)量的副本在運行。
用的不多,大多數(shù)環(huán)境中使用deployment資源,deployment的功能包括ReplicaSet
定義ReplicaSet時,需要定義要創(chuàng)建的pod的模板,相當于pod做了多份的負載均衡
以下是一個replicatest的示例文件
#查看幫助
kubectl explain rs
apiVersion <string>
kind <string>
metadata <Object>
spec <Object>
status <Object>
kubectl explain rs.spec
minReadySeconds
replicas
selector
template
kubectl explain rs.spec.template.spec #與pod的spec相同
cat > rs.yaml << EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rstest
namespace: default
spec:
replicas: 5 #副本數(shù)
selector: #篩選器,與pod關聯(lián)
matchLabels:
user: ws #匹配標簽user=ws的pod
template: #pod模板
metadata:
labels: #pod標簽
user: ws
spec:
containers:
- name: test1
image: docker.io/library/nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
startupProbe: #啟動探測
periodSeconds: 5
initialDelaySeconds: 10
timeoutSeconds: 5
httpGet:
scheme: HTTP
port: 80
path: /
EOF
kubectl apply -f rs.yaml
kubectl get pods -w | grep Running
rstest-2qbrw 1/1 Running 0 2m34s
rstest-6j9p6 1/1 Running 0 2m34s
rstest-ltpn5 1/1 Running 0 2m34s
rstest-z7h27 1/1 Running 0 2m34s
rstest-z8cnf 1/1 Running 0 2m34s
#desired期望3,current當前啟動5,ready就緒5
kubectl get rs
NAME DESIRED CURRENT READY AGE
rstest 5 5 5 2m56s、
#退出其中一個pod,刪除或異常退出都可以
kubectl delete pods rstest-hrvtj
#創(chuàng)建了一個新pod
kubectl get pods -w | grep Running
rstest-6j9p6 1/1 Running 0 6m41s
rstest-hrvtj 1/1 Running 0 32s
rstest-ltpn5 1/1 Running 0 6m41s
rstest-z7h27 1/1 Running 0 6m41s
rstest-z8cnf 1/1 Running 0 6m41s
rstest-rmxcq 0/1 Running 0 1s
rstest-rmxcq 0/1 Running 0 10s
rstest-rmxcq 1/1 Running 0 10s
擴容與縮容、更新鏡像
#擴容與縮容
#修改yaml文件
...
? replicas: 6 ?#副本數(shù)
...
kubectl apply -f rs.yaml
#創(chuàng)建了一個新pod
kubectl get pods -w | grep Running
rstest-6j9p6 ? 1/1 ? ? Running ? 0 ? ? ? ? ?12m
rstest-ltpn5 ? 1/1 ? ? Running ? 0 ? ? ? ? ?12m
rstest-rmxcq ? 1/1 ? ? Running ? 0 ? ? ? ? ?5m29s
rstest-z7h27 ? 1/1 ? ? Running ? 0 ? ? ? ? ?12m
rstest-z8cnf ? 1/1 ? ? Running ? 0 ? ? ? ? ?12m
rstest-zwgnl ? 0/1 ? ? Running ? ? ? ? ? ? 0 ? ? ? ? ?1s
rstest-zwgnl ? 0/1 ? ? Running ? ? ? ? ? ? 0 ? ? ? ? ?10s
rstest-zwgnl ? 1/1 ? ? Running ? ? ? ? ? ? 0 ? ? ? ? ?10s
#修改yaml文件
...
? replicas: 2 ?#副本數(shù)
...
kubectl apply -f rs.yaml
#全部被關閉,只剩倆
?文章來源:http://www.zghlxwxcb.cn/news/detail-812264.html
#手動更新鏡像,ReplicaSet無法實現(xiàn)滾動更新
#原本狀態(tài)
curl 10.10.179.34:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="<http://nginx.org/>">nginx.org</a>.<br/>
Commercial support is available at
<a href="<http://nginx.com/>">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#修改yaml文件
...
image: docker.io/library/tomcat
...
kubectl apply -f rs.yaml
#刪除原有pods,因為replicatest無法實現(xiàn)滾動更新,而deployment可以
kubectl delete pods rstest-6j9p6
kubectl delete pods rstest-z8cnf
#因為某些原因沒法起來,不過問題不大,注釋掉探活部分就起來了
kubectl get pods -w
NAME READY STATUS RESTARTS AGE
rstest-c2m98 1/1 Running 0 3m54s
rstest-xkqnl 1/1 Running 0 3m54s
#當前狀態(tài),說明當前鏡像已經(jīng)被修改
curl 10.10.234.124:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.1.17</h3></body></html>[
#清理
kubectl delete -f rs.yaml
文章來源地址http://www.zghlxwxcb.cn/news/detail-812264.html
到了這里,關于K8s(五)ReplicaSet控制器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!