Rolling update
1. Create Deployment
# kubectl apply -f wether.cm.yaml
# kubectl apply -f weather-jmx.deploy.yaml --record
產生了新的rs 。
[root@k8s-master ~]# kubectl get rs -w | egrep "jmx|NAME"
NAME DESIRED CURRENT READY AGE
weather-jmx-1809594718 3 3 3 35s
2. Update image
模擬版本升級,更換deployment 中使用的鏡像。
這裏以直接修改yaml文件爲例:
用文本編輯器打開weather-jmx.deploy.yaml, 將鏡像改爲:
172.16.18.5:30088/admin/centos7.1-v1-wth:test
執行:
[root@k8s-master ~]# kubectl replace -f weather-jmx.deploy.yaml --record
deployment "weather-jmx" replaced
3. Observation
[root@k8s-master ~]# kubectl get rs -w | egrep "jmx"
weather-jmx-1809594718 3 3 3 52s
weather-jmx-790510206 1 0 0 0s
weather-jmx-1809594718 2 3 3 9m
weather-jmx-790510206 1 0 0 0s
weather-jmx-790510206 1 1 0 0s
weather-jmx-1809594718 2 3 3 9m
weather-jmx-1809594718 2 2 2 9m
weather-jmx-790510206 2 1 0 0s
weather-jmx-790510206 2 1 0 0s
weather-jmx-790510206 2 2 0 0s
weather-jmx-790510206 2 2 1 4s
weather-jmx-1809594718 1 2 2 9m
weather-jmx-1809594718 1 2 2 9m
weather-jmx-790510206 3 2 1 4s
weather-jmx-1809594718 1 1 1 9m
weather-jmx-790510206 3 2 1 4s
weather-jmx-790510206 3 3 1 4s
weather-jmx-790510206 3 3 2 6s
weather-jmx-1809594718 0 1 1 9m
weather-jmx-1809594718 0 1 1 9m
weather-jmx-1809594718 0 0 0 9m
weather-jmx-790510206 3 3 3 7s
通過 kubectl get rs -w
命令可以觀察到,更新鏡像後創建了一個新的rs.
從上圖可以看出,新的rs 從1開始,逐漸的替換掉來原來的3個pod. 而原來的rs也由3個pod,逐漸變爲0個pod. 由此完成了整個滾動升級的過程。
升級完成
可以通過 rollout history
來查看Deployment的滾動日誌。
[root@k8s-master ~]# kubectl rollout history deployment/weather-jmx
deployments "weather-jmx"
REVISION CHANGE-CAUSE
1 kubectl apply -f weather-jmx.deploy.yaml --record
2 kubectl replace -f weather-jmx.deploy.yaml --record
4. Rollback
如果需要回退到上個版本,執行:
[root@k8s-master ~]# kubectl rollout undo deployment/weather-jmx
deployment "weather-jmx" rolled back
回退過程同滾動升級過程一致:
[root@k8s-master ~]# kubectl get rs -w | egrep "jmx"
weather-jmx-1809594718 0 0 0 13m
weather-jmx-790510206 3 3 3 3m
weather-jmx-1809594718 0 0 0 13m
weather-jmx-1809594718 1 0 0 13m
weather-jmx-790510206 2 3 3 4m
weather-jmx-1809594718 1 0 0 13m
weather-jmx-1809594718 1 1 0 13m
weather-jmx-790510206 2 3 3 4m
weather-jmx-790510206 2 2 2 4m
weather-jmx-1809594718 2 1 0 13m
weather-jmx-1809594718 2 1 0 13m
weather-jmx-1809594718 2 2 0 13m
weather-jmx-1809594718 2 2 1 13m
weather-jmx-790510206 1 2 2 4m
weather-jmx-790510206 1 2 2 4m
weather-jmx-1809594718 3 2 1 13m
weather-jmx-790510206 1 1 1 4m
weather-jmx-1809594718 3 2 1 13m
weather-jmx-1809594718 3 3 1 13m
weather-jmx-1809594718 3 3 2 13m
weather-jmx-790510206 0 1 1 4m
weather-jmx-790510206 0 1 1 4m
weather-jmx-790510206 0 0 0 4m
weather-jmx-1809594718 3 3 3 13m
[root@k8s-master ~]# kubectl rollout history deployment/weather-jmx
deployments "weather-jmx"
REVISION CHANGE-CAUSE
2 kubectl replace -f weather-jmx.deploy.yaml --record
3 kubectl apply -f weather-jmx.deploy.yaml --record
通過 rollout history
可以發現,當前的revision 爲3。也可以通過
# kubectl rollout undo deployment/weather-jmx --revision=<REVISION>
來回退到具體某個版本
參考鏈接:
https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
https://tachingchen.com/tw/blog/Kubernetes-Rolling-Update-with-Deployment/