Pod滚动更新与回滚

  • Post author:
  • Post category:Kubernetes
  • Page Views 597 阅读

1.场景

应用两个配置文件deploy-nginx-v1.yaml、deploy-nginx-v2.yaml,nginx镜像分别为1.16.1与latest。

2.实验

应用配置文件deploy-nginx-v1.yaml。

fxx2@kube-node-1:~/yaml$ kubectl apply -f deploy-nginx-v1.yaml
deployment.apps/nginx-deploy created
fxx2@kube-node-1:~/yaml$ kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deploy-848ff6b4cf 2 2 0 6s nginx nginx:1.16.1 app=web_server,pod-template-hash=848ff6b4cf
fxx2@kube-node-1:~/yaml$

应用配置文件deploy-nginx-v2.yaml,将nginx版本升级为latest。

fxx2@kube-node-1:~/yaml$ kubectl apply -f deploy-nginx-v2.yaml
deployment.apps/nginx-deploy configured
fxx2@kube-node-1:~/yaml$ kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deploy-749bb6d465 2 2 2 4s nginx nginx:latest app=web_server,pod-template-hash=749bb6d465
nginx-deploy-848ff6b4cf 0 0 0 3m27s nginx nginx:1.16.1 app=web_server,pod-template-hash=848ff6b4cf
fxx2@kube-node-1:~/yaml$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deploy-749bb6d465-nl72r 1/1 Running 0 3m52s
nginx-deploy-749bb6d465-qzsv4 1/1 Running 0 3m47s

可以看到发生如下变化:
新创建的RS:nginx-deploy-749bb6d465,镜像为latest,并且管理了两个pod。
旧RS:nginx-deploy-848ff6b4cf,已经没有pod。
使用describe命令查看具体过程,每次只更新替换一个pod。
fxx2@kube-node-1:~/yaml$ kubectl describe deploy nginx-deploy
....
OldReplicaSets: <none>
NewReplicaSet: nginx-deploy-749bb6d465 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 20m deployment-controller Scaled up replica set nginx-deploy-848ff6b4cf to 2
Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set nginx-deploy-749bb6d465 to 1
Normal ScalingReplicaSet 17m deployment-controller Scaled down replica set nginx-deploy-848ff6b4cf to 1
Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set nginx-deploy-749bb6d465 to 2
Normal ScalingReplicaSet 17m deployment-controller Scaled down replica set nginx-deploy-848ff6b4cf to 0
fxx2@kube-node-1:~/yaml$

3.回滚

使用kubectl rollout history可以查看变动的版次(revision),可以使用revisionHistoryLimit属性设定revision的数量。

fxx2@kube-node-1:~/yaml$ kubectl rollout history deployment nginx-deploy
deployment.apps/nginx-deploy
REVISION CHANGE-CAUSE
1 <none>
2 <none>

上面CHANGE-CAUSE记录值为空,回滚时不方便确定配置文件,可以使用–record参数记录,下面重新开始测试:

fxx2@kube-node-1:~/yaml$ kubectl apply -f deploy-nginx-v1.yaml --record
deployment.apps/nginx-deploy created
fxx2@kube-node-1:~/yaml$
fxx2@kube-node-1:~/yaml$ kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deploy-848ff6b4cf 2 2 2 20s nginx nginx:1.16.1 app=web_server,pod-template-hash=848ff6b4cf
fxx2@kube-node-1:~/yaml$ kubectl apply -f deploy-nginx-v2.yaml --record
deployment.apps/nginx-deploy configured
fxx2@kube-node-1:~/yaml$
fxx2@kube-node-1:~/yaml$ kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deploy-749bb6d465 2 2 2 12s nginx nginx:latest app=web_server,pod-template-hash=749bb6d465
nginx-deploy-848ff6b4cf 0 0 0 39s nginx nginx:1.16.1 app=web_server,pod-template-hash=848ff6b4cf

fxx2@kube-node-1:~/yaml$ kubectl rollout history deployment nginx-deploy
deployment.apps/nginx-deploy
REVISION CHANGE-CAUSE
1 kubectl apply --filename=deploy-nginx-v1.yaml --record=true
2 kubectl apply --filename=deploy-nginx-v2.yaml --record=true

fxx2@kube-node-1:~/yaml$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deploy-749bb6d465-fx2r7 1/1 Running 0 4m11s 10.244.0.249 kube-node-1 <none> <none>
nginx-deploy-749bb6d465-vlvcc 1/1 Running 0 4m16s 10.244.0.248 kube-node-1 <none> <none>
fxx2@kube-node-1:~/yaml$

回滚至nginx1.16.1版本

fxx2@kube-node-1:~/yaml$ kubectl rollout undo deployment nginx-deploy --to-revision=1
deployment.apps/nginx-deploy rolled back
fxx2@kube-node-1:~/yaml$ kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deploy-749bb6d465 0 0 0 7m2s nginx nginx:latest app=web_server,pod-template-hash=749bb6d465
nginx-deploy-848ff6b4cf 2 2 2 7m29s nginx nginx:1.16.1 app=web_server,pod-template-hash=848ff6b4cf
fxx2@kube-node-1:~/yaml$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deploy-848ff6b4cf-8lz4l 1/1 Running 0 27s 10.244.0.250 kube-node-1 <none> <none>
nginx-deploy-848ff6b4cf-gnd7t 1/1 Running 0 21s 10.244.0.251 kube-node-1 <none> <none>​

 


「 文章如果对你有帮助,请点个赞哦^^ 」 

0