用户希望应用程序始终可用,开发人员应该每天多次部署新版本的应用程序。在Kubernetes中,这是通过滚动更新完成的。滚动更新允许通过使用新的实例逐步更新Pods实例来实现部署的更新,从而实现零停机。新的Pod将在具有可用资源的节点上进行调度。
在上一个模块中,我们将应用程序扩展为运行多个实例。这是执行更新而不影响应用程序可用性的要求。默认情况下,更新期间可用的最大Pod数和可以创建的新Pod的最大数量是一个。两个选项都可以配置为数字或百分比(Pod)。在Kubernetes中,更新是版本化的,任何部署更新都可以恢复为以前的(稳定)版本。
版本记录:每次对Deployment的操作,都能保存下来,给予后续可能的回滚使用。
1. 查看正在运行的Pods
[root@master ~]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESkubernetes-bootcamp-7f56b875dc-6zbjt 1/1 Running 0 5h31m 10.244.0.15 masterkubernetes-bootcamp-7f56b875dc-cp7cq 1/1 Running 0 19m 10.244.0.16 master kubernetes-bootcamp-7f56b875dc-n4wkn 1/1 Running 0 19m 10.244.0.17 master kubernetes-bootcamp-7f56b875dc-pmvck 1/1 Running 0 19m 10.244.0.18 master
2. 使用新的镜像更新程序
[root@master ~]# kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2deployment.extensions/kubernetes-bootcamp image updated
3. 确认更新完成
[root@master ~]# kubectl rollout status deployment/kubernetes-bootcampdeployment "kubernetes-bootcamp" successfully rolled out
4. 查看Pod的变化
可以看见Pod的IP都变成新的了
[root@master ~]# kubectl get pods -l run=kubernetes-bootcamp -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESkubernetes-bootcamp-5bf4d5689b-5f68t 1/1 Running 0 3m44s 10.244.0.20 masterkubernetes-bootcamp-5bf4d5689b-c4zgm 1/1 Running 0 3m41s 10.244.0.22 master kubernetes-bootcamp-5bf4d5689b-c99hl 1/1 Running 0 3m45s 10.244.0.19 master kubernetes-bootcamp-5bf4d5689b-rpff2 1/1 Running 0 3m41s 10.244.0.21 master
5. 回滚应用
第一种情况:
我们这里假设更新错了一个不存在的镜像
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10deployment.extensions/kubernetes-bootcamp image updated
查看Pods
[root@master ~]# kubectl get pods -l run=kubernetes-bootcamp -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESkubernetes-bootcamp-597cfc5b76-jtmrr 0/1 ImagePullBackOff 0 51s 10.244.0.23 masterkubernetes-bootcamp-597cfc5b76-pslgq 0/1 ImagePullBackOff 0 51s 10.244.0.24 master kubernetes-bootcamp-5bf4d5689b-5f68t 1/1 Running 0 23m 10.244.0.20 master kubernetes-bootcamp-5bf4d5689b-c99hl 1/1 Running 0 23m 10.244.0.19 master kubernetes-bootcamp-5bf4d5689b-rpff2 1/1 Running 0 23m 10.244.0.21 master
查看部署
[root@master ~]# kubectl get deployments -l run=kubernetes-bootcampNAME READY UP-TO-DATE AVAILABLE AGEkubernetes-bootcamp 3/4 2 3 5h58m
回滚
[root@master ~]# kubectl rollout undo deployments/kubernetes-bootcampdeployment.extensions/kubernetes-bootcamp rolled back
查看pods
[root@master ~]# kubectl get pods -l run=kubernetes-bootcamp -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESkubernetes-bootcamp-5bf4d5689b-5f68t 1/1 Running 0 29m 10.244.0.20 masterkubernetes-bootcamp-5bf4d5689b-c99hl 1/1 Running 0 29m 10.244.0.19 master kubernetes-bootcamp-5bf4d5689b-jrrh9 1/1 Running 0 70s 10.244.0.25 master kubernetes-bootcamp-5bf4d5689b-rpff2 1/1 Running 0 28m 10.244.0.21 master
可以看出两个错误的Pods已经不见了,原来的10.244.0.22的pods被新的Pods(10.244.0.25)取代。
第二种情况,我们全部更新完成,是否还可以回滚
[root@master ~]# curl http://192.168.0.10:31238Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-h4wsw | v=2[root@master ~]# kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v1 deployment.extensions/kubernetes-bootcamp image updated[root@master ~]# curl http://192.168.0.10:31238Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7f56b875dc-kvxn5 | v=1[root@master ~]# kubectl rollout undo deployments/kubernetes-bootcampdeployment.extensions/kubernetes-bootcamp rolled back[root@master ~]# curl http://192.168.0.10:31238Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-rfhqx | v=2
证明也是可以回滚前一个版本的。