一、RollingUpdate滚动发布
1.1、基本概念
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
RollingUpdate 可以是Recreate或RollingUpdate。RollingUpdate是默认值。Recreate时,在创建出新的Pod之前会先杀掉所有已存在的Pod。
RollingUpdate时,Deployment使用rolling update 的方式更新Pod 。可以指定maxUnavailable 和maxSurge来控制 rolling update 进程。
spec.strategy.rollingUpdate.maxSurge,滚动更新期间可以创建的pod的最大数量超过指定数量的pod。
1表示当一个新的pod被创建才会删除一个pod,以此类推。可以是具体的整数,也可以是百分百 默认值为25%
spec.strategy.rollingUpdate.maxUnavailable,更新过程中,最多有一个 Pod 不可用。
在rollgin更新期间无法使用的最大pod数。该值可以设置为具有大于0的整数的pod的绝对数量,并且百分比表示也是可能的,例如“25%”
maxUnavailable中的百分比计算向下舍入,默认值为25%。
maxSurge和maxUnavailable值不能同时为零。
1.2、滚动发布
# 启动buildkitd
[root@VM-16-14-centos go_hello]# cd /data/buildkit-v0.9.2/bin/
[root@VM-16-14-centos bin]# ./buildkitd &
# 修改源码,为重新打包准备
[root@VM-16-14-centos go_hello_8080]# vim main.go
# 打包并推送镜像
[root@VM-16-14-centos go_hello_8080]# ../bin/buildctl build --output type=image,name=127.0.0.1:81/mylibary/go_hello_8080:v2,push=true --export-cache type=inline --frontend=dockerfile.v0 --local context=. --local dockerfile=.
# 增加滚动发布策略
[root@VM-16-14-centos buildkit-v0.9.2]# cd go_hello_8080/
[root@VM-16-14-centos go_hello_8080]# vim go.dep.yml
spec增加
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
# 修改镜像版本,提交更新操作
[root@VM-16-14-centos go_hello_8080]# kubectl set image deployment/go-hello8080-deployment go-hello8080-container=*.*.*.*:81/mylibary/go_hello_8080:v2
deployment.apps/go-hello8080-deployment image updated
1.3、观察发布过程
-
更新前截图(重新发布,方便对比)
-
更新后截图
-
时间不一致,是因为10个副本,在新pod逐个替换旧pod存在前后顺序,导致
-
查看deployments进程
[root@VM-16-14-centos go_hello_8080]# kubectl get deployments go-hello8080-deployment
READY:表示用户期望的Pod副本个数,以及当前处于Running状态的Pod个数。
UP-TO-DATE:当前处于最新版本的Pod个数。所谓最新版本,指的是Pod的Spec部分与Deployment中Pod模版里定义的完全一致。
AVAILABLE:当前已经可用的Pod的个数——既是Running状态,又是最新版本,并且已经处于Ready(监控检查正确)状态的Pod个数。
- 查看ReplicaSet (rs)进程
[root@VM-16-14-centos ~]# kubectl get rs go-hello8080-deployment-5988c6db6c
1.3.1、查看滚动发布进程
- 查看发布状态rollout status
[root@VM-16-14-centos ~]# kubectl rollout status deployment/go-hello8080-deployment
[root@VM-16-14-centos ~]# kubectl rollout status deployment/go-hello8080-deployment
Waiting for deployment "go-hello8080-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 8 of 10 updated replicas are available...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 9 of 10 updated replicas are available...
deployment "go-hello8080-deployment" successfully rolled out
1.4、回滚至上一个版本
- 以镜像版本从v3回退至v2为例,使用rollout undo指令
- 通过k describe pod xxxx ,可验证镜像版本确实完成回退
[root@VM-16-14-centos go_hello_8080]# kubectl rollout undo deployment go-hello8080-deployment
[root@VM-16-14-centos go_hello_8080]# kubectl rollout undo deployment go-hello8080-deployment
deployment.apps/go-hello8080-deployment rolled back
[root@VM-16-14-centos ~]# kubectl rollout status deployment/go-hello8080-deployment
Waiting for deployment "go-hello8080-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 8 of 10 updated replicas are available...
Waiting for deployment "go-hello8080-deployment" rollout to finish: 9 of 10 updated replicas are available...
deployment "go-hello8080-deployment" successfully rolled out