kubernetes Deployment之捲動更新策略。

2022-07-28 12:00:38

1.Deployment控制器詳細資訊中包含了其更新策略的相關設定。kubectl describe命令中輸出的StrategyType、RollingUpdateStrategy欄位等;

root@kubernetes-master01:~# kubectl describe deploy sleep 
Name:                   sleep
Namespace:              default
CreationTimestamp:      Wed, 25 May 2022 15:03:14 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=sleep
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
# Deployment預設更新策略就是RollingUpdate預設更新策略是25%

1.Deployment的更新策略:

Deployment控制器支援兩種更新策略: 捲動更新(rolling update)和重建建立(recreate)也稱為單批次更新。
1.重建(Recreate),當更新策略設定為Recreate,在更新映象時,它會先刪除現在正在執行的Pod,等徹底殺死後,重新建立新的RS(ReplicaSet)然後啟動對應的Pod,在整個更新過程中,會造成服務一段時間無法提供服務。也稱之為單批次更新。
2.捲動更新(Rolling Update)捲動更新是預設的更新策略,一次僅更新一批Pod,當更新的Pod就緒後再更新另一批,直到全部更新完成為止;該策略實現了不間斷服務的目標,但是在更新過程中,不同使用者端得到的響應內容可能會來自不同版本的應用。會出現新老版本共存狀態。

2.ReCreate實踐;

Recreate分為三個步驟:
1.殺死所有舊版本的Pod,此時Pod無法正常對外提供服務;
2.建立新的RS,啟動新的Pod;
3.等待Pod就緒,對外提供服務;

2.1應用設定範例

# 須在Spec欄位中明確定義strategy捲動更新策略和type型別
root@kubernetes-master01:~# cat nginx-deployment-test.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-nginx-test
  namespace: default
spec:
  strategy:         # 捲動更新策略
    type: Recreate  # Recreate表示的是單批次更新。
  replicas: 2
  selector:
    matchLabels:
      app: nginx-deployment

  template:
    metadata:
      labels:
        app: nginx-deployment
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        #imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
root@kubernetes-master01:~# kubectl apply -f nginx-deployment-test.yaml 

2.2存取測試,同時組態檔修改Nginx的版本測試服務是否會中斷;

# 跟上任何符號會報錯Nginx的錯誤頁,現版本是1.16。當然是為了證實nginx版本。
root@kubernetes-master01:~# curl 10.107.246.117/v
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

2.3現版本是1.16,現在測試卷動更新;

1.修改yaml組態檔修改編輯組態檔的spec.containers.image欄位修改Nginx的版本
2.通過set image來修改

我是通過set image來修改。
root@kubernetes-master01:~# kubectl set image deployment/deployment-nginx-test nginx=nginx:latest
deployment.apps/deployment-nginx-test image updated

2.4存取測試,的確中間是有業務存取間斷的,因此在生產環境不建議這種方式。在更新的過程中舊Pod是處於Terminating狀態。

root@kubernetes-master01:~# while sleep 0.5; do curl http://10.107.246.117/version;done
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
curl: (7) Failed to connect to 10.107.246.117 port 80: Connection refused
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>

3.RollingUpdate實踐

捲動更新時,應用升級期間還要確保可用的Pod物件數量不低於閾值以確保可用持續處理使用者端的服務請求,變動的方式和Pod物件的數量範圍將通過spec.strategy.rollingUpdate.maxSurge和spec.strategy.rollingPudate.maxUnavailable兩個屬性同時進行定義;

捲動更新(RollingUpdate)一次僅更新一批Pod,當更新的Pod就緒後,再更新另一批,直到全部更新完成為止,該策略實現了不間斷服務的目標,在更新過程中可能會出現不同的應用版本且並存,同時提供服務的情況。

1.建立新的RS,然後根據新的映象執行新的Pod。
2.刪除舊的Pod,啟動新的Pod,當新Pod就緒後,繼續刪除舊Pod,啟動新Pod。
3.持續第二步過程,一直到所有Pod都被更新成功。

3.1準備應用組態檔

# 這個欄位需設定為RollingUpdate
spec:
  strategy:
    type: RollingUpdate
# 應用組態檔,現版本Nginx是1.16版本,要捲動到1.21.5
root@kubernetes-master01:~# cat nginx-deployment-test.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-nginx-test
  namespace: default
spec:
  strategy:
    type: RollingUpdate
  replicas: 2
  selector:
    matchLabels:
      app: nginx-deployment

  template:
    metadata:
      labels:
        app: nginx-deployment
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        #imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
root@kubernetes-master01:~# kubectl apply -f nginx-deployment-test.yaml 
deployment.apps/deployment-nginx-test created

# Pod也是執行正常的。
root@kubernetes-master01:~# kubectl get pods -l app=nginx-deployment
NAME                                     READY   STATUS    RESTARTS   AGE
deployment-nginx-test-65b579f8d5-gdpd5   1/1     Running   0          3m14s
deployment-nginx-test-65b579f8d5-spq9w   1/1     Running   0          3m14s

3.2,我們現在存取前端的Service;OK版本一直是1.16;

root@kubernetes-master01:~# while sleep 0.5; do curl http://10.103.162.78/v;done
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>

3.3修改yaml組態檔升級;並觀察是否業務中斷;

# Pod狀態;1.16版本的Pod是65b579f8d5,現在是76f7b87b7c
root@kubernetes-master01:~# kubectl get pods
deployment-nginx-test-76f7b87b7c-n2jmm       1/1     Running             0          35s
deployment-nginx-test-76f7b87b7c-qvxhg       0/1     ContainerCreating   0          11s

# 捲動升級狀態;
root@kubernetes-master01:~# kubectl apply -f nginx-deployment-test.yaml  && kubectl rollout status deploy deployment-nginx-test 
deployment.apps/deployment-nginx-test configured
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 old replicas are pending termination...
deployment "deployment-nginx-test" successfully rolled out

# 檢視Replicaset;現在65b579的Pod數量被置於0
root@kubernetes-master01:~# kubectl get replicaset
deployment-nginx-test-577977f4b6       2         2         2       7m
deployment-nginx-test-65b579f8d5       0         0         0       17m

3.4觀察存取

# 業務存取正常,沒有中斷,但是一段時間會出現新老版本共存狀態;
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
^C

4.應用的回滾

4.1.我們可以通過命令來檢視更新的歷史版本;

root@kubernetes-master01:~# kubectl  rollout history  deployment deployment-nginx-test
deployment.apps/deployment-nginx-test 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

4.2.也可以檢視具體映象詳情,跟上序號;

root@kubernetes-master01:~# kubectl rollout history deploy  deployment-nginx-test --revision=2
deployment.apps/deployment-nginx-test with revision #2
Pod Template:
  Labels:	app=nginx-deployment
	pod-template-hash=76f7b87b7c
  Containers:
   nginx:
    Image:	nginx:1.16
    Port:	80/TCP
    Host Port:	0/TCP
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

4.3.現在想要回滾到1.16這個版本;

root@kubernetes-master01:~# kubectl rollout undo deploy  deployment-nginx-test  --to-revision=2 && kubectl rollout status deploy deployment-nginx-test
deployment.apps/deployment-nginx-test rolled back
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "deployment-nginx-test" rollout to finish: 1 old replicas are pending termination...
deployment "deployment-nginx-test" successfully rolled out

4.4.觀察存取狀態;亦會出現新老版本交替情況

<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.5</center>
</body>
</html>
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
^C

4.5.檢視Pod和Replicaset

root@kubernetes-master01:~/cloud-Native/deployment/replicas# kubectl get pods -l app=nginx-deployment
NAME                                     READY   STATUS    RESTARTS   AGE
deployment-nginx-test-76f7b87b7c-5tx7z   1/1     Running   0          2m34s
deployment-nginx-test-76f7b87b7c-t9ld2   1/1     Running   0          2m57s

# 也是沒有問題。
root@kubernetes-master01:~/cloud-Native/deployment/replicas# kubectl get replicaset -l app=nginx-deployment
NAME                               DESIRED   CURRENT   READY   AGE
deployment-nginx-test-577977f4b6   0         0         0       20m
deployment-nginx-test-65b579f8d5   0         0         0       31m
deployment-nginx-test-76f7b87b7c   2         2         2       24m