一、版本升级过程的变化原理
编辑pc_deployment.yaml文件,内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
使用如下命令创建资源,注意加--record参数,用于记录创建记录信息
[root@master pod_controller]# kubectl apply -f pc_deployment.yaml --record=true
namespace/dev created
deployment.apps/pc-deployment created
[root@master pod_controller]#
然后编辑yaml文件,将镜像版本升级到1.17.2,再次使用上述命令
然后编辑yaml文件,将镜像版本升级到1.17.3,再次使用上述命令
然后编辑yaml文件,将镜像版本升级到1.17.4,再次使用上述命令
此时,通过如下命令可以查看到,有四个ReplicaSet
[root@master pod_controller]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-5d9c9b97bb 0 0 0 4m9s
pc-deployment-6b575b9c45 3 3 3 47s
pc-deployment-76fd8c7f84 0 0 0 88s
pc-deployment-7c7477c7ff 0 0 0 118s
而通过监控ReplicaSet的变化过程如下看可以发现,在每次镜像升级的过程中,其实本质是每次都创建一个新的ReplicaSet,然后依次在新的ReplicaSet上创建pod
[root@master ~]# kubectl get rs -n dev -w
NAME DESIRED CURRENT READY AGE
pc-deployment-5d9c9b97bb 3 3 3 2m7s
pc-deployment-7c7477c7ff 1 0 0 0s
pc-deployment-7c7477c7ff 1 0 0 0s
pc-deployment-7c7477c7ff 1 1 0 0s
pc-deployment-7c7477c7ff 1 1 1 1s
pc-deployment-5d9c9b97bb 2 3 3 2m12s
pc-deployment-5d9c9b97bb 2 3 3 2m12s
pc-deployment-7c7477c7ff 2 1 1 1s
pc-deployment-5d9c9b97bb 2 2 2 2m12s
pc-deployment-7c7477c7ff 2 1 1 1s
pc-deployment-7c7477c7ff 2 2 1 1s
pc-deployment-7c7477c7ff 2 2 2 2s
pc-deployment-5d9c9b97bb 1 2 2 2m13s
pc-deployment-5d9c9b97bb 1 2 2 2m13s
pc-deployment-7c7477c7ff 3 2 2 2s
pc-deployment-5d9c9b97bb 1 1 1 2m13s
pc-deployment-7c7477c7ff 3 2 2 2s
pc-deployment-7c7477c7ff 3 3 2 2s
pc-deployment-7c7477c7ff 3 3 3 24s
pc-deployment-5d9c9b97bb 0 1 1 2m35s
pc-deployment-5d9c9b97bb 0 1 1 2m35s
pc-deployment-5d9c9b97bb 0 0 0 2m35s
pc-deployment-76fd8c7f84 1 0 0 0s
pc-deployment-76fd8c7f84 1 0 0 0s
pc-deployment-76fd8c7f84 1 1 0 0s
pc-deployment-76fd8c7f84 1 1 1 1s
pc-deployment-7c7477c7ff 2 3 3 31s
pc-deployment-7c7477c7ff 2 3 3 31s
pc-deployment-76fd8c7f84 2 1 1 1s
pc-deployment-76fd8c7f84 2 1 1 1s
pc-deployment-7c7477c7ff 2 2 2 31s
pc-deployment-76fd8c7f84 2 2 1 1s
pc-deployment-76fd8c7f84 2 2 2 2s
pc-deployment-7c7477c7ff 1 2 2 32s
pc-deployment-7c7477c7ff 1 2 2 32s
pc-deployment-76fd8c7f84 3 2 2 2s
pc-deployment-7c7477c7ff 1 1 1 32s
pc-deployment-76fd8c7f84 3 2 2 2s
pc-deployment-76fd8c7f84 3 3 2 2s
pc-deployment-76fd8c7f84 3 3 3 3s
pc-deployment-7c7477c7ff 0 1 1 33s
pc-deployment-7c7477c7ff 0 1 1 33s
pc-deployment-7c7477c7ff 0 0 0 33s
pc-deployment-6b575b9c45 1 0 0 0s
pc-deployment-6b575b9c45 1 0 0 0s
pc-deployment-6b575b9c45 1 1 0 0s
pc-deployment-6b575b9c45 1 1 1 22s
pc-deployment-76fd8c7f84 2 3 3 63s
pc-deployment-6b575b9c45 2 1 1 22s
pc-deployment-76fd8c7f84 2 3 3 63s
pc-deployment-6b575b9c45 2 1 1 22s
pc-deployment-6b575b9c45 2 2 1 22s
pc-deployment-76fd8c7f84 2 2 2 63s
pc-deployment-6b575b9c45 2 2 2 45s
pc-deployment-76fd8c7f84 1 2 2 86s
pc-deployment-6b575b9c45 3 2 2 45s
pc-deployment-76fd8c7f84 1 2 2 86s
pc-deployment-76fd8c7f84 1 1 1 86s
pc-deployment-6b575b9c45 3 2 2 45s
pc-deployment-6b575b9c45 3 3 2 45s
pc-deployment-6b575b9c45 3 3 3 46s
pc-deployment-76fd8c7f84 0 1 1 87s
pc-deployment-76fd8c7f84 0 1 1 87s
pc-deployment-76fd8c7f84 0 0 0 87s
二、版本回退
2.1 版本回退简介
deployment控制器支持版本回退,因为在每次升级的时候deployment都是创建了一个新的ReplicaSet,依次,版本回退就是回退到旧的ReplicaSet上即可
deployment版本回退相关的命令是:kubectl rollout 命令,后跟如下选项
- status 显示当前升级状态
- history 显示升级历史记录
- pause 暂停版本升级过程
- resume 继续已经暂停的版本升级过程
- restart 重启版本升级过程
- undo 回滚到上一级版本(可以使用 --to-revision回滚到指定的版本)
2.2 版本回退
如下即查看当前升级状态
[root@master pod_controller]# kubectl rollout status deployment pc-deployment -n dev
deployment "pc-deployment" successfully rolled out
[root@master pod_controller]#
如下命令可以查看到升级历史记录
[root@master pod_controller]# kubectl rollout history deployment pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=pc_deployment.yaml --record=true
2 kubectl apply --filename=pc_deployment.yaml --record=true
3 kubectl apply --filename=pc_deployment.yaml --record=true
4 kubectl apply --filename=pc_deployment.yaml --record=true
[root@master pod_controller]#
通过如下命令即可回退到上个版本
[root@master pod_controller]# kubectl rollout undo deployment pc-deployment -n dev
deployment.apps/pc-deployment rolled back
[root@master pod_controller]#
此时查看历史版本如下,已经没有3了,而是多了一个5
[root@master pod_controller]# kubectl rollout history deployment pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=pc_deployment.yaml --record=true
2 kubectl apply --filename=pc_deployment.yaml --record=true
3 kubectl apply --filename=pc_deployment.yaml --record=true
4 kubectl apply --filename=pc_deployment.yaml --record=true
[root@master pod_controller]#
若直接回退到第一个版本,则使用如下命令即可
[root@master pod_controller]# kubectl rollout undo deployment pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back
[root@master pod_controller]#
此时再次查看历史记录如下,此时1已经没有了,而多出来一个6
[root@master pod_controller]# kubectl rollout history deployment pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
2 kubectl apply --filename=pc_deployment.yaml --record=true
4 kubectl apply --filename=pc_deployment.yaml --record=true
5 kubectl apply --filename=pc_deployment.yaml --record=true
6 kubectl apply --filename=pc_deployment.yaml --record=true
[root@master pod_controller]#