k8s滾動更新(六)--技術流ken

 

實踐

 

滾動更新是一次只更新一小部分副本,成功後,再更新更多的副本,最終完成全部副本的更新。滾動更新的最大的好處是零停機,整個更新過程始終有副本在運行,從而保證了業務的連續性。api

 

下面咱們部署三副本應用,初始鏡像爲 httpd:2.2.31,而後將其更新到 httpd:2.2.32。app

 

第一步: httpd:2.2.31 的配置文件以下:ide

[root@ken ~]# cat httpd.yml 
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.2.31
        ports:
        - containerPort: 80

 

第二步:部署應用並查看spa

[root@ken ~]# kubectl apply -f httpd.yml
deployment.apps/httpd created

[root@ken ~]# kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd   0/3     3            0           10s   httpd        httpd:2.2.31   run=httpd

[root@ken ~]# kubectl get replicaset -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpd-76cfb94bf4 3 3 0 23s httpd httpd:2.2.31 pod-template-hash=76cfb94bf4,run=httpdcode



[root@ken ~]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP            NODE    NOMINATED NODE   READINESS GATES
httpd-76cfb94bf4-629s2   1/1     Running   0          113s   10.244.1.34   host1   <none>           <none>
httpd-76cfb94bf4-lt9wb   1/1     Running   0          113s   10.244.1.33   host1   <none>           <none>
httpd-76cfb94bf4-n62nj   1/1     Running   0          113s   10.244.2.23   host2   <none>           <none>

 

部署過程以下:orm

 

建立 Deployment httpdblog

建立 ReplicaSet httpd-76cfb94bf4部署

建立三個 Podget

當前鏡像爲 httpd:2.2.31hash

 

第三步:將配置文件中 httpd:2.2.31 替換爲 httpd:2.2.32,再次執行 kubectl apply。

[root@ken ~]# kubectl apply -f httpd.yml
deployment.apps/httpd configured

[root@ken ~]# kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
httpd   3/3     1            3           4m2s   httpd        httpd:2.2.32   run=httpd

[root@ken ~]# kubectl get replicaset -o wide  #這一步要稍等幾分鐘纔會切換到32版本
NAME               DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         SELECTOR
httpd-6cf6bf9f57   3         3         3       2m47s   httpd        httpd:2.2.32   pod-template-hash=6cf6bf9f57,run=httpd
httpd-76cfb94bf4   0         0         0       6m30s   httpd        httpd:2.2.31   pod-template-hash=76cfb94bf4,run=httpd

[root@ken ~]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
httpd-6cf6bf9f57-5md94   1/1     Running   0          2m58s   10.244.2.24   host2   <none>           <none>
httpd-6cf6bf9f57-nvxnc   1/1     Running   0          75s     10.244.1.35   host1   <none>           <none>
httpd-6cf6bf9f57-v7lpg   1/1     Running   0          22s     10.244.1.36   host1   <none>           <none>

咱們發現了以下變化:

 

Deployment httpd 的鏡像更新爲 httpd:2.2.32

新建立了 ReplicaSethttpd-6cf6bf9f57,鏡像爲 httpd:2.2.32,而且管理了三個新的 Pod。

以前的 ReplicaSet httpd-76cfb94bf4裏面已經沒有任何 Pod。

結論是:ReplicaSethttpd-76cfb94bf4的三個 httpd:2.2.31 Pod 已經被 ReplicaSethttpd-6cf6bf9f57的三個 httpd:2.2.32 Pod 替換了。

 

第四步:具體過程能夠經過 kubectl describe deployment httpd 查看。

[root@ken ~]# kubectl describe deployment httpd
...
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  9m11s  deployment-controller  Scaled up replica set httpd-76cfb94bf4 to 3
  Normal  ScalingReplicaSet  5m28s  deployment-controller  Scaled up replica set httpd-6cf6bf9f57 to 1
  Normal  ScalingReplicaSet  3m45s  deployment-controller  Scaled down replica set httpd-76cfb94bf4 to 2
  Normal  ScalingReplicaSet  3m45s  deployment-controller  Scaled up replica set httpd-6cf6bf9f57 to 2
  Normal  ScalingReplicaSet  2m52s  deployment-controller  Scaled down replica set httpd-76cfb94bf4 to 1
  Normal  ScalingReplicaSet  2m52s  deployment-controller  Scaled up replica set httpd-6cf6bf9f57 to 3
  Normal  ScalingReplicaSet  2m50s  deployment-controller  Scaled down replica set httpd-76cfb94bf4 to 0

每次只更新替換一個 Pod:

 

ReplicaSet httpd-6cf6bf9f57 增長一個 Pod,總數爲 1

ReplicaSet httpd-76cfb94bf4 減小一個 Pod,總數爲 2

ReplicaSet httpd-6cf6bf9f57 增長一個 Pod,總數爲 2

ReplicaSet httpd-76cfb94bf4 減小一個 Pod,總數爲 1

ReplicaSet httpd-6cf6bf9f57 增長一個 Pod,總數爲 3

ReplicaSet httpd-76cfb94bf4 減小一個 Pod,總數爲 0

 

每次替換的 Pod 數量是能夠定製的。Kubernetes 提供了兩個參數 maxSurge maxUnavailable 來精細控制 Pod 的替換數量,咱們將在後面結合 Health Check 特性一塊兒討論。

 

更新回滾

 

kubectl apply 每次更新應用時 Kubernetes 都會記錄下當前的配置,保存爲一個 revision(版次),這樣就能夠回滾到某個特定 revision。

默認配置下,Kubernetes 只會保留最近的幾個 revision,能夠在 Deployment 配置文件中經過 revisionHistoryLimit 屬性增長 revision 數量。

 

第一步:下面實踐回滾功能。

應用有以下三個配置文件 httpd.v1.yml,httpd.v2.yml 和 httpd.v3.yml,分別對應不一樣的 httpd 鏡像 2.4.16,2.4.17 和 2.4.18:

 

第二步:部署應用並更新

後面一個部署的應用,會覆蓋掉前面的(名稱相同)

[root@ken ~]# kubectl apply -f httpd_v1.yml --record
deployment.apps/httpd created
[root@ken ~]# kubectl get deployment httpd -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd   3/3     3            3           18s   httpd        httpd:2.4.16   run=httpd


[root@ken ~]# kubectl apply -f httpd_v2.yml --record
deployment.apps/httpd configured
[root@ken ~]# kubectl get deployment httpd -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd   3/3     2            3           75s   httpd        httpd:2.4.17   run=httpd

[root@ken ~]# kubectl apply -f httpd_v3.yml --record
deployment.apps/httpd configured
[root@ken ~]# kubectl get deployment httpd -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd   3/3     1            3           94s   httpd        httpd:2.4.18   run=httpd

--record 的做用是將當前命令記錄到 revision 記錄中,這樣咱們就能夠知道每一個 revison 對應的是哪一個配置文件。

 

第三步:經過 kubectl rollout history deployment httpd 查看 revison 歷史記錄。

[root@ken ~]# kubectl rollout history deployment httpd
deployment.extensions/httpd 
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=httpd_v1.yml --record=true
2         kubectl apply --filename=httpd_v2.yml --record=true
3         kubectl apply --filename=httpd_v3.yml --record=true

CHANGE-CAUSE 就是 --record 的結果。

 

第四步:若是要回滾到某個版本,好比 revision 1,能夠執行命令 kubectl rollout undo deployment httpd --to-revision=1:

[root@ken ~]# kubectl rollout undo deployment httpd --to-revision=1
deployment.extensions/httpd rolled back
[root@ken ~]# kubectl get deployment httpd -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
httpd   3/3     3            3           5m26s   httpd        httpd:2.4.16   run=httpd

 

第五步:此時,revison 歷史記錄也會發生相應變化。

[root@ken ~]# kubectl rollout history deployment httpd
deployment.extensions/httpd 
REVISION  CHANGE-CAUSE
2         kubectl apply --filename=httpd_v2.yml --record=true
3         kubectl apply --filename=httpd_v3.yml --record=true
4         kubectl apply --filename=httpd_v1.yml --record=true

revison 1 變成了 revison 4。不過咱們能夠經過 CHANGE-CAUSE 知道每一個 revison 的具體含義。因此必定要在執行 kubectl apply 時加上 --record參數。

相關文章
相關標籤/搜索