kubernetes rolling update

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/

发布了31 篇原创文章 · 获赞 6 · 访问量 7万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章