一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第11天, 点击查看活动详情 。
一、
Deployment
回滚
默认情况下,
kubernetes
会在系统中保存所有
Deployment
的
rollout
历史记录,方便随时回退。
Tips:
只有更新
Deployment template
中的
label
和
image
,才会创建一个新的
revision
,而扩缩容不会创建
revision
,所以回退历史
revision
时,只有
Deployment
中的
template
部分才会回退。
(1)举个栗子
进行一次升级,要修改容器的镜像,但是不小心手误写成了
nginx:1.91
,这是一个不存在的版本镜像,所以
Deployment
不会更新成功:
deployment
$ kubectl set image deployment/nginx-deployment nginx=nginx:1.91
deployment.extensions/nginx-deployment image updated
查看 Deployment
的部署过程以及 RS
的状态:
# 可以看到部署过程卡住了,按 Ctrl+C 终止查看
$ kubectl rollout status deployments nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
# 新创建的 nginx-deployment-5b4b548d5f RS 也卡在了第一个 Pod 的创建过程中,主要是镜像拉取不成功
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-5754944d6c 0 0 0 4h23m
nginx-deployment-5b4b548d5f 1 1 0 52s
nginx-deployment-7448597cd5 3 3 3 126m
nginx-deployment-8ff4cd577 3 3 3 26m
查看历史记录
使用 kubectl rollout history
命令查看部署的历史记录,由于创建 Deployment
的时候没有使用 --record
参数,CHANGE-CAUSE
中没有记录下更新每个版本使用的命令
$ kubectl rollout history deployment/nginx-deployment --revision=2
deployment.extensions/nginx-deployment with revision #2
Pod Template:
Labels: app=nginx
k8s=nginx-pod
pod-template-hash=5b4b548d5f
Containers:
nginx:
Image: nginx:1.91
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
撤销本次发布并回滚到上一个部署版本
$ kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment rolled back
# 也可以使用参数 --to-revision 指定回滚到的部署版本
$ kubectl rollout undo deployment/nginx-deployment --to-revision=1
查看deployment
是否回滚到了上一个版本
$ kubectl describe deployment/nginx-deployment
Pod Template:
Labels: app=nginx
k8s=nginx-pod
Containers:
nginx:
Image: nginx:1.9.1
NewReplicaSet: nginx-deployment-8ff4cd577 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 30m deployment-controller Scaled up replica set nginx-deployment-8ff4cd577 to 3
Normal ScalingReplicaSet 4m50s deployment-controller Scaled up replica set nginx-deployment-5b4b548d5f to 1
Normal ScalingReplicaSet 22s deployment-controller Scaled down replica set nginx-deployment-5b4b548d5f to 0
从 Events
事件中看到:
Deployment
回滚的过程是:先创建一个新 RS(nginx-deployment-8ff4cd577)
扩容到 3 个 Pod
,然后旧的 RS(nginx-deployment-5b4b548d5f)
的 Pod
从 1 缩减为 0。
二、Deployment
暂停与恢复
需要频繁的对 Deployment
的配置进行修改,如果每修改一次就触发一次更新的话会显得比较麻烦,这个时候可以暂停 Deployment
的更新操作,多次修改配置,然后再恢复 Deployment
,一次性触发完整的更新操作。
使用文件进行创建(注意创建的时候可以加上 --record
参数)
$ kubectl create -f nginx-deployment.yaml --record
deployment.apps/nginx-deployment created
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 28s
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-5754944d6c 3 3 3 34s
使用命令kubectl rollout pause
暂停Deployment
的更新操作
$ kubectl rollout pause deployment/nginx-deployment
deployment.extensions/nginx-deployment paused
修改 Deployment
的镜像信息为 nginx:1.9.1
:
$ kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
deployment.extensions/nginx-deployment image updated
查看历史记录
$ kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
1 kubectl create --filename=nginx-deployment.yaml --record=true
更新Deployment
配置,限制容器的资源使用
$ kubectl set resources deployment nginx-deployment -c=nginx --limits=cpu=200m,memory=512Mi
deployment.extensions/nginx-deployment resource requirements updated
恢复Deployment
的部署操作
$ kubectl rollout resume deploy nginx-deployment
deployment.extensions/nginx-deployment resumed
查看RS
资源和Deployment
的详细信息
# 可以看到新创建的 RS(nginx-deployment-7576c67d77)
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-5754944d6c 0 0 0 6m40s
nginx-deployment-7576c67d77 3 3 3 58s
# 观察 nginx-deployment 的配置信息是否修改
$ kubectl describe deployment/nginx-deployment
Annotations: deployment.kubernetes.io/revision: 2
kubernetes.io/change-cause: kubectl create --filename=nginx-deployment.yaml --record=true
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.9.1 # 镜像已经更新
Port: 80/TCP
Host Port: 0/TCP
Limits: # 容器资源的限制已经生效
cpu: 200m
memory: 512Mi
软件安装工程师