金丝雀发布/灰度发布(Canary Release)
一、金丝雀发布简介
Deployment控制器**支持自定义控制更新过程中的滚动节奏,**如"暂停(pause)"或"继续(resume)"更新操作。比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,在筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。
二、更新deployment的版本,并配置暂停deployment
2.1、创建pods
css
kubectl create deployment nginx-test --image=nginx:1.14 --replicas=3
kubectl get pods,deploy -o wide

2.2、发布服务
css
kubectl expose deploy nginx-test --port=80 --target-port=80 --name=nginx-service --type=NodePort
kubectl get svc -o wide

2.3、查看nginx版本
css
curl -I 192.168.10.103:31027
kubectl describe deployments nginx-test | grep Image

2.4、定义版本change-cause
2.4.1 查看历史版本
在不定义CHANGE-CAUSE的情况下,缺省值为,当历史版本较多时,不便于咱们回滚时辨认版本号。因此,建议定义CHANGE-CAUSE为服务版本以帮助咱们辨认当前服务。
css
kubectl rollout history deployment/nginx-test

2.4.2 定义版本
一般通过修改配置的方式定义change-cause
css
[root@master ~]# kubectl edit deploy/nginx-test
......
kind: Deployment
metadata:
annotations:
#下行可定义历史版本revision
deployment.kubernetes.io/revision: "1"
#在Deployment的matadata项下的annotations中如下行定义change-cause
kubernetes.io/change-cause: "nginx1.14"
......

2.4.3 再次查看历史版本
css
kubectl rollout history deployment/nginx-test

2.4.4 更新nginx版本为1.15并配置暂停
css
kubectl set image deployment/nginx-test nginx=nginx:1.15 && kubectl rollout pause deployment/nginx-test

2.4.5 观察更新状态
css
kubectl rollout status deployment/nginx-test

2.4.6 监控更新的过程
可以看到已经新增了一个pod,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause暂停命令
css
kubectl get pods -w

2.4.7 查看nginx版本
css
kubectl get pod -o wide

2.4.8 查看并更新历史版本change-cause
css
kubectl rollout history deploy/nginx-test

css
[root@master ~]# kubectl edit deploy/nginx-test
kind: Deployment
metadata:
annotations:
#下行的revison自动更新为2
deployment.kubernetes.io/revision: "2"
#修改下行的change-cause为nginx1.15
kubernetes.io/change-cause: nginx1.15


2.4.9 resume继续更新
css
kubectl rollout resume deploy/nginx-test

2.4.10 查看最后的更新情况
css
kubectl get pods -w
