关于Kubernetes-v1.23.6-资源调度-StatefulSet-实现金丝雀发布

发布时间 2023-09-24 16:28:19作者: 520_1351

StatefulSet  也可以采用滚动更新策略,同样是修改 pod template 属性后会触发更新,但是由于 pod 是有序的,在 StatefulSet 中更新时是基于 pod 的顺序倒序更新的

利用滚动更新中的 partition 属性,可以实现简易的灰度发布的效果,记录一下,updateStrategy: 更新策略是与replicas 和 template 同级的位置

例如我们有 5 个 pod,如果当前 partition 设置为 3,那么此时滚动更新时,只会更新那些 序号 >= 3 的 pod

利用该机制,我们可以通过控制 partition 的值,来决定只更新其中一部分 pod,确认没有问题后再主键增大更新的 pod 数量,最终实现全部 pod 更新

1、还是先将sts的副本数更新为5,即一共会有5个pods,可以使用如下命令(版本我们可以使用1.7.9):

kubectl patch sts web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"nginx:1.7.9"}]'

kubectl scale statefulset web --replicas=5

2、此时我们再修改上图位置的,partition的值为3, 图上的值为0,代表滚动更新所有序号的pod,注意序号是从0开始的,可以看到第一个pod的名称为 web-0 

使用 kubectl edit sts web 命令,修改 partition的值为3,也顺便将nginx的版本更新到 1.9.1 , 这样也即是只会更新web-3 和 web-4 两个pod

3、然后可以通过查看pods的描述,看到效果和变化,可以看到,只是更新了序号 >=3 的pods 到1.9.1版本

 

[root@k8s-master ~]# kubectl describe po web-3 | grep Image:
    Image:          nginx:1.9.1
[root@k8s-master ~]# kubectl describe po web-4 | grep Image:
    Image:          nginx:1.9.1
[root@k8s-master ~]# kubectl describe po web-0 | grep Image:
    Image:          nginx:1.7.9
[root@k8s-master ~]# kubectl describe po web-1 | grep Image:
    Image:          nginx:1.7.9
[root@k8s-master ~]# kubectl describe po web-2 | grep Image:
    Image:          nginx:1.7.9
[root@k8s-master ~]#

4、如果发现新版本没有问题,我们就可以继续修改 partition的值为0,就可以完成所有的pod的版本更新了

[root@k8s-master ~]# kubectl edit sts web
statefulset.apps/web edited
[root@k8s-master ~]# kubectl get sts
NAME   READY   AGE
web    5/5     22h
[root@k8s-master ~]# kubectl describe po web-0 | grep Image:
    Image:          nginx:1.9.1
[root@k8s-master ~]# kubectl describe po web-1 | grep Image:
    Image:          nginx:1.9.1
[root@k8s-master ~]# kubectl describe po web-2 | grep Image:
    Image:          nginx:1.9.1
[root@k8s-master ~]#

 

 

 

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/17726150.html