Kubernetes的Rolling Update實戰

一.背景

隨着Kubernetes的進一步深刻使用,咱們愈來愈體會到它給咱們的工做帶來的高效與便利。Rolling Update是Kubernetes系統中的一個強大的功能,可以爲咱們的運維工做帶來極大的便利。git

二.步驟

2.1 部署最初始版本Deployment。

初始Deployment的YAML以下:github

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: deployment-rollout
spec:
  replicas: 4
  template:
    metadata:
      labels:
        test: httpd
    spec:
      containers:
      - name: httpd-rollout-test
        image: httpd:2.2.31
        ports:
        - containerPort: 80

執行命令:api

[root@k8s-m ~]# kubectl get deployment -o wide
NAME                 DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS           IMAGES         SELECTOR
deployment-rollout   4         4         4            4           107s   httpd-rollout-test   httpd:2.2.31   test=httpd
[root@k8s-m ~]# kubectl get replicaset -o wide
NAME                            DESIRED   CURRENT   READY   AGE    CONTAINERS           IMAGES         SELECTOR
deployment-rollout-5fb9c69c5c   4         4         4       116s   httpd-rollout-test   httpd:2.2.31   pod-template-hash=5fb9c69c5c,test=httpd

2.2 將初始版本升級。

升級到2.4.31,YAML以下:bash

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: deployment-rollout
spec:
  revisionHistoryLimit: 10
  replicas: 4
  template:
    metadata:
      labels:
        test: httpd
    spec:
      containers:
      - name: httpd-rollout-test
        image: httpd:2.4.33
        ports:
        - containerPort: 80

執行命令:app

[root@k8s-m ~]# kubectl apply -f Httpd-Deployment-rollout-v1.yaml
deployment.apps/deployment-rollout configured
[root@k8s-m ~]# kubectl get deployment -o wide
NAME                 DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS           IMAGES         SELECTOR
deployment-rollout   4         5         2            3           3m57s   httpd-rollout-test   httpd:2.4.33   test=httpd
[root@k8s-m ~]# kubectl get replicaset -o wide
NAME                            DESIRED   CURRENT   READY   AGE     CONTAINERS           IMAGES         SELECTOR
deployment-rollout-54766f574f   2         2         0       93s     httpd-rollout-test   httpd:2.4.33   pod-template-hash=54766f574f,test=httpd
deployment-rollout-5fb9c69c5c   3         3         3       4m12s   httpd-rollout-test   httpd:2.2.31   pod-template-hash=5fb9c69c5c,test=httpd
[root@k8s-m ~]# kubectl get pod -o wide
NAME                                  READY   STATUS              RESTARTS   AGE     IP             NODE     NOMINATED NODE
deployment-rollout-54766f574f-668pc   0/1     ContainerCreating   0          114s    <none>         k8s-n1   <none>
deployment-rollout-54766f574f-ssz67   0/1     ContainerCreating   0          115s    <none>         k8s-n2   <none>
deployment-rollout-5fb9c69c5c-m5dvs   1/1     Running             0          4m34s   10.244.2.47    k8s-n2   <none>
deployment-rollout-5fb9c69c5c-p9grr   1/1     Running             0          4m34s   10.244.1.113   k8s-n1   <none>
deployment-rollout-5fb9c69c5c-tzm7w   1/1     Running             0          4m34s   10.244.2.48    k8s-n2   <none>

一下子後再觀察一下:運維

[root@k8s-m ~]# kubectl get replicaset -o wide
NAME                            DESIRED   CURRENT   READY   AGE     CONTAINERS           IMAGES         SELECTOR
deployment-rollout-54766f574f   4         4         4       4m24s   httpd-rollout-test   httpd:2.4.33   pod-template-hash=54766f574f,test=httpd
deployment-rollout-5fb9c69c5c   0         0         0       7m3s    httpd-rollout-test   httpd:2.2.31   pod-template-hash=5fb9c69c5c,test=httpd
[root@k8s-m ~]# kubectl get deployment -o wide
NAME                 DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS           IMAGES         SELECTOR
deployment-rollout   4         4         4            4           7m18s   httpd-rollout-test   httpd:2.4.33   test=httpd
[root@k8s-m ~]# kubectl get pod -o wide
NAME                                  READY   STATUS    RESTARTS   AGE     IP             NODE     NOMINATED NODE
deployment-rollout-54766f574f-668pc   1/1     Running   0          4m48s   10.244.1.114   k8s-n1   <none>
deployment-rollout-54766f574f-h7gf9   1/1     Running   0          99s     10.244.2.50    k8s-n2   <none>
deployment-rollout-54766f574f-ssz67   1/1     Running   0          4m49s   10.244.2.49    k8s-n2   <none>
deployment-rollout-54766f574f-vklvr   1/1     Running   0          41s     10.244.1.115   k8s-n1   <none>

在此過程當中,咱們會發現,ReplicaSet deployment-rollout-5fb9c69c5c逐步被deployment-rollout-54766f574f取代,建立新版本的Pod以後,原始版本的Pod被終止。這是一個平滑的升級過程。Kubernetes提供了兩個參數maxSurge和maxUnavailable來精細控制Pod的替換數量。ide

2.3 版本的回滾。

默認狀況下,Kubernetes只會保留最近幾個revision。在上面的升級過程當中,經過revisionHistoryLimit設置了revision的數量。咱們經過命令來看一下:測試

[root@k8s-m ~]# kubectl rollout history deployment deployment-rollout
deployment.extensions/deployment-rollout
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

繼續升級。新版的YAML以下:日誌

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: deployment-rollout
spec:
  revisionHistoryLimit: 10
  replicas: 4
  template:
    metadata:
      labels:
        test: httpd
    spec:
      containers:
      - name: httpd-rollout-test
        image: httpd:2.4.34
        ports:
        - containerPort: 80

執行升級並記錄升級日誌。code

[root@k8s-m ~]# kubectl apply -f Httpd-Deployment-rollout-v2.yaml --record
deployment.apps/deployment-rollout configured
[root@k8s-m ~]# kubectl rollout history deployment deployment-rollout
deployment.extensions/deployment-rollout
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         kubectl apply --filename=Httpd-Deployment-rollout-v2.yaml --record=true
[root@k8s-m ~]# kubectl get deployment deployment-rollout -o wide
NAME                 DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS           IMAGES         SELECTOR
deployment-rollout   4         4         4            4           31m   httpd-rollout-test   httpd:2.4.34   test=httpd
[root@k8s-m ~]# kubectl get replicaset -o wide
NAME                            DESIRED   CURRENT   READY   AGE     CONTAINERS           IMAGES         SELECTOR
deployment-rollout-54766f574f   0         0         0       29m     httpd-rollout-test   httpd:2.4.33   pod-template-hash=54766f574f,test=httpd
deployment-rollout-54fc66bb6    4         4         4       6m12s   httpd-rollout-test   httpd:2.4.34   pod-template-hash=54fc66bb6,test=httpd
deployment-rollout-5fb9c69c5c   0         0         0       32m     httpd-rollout-test   httpd:2.2.31   pod-template-hash=5fb9c69c5c,test=httpd
[root@k8s-m ~]# kubectl get pod -o wide
NAME                                 READY   STATUS    RESTARTS   AGE     IP             NODE     NOMINATED NODE
deployment-rollout-54fc66bb6-2zvhk   1/1     Running   0          6m26s   10.244.1.116   k8s-n1   <none>
deployment-rollout-54fc66bb6-7rsj6   1/1     Running   0          4m16s   10.244.1.117   k8s-n1   <none>
deployment-rollout-54fc66bb6-nlx4h   1/1     Running   0          6m26s   10.244.2.51    k8s-n2   <none>
deployment-rollout-54fc66bb6-xxnrg   1/1     Running   0          6m24s   10.244.2.52    k8s-n2   <none>

修改YAML文件中鏡像的版本繼續升級,觀察升級結果:

[root@k8s-m ~]# kubectl rollout history deployment deployment-rollout
deployment.extensions/deployment-rollout
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         kubectl apply --filename=Httpd-Deployment-rollout-v2.yaml --record=true
4         kubectl apply --filename=Httpd-Deployment-rollout-v3.yaml --record=true

[root@k8s-m ~]# kubectl get replicaset -o wide
NAME                            DESIRED   CURRENT   READY   AGE     CONTAINERS           IMAGES         SELECTOR
deployment-rollout-54766f574f   0         0         0       33m     httpd-rollout-test   httpd:2.4.33   pod-template-hash=54766f574f,test=httpd
deployment-rollout-54fc66bb6    0         0         0       10m     httpd-rollout-test   httpd:2.4.34   pod-template-hash=54fc66bb6,test=httpd
deployment-rollout-5b99bbfbbc   4         4         4       2m33s   httpd-rollout-test   httpd:2.4.35   pod-template-hash=5b99bbfbbc,test=httpd
deployment-rollout-5fb9c69c5c   0         0         0       36m     httpd-rollout-test   httpd:2.2.31   pod-template-hash=5fb9c69c5c,test=httpd
[root@k8s-m ~]# kubectl get pod -o wide
NAME                                  READY   STATUS    RESTARTS   AGE     IP             NODE     NOMINATED NODE
deployment-rollout-5b99bbfbbc-7mdg9   1/1     Running   0          65s     10.244.1.119   k8s-n1   <none>
deployment-rollout-5b99bbfbbc-9mfbk   1/1     Running   0          2m40s   10.244.1.118   k8s-n1   <none>
deployment-rollout-5b99bbfbbc-jn5sb   1/1     Running   0          2m40s   10.244.2.53    k8s-n2   <none>
deployment-rollout-5b99bbfbbc-tr6t2   1/1     Running   0          2m38s   10.244.2.54    k8s-n2   <none>

httpd的image均已經升級到2.4.35,接下來咱們試一下回滾到v2版本:

[root@k8s-m ~]# kubectl rollout undo deployment deployment-rollout --to-revision=3
deployment.extensions/deployment-rollout
[root@k8s-m ~]# kubectl get deployment -o wide
NAME                 DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS           IMAGES         SELECTOR
deployment-rollout   4         4         4            4           42m   httpd-rollout-test   httpd:2.4.34   test=httpd
[root@k8s-m ~]# kubectl get replicaset -o wide
NAME                            DESIRED   CURRENT   READY   AGE     CONTAINERS           IMAGES         SELECTOR
deployment-rollout-54766f574f   0         0         0       39m     httpd-rollout-test   httpd:2.4.33   pod-template-hash=54766f574f,test=httpd
deployment-rollout-54fc66bb6    4         4         4       16m     httpd-rollout-test   httpd:2.4.34   pod-template-hash=54fc66bb6,test=httpd
deployment-rollout-5b99bbfbbc   0         0         0       8m35s   httpd-rollout-test   httpd:2.4.35   pod-template-hash=5b99bbfbbc,test=httpd
deployment-rollout-5fb9c69c5c   0         0         0       42m     httpd-rollout-test   httpd:2.2.31   pod-template-hash=5fb9c69c5c,test=httpd
[root@k8s-m ~]# kubectl get pod -o wide
NAME                                 READY   STATUS    RESTARTS   AGE   IP             NODE     NOMINATED NODE
deployment-rollout-54fc66bb6-bjskn   1/1     Running   0          68s   10.244.1.121   k8s-n1   <none>
deployment-rollout-54fc66bb6-c65th   1/1     Running   0          70s   10.244.2.55    k8s-n2   <none>
deployment-rollout-54fc66bb6-cwpbk   1/1     Running   0          70s   10.244.1.120   k8s-n1   <none>
deployment-rollout-54fc66bb6-lz9xt   1/1     Running   0          68s   10.244.2.56    k8s-n2   <none>
[root@k8s-m ~]# kubectl rollout history deployment deployment-rollout
deployment.extensions/deployment-rollout
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
4         kubectl apply --filename=Httpd-Deployment-rollout-v3.yaml --record=true
5         kubectl apply --filename=Httpd-Deployment-rollout-v2.yaml --record=true

每次升級或回滾操做以後REVISION的數字都會增長1。經過kubectl history,咱們能夠看到每次升級記錄,--record記錄了每次操做的詳細過程。經過kubectl undo執行回滾操做,選項--to-revision指定了回滾的修訂版本號。

三.總結

3.1. 再次被Kubernetes的魅力所折服,日常工做中的灰度升級/發佈/更新(按批次中止老版本實例,啓用新版本實例,新老版本共存,逐步擴大新版本範圍,最終把全部用戶遷移到新版本上),藍綠升級/發佈/更新(不中止老版本,另外部署一套新版本,新版本經過測試發佈後刪除老版本),滾動升級/發佈/更新(一個或多個服務中止,執行更新,逐步將新版本投入使用,周而復始,最終完成整個集羣中全部實例的版本更新)這些概念理解更加透徹。

3.2. 目前有些資料已經不適用於新版本了,不要被網上的資料所誤導,必定要立足與官方文檔和系統幫助。例如:

Kubernetes 的Deploy相關的命令只有:

Deploy Commands:
  rollout        Manage the rollout of a resource
  scale          爲 Deployment, ReplicaSet, Replication Controller 或者 Job 設置一個新的副本數量
  autoscale      自動調整一個 Deployment, ReplicaSet, 或者 ReplicationController 的副本數量

Rollout可用資源類型:

Valid resource types include:
  * deployments
  * daemonsets
  * statefulsets

3.3 Rollout updat也能夠經過YAML來實現,後續文章會介紹。

四.相關資料

4.1 本文相關的yaml文件

4.2 官方資料

相關文章
相關標籤/搜索