执行升级
[root@qd01-stop-k8s-master001 demo]# kubectl apply -f CloneSet.yaml cloneset.apps.kruise.io/nginx-2 configured 使用 kubectl describe查看升级过程 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning FailedScheduling 59m default-scheduler 0/22 nodes are available: 22 pod has unbound immediate PersistentVolumeClaims. Warning FailedScheduling 59m default-scheduler 0/22 nodes are available: 22 pod has unbound immediate PersistentVolumeClaims. Warning FailedScheduling 59m default-scheduler 0/22 nodes are available: 22 pod has unbound immediate PersistentVolumeClaims. Normal Scheduled 59m default-scheduler Successfully assigned default/nginx-2-l54dz to qd01-stop-k8s-node007.ps.easou.com Normal SuccessfulAttachVolume 59m attachdetach-controller AttachVolume.Attach succeeded for volume "pvc-e5302eab-a9f2-4a71-a5a3-4cd43205e8a0" Normal Pulling 58m kubelet Pulling image "nginx:alpine" Normal Pulled 58m kubelet Successfully pulled image "nginx:alpine" in 6.230045975s Normal Killing 55s kubelet Container nginx definition changed, will be restarted Normal Pulling 55s kubelet Pulling image "nginx" Normal Pulled 26s kubelet Successfully pulled image "nginx" in 29.136659264s Normal Created 23s (x2 over 58m) kubelet Created container nginx Normal Started 23s (x2 over 58m) kubelet Started container nginx从输出可以看到,Container nginx definition changed, will be restarted,Pod并没有删除在重建,而是在原来的基础上直接更新了镜像文件,并重启了服务。
原地升级减少了删除重建环节,节省了升级时间和资源调度频率。。。
6、Partition 分批灰度
Partition 的语义是 保留旧版本 Pod 的数量或百分比,默认为 0。这里的 partition 不表示任何 order 序号。
现在我将上面的例子的 image 更新为 nginx:1.19.6-alpine 并且设置 partition=3
kind: CloneSet metadata: labels: app: nginx-2 name: nginx-2 spec: replicas: 5 updateStrategy: type: InPlaceIfPossible inPlaceUpdateStrategy: gracePeriodSeconds: 10 partition: 3 selector: matchLabels: app: nginx-2 template: metadata: labels: app: nginx-2 spec: containers: - name: nginx image: nginx:1.19.6-alpine查看结果
Status: Available Replicas: 5 Collision Count: 0 Label Selector: app=nginx-2 Observed Generation: 6 Ready Replicas: 5 Replicas: 5 Update Revision: nginx-2-7b44cb9c8 Updated Ready Replicas: 2 Updated Replicas: 2 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulUpdatePodInPlace 45m cloneset-controller successfully update pod nginx-2-l54dz in-place(revision nginx-2-5879fd9f7) Normal SuccessfulUpdatePodInPlace 44m cloneset-controller successfully update pod nginx-2-t49mk in-place(revision nginx-2-5879fd9f7) Normal SuccessfulUpdatePodInPlace 43m cloneset-controller successfully update pod nginx-2-b5fdd in-place(revision nginx-2-5879fd9f7) Normal SuccessfulUpdatePodInPlace 43m cloneset-controller successfully update pod nginx-2-jw2zp in-place(revision nginx-2-5879fd9f7) Normal SuccessfulCreate 22m cloneset-controller succeed to create pod nginx-2-zpp8z Normal SuccessfulUpdatePodInPlace 5m22s cloneset-controller successfully update pod nginx-2-zpp8z in-place(revision nginx-2-7b44cb9c8) Normal SuccessfulUpdatePodInPlace 4m55s cloneset-controller successfully update pod nginx-2-jw2zp in-place(revision nginx-2-7b44cb9c8) [root@qd01-stop-k8s-master001 demo]# kubectl get pod -L controller-revision-hash NAME READY STATUS RESTARTS AGE CONTROLLER-REVISION-HASH nginx-2-b5fdd 1/1 Running 1 99m nginx-2-5879fd9f7 nginx-2-jw2zp 1/1 Running 2 99m nginx-2-7b44cb9c8 nginx-2-l54dz 1/1 Running 1 99m nginx-2-5879fd9f7 nginx-2-t49mk 1/1 Running 1 99m nginx-2-5879fd9f7 nginx-2-zpp8z 1/1 Running 1 19m nginx-2-7b44cb9c8从输出信息我们可以看到,Update Revision已经更新为nginx-2-7b44cb9c8,而Pod中只有两个Pod升级了。
由于我们设置了 partition=3,控制器只升级了 2 个 Pod。
Partition 分批灰度功能完善了原生的Pod升级方式,使得升级能够进行更灵活,能够进行灰度上线。超赞。。。
7、最后再演示下发布暂停
用户可以通过设置 paused 为 true 暂停发布,不过控制器还是会做 replicas 数量管理:
首先,我们将示例中image改为nginx:1.18.0 并设置副本数为10,修改后更新yaml,运行结果如下:
[root@qd01-stop-k8s-master001 demo]# kubectl get po -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |sort nginx-2-7lzx9: nginx:1.18.0, nginx-2-b5fdd: nginx:1.18.0, nginx-2-jw2zp: nginx:1.18.0, nginx-2-l54dz: nginx:1.18.0, nginx-2-nknrt: nginx:1.18.0, nginx-2-rgmsc: nginx:1.18.0, nginx-2-rpr5z: nginx:1.18.0, nginx-2-t49mk: nginx:1.18.0, nginx-2-v2bpx: nginx:1.18.0, nginx-2-zpp8z: nginx:1.18.0,