本文主要是介绍Kubernetes基础:滚动升级:RC之rolling-update示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
虽然RC已经是过时的特性,为了和其他特性进行比较,这篇文章还是将早期最为广泛使用的特性之一进行展开,使用一个具体的示例来介绍在RC中进行滚动升级的方式。
滚动演示场景说明
使用busybox的镜像,执行sleep 1000启动的RC作为初始状态(设定replicas为3),然后修改启动参数进行滚动升级,整体要验证的滚动过程如下图所示
RC初始版本
使用如下YAML文件的RC,busybox容器sleep 1000
[root@host131 RollingUpdate]# cat v1.yaml
---
apiVersion: v1
kind: ReplicationController
metadata:name: busybox-rc
spec:replicas: 3selector:app: busybox-v1template:metadata:labels:app: busybox-v1spec:containers:- name: busybox-hostimage: busybox:latestcommand: ["sleep"]args: ["1000"]
...
[root@host131 RollingUpdate]#
- 创建并确认结果
[root@host131 RollingUpdate]# kubectl create -f v1.yaml
replicationcontroller/busybox-rc created
[root@host131 RollingUpdate]#
[root@host131 RollingUpdate]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
busybox-rc 3 3 3 19s busybox-host busybox:latest app=busybox-v1
[root@host131 RollingUpdate]#
[root@host131 RollingUpdate]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox-rc-mfjk4 1/1 Running 0 24s 10.254.152.7 192.168.163.131 <none> <none>
busybox-rc-q5ltd 1/1 Running 0 24s 10.254.152.6 192.168.163.131 <none> <none>
busybox-rc-sz6pz 1/1 Running 0 24s 10.254.152.5 192.168.163.131 <none> <none>
[root@host131 RollingUpdate]#
[root@host131 RollingUpdate]# kubectl exec -it busybox-rc-mfjk4 sh
/ # ps -ef
PID USER TIME COMMAND1 root 0:00 sleep 10006 root 0:00 sh11 root 0:00 ps -ef
/ #
rolling-update注意事项
rolling-update在1.11版开始过时的,在RC中使用rolling-update进行滚动升级的注意事项,这里具体示例我们希望只是改一下RC启动的Pod模版中的sleep命令的参数,从1000到10000,这样也比较方便确认结果。
首先使用v1.yaml生成v2.yaml,只修改args从1000到10000,执行滚动升级命令:
[root@host131 RollingUpdate]# kubectl rolling-update busybox-rc -f v2.yaml
Command "rolling-update" is deprecated, use "rollout" instead
error: v2.yaml cannot have the same name as the existing ReplicationController busybox-rc
[root@host131 RollingUpdate]#
从错误提示信息中可以看到如下注意事项:
*注意事项1: 滚动后的RC的名字需要不同
然后将v2.yaml的rc名称改为busybox-rc-v2,以区别于现在的busybox-rc,然后执行滚动升级命令:
[root@host131 RollingUpdate]# vi v2.yaml
[root@host131 RollingUpdate]# kubectl rolling-update busybox-rc -f v2.yaml
Command "rolling-update" is deprecated, use "rollout" instead
error: v2.yaml must specify a matching key with non-equal value in Selector for busybox-rc
[root@host131 RollingUpdate]#
从错误提示信息中可以看到如下注意事项:
*注意事项2: 滚动后的RC的Label需要不同
修改后的v2.yaml如下所示:
[root@host131 RollingUpdate]# cat v2.yaml
---
apiVersion: v1
kind: ReplicationController
metadata:name: busybox-rc-v2
spec:replicas: 3selector:app: busybox-v2template:metadata:labels:app: busybox-v2spec:containers:- name: busybox-hostimage: busybox:latestcommand: ["sleep"]args: ["10000"]
...
[root@host131 RollingUpdate]#
滚动升级
执行日志如下所示:
[root@host131 RollingUpdate]# kubectl rolling-update busybox-rc -f v2.yaml
Command "rolling-update" is deprecated, use "rollout" instead
Created busybox-rc-v2
Scaling up busybox-rc-v2 from 0 to 3, scaling down busybox-rc from 3 to 0 (keep 3 pods available, don't exceed 4 pods)
Scaling busybox-rc-v2 up to 1
Scaling busybox-rc down to 2
Scaling busybox-rc-v2 up to 2
Scaling busybox-rc down to 1
Scaling busybox-rc-v2 up to 3
Scaling busybox-rc down to 0
Update succeeded. Deleting old controller: busybox-rc
Renaming busybox-rc-v2 to busybox-rc
replicationcontroller/busybox-rc rolling updated
[root@host131 RollingUpdate]#
从日志中可以清晰地看到滚动的过程,在另外的终端结合上述执行,可以确认到如下的滚动升级的中间状态,RC生成了两个,滚动前和滚动后的同时存在,另外Pod启动和停止的方式是启动一个新的,启动成功之后,停止旧的,最大可用的Pod不超过三个,同样的方式在后续的Deployment中也是相同的。
首先确认RC,新旧RC同时并存,需要注意的是此时Pod数量均为1,因为刚开始滚动第一个,可以结合下图的第一个箭头进行理解
[root@host131 prometheus]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
busybox-rc 3 3 3 104s busybox-host busybox:latest app=busybox-v1
busybox-rc-v2 1 1 1 11s busybox-host busybox:latest app=busybox-v2
[root@host131 prometheus]#
Pod状况如下所示,此时4个Pod同时可用
[root@host131 prometheus]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox-rc-mfjk4 1/1 Running 0 2m3s 10.254.152.7 192.168.163.131 <none> <none>
busybox-rc-q5ltd 1/1 Running 0 2m3s 10.254.152.6 192.168.163.131 <none> <none>
busybox-rc-sz6pz 1/1 Running 0 2m3s 10.254.152.5 192.168.163.131 <none> <none>
busybox-rc-v2-d8p4l 1/1 Running 0 29s 10.254.152.8 192.168.163.131 <none> <none>
[root@host131 prometheus]#
验证新启动的Pod(sleep 10000)是滚动后的版本
[root@host131 prometheus]# kubectl exec -it busybox-rc-v2-d8p4l sh
/ # ps -ef
PID USER TIME COMMAND1 root 0:00 sleep 100006 root 0:00 sh11 root 0:00 ps -ef
/ #
随着逐渐的滚动,开始第2个Pod的启动和停止
[root@host131 prometheus]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox-rc-mfjk4 1/1 Running 0 2m51s 10.254.152.7 192.168.163.131 <none> <none>
busybox-rc-q5ltd 1/1 Running 0 2m51s 10.254.152.6 192.168.163.131 <none> <none>
busybox-rc-sz6pz 1/1 Terminating 0 2m51s 10.254.152.5 192.168.163.131 <none> <none>
busybox-rc-v2-d8p4l 1/1 Running 0 77s 10.254.152.8 192.168.163.131 <none> <none>
busybox-rc-v2-ffvm6 1/1 Running 0 11s 10.254.152.9 192.168.163.131 <none> <none>
[root@host131 prometheus]#
达到第2个Pod滚动的稳定状态后
[root@host131 prometheus]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox-rc-mfjk4 1/1 Running 0 3m25s 10.254.152.7 192.168.163.131 <none> <none>
busybox-rc-q5ltd 1/1 Running 0 3m25s 10.254.152.6 192.168.163.131 <none> <none>
busybox-rc-v2-d8p4l 1/1 Running 0 111s 10.254.152.8 192.168.163.131 <none> <none>
busybox-rc-v2-ffvm6 1/1 Running 0 45s 10.254.152.9 192.168.163.131 <none> <none>
[root@host131 prometheus]#
所有的Pod全部滚动升级成功之后,Pod最终为三个副本,旧的RC也被删除
[root@host131 prometheus]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox-rc-v2-6mqm6 1/1 Running 0 3m14s 10.254.152.5 192.168.163.131 <none> <none>
busybox-rc-v2-d8p4l 1/1 Running 0 5m26s 10.254.152.8 192.168.163.131 <none> <none>
busybox-rc-v2-ffvm6 1/1 Running 0 4m20s 10.254.152.9 192.168.163.131 <none> <none>
[root@host131 prometheus]#
[root@host131 prometheus]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
busybox-rc 3 3 3 2m10s busybox-host busybox:latest app=busybox-v2
[root@host131 prometheus]#
总结
滚动升级的特性非常适合进行持续部署中的灰度发布或者金丝雀发布实践,或者说这就是容器化的持续部署功能特性的固化,在后续将会就此进行进一步的展开说明。
这篇关于Kubernetes基础:滚动升级:RC之rolling-update示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!