Kubernetes----Deployment控制器实现版本滚动发布

【原文链接】

一、镜像更新策略简介

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

滚动更新过程如下图所示
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值