您好,登錄后才能下訂單哦!
隨著Kubernetes的進一步深入使用,我們越來越體會到它給我們的工作帶來的高效與便利。Rolling Update是Kubernetes系統中的一個強大的功能,能夠為我們的運維工作帶來極大的便利。
初始Deployment的YAML如下:
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
執行命令:
[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.4.31,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.33
ports:
- containerPort: 80
執行命令:
[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的替換數量。
默認情況下,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
執行升級并記錄升級日志。
[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 官方資料
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。