3.1 控制器之ReplicaSet/ReplicationController
文章目錄
- ReplicaSet使用
- 1、創建ReplicaSet
- 2、刪除一個pod
- 3、修改pod標簽
- 4、標簽選擇器
- 4、修改ReplicaSet標簽選擇器
- 5、水平縮放pod
- 6、刪除ReplicaSet控制器
在新版本k8s中,ReplicaSet取代了ReplicationController,ReplicaSet除了滿足ReplicationController,其強大之處還在于支持標簽選擇器。所以本文主要以ReplicaSet為主。
ReplicaSet主要用來監控k8s中的pod,使其數量與副本數保持一致,當pod數量少于副本數量時會根據定義的pod模板啟動相應的pod數量,當pod數量多于副本數數量時,它將刪除多于的pod。手動增加ReplicaSet選擇的標簽類型的pod會k8s中pod數量多于副本數,ReplicaSet將刪除多于的pod。
ReplicaSet優點:
- 當集群幾點發生故障時,為故障的pod創建替代的副本;
- 可以輕松實現pod的水平伸縮
ReplicaSet使用
1、創建ReplicaSet
replica_set_example.yaml配置如下:
apiVersion: extensions/v1beta1 kind: ReplicaSet #控制器類型為ReplicaSet metadata:name: nginx-relica #ReplicaSet控制器名字為nginx-relica spec: #ReplicaSet控制器的描述replicas: 3 #pod的副本數selector: #ReplicaSet控制器的選擇器matchLabels: #ReplicaSet控制器定義標簽app: nginx-label #ReplicaSet控制器管理標簽為app=nginx-label的podtemplate: #pod的模板metadata: #pod的元數據labels: #定義pod的標簽app: nginx-label #定義pod的標簽為app=nginx-labelspec: #pod的描述containers: #定義pod中容器- name: nginx-pod #容器的名字為nginx-podimage: nginx #容器的鏡像為nginximagePullPolicy: IfNotPresent #鏡像策略,如果本地有nginx鏡像,就用本地的鏡像啟動容器,如果本地沒有默認從docker hub上獲取latest版本nginx鏡像ports: #定義容器端口- containerPort: 80 #暴露容器中80端口command: ["/bin/sh", "-c", "echo hello world > /usr/share/nginx/html/hello.html; sleep 3600"] #容器啟動后,執行command命令用create創建ReplicaSet, kubectl create -f replica_set_example.yaml,執行如下
[root@k8s-master01 sc_work]# kubectl create -f replica_set_example.yaml replicaset.extensions/nginx-relica created [root@k8s-master01 sc_work]# kubectl get replicaset NAME DESIRED CURRENT READY AGE nginx-relica 3 3 3 12s [root@k8s-master01 sc_work]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-relica-8b26w 1/1 Running 0 20s nginx-relica-grs84 1/1 Running 0 20s nginx-relica-ts6n4 1/1 Running 0 20s2、刪除一個pod
ReplicaSet管理了3個副本的pod,刪除其中一個pod后,ReplicaSet會根據副本數重新啟動一個pod,保持集群中有3個副本,如命令所示
[root@k8s-master01 sc_work]# kubectl delete pod nginx-relica-grs84 pod "nginx-relica-grs84" deleted [root@k8s-master01 sc_work]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-relica-kpq7k 1/1 Running 0 33s nginx-relica-rrsph 1/1 Running 0 2m13s nginx-relica-ts6n4 1/1 Running 0 3m5s3、修改pod標簽
通過ReplicaSet創建pod后,如果修改了pod的標簽,ReplicaSet檢查管理的pod數量少于副本數,會重新啟動pod維持副本數量的pod。為修改pod標簽前如下
[root@k8s-master01 sc_work]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-relica-kpq7k 1/1 Running 47 47h app=nginx-label nginx-relica-rrsph 1/1 Running 47 47h app=nginx-label nginx-relica-ts6n4 1/1 Running 47 47h app=nginx-label修改其中一個pod的標簽app=nginx-label修改為app=nginx-debug
[root@k8s-master01 sc_work]# kubectl label pod nginx-relica-kpq7k app=nginx-debug --overwrite pod/nginx-relica-kpq7k labeled然后再查詢pod數量
[root@k8s-master01 sc_work]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-relica-hpk5f 1/1 Running 0 15s app=nginx-label nginx-relica-kpq7k 1/1 Running 47 47h app=nginx-debug nginx-relica-rrsph 1/1 Running 47 47h app=nginx-label nginx-relica-ts6n4 1/1 Running 47 47h app=nginx-label發現又重新啟動了一個app=nginx-label的pod,始終維持在副本數量的pod。
4、標簽選擇器
前面示例已經用過了標簽選擇器,標簽選擇器主要是用來控制pod的
spec: #ReplicaSet控制器的描述replicas: 3 #pod的副本數selector: #ReplicaSet控制器的選擇器matchLabels: #ReplicaSet控制器定義標簽app: nginx-label #ReplicaSet控制器管理標簽為app=nginx-label的podselector選擇器下的matchLabels用來匹配單個標簽鍵值對形式的,相當于k8s老版本中ReplicationController控制器的作用。另外selector選擇器下的matchExpressions擁有更強大的選擇功能。例如
spec:replicas: 3selector:matchExpressions:- key: app #matchExpressions為list類型operator: Invalues:- nginx-prod #values為List類型該標簽選擇器會選擇標簽app在nginx-prod標簽的pod進行管理。
matchExpressions選擇模式強大,有如下幾種選擇模式:
- In : key指定的標簽必須與values中列舉的標簽其中一個匹配;
- Notin : key指定的標簽與values中列舉的標簽任何一個都不匹配;
- Exists : key指定的標簽名字只要與pod中標簽名字一致,pod就會被管理,不管標簽的value值是否一致,只要標簽的名字一致就可以;
- DoesNotExist : 與Exists 相反。
例如,只要創建的pod含有便簽app,不管標簽的value值如何,都會被ReplicaSet進行管理。
spec:replicas: 3selector:matchExpressions:- key: appoperator: Exists注意:上面示例中,只指定了matchExpressions中的一個標簽表達式,因為matchExpressions是list類型,可以指定多個選擇器表達式,如果指定多個表達式,多有選擇器必須與Pod匹配才能選中并管理pod。另外,如果同時指定了matchExpressions和matchLabels兩種選擇模式,則兩種選擇模式必須都匹配,pod才會被選中并進行管理。
4、修改ReplicaSet標簽選擇器
replicaset管理pod后,如果修改了ReplicaSet標簽選擇器,會使ReplicaSet放棄正在管理的pod,轉而去管理修改選擇器后的指定的pod或者重新創建指定標簽的pod。
比如用replica_set_example.yaml創建replicaset,并創建管理4個pod
執行上述命令后,nginx-relica管理著4個標簽為nginx-prod的pod,下面修改一下nginx-relica的選擇器,使其管理4個標簽為nginx-debug的pod
kubectl edit rs nginx-relica #編輯nginx-relica ……省略 spec:replicas: 4selector:matchLabels:app: nginx-debug #標簽選擇器從nginx-prod修改為nginx-debugtemplate:metadata:creationTimestamp: nulllabels:app: nginx-debug #pod的模板標簽從nginx-prod修改為nginx-debugspec:……省略編輯完replicaset后,重新查看pod情況,發現replicaset重新管理了4個標簽為app=nginx-debug的pod,但原來的app=nginx-prod的pod并沒有自動刪除,如果手動刪除后,不會再自動重建,如果手動刪除標簽為app=nginx-debug的pod,replicaset會自動進行重新重建標簽為app=nginx-debug的pod,使其數量達到副本數。
[root@k8s-master01 sc_work]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-relica-4wcvq 1/1 Running 0 4m26s app=nginx-prod nginx-relica-5q59d 1/1 Running 0 4m26s app=nginx-prod nginx-relica-5wspn 1/1 Running 0 21s app=nginx-debug nginx-relica-795dg 1/1 Running 0 21s app=nginx-debug nginx-relica-cp2fx 1/1 Running 0 21s app=nginx-debug nginx-relica-nx8d6 1/1 Running 0 21s app=nginx-debug nginx-relica-x694n 1/1 Running 0 4m26s app=nginx-prod nginx-relica-ztp6p 1/1 Running 0 4m26s app=nginx-prod5、水平縮放pod
集群中,有時訪問量增加,需要水平擴展pod數量,或者晚上訪問量小,縮小pod數量,節省資源。
下面通過ReplicaSet創建3個pod,然后再水平擴展到4個pod
6、刪除ReplicaSet控制器
- 直接刪除ReplicaSet后也會連帶刪除ReplicaSet控制器管理的Pod
- 刪除ReplicaSet但保留pod運行
注:ReplicaSet可以簡寫為rs,比如
kubectl get rs參考《kubernetes in action》
總結
以上是生活随笔為你收集整理的3.1 控制器之ReplicaSet/ReplicationController的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搜狗安装输入法操作
- 下一篇: 音频单元组件服务参考(Audio Uni