k8s(6)-滾動更新

  

   用戶但願應用程序始終可用,開發人員應該天天屢次部署新版本的應用程序。在Kubernetes中,這是經過滾動更新完成的。滾動更新容許經過使用新的實例逐步更新Pods實例來實現部署的更新,從而實現零停機。新的Pod將在具備可用資源的節點上進行調度。bash

在上一個模塊中,咱們將應用程序擴展爲運行多個實例。這是執行更新而不影響應用程序可用性的要求。默認狀況下,更新期間可用的最大Pod數和能夠建立的新Pod的最大數量是一個。兩個選項均可以配置爲數字或百分比(Pod)。在Kubernetes中,更新是版本化的,任何部署更新均可以恢復爲之前的(穩定)版本。mvc

 

 

 

 版本記錄:每次對Deployment的操做,都能保存下來,給予後續可能的回滾使用。curl

 

 1. 查看正在運行的Pods
[root@master ~]# kubectl get pods -o wide
NAME                                   READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-7f56b875dc-6zbjt   1/1     Running   0          5h31m   10.244.0.15   master   <none>           <none>
kubernetes-bootcamp-7f56b875dc-cp7cq   1/1     Running   0          19m     10.244.0.16   master   <none>           <none>
kubernetes-bootcamp-7f56b875dc-n4wkn   1/1     Running   0          19m     10.244.0.17   master   <none>           <none>
kubernetes-bootcamp-7f56b875dc-pmvck   1/1     Running   0          19m     10.244.0.18   master   <none>           <none>

  

 2. 使用新的鏡像更新程序
[root@master ~]# kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.extensions/kubernetes-bootcamp image updated

  

3. 確認更新完成
[root@master ~]# kubectl rollout status deployment/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out

  

4. 查看Pod的變化

能夠看見Pod的IP都變成新的了ide

[root@master ~]# kubectl get pods -l run=kubernetes-bootcamp -o wide 
NAME                                   READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-5bf4d5689b-5f68t   1/1     Running   0          3m44s   10.244.0.20   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-c4zgm   1/1     Running   0          3m41s   10.244.0.22   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-c99hl   1/1     Running   0          3m45s   10.244.0.19   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-rpff2   1/1     Running   0          3m41s   10.244.0.21   master   <none>           <none>

  

5. 回滾應用

第一種狀況:google

   咱們這裏假設更新錯了一個不存在的鏡像url

kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
deployment.extensions/kubernetes-bootcamp image updated

  

查看Podsspa

[root@master ~]# kubectl get pods -l run=kubernetes-bootcamp -o wide 
NAME                                   READY   STATUS             RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-597cfc5b76-jtmrr   0/1     ImagePullBackOff   0          51s   10.244.0.23   master   <none>           <none>
kubernetes-bootcamp-597cfc5b76-pslgq   0/1     ImagePullBackOff   0          51s   10.244.0.24   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-5f68t   1/1     Running            0          23m   10.244.0.20   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-c99hl   1/1     Running            0          23m   10.244.0.19   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-rpff2   1/1     Running            0          23m   10.244.0.21   master   <none>           <none>

  

查看部署blog

[root@master ~]# kubectl get deployments  -l run=kubernetes-bootcamp
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   3/4     2            3           5h58m

  

回滾資源

[root@master ~]# kubectl rollout undo deployments/kubernetes-bootcamp
deployment.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 GATES
kubernetes-bootcamp-5bf4d5689b-5f68t   1/1     Running   0          29m   10.244.0.20   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-c99hl   1/1     Running   0          29m   10.244.0.19   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-jrrh9   1/1     Running   0          70s   10.244.0.25   master   <none>           <none>
kubernetes-bootcamp-5bf4d5689b-rpff2   1/1     Running   0          28m   10.244.0.21   master   <none>           <none>

  能夠看出兩個錯誤的Pods已經不見了,原來的10.244.0.22的pods被新的Pods(10.244.0.25)取代。

 

 第二種狀況,咱們所有更新完成,是否還能夠回滾

[root@master ~]# curl http://192.168.0.10:31238
Hello 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:31238
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7f56b875dc-kvxn5 | v=1

[root@master ~]# kubectl rollout undo deployments/kubernetes-bootcamp
deployment.extensions/kubernetes-bootcamp rolled back

[root@master ~]# curl http://192.168.0.10:31238
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-rfhqx | v=2

  證實也是能夠回滾前一個版本的。

相關文章
相關標籤/搜索