1. 程式人生 > >Kubernetes Pod應用的滾動更新(八)

Kubernetes Pod應用的滾動更新(八)

一、環境準備

我們緊接上一節的環境,進行下面的操作,如果不清楚的,可以先檢視上一篇博文。

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

二、更新

我們檢視一下上一節的配置檔案mytest-deploy.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mytest
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: mytest
    spec:
      containers:
      - name: mytest
        image: wangzan18/mytest:v1
        ports:
        - containerPort: 80

我們看到設定的映象版本為v1,我們檢視一下建立的ReplicaSet

[[email protected] ~]# kubectl get rs -o wide
NAME               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                SELECTOR
mytest-88d46bf99   3         3         3       68m   mytest       wangzan18/mytest:v1   pod-template-hash=88d46bf99,run=mytest

目前執行的 Pod 由 ReplicaSet mytest-88d46bf99進行控制,我們將配置檔案中的 v1 替換為 v2,再次應用。

[[email protected] ~]# kubectl apply -f mytest-deploy.yaml 
deployment.extensions/mytest configured
[[email protected] ~]# kubectl get pods
NAME                     READY   STATUS        RESTARTS   AGE
mytest-56c55b4c6-6gjxc   1/1     Running       0          24s
mytest-56c55b4c6-f5trx   1/1     Running       0          18s
mytest-56c55b4c6-sh5wd   1/1     Running       0          24s
mytest-88d46bf99-48f6n   1/1     Terminating   0          70m
mytest-88d46bf99-mv6cf   1/1     Terminating   0          70m
mytest-88d46bf99-p9w79   1/1     Terminating   0          70m
[[email protected] ~]# kubectl get deploy -o wide
NAME     READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                SELECTOR
mytest   3/3     3            3           72m   mytest       wangzan18/mytest:v2   run=mytest
[[email protected] ~]# kubectl get rs -o wide
NAME               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                SELECTOR
mytest-56c55b4c6   3         3         3       59s   mytest       wangzan18/mytest:v2   pod-template-hash=56c55b4c6,run=mytest
mytest-88d46bf99   0         0         0       71m   mytest       wangzan18/mytest:v1   pod-template-hash=88d46bf99,run=mytest

我們可以看到 Deployment 的映象更新為wangzan18/mytest:v2,建立了新的 ReplicaSet mytest-56c55b4c6,並管理了三個新的 Pod。

[[email protected] ~]# kubectl describe deployment mytest
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  4m9s  deployment-controller  Scaled up replica set mytest-56c55b4c6 to 1
  Normal  ScalingReplicaSet  4m9s  deployment-controller  Scaled down replica set mytest-88d46bf99 to 2
  Normal  ScalingReplicaSet  4m9s  deployment-controller  Scaled up replica set mytest-56c55b4c6 to 2
  Normal  ScalingReplicaSet  4m3s  deployment-controller  Scaled down replica set mytest-88d46bf99 to 1
  Normal  ScalingReplicaSet  4m3s  deployment-controller  Scaled up replica set mytest-56c55b4c6 to 3
  Normal  ScalingReplicaSet  4m3s  deployment-controller  Scaled down replica set mytest-88d46bf99 to 0

每次只更新替換一個 Pod。

三、回滾

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

預設配置下,Kubernetes 只會保留最近的幾個 revision,可以在 Deployment 配置檔案中通過 revisionHistoryLimit 屬性增加 revision 數量。

通過命令kubectl rollout undo我們可以回滾到上一個版本。

[[email protected] ~]# kubectl rollout undo deploy mytest
deployment.extensions/mytest rolled back
[[email protected] ~]# kubectl get rs -o wide
NAME               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                SELECTOR
mytest-56c55b4c6   0         0         0       12m   mytest       wangzan18/mytest:v2   pod-template-hash=56c55b4c6,run=mytest
mytest-88d46bf99   3         3         3       82m   mytest       wangzan18/mytest:v1   pod-template-hash=88d46bf99,run=mytest

可以看到三個 Pod 重新由映象為wangzan18/mytest:v1的ReplicaSet進行接管。