一、镜像更新策略简介
Deployment控制器支持两种镜像更新策略:重建更新和滚动更新(默认),可以通过strategy字段进行配置
配置资料如下:
strategy: # 指定新的Pod替换旧的Pod的策略,支持两个属性
type: # 指定策略类型,支持两种策略
Recreate: # 在创建出新的额Pod之前会先杀掉所有已存在的Pod
RollingUpdate: # 滚动更新,就是杀死一部分,就启动一部分,在更新的过程中,存在两个版本的Pod,可以做到业务无中断
rollingUpdate: # 当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持如下两个属性
maxUnavailable: # 用来指定在升级过程中不可用Pod的最大数量,默认为25%
maxSurge: # 用来指定在升级过程中可以超过期望的Pod的最大数量,默认为25%
二、重建更新
编辑 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: Recreate
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
然后使用如下命令创建资源
[root@master pod_controller]# kubectl apply -f pc_deployment.yaml
namespace/dev created
deployment.apps/pc-deployment created
[root@master pod_controller]#
此时可在另外一个终端中使用如下命令监控pod变化过程
kubectl get pod -n dev -w
然后更新镜像版本,可以通过修改yaml文件,然后使用如下命令修改
kubectl apply -f pc_deployment.yaml
也可以不修改yaml文件,通过如下命令直接修改镜像版本,如修改为1.17.3
kubectl set image deploy pc-depoyment nginx=nginx:1.17.3 -n dev
此时监控窗口输出如下,可以看出此时是一次性将三个pod杀掉,然后重新创建了三个pod
[root@master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pc-deployment-5d9c9b97bb-cxsrn 1/1 Running 0 8s
pc-deployment-5d9c9b97bb-j5vfn 1/1 Running 0 8s
pc-deployment-5d9c9b97bb-z4hjg 1/1 Running 0 8s
pc-deployment-5d9c9b97bb-z4hjg 1/1 Terminating 0 30s
pc-deployment-5d9c9b97bb-j5vfn 1/1 Terminating 0 30s
pc-deployment-5d9c9b97bb-cxsrn 1/1 Terminating 0 30s
pc-deployment-5d9c9b97bb-z4hjg 0/1 Terminating 0 30s
pc-deployment-5d9c9b97bb-j5vfn 0/1 Terminating 0 30s
pc-deployment-5d9c9b97bb-cxsrn 0/1 Terminating 0 30s
pc-deployment-5d9c9b97bb-z4hjg 0/1 Terminating 0 34s
pc-deployment-5d9c9b97bb-z4hjg 0/1 Terminating 0 34s
pc-deployment-5d9c9b97bb-j5vfn 0/1 Terminating 0 34s
pc-deployment-5d9c9b97bb-j5vfn 0/1 Terminating 0 34s
pc-deployment-5d9c9b97bb-cxsrn 0/1 Terminating 0 40s
pc-deployment-5d9c9b97bb-cxsrn 0/1 Terminating 0 40s
pc-deployment-76fd8c7f84-jjtxb 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-z9bwm 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-jjtxb 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-lwwjb 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-z9bwm 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-lwwjb 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-jjtxb 0/1 ContainerCreating 0 0s
pc-deployment-76fd8c7f84-z9bwm 0/1 ContainerCreating 0 0s
pc-deployment-76fd8c7f84-lwwjb 0/1 ContainerCreating 0 0s
pc-deployment-76fd8c7f84-z9bwm 1/1 Running 0 1s
pc-deployment-76fd8c7f84-lwwjb 1/1 Running 0 1s
pc-deployment-76fd8c7f84-jjtxb 1/1 Running 0 1s
三、滚动更新
编写pc_deployment.yaml文件,内容如下,为了演示滚动效果,这里设置3个副本,初始化镜像设置为1.17.1的版本,滚动更新比例设置为25%,如下:
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
然后使用如下命令创建资源
[root@master pod_controller]# kubectl apply -f pc_deployment.yaml
namespace/dev created
deployment.apps/pc-deployment created
[root@master pod_controller]#
这里同样打开一个窗口使用如下命令监视pod的状态
kubectl get pod -n dev -w
这里同样也可以通过修改yaml文件中镜像的版本号和使用kubectl apply -f pc_deployment.yaml命令的方式,或者可以直接通过使用如下命令设置镜像版本号
[root@master pod_controller]# kubectl set image deploy pc-deployment nginx=nginx:1.17.3 -n dev
deployment.apps/pc-deployment image updated
[root@master pod_controller]#
此时通过监视窗口可以看到pod的变化过程,即先创建一个pod,然后停一个pod,再创建一个pod,在停一个pod,再创建一个pod,再停一个pod
[root@master ~]# kubectl get pod -n dev -w
NAME READY STATUS RESTARTS AGE
pc-deployment-5d9c9b97bb-pqm9j 1/1 Running 0 59s
pc-deployment-5d9c9b97bb-ql5ps 1/1 Running 0 60s
pc-deployment-5d9c9b97bb-w49wz 1/1 Running 0 61s
pc-deployment-76fd8c7f84-w4pw7 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-w4pw7 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-w4pw7 0/1 ContainerCreating 0 0s
pc-deployment-76fd8c7f84-w4pw7 1/1 Running 0 1s
pc-deployment-5d9c9b97bb-pqm9j 1/1 Terminating 0 67s
pc-deployment-76fd8c7f84-kmrb7 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-kmrb7 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-kmrb7 0/1 ContainerCreating 0 0s
pc-deployment-5d9c9b97bb-pqm9j 0/1 Terminating 0 67s
pc-deployment-5d9c9b97bb-pqm9j 0/1 Terminating 0 68s
pc-deployment-5d9c9b97bb-pqm9j 0/1 Terminating 0 68s
pc-deployment-76fd8c7f84-kmrb7 1/1 Running 0 1s
pc-deployment-5d9c9b97bb-ql5ps 1/1 Terminating 0 69s
pc-deployment-76fd8c7f84-h4d87 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-h4d87 0/1 Pending 0 0s
pc-deployment-76fd8c7f84-h4d87 0/1 ContainerCreating 0 0s
pc-deployment-5d9c9b97bb-ql5ps 0/1 Terminating 0 70s
pc-deployment-76fd8c7f84-h4d87 1/1 Running 0 1s
pc-deployment-5d9c9b97bb-w49wz 1/1 Terminating 0 71s
pc-deployment-5d9c9b97bb-ql5ps 0/1 Terminating 0 71s
pc-deployment-5d9c9b97bb-ql5ps 0/1 Terminating 0 71s
pc-deployment-5d9c9b97bb-w49wz 0/1 Terminating 0 72s
pc-deployment-5d9c9b97bb-w49wz 0/1 Terminating 0 84s
pc-deployment-5d9c9b97bb-w49wz 0/1 Terminating 0 84s
滚动更新过程如下图所示