kubenetes 更新回滚
以nginx镜像为例
[root@k8s-master1 opt]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES deploy-nginx-6d46d6548f-7qqcp 1/1 Running 0 3m10s 192.168.122.95 k8s-node4 <none> <none> deploy-nginx-6d46d6548f-ct7f7 1/1 Running 0 3m10s 192.168.169.153 k8s-node2 <none> <none> deploy-nginx-6d46d6548f-dv6mt 1/1 Running 0 3m7s 192.168.107.221 k8s-node3 <none> <none> deploy-nginx-6d46d6548f-fpprd 1/1 Running 0 3m10s 192.168.36.156 k8s-node1 <none> <none> deploy-nginx-6d46d6548f-gqvfz 1/1 Running 0 3m8s 192.168.122.96 k8s-node4 <none> <none> deploy-nginx-6d46d6548f-pvmrr 1/1 Running 0 3m7s 192.168.169.154 k8s-node2 <none> <none>
1.更新 使用--record=true 记录更新内容 我们从nginx:1.14升级nginx:1.15再升级nginx:1.16
[root@k8s-master1 opt]# kubectl set image deployment/deploy-nginx nginx=nginx:1.14 --record=true [root@k8s-master1 opt]# kubectl set image deployment/deploy-nginx nginx=nginx:1.15 --record=true deployment.apps/deploy-nginx image updated [root@k8s-master1 opt]# kubectl set image deployment/deploy-nginx nginx=nginx:1.16 --record=true deployment.apps/deploy-nginx image updated [root@k8s-master1 opt]#
2.查看更新记录,进行回滚
[root@k8s-master1 opt]# kubectl rollout history deployment/deploy-nginx deployment.apps/deploy-nginx REVISION CHANGE-CAUSE 12 kubectl set image deployment/deploy-nginx nginx=nginx:1.14 --record=true 13 kubectl set image deployment/deploy-nginx nginx=nginx:1.15 --record=true 14 kubectl set image deployment/deploy-nginx nginx=nginx:1.16 --record=true
3.指定回滚 --revision=12
[root@k8s-master1 opt]# kubectl rollout undo deployment/deploy-nginx --to-revision=12 deployment.apps/deploy-nginx rolled back [root@k8s-master1 opt]# kubectl rollout history deployment/deploy-nginx deployment.apps/deploy-nginx REVISION CHANGE-CAUSE 13 kubectl set image deployment/deploy-nginx nginx=nginx:1.15 --record=true 14 kubectl set image deployment/deploy-nginx nginx=nginx:1.16 --record=true 15 kubectl set image deployment/deploy-nginx nginx=nginx:1.14 --record=true
4.回滚到上版本
[root@k8s-master1 opt]# kubectl rollout undo deployment/deploy-nginx deployment.apps/deploy-nginx rolled back [root@k8s-master1 opt]# kubectl rollout history deployment/deploy-nginx deployment.apps/deploy-nginx REVISION CHANGE-CAUSE 13 kubectl set image deployment/deploy-nginx nginx=nginx:1.15 --record=true 15 kubectl set image deployment/deploy-nginx nginx=nginx:1.14 --record=true 16 kubectl set image deployment/deploy-nginx nginx=nginx:1.16 --record=true
5.查看回滚状态
[root@k8s-master1 ~]# kubectl rollout status deployment/deploy-nginx Waiting for deployment "deploy-nginx" rollout to finish: 3 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 3 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 3 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 4 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 4 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 4 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 4 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 4 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 5 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 5 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 5 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 5 out of 6 new replicas have been updated... Waiting for deployment "deploy-nginx" rollout to finish: 2 old replicas are pending termination... Waiting for deployment "deploy-nginx" rollout to finish: 2 old replicas are pending termination... Waiting for deployment "deploy-nginx" rollout to finish: 2 old replicas are pending termination... Waiting for deployment "deploy-nginx" rollout to finish: 1 old replicas are pending termination... deployment "deploy-nginx" successfully rolled out