Depoloymen 回滚/暂停/启动
1. 历史记录和回滚
- 写一个示例
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-test
namespace: test
spec:
replicas: 1
template:
metadata:
labels:
app: nginx-test
spec:
containers:
- name: nginx-test
image: "harbocto.XXX.com.cn/public/nginx"
ports:
- containerPort: 80
创建deployment,等pod启动后,修改成错误的镜像,然后applay一下,pod处于错误状态:
[root@DoM01 deployment]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
nginx-test-c5c7fccb6-vtbz4 0/1 ErrImagePull 0 1h
然后进行测试如下:
1.1 历史记录
- 查看历史版本
[root@DoM01 ~]# kubectl rollout -n test history deployment nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 <none>
2 <none>
- 某一版本详细信息
[root@DoM01 deployment]# kubectl rollout -n test history deployment nginx-test --revision=1
deployment.extensions/nginx-test with revision #1
Pod Template:
Labels: app=nginx-test
pod-template-hash=5f9454646c
Containers:
nginx-test:
Image: harbocto.XXX.com.cn/public/nginx
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
1.2 撤销本次发布(即回滚到上一版)
- 回滚到上一版
[root@DoM01 deployment]# kubectl rollout undo -n test deployment/nginx-test
deployment.extensions/nginx-test rolled back
- 查看可知,pod启动起来了。(如果用edit可以看到镜像已经是第一版的正确镜像了。)
[root@DoM01 deployment]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
nginx-test-5f9454646c-v6w74 1/1 Running 0 9s
- 此时查看历史版本,可见刚才的 1版 变成了 3版
[root@DoM01 deployment]# kubectl rollout history -n test deployment nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
2 <none>
3 <none>
1.3 回滚到指定版本
添加 –to-revision=1 参数即可
[root@DoM01 deployment]# kubectl rollout -n test undo deployment/nginx-test --to-revision=2
deployment/nginx-test 和 deployment nginx-test 这里是一样的,只是写法不同。
2. 暂停/恢复
2.1 暂停测试
- 命令
kubectl rollout pause xxxx
[root@DoM01 deployment]# kubectl rollout pause -n test deployment nginx-test
deployment.extensions/nginx-test paused
- 查看,如下可见,似乎deployment和pod都没有任何变化
[root@DoM01 deployment]# kubectl get deployments -n test
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 1/1 1 1 7d23h
[root@DoM01 deployment]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
nginx-test-5f9454646c-v6w74 1/1 Running 0 13m
- 修改成错误的镜像
[root@DoM01 deployment]# kubectl set image -n test deployment nginx-test nginx-test=nginx:1.15
deployment.extensions/nginx-test image updated
- 查看可见,仍有一个pod在正常运行。虽然镜像已经是刚才更改的了,但是UP-TO-DATE 却是0。
[root@DoM01 deployment]# kubectl get deployments -n test -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-test 1/1 0 1 7d23h nginx-test nginx:1.15 app=nginx-test
- 查看历史版本
可见,并没有新的版本产生
[root@DoM01 deployment]# kubectl rollout -n test history deployment nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
2 <none>
3 <none>
2.2 恢复
- 命令
kubectl rollout resume deploy deployment_name
[root@DoM01 deployment]# kubectl rollout resume deploy -n test nginx-test
deployment.extensions/nginx-test resumed
- 查看历史版本,多了一个4
[root@DoM01 deployment]# kubectl rollout history -n test deployment nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>
3. 小结
- 查看历史版本
kubectl rollout history deployment Deployment_Name
- 查看某版本详细信息
kubectl rollout history deployment Deployment_Name --revision=N
添加 ** --revision=N** 参数,N是版本号
- 回滚到上一版
kubectl rollout undo -n test deployment Deployment_Name
- 回滚到制定版本
kubectl rollout undo -n test deployment Deployment_Name --to-revision=N
添加 –to-revision=N 参数,N是版本号
- 暂停
kubectl rollout pause deployment Deployment_Name
- 恢复
kubectl rollout resume deploy Deployment_Name