k8s之deployment詳解

路過的柚子廚發表於2021-07-19

Deployment介紹

為了更好地解決服務編排的問題,k8s在V1.2版本開始,引入了deployment控制器,值得一提的是,這種控制器並不直接管理pod,

而是通過管理replicaset來間接管理pod,即:deployment管理replicaset,replicaset管理pod。所以deployment比replicaset的功能更強大。

 

deployment的主要功能有下面幾個:

  • 支援replicaset的所有功能
  • 支援釋出的停止、繼續
  • 支援版本的滾動更新和版本回退

deployment的資源清單檔案

apiVersion: apps/v1  #版本號
kind: Deployment  #型別
metadata:    #後設資料
  name:    #rs名稱
  namespace:   #所屬名稱空間
  labels:   #標籤
    controller: deploy
spec:   #詳情描述
  replicas:  #副本數量
  revisionHistoryLimit: #保留歷史版本,預設是10
  paused: #暫停部署,預設是false
  progressDeadlineSeconds: #部署超時時間(s),預設是600
  strategy: #策略
    type: RollingUpdates  #滾動更新策略
    rollingUpdate:  #滾動更新
      maxSurge: #最大額外可以存在的副本數,可以為百分比,也可以為整數
      maxUnavaliable: #最大不可用狀態的pod的最大值,可以為百分比,也可以為整數
  selector:  #選擇器,通過它指定該控制器管理哪些pod
    matchLabels:   #Labels匹配規則
       app: nginx-pod
    matchExpressions:   #Expression匹配規則
      - {key: app, operator: In, values: [nginx-pod]}
  template:  #模板,當副本數量不足時,會根據下面的模板建立pod副本
    metadata:
        labels:
          app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80

建立deployment

建立pc-deployment.yaml,內容如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pc-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
     app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1

 使用配置檔案

[root@master ~]# kubectl create -f pc-deployment.yaml 
deployment.apps/pc-deployment created
[root@master ~]# kubectl get deploy -n dev -o wide
NAME            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
pc-deployment   3/3     3            3           16s   nginx        nginx:1.17.1   app=nginx-pod

檢視deployment控制的rs和pod,發現rs是在deployment之後加了一段字串,而pod是在rs之後加了一段字串

[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   3         3         3       2m13s
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-k8j9n   1/1     Running   0          2m42s
pc-deployment-5d89bdfbf9-vw87k   1/1     Running   0          2m42s
pc-deployment-5d89bdfbf9-x7nsm   1/1     Running   0          2m42s

deployment功能

擴縮容

方式一:命令列

格式

kubectl scale deploy deploy名稱 --replicas=pod數量 -n 名稱空間

通過命令列變更pod數量為5個

[root@master ~]# kubectl scale deploy pc-deployment --replicas=5 -n dev
deployment.apps/pc-deployment scaled
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-bhcns   1/1     Running   0          83s
pc-deployment-5d89bdfbf9-cfls7   1/1     Running   0          83s
pc-deployment-5d89bdfbf9-k8j9n   1/1     Running   0          8m54s
pc-deployment-5d89bdfbf9-vw87k   1/1     Running   0          8m54s
pc-deployment-5d89bdfbf9-x7nsm   1/1     Running   0          8m54s

方式二:編輯deploy檔案

格式

kubectl edit deploy deploy名字 -n 名稱空間

通過編輯deploy檔案編輯pod數量為3個

[root@master ~]# kubectl edit deploy pc-deployment -n dev
找到replicas,將其數量改為3
spec:
  progressDeadlineSeconds: 600
  replicas: 3
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-k8j9n   1/1     Running   0          15m
pc-deployment-5d89bdfbf9-vw87k   1/1     Running   0          15m
pc-deployment-5d89bdfbf9-x7nsm   1/1     Running   0          15m

映象更新

deployment支援兩種映象更新策略:重建更新滾動更新(預設),可以通過strategy選項進行配置

strategy:指定新的pod替換舊的pod的策略,支援兩個屬性:
  type:指定策略型別,支援兩種策略
    Recreate:在建立出新的pod之前會先殺掉所有已存在的pod
    RollingUpdate:滾動更新,就是殺死一部分,就啟動一部分,在更新過程中,存在兩個版本pod
  rollingUpdate:當type為RollingUpdate時生效,用於為RollingUpdate設定引數,支援兩個屬性
    maxUnavailable:用來指定在升級過程中不可用pod的最大數量,預設為25%
    maxSurge:用來指定在升級過程中可以超過期望的pod的最大數量,預設為25%

重建更新

編輯pc-deployment.yaml,在spec節點下新增更新策略

spec:
  strategy:  #策略
    type: Recreate  #重建更新策略
[root@master ~]# vim pc-deployment.yaml 
[root@master ~]# kubectl apply -f pc-deployment.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/pc-deployment configured

建立deploy進行驗證

#首先記錄原本的pod名
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-bqf86   1/1     Running   0          8s
pc-deployment-5d89bdfbf9-kz6jt   1/1     Running   0          8s
pc-deployment-5d89bdfbf9-z7d9z   1/1     Running   0          8s

#更改pod映象 [root@master
~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev deployment.apps/pc-deployment image updated
#再次檢視映象 [root@master
~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE pc-deployment-675d469f8b-b9rwd 1/1 Running 0 27s pc-deployment-675d469f8b-kc7rr 1/1 Running 0 27s pc-deployment-675d469f8b-kxgkq 1/1 Running 0 27s

發現pod映象已經改變了

滾動更新

編輯pc-deployment.yaml,在spec節點下新增滾動更新策略(也可以把strategy去掉,因為預設滾動更新策略)

strategy:
  type: RollingUpdate #滾動更新策略
  rollingUpdate:
    maxUnavailable: 25%
    maxSurge: 25%
[root@master ~]# vim pc-deployment.yaml 
[root@master ~]# kubectl apply -f pc-deployment.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/pc-deployment configured

建立deploy進行驗證

#記錄以前的pod
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-526wf   1/1     Running   0          61s
pc-deployment-5d89bdfbf9-b5x5v   1/1     Running   0          64s
pc-deployment-5d89bdfbf9-kc7hb   1/1     Running   0          59s
#更新映象
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev
deployment.apps/pc-deployment image updated
#檢視pod狀態
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS              RESTARTS   AGE
pc-deployment-5d89bdfbf9-526wf   0/1     Terminating         0          2m2s
pc-deployment-5d89bdfbf9-b5x5v   1/1     Running             0          2m5s
pc-deployment-5d89bdfbf9-kc7hb   0/1     Terminating         0          2m
pc-deployment-675d469f8b-7vw6x   1/1     Running             0          3s
pc-deployment-675d469f8b-rzq82   0/1     ContainerCreating   0          2s
pc-deployment-675d469f8b-zk4fs   1/1     Running             0          5s
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-675d469f8b-7vw6x   1/1     Running   0          38s
pc-deployment-675d469f8b-rzq82   1/1     Running   0          37s
pc-deployment-675d469f8b-zk4fs   1/1     Running   0          40s

發現pod是舊的一遍停止新的一邊建立,最後全變成了新的

滾動更新的過程

 

 

映象更新中rs的變化

前期準備:

#重建deployment
[root@master ~]# kubectl delete -f pc-deployment.yaml 
deployment.apps "pc-deployment" deleted
#新增record引數,表明建立時記錄
[root@master ~]# kubectl create -f pc-deployment.yaml --record
deployment.apps/pc-deployment created
[root@master ~]# kubectl get deploy,rs,pod -n dev
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/pc-deployment   3/3     3            3           81s

NAME                                       DESIRED   CURRENT   READY   AGE
replicaset.apps/pc-deployment-5d89bdfbf9   3         3         3       81s

NAME                                 READY   STATUS    RESTARTS   AGE
pod/pc-deployment-5d89bdfbf9-4bg2j   1/1     Running   0          81s
pod/pc-deployment-5d89bdfbf9-gbt95   1/1     Running   0          81s
pod/pc-deployment-5d89bdfbf9-tstlh   1/1     Running   0          81s

新建兩個xshell視窗,用於監聽rs和pod

 

 在2視窗中監聽rs,3視窗中監聽pod

#在2視窗中輸入
[root@master ~]# kubectl get rs -n dev -w
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   3         3         3       6m18s
#在3視窗中輸入
[root@master ~]# kubectl get pod -n dev -w
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-4bg2j   1/1     Running   0          6m56s
pc-deployment-5d89bdfbf9-gbt95   1/1     Running   0          6m56s
pc-deployment-5d89bdfbf9-tstlh   1/1     Running   0          6m56s

在1視窗中改變pod映象

[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev
deployment.apps/pc-deployment image updated

檢視3視窗中pod的變化,發現序號5開頭的pod在逐漸暫停,序號6開頭的pod在逐漸建立

檢視2視窗中rs的變化,可以看見序號5開頭的rs的pod數在減少,序號6開頭的rs的pod數在增加

 

 在1視窗中檢視最終rs變化,發現原來的rs依舊存在,只是pod數量變為了0,而後又新產生了一個rs,pod數量為3,其實這就是deployment能夠進行版本回退的奧妙所在,後面會詳細解釋

[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   0         0         0       11m
pc-deployment-675d469f8b   3         3         3       3m12s

版本回退

deployment支援版本升級過程中的暫停,繼續功能以及版本回退等諸多功能,下面具體來看

kubectl rollout:版本升級相關功能,支援下面的選項:

  • status:顯示當前升級狀態
  • history:顯示升級歷史記錄
  • pause:暫停版本升級過程
  • resume:繼續已經暫停的版本升級過程
  • restart:重啟版本升級過程
  • undo:回滾到上一級版本(可以使用--to-revision回滾到指定版本)
#檢視升級狀態
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out

#檢視升級歷史(注意:如果只顯示版本號說明一開始使用yaml建立檔案的時候沒有加上--record命令)
[root@master ~]# kubectl rollout history deploy pc-deployment -n dev
deployment.apps/pc-deployment 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=pc-deployment.yaml --record=true
2         kubectl create --filename=pc-deployment.yaml --record=true

#版本回滾
#這裡使用--to-revision=1回滾到1版本,如果省略這個選項,則會回退到上個版本
[root@master ~]# kubectl rollout undo deploy pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back

#檢視是否回滾成功,發現5序號開頭的rs被啟動了
[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   3         3         3       31m
pc-deployment-675d469f8b   0         0         0       22m

金絲雀釋出

deployment支援更新過程中的控制,如"暫停(pause)"或"繼續(resume)"更新操作

比如有一批新的pod資源建立完成後立即暫停更新過程,此時,僅存在一部分新版本的應用,主體部分還是舊的版本。然後,再篩選一小部分的使用者請求路由到新的pod應用,繼續觀察能否穩定地按期望的方式執行。確定沒問題之後再繼續完成餘下的pod資源滾動更新,否則立即回滾更新操作。這就是所謂的金絲雀釋出。

#更新deployment版本,並配置暫停deployment
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev && kubectl rollout pause deploy pc-deployment -n dev
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused

#檢視rs,發現老版本rs沒有減少,新版本rs增加一個
[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   3         3         3       44m
pc-deployment-675d469f8b   1         1         1       35m

#在視窗2中檢視deploy狀態,發現deploy正在等待更新且已經有1個更新好了
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...


#在視窗1中繼續deploy的更新
[root@master ~]# kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed

#檢視視窗2的狀態
Waiting for deployment spec update to be observed...
Waiting for deployment spec update to be observed...
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "pc-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "pc-deployment" successfully rolled out

#在視窗1檢視rs更新結果,發現老版本均停止,新版本已經建立好
[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   0         0         0       49m
pc-deployment-675d469f8b   3         3         3       40m

 

相關文章