kubernetes(6):rc副本控制器—故障自愈和弹性伸缩
rc副本控制器—故障自愈和弹性伸缩
https://blog.51cto.com/forall/2048671
https://jimmysong.io/kubernetes-handbook/concepts/replicaset.html
1 rc简介
Replication Controller简称RC,它能够保证Pod持续运行,并且在任何时候都有指定数量的Pod副本,在此基础上提供一些高级特性,比如滚动升级和弹性伸缩。
用来确保容器应用的副本数始终保持在用户定义的副本数,即如果有容器异常退出,会自动创建新的Pod来替代;而如果异常多出来的容器也会自动回收。
在新版本的Kubernetes中建议使用ReplicaSet来取代ReplicationController。ReplicaSet跟ReplicationController没有本质的不同,只是名字不一样,并且ReplicaSet支持集合式的selector。
虽然ReplicaSet可以独立使用,但一般还是建议使用 Deployment 来自动管理ReplicaSet,这样就无需担心跟其他机制的不兼容问题(比如ReplicaSet不支持rolling-update但Deployment支持)。
主要的功能如下:
- 确保pod数量:它会确保Kubernetes中有指定数量的Pod在运行。如果少于指定数量的pod,Replication Controller会创建新的,反之则会删除掉多余的以保证Pod数量不变。
- 确保pod健康:当pod不健康,运行出错或者无法提供服务时,Replication Controller也会杀死不健康的pod,重新创建新的。
- 弹性伸缩 :在业务高峰或者低峰期的时候,可以通过Replication Controller动态的调整pod的数量来提高资源的利用率。同时,配置相应的监控功能(Hroizontal Pod Autoscaler),会定时自动从监控平台获取Replication Controller关联pod的整体资源使用情况,做到自动伸缩。
- 滚动升级:滚动升级为一种平滑的升级方式,通过逐步替换的策略,保证整体系统的稳定,在初始化升级的时候就可以及时发现和解决问题,避免问题不断扩大。
2 测试yaml文件
[root@k8s-master k8s]# cat myweb-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: myweb #rc的名字 spec: replicas: 3 #副本数,即集群数目 selector: app: myweb #选择器,如果容器里是app-nginx归它管理 template: #模板 metadata: labels: #标签 app: myweb spec: containers: - name: nginx image: 192.168.0.136:5000/nginx:latest ports: - containerPort: 80
3 创建rc并查看状态
[root@k8s-master k8s]# kubectl create -f myweb-rc.yaml replicationcontroller "myweb" created [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myweb-d5z6j 1/1 Running 0 2m 172.16.73.3 k8s-node-1 myweb-rxgpc 1/1 Running 0 2m 172.16.14.2 k8s-node-2 myweb-v0p4l 1/1 Running 0 2m 172.16.14.3 k8s-node-2 test1 2/2 Running 0 22m 172.16.73.2 k8s-node-1 [root@k8s-master k8s]#
4 查询rc
[root@k8s-master k8s]# kubectl get rc NAME DESIRED CURRENT READY AGE myweb 3 3 3 5m [root@k8s-master k8s]# kubectl get rc myweb NAME DESIRED CURRENT READY AGE myweb 3 3 3 5m [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl describe rc myweb Name: myweb Namespace: default Image(s): 192.168.0.136:5000/nginx:latest Selector: app=myweb Labels: app=myweb Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed No volumes. Events: FirstSeen LastSeen Count From SubObjectPath Type Reason Message --------- -------- ----- ---- ------------- -------- ------ ------- 6m 6m 1 {replication-controller } Normal SuccessfulCreate Created pod: myweb-v0p4l 6m 6m 1 {replication-controller } Normal SuccessfulCreate Created pod: myweb-rxgpc 6m 6m 1 {replication-controller } Normal SuccessfulCreate Created pod: myweb-d5z6j 2m 2m 1 {replication-controller } Normal SuccessfulCreate Created pod: myweb-gjwr5 2m 2m 1 {replication-controller } Normal SuccessfulCreate Created pod: myweb-0cx7z [root@k8s-master k8s]#
5 故障自愈(多次删除自动新启动容器)
[root@k8s-master k8s]# kubectl delete pod myweb-d5z6j pod "myweb-d5z6j" deleted [root@k8s-master k8s]# kubectl delete pod myweb-rxgpc pod "myweb-rxgpc" deleted [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myweb-0cx7z 0/1 ContainerCreating 0 4s <none> k8s-node-2 myweb-d5z6j 1/1 Terminating 0 4m 172.16.73.3 k8s-node-1 myweb-gjwr5 0/1 ContainerCreating 0 11s <none> k8s-node-1 myweb-rxgpc 1/1 Terminating 0 4m 172.16.14.2 k8s-node-2 myweb-v0p4l 1/1 Running 0 4m 172.16.14.3 k8s-node-2 test1 2/2 Running 0 23m 172.16.73.2 k8s-node-1 [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myweb-0cx7z 1/1 Running 0 19s 172.16.14.4 k8s-node-2 myweb-d5z6j 1/1 Terminating 0 4m 172.16.73.3 k8s-node-1 myweb-gjwr5 0/1 ContainerCreating 0 26s <none> k8s-node-1 myweb-v0p4l 1/1 Running 0 4m 172.16.14.3 k8s-node-2 test1 2/2 Running 0 24m 172.16.73.2 k8s-node-1 [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myweb-0cx7z 1/1 Running 0 24s 172.16.14.4 k8s-node-2 myweb-gjwr5 1/1 Running 0 32s 172.16.73.4 k8s-node-1 myweb-v0p4l 1/1 Running 0 4m 172.16.14.3 k8s-node-2 test1 2/2 Running 0 24m 172.16.73.2 k8s-node-1 [root@k8s-master k8s]#
6 删除rc,pod也被删除
--cascade=false只删除rc保留创建的pod
[root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myweb-0cx7z 1/1 Running 0 24s 172.16.14.4 k8s-node-2 myweb-gjwr5 1/1 Running 0 32s 172.16.73.4 k8s-node-1 myweb-v0p4l 1/1 Running 0 4m 172.16.14.3 k8s-node-2 test1 2/2 Running 0 24m 172.16.73.2 k8s-node-1 [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl delete rc myweb replicationcontroller "myweb" deleted [root@k8s-master k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE test1 2/2 Running 0 27m 172.16.73.2 k8s-node-1 [root@k8s-master k8s]#
7 弹性伸缩—手动
方式一kubectl scale rc 【rc-name】 --replicas=1
[root@k8s-master k8s]# kubectl get pods -o wide --selector app=myweb NAME READY STATUS RESTARTS AGE IP NODE myweb-93168 1/1 Running 0 1m 172.16.14.3 k8s-node-2 myweb-fwrn6 1/1 Running 0 1m 172.16.73.3 k8s-node-1 myweb-q0ndl 1/1 Running 0 1m 172.16.14.2 k8s-node-2 [root@k8s-master k8s]# kubectl scale rc myweb --replicas=5 replicationcontroller "myweb" scaled [root@k8s-master k8s]# [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl get pods -o wide --selector app=myweb NAME READY STATUS RESTARTS AGE IP NODE myweb-93168 1/1 Running 0 4m 172.16.14.3 k8s-node-2 myweb-fwrn6 1/1 Running 0 4m 172.16.73.3 k8s-node-1 myweb-n56xg 1/1 Running 0 1m 172.16.14.4 k8s-node-2 myweb-q0ndl 1/1 Running 0 4m 172.16.14.2 k8s-node-2 myweb-v6f0m 1/1 Running 0 1m 172.16.73.4 k8s-node-1 [root@k8s-master k8s]# [root@k8s-master k8s]# [root@k8s-master k8s]# kubectl scale rc myweb --replicas=2 replicationcontroller "myweb" scaled [root@k8s-master k8s]# kubectl get pods -o wide --selector app=myweb NAME READY STATUS RESTARTS AGE IP NODE myweb-fwrn6 1/1 Running 0 4m 172.16.73.3 k8s-node-1 myweb-q0ndl 1/1 Running 0 4m 172.16.14.2 k8s-node-2 [root@k8s-master k8s]#
方式二 手动编辑 kubectl edit rc myweb
设置 replicas
8 弹性伸缩—自动
Kubernetes有一个HPA(Horizontal Pod Autoscaler)的东东,可以实现基于CPU使用率的Pod自动伸缩的功能。 HPA基于Master Node上的kube-controller-manager服务启动参数--horizontal-pod-autoscaler-sync-period
定义的时长(默认为30秒),周期性的检测Pod的CPU使用率(需要事先安装heapster)。使用kubeadm初始化的集群,如果需要设置--horizontal-pod-autoscaler-sync-period
可以在Master Node上的/etc/kubernetes/manifests/kube-controller-manager.json中修改。
这个在水平后面伸缩,再写吧!