K8S之Pod控制器Deployment(Deploy)

文章目录

    • 基本介绍
    • Deployment的资源清单文件
    • 创建deployment
    • 扩缩容
    • 镜像更新
    • 版本回退
    • 金丝雀发布
    • 删除deploy

基本介绍

为了更好的解决服务编排的问题,kubernetes在V1.2版本开始,引入了Deployment控制器。

Deployment对象,顾名思义,是用于部署应用的对象。它使Kubernetes中最常用的一个对象,它为ReplicaSet和Pod的创建提供了一种声明式的定义方法,从而无需手动创建ReplicaSet和Pod对象(使用Deployment而不直接创建ReplicaSet是因为Deployment对象拥有许多ReplicaSet没有的特性,例如滚动升级和回滚)。

deployment也是保证pod高可用的一种方式,明明已经有RC(Replication Controller),为什么还要引入deployment呢?

是因为deployment解决了RC(Replication Controller)的一个痛点。是因为RC在进行版本升级的时候,会造成一个问题,就是它的svc(Service)短时间内访问不了了,deployment就是解决这个问题的。

值得一提的是,Deployment控制器并不直接管理pod,而是通过管理ReplicaSet来间接管理Pod,即:Deployment管理ReplicaSet,ReplicaSet管理Pod。所以Deployment比ReplicaSet功能更加强大。

K8S之Pod控制器Deployment(Deploy)_第1张图片

Deployment可以帮我们做什么:

  • 定义一组Pod期望数量,Controller会维持Pod数量与期望数量一致
  • 配置Pod的发布方式,controller会按照给定的策略更新Pod,保证更新过程中不可用Pod维持在限定数量范围内
  • 如果发布有问题支持回滚

典型的应用场景包括:

  • 定义 Deployment 来创建 Pod 和 ReplicaSet
  • 滚动升级和回滚应用
  • 扩容和缩容
  • 暂停和继续 Deployment

Deployment的资源清单文件

apiVersion: apps/v1 	# 版本号
kind: Deployment 		# 类型       
metadata: 				# 元数据
  name: 				# rs名称 
  namespace:		 	# 所属命名空间 
  labels: 				#标签
    controller: deploy  
spec: 					# 详情描述
  replicas: 3 			# 副本数量
  revisionHistoryLimit: 3 		# 保留历史版本,主要作用是实现版本回退
  paused: false 				# 暂停部署,默认是false。当deploy创建成功是否立即创建pod,false表示立即创建。true表示不立即创建
  progressDeadlineSeconds: 600 	# 部署超时时间(s),默认是600
  strategy: 		# 镜像更新策略
    type: RollingUpdate 		# 滚动更新策略
    rollingUpdate: 				# 滚动更新
      maxSurge: 30% 			# 最大额外可以存在的副本数,可以为百分比,也可以为整数
      maxUnavailable: 30% 		# 最大不可用状态的 Pod 的最大值,可以为百分比,也可以为整数
  selector: 			# 选择器,通过它指定该控制器管理哪些pod
    matchLabels:     	# Labels匹配规则
      app: nginx-pod
#   matchExpressions: 	# Expressions匹配规则
#     - {key: app, operator: In, values: [nginx-pod]}
  template: 		# 模板,当副本数量不足时,会根据下面的模板创建pod副本。该行及以下是Deployment控制器控制的pod的信息
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80
使用yaml资源清单文件创建 Deployment 流程:

1. 用户通过 kubectl 创建 Deployment。
2. Deployment 创建 ReplicaSet。
3. ReplicaSet 创建 Pod。
对象的命名方式是:子对象的名字 = 父对象名字 + 随机字符串或数字

K8S之Pod控制器Deployment(Deploy)_第2张图片

创建deployment

# 编写资源清单文件
[root@k8s-master ~]# vim pc-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pc-deployment
  namespace: test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80
# 创建deploy
[root@k8s-master ~]# kubectl create -f pc-deployment.yaml
deployment.apps/pc-deployment created

# 查看deploy
[root@k8s-master ~]# kubectl get deploy pc-deployment -n test -o wide
NAME            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
pc-deployment   3/3     3            3           92s   nginx        nginx:1.17.1   app=nginx-pod
# UP-TO-DATE 最新版本的pod数量 ; AVAILABLE 当前可用的pod数量

# 查看deploy创建的rs, rs的名称是在deployment的名字后加上一个10位数的随机串
[root@k8s-master ~]# kubectl get rs -n test -o wide
NAME                       DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         SELECTOR
pc-deployment-6696798b78   3         3         3       3m57s   nginx        nginx:1.17.1   app=nginx-pod,pod-template-hash=6696798b78

# 查看rs创建的pod, pod的名称是在rs的名字后面再加上一个5位数的随机串
[root@k8s-master ~]# kubectl get pod -n test -o wide
NAME                             READY   STATUS    RESTARTS   AGE    IP            NODE         NOMINATED NODE   READINESS GATES
pc-deployment-6696798b78-4z8pt   1/1     Running   0          3m6s   10.244.2.53   k8s-node02   <none>           <none>
pc-deployment-6696798b78-8679f   1/1     Running   0          3m6s   10.244.1.72   k8s-node01   <none>           <none>
pc-deployment-6696798b78-f6qrp   1/1     Running   0          3m6s   10.244.1.71   k8s-node01   <none>           <none>

扩缩容

两种方式:

  1. 直接编辑资源清单文件
kubectl edit deploy 控制器名 -n namespace
# 然后修改文件中的 spec:replicas:副本数量 即可

# 如果是以配置清单文件创建的控制器,也可以直接` vim 配置清单文件 ` 修改replicas指定的副本数量,
# 然后使用` kubectl apply -f 配置清单文件 `进行扩缩用
  1. 命令实现
kubectl scale deploy 控制器名 --replicas=副本数量 -n namespace	

【例 】

# 使用上面创建好的deploy
[root@k8s-master ~]# kubectl get deploy -n test -o wide
NAME            READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
pc-deployment   3/3     3            3           9m29s   nginx        nginx:1.17.1   app=nginx-pod

# 使用第二种方式调整副本数量为5个
[root@k8s-master ~]# kubectl scale deploy pc-deployment --replicas=5 -n test
deployment.apps/pc-deployment scaled

# 查看deploy
[root@k8s-master ~]# kubectl get deploy -n test -o wide
NAME            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
pc-deployment   5/5     5            5           11m   nginx        nginx:1.17.1   app=nginx-pod

# 查看rs
[root@k8s-master ~]# kubectl get rs -n test -o wide
NAME                       DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
pc-deployment-6696798b78   5         5         5       11m   nginx        nginx:1.17.1   app=nginx-pod,pod-template-hash=6696798b78

# 查看pod
[root@k8s-master ~]# kubectl get pod -n test -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
pc-deployment-6696798b78-4z8pt   1/1     Running   0          10m   10.244.2.53   k8s-node02   <none>           <none>
pc-deployment-6696798b78-8679f   1/1     Running   0          10m   10.244.1.72   k8s-node01   <none>           <none>
pc-deployment-6696798b78-dkb9x   1/1     Running   0          54s   10.244.2.55   k8s-node02   <none>           <none>
pc-deployment-6696798b78-f6qrp   1/1     Running   0          10m   10.244.1.71   k8s-node01   <none>           <none>
pc-deployment-6696798b78-hl2vd   1/1     Running   0          54s   10.244.2.54   k8s-node02   <none>           <none>

# 使用第一种方式将副本数调整为4
[root@k8s-master ~]# kubectl edit deploy pc-deployment -n test
#   replicas: 5  # 找到该行将5改为4,保存退出即可
deployment.apps/pc-deployment edited

# 查看deploy,rs,pod
[root@k8s-master ~]# kubectl get deploy,rs,pod -n test -o wide
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/pc-deployment   4/4     4            4           15m   nginx        nginx:1.17.1   app=nginx-pod

NAME                                       DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
replicaset.apps/pc-deployment-6696798b78   4         4         4       15m   nginx        nginx:1.17.1   app=nginx-pod,pod-template-hash=6696798b78

NAME                                 READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
pod/pc-deployment-6696798b78-4z8pt   1/1     Running   0          13m     10.244.2.53   k8s-node02   <none>           <none>
pod/pc-deployment-6696798b78-8679f   1/1     Running   0          13m     10.244.1.72   k8s-node01   <none>           <none>
pod/pc-deployment-6696798b78-dkb9x   1/1     Running   0          3m51s   10.244.2.55   k8s-node02   <none>           <none>
pod/pc-deployment-6696798b78-f6qrp   1/1     Running   0          13m     10.244.1.71   k8s-node01   <none>           <none>

镜像更新

deployment支持两种更新策略:重建更新以及滚动更新(默认)。

重建更新:一次性删除老版本pod,再立即创建同等数量的新版本的pod
滚动更新:先删除一部分老版本的pod,再创建启动一部分新版本的pod,再删除老版本,再启动一部分新版本,直到更新完毕(期间老版本和新版本是同时存在,新版本的pod逐渐更新老版本的pod)

可以通过strategy指定策略类型,支持两个属性:

strategy: 	# 指定新的Pod替换旧的Pod的策略, 支持两个属性:
  type: 	# 指定策略类型,支持两种策略
    Recreate 	# 在创建出新的Pod之前会先杀掉所有已存在的Pod
    RollingUpdate 	# 滚动更新,就是杀死一部分,就启动一部分,在更新过程中,存在两个版本Pod
  rollingUpdate: 	# 当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持两个属性:
    maxUnavailable 	# 用来指定在升级过程中不可用Pod的最大数量,默认为25%。
    maxSurge 		# 用来指定在升级过程中可以超过期望的Pod的最大数量,默认为25%。

实现镜像更新的两种方式:

  1. 直接编辑资源清单文件
kubectl edit deploy 控制器名 -n namespace
# 然后修改文件中的 image: xxxxxx 即可

# 如果是以配置清单文件创建的控制器,也可以直接` vim 配置清单文件 ` 修改image指定的镜像,
# 然后使用` kubectl apply -f 配置清单文件 `进行镜像升级
  1. 命令实现
kubectl set image deploy 控制器名 容器名=镜像版本 -n namespace

【例 】

# 重建更新实现镜像变更

# 编辑资源清单文件
[root@k8s-master ~]# vim pc-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pc-deployment
  namespace: test
spec:
  strategy:
    type: Recreate    # 指定更新镜像的策略为重建更新策略(即一次性全部退出旧版本,同时全部升级到新版本)
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80

# 创建该deploy
[root@k8s-master ~]# kubectl create -f pc-deployment.yaml
deployment.apps/pc-deployment created

# 查看
[root@k8s-master ~]# kubectl get deploy,pod  -n test -o wide
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/pc-deployment   3/3     3            3           92s   nginx        nginx:1.17.1   app=nginx-pod

NAME                                 READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
pod/pc-deployment-6696798b78-bpgmz   1/1     Running   0          92s   10.244.2.56   k8s-node02   <none>           <none>
pod/pc-deployment-6696798b78-fms7b   1/1     Running   0          92s   10.244.2.57   k8s-node02   <none>           <none>
pod/pc-deployment-6696798b78-jpbxm   1/1     Running   0          92s   10.244.1.73   k8s-node01   <none>           <none>

# 使用第一种方式进行镜像更新为1.17.2
[root@k8s-master ~]# kubectl edit deploy pc-deployment -n test
#       - image: nginx:1.17.1 找到该行将1.17.1 更改为1.17.2 后,保存退出
deployment.apps/pc-deployment edited

# 在执行上步的同时观察升级过程
[root@k8s-master ~]# kubectl get pod -n test -w
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-6696798b78-bpgmz   1/1     Running   0          2m18s
pc-deployment-6696798b78-fms7b   1/1     Running   0          2m18s
pc-deployment-6696798b78-jpbxm   1/1     Running   0          2m18s
pc-deployment-6696798b78-bpgmz   1/1     Terminating   0          4m38s
pc-deployment-6696798b78-fms7b   1/1     Terminating   0          4m38s
pc-deployment-6696798b78-jpbxm   1/1     Terminating   0          4m38s
pc-deployment-6696798b78-fms7b   0/1     Terminating   0          4m38s
pc-deployment-6696798b78-bpgmz   0/1     Terminating   0          4m38s
pc-deployment-6696798b78-jpbxm   0/1     Terminating   0          4m38s
pc-deployment-6696798b78-bpgmz   0/1     Terminating   0          4m42s
pc-deployment-6696798b78-bpgmz   0/1     Terminating   0          4m42s
pc-deployment-6696798b78-fms7b   0/1     Terminating   0          4m42s
pc-deployment-6696798b78-fms7b   0/1     Terminating   0          4m42s
pc-deployment-6696798b78-jpbxm   0/1     Terminating   0          4m50s
pc-deployment-6696798b78-jpbxm   0/1     Terminating   0          4m50s
pc-deployment-c848d767-npgbh     0/1     Pending       0          0s
pc-deployment-c848d767-npgbh     0/1     Pending       0          0s
pc-deployment-c848d767-h8qlx     0/1     Pending       0          0s
pc-deployment-c848d767-ljzbv     0/1     Pending       0          0s
pc-deployment-c848d767-h8qlx     0/1     Pending       0          0s
pc-deployment-c848d767-npgbh     0/1     ContainerCreating   0          0s
pc-deployment-c848d767-ljzbv     0/1     Pending             0          0s
pc-deployment-c848d767-h8qlx     0/1     ContainerCreating   0          0s
pc-deployment-c848d767-ljzbv     0/1     ContainerCreating   0          0s
pc-deployment-c848d767-h8qlx     1/1     Running             0          40s
pc-deployment-c848d767-ljzbv     1/1     Running             0          50s
pc-deployment-c848d767-npgbh     1/1     Running             0          55s
# 可以看到,因为使用的是重建更新策略,所以旧版本的容器直接全部退出,然后拉取新版本镜像,运行新版本容器

# 滚动更新实现镜像变更
# 修改上面的deployment的镜像更新策略从重建更新改为滚动更新
[root@k8s-master ~]# vim pc-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pc-deployment
  namespace: test
spec:
  strategy:	# 镜像更新策略
    type: RollingUpdate     # 滚动更新
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80

# 应用该配置
[root@k8s-master ~]# kubectl apply -f pc-deployment.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/pc-deployment configured

# 查看
[root@k8s-master ~]# kubectl get deploy,rs,pod -n test -o wide
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/pc-deployment   3/3     3            3           4s    nginx        nginx:1.17.1   app=nginx-pod

NAME                                       DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
replicaset.apps/pc-deployment-6696798b78   3         3         3       4s    nginx        nginx:1.17.1   app=nginx-pod,pod-template-hash=6696798b78

NAME                                 READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
pod/pc-deployment-6696798b78-6g949   1/1     Running   0          4s    10.244.1.94   k8s-node01   <none>           <none>
pod/pc-deployment-6696798b78-pdvt2   1/1     Running   0          4s    10.244.2.75   k8s-node02   <none>           <none>
pod/pc-deployment-6696798b78-qd2bl   1/1     Running   0          4s    10.244.2.74   k8s-node02   <none>           <none>

# 使用第二种方式将镜像版本1.17.1修改为1.17.3
[root@k8s-master ~]# kubectl set image deploy pc-deployment nginx=1.17.3 -n test
deployment.apps/pc-deployment image updated

# # 在执行上步的同时观察升级过程
# 观察升级过程
[root@k8s-master ~]# kubectl get pods -n test -w
NAME                           READY   STATUS    RESTARTS   AGE
pc-deployment-c848d767-8rbzt   1/1     Running   0          31m
pc-deployment-c848d767-h4p68   1/1     Running   0          31m
pc-deployment-c848d767-hlmz4   1/1     Running   0          31m
pc-deployment-c848d767-rrqcn   1/1     Running   0          31m

pc-deployment-966bf7f44-226rx   0/1     Pending             0          0s
pc-deployment-966bf7f44-226rx   0/1     ContainerCreating   0          0s
pc-deployment-966bf7f44-226rx   1/1     Running             0          1s
pc-deployment-c848d767-h4p68    0/1     Terminating         0          34m

pc-deployment-966bf7f44-cnd44   0/1     Pending             0          0s
pc-deployment-966bf7f44-cnd44   0/1     ContainerCreating   0          0s
pc-deployment-966bf7f44-cnd44   1/1     Running             0          2s
pc-deployment-c848d767-hlmz4    0/1     Terminating         0          34m

pc-deployment-966bf7f44-px48p   0/1     Pending             0          0s
pc-deployment-966bf7f44-px48p   0/1     ContainerCreating   0          0s
pc-deployment-966bf7f44-px48p   1/1     Running             0          0s
pc-deployment-c848d767-8rbzt    0/1     Terminating         0          34m

pc-deployment-966bf7f44-dkmqp   0/1     Pending             0          0s
pc-deployment-966bf7f44-dkmqp   0/1     ContainerCreating   0          0s
pc-deployment-966bf7f44-dkmqp   1/1     Running             0          2s
pc-deployment-c848d767-rrqcn    0/1     Terminating         0          34m

# 至此,新版本的pod创建完毕,就版本的pod销毁完毕
# 中间过程是滚动进行的,也就是边销毁边创建

滚动更新的过程:
K8S之Pod控制器Deployment(Deploy)_第3张图片

版本回退

deployment支持版本升级过程中的暂停、继续功能以及版本回退等诸多功能

kubectl rollout	<command> 控制器 控制器名 -n namespace	# 版本升级相关功能

# 支持以下命令:

		status 显示当前升级状态

		history 显示 升级历史记录

		pause 暂停版本升级过程

		resume 继续已经暂停的版本升级过程

		restart 重启版本升级过程

		undo 回滚到上一级版本(可以使用--to-revision回滚到指定版本)

【例 】

# 编写deploy资源配置文件
[root@k8s-master ~]# vim pc-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pc-deployment
  namespace: test
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80

# 创建deploy(--record表示记录版本更新过程)
[root@k8s-master ~]# kubectl create -f pc-deployment.yaml --record
deployment.apps/pc-deployment created

# 查看
[root@k8s-master ~]# kubectl get deploy,rs,pod -n test -o wide 
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/pc-deployment   3/3     3            3           96s   nginx        nginx:1.17.1   app=nginx-pod

NAME                                       DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
replicaset.apps/pc-deployment-6696798b78   3         3         3       96s   nginx        nginx:1.17.1   app=nginx-pod,pod-template-hash=6696798b78

NAME                                 READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
pod/pc-deployment-6696798b78-6sfqm   1/1     Running   0          96s   10.244.1.125   k8s-node01   <none>           <none>
pod/pc-deployment-6696798b78-npjkj   1/1     Running   0          96s   10.244.2.105   k8s-node02   <none>           <none>
pod/pc-deployment-6696798b78-p7pw5   1/1     Running   0          96s   10.244.2.106   k8s-node02   <none>           <none>

# 镜像更新
[root@k8s-master ~]# kubectl set image deploy pc-deployment nginx=1.17.2 -n test
deployment.apps/pc-deployment image updated

# 更新完毕后,查看rs
[root@k8s-master ~]# kubectl get rs -n test
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-54f9bd68f7   0         0         0       4m16s
pc-deployment-6696798b78   3         3         3       88s
# 发现原来的rs依旧存在,只是pod的数量变为了0,而后又产生一个新的rs,pod数量为3
# 其实deploy能够进行版本回退的原因就是通过记录历史rs来实现的

# 也可以通过以下方式查看镜像更新是否成功
[root@k8s-master ~]# kubectl rollout status deploy pc-deployment -n test
deployment "pc-deployment" successfully rolled out

# 查看镜像更新的历史(如果未查看到任何信息,在创建时未使用 --record 选项)
[root@k8s-master ~]# kubectl rollout history deploy pc-deployment -n test
deployment.apps/pc-deployment 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=pc-deployment.yaml --record=true 	# 第一次创建的
2         kubectl create --filename=pc-deployment.yaml --record=true	# 更新的

# 再对镜像进行升级
[root@k8s-master ~]# kubectl set image deploy pc-deployment nginx=1.17.3 -n test

# 查看镜像更新是否成功
[root@k8s-master ~]# kubectl rollout status deploy pc-deployment -n test
deployment "pc-deployment" successfully rolled out

# 再次查看更新历史记录
[root@k8s-master ~]# kubectl rollout history deploy pc-deployment -n test
deployment.apps/pc-deployment 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=pc-deployment.yaml --record=true 	# 第一次创建的(版本为1.17.1)
2         kubectl create --filename=pc-deployment.yaml --record=true	# 第一次更新的(版本为1.17.2)
3         kubectl create --filename=pc-deployment.yaml --record=true	# 第二次更新的(版本为1.17.3)
# 可以发现有三次版本记录,说明完成两次升级

# 此时也会有三个rs
[root@k8s-master ~]# kubectl get rs -n test -o wide
NAME                       DESIRED   CURRENT   READY   AGE  
pc-deployment-6696798b78   0         0         0       23m  
pc-deployment-54f9bd68f7   0         0         0       21m   
pc-deployment-7865c58bdf   3         3         3       109s   
# 版本回滚
# 这里直接使用 --to-revision=1 回滚到了 1 版本(1.17.1),如果省略这个选项,就是回退到上个版本,就是2版本(1.17.2)
[root@k8s-master ~]# kubectl rollout undo deploy pc-deployment --to-revision=1 -n test
deployment.apps/pc-deployment rolled back
# 此时回退到 1.17.1 版本

# 查看历史记录
[root@k8s-master ~]# kubectl rollout history deploy pc-deployment -n test
deployment.apps/pc-deployment 
REVISION  CHANGE-CAUSE
2         kubectl create --filename=pc-deployment.yaml --record=true 	# 版本为1.17.2
3         kubectl create --filename=pc-deployment.yaml --record=true	# 版本为1.17.3
4         kubectl create --filename=pc-deployment.yaml --record=true	# 版本为1.17.1 (相当于第一次创建时)

金丝雀发布

Deployment控制器支持控制更新过程中的控制,如“暂停(pause)”或“继续(resume)”更新操作。

比如有一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。

然后,再筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。

# 更新deployment的版本,并配置暂停deployment
[root@k8s-master ~]#  kubectl set image deploy pc-deployment nginx=nginx:1.17.4 -n test && kubectl rollout pause deployment pc-deployment  -n test
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused

#观察更新状态
[root@k8s-master ~]# kubectl rollout status deploy pc-deployment -n test
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 4 new replicas have been updated...

# 监控更新的过程,可以看到已经新增了一个资源,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause暂停命令

[root@k8s-master ~]# kubectl get rs -n test -o wide
NAME                       DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         
pc-deployment-5d89bdfbf9   3         3         3       19m     nginx        nginx:1.17.1   
pc-deployment-675d469f8b   0         0         0       14m     nginx        nginx:1.17.2   
pc-deployment-6c9f56fcfb   2         2         2       3m16s   nginx        nginx:1.17.4   
[root@k8s-master ~]# kubectl get pods -n test
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-rj8sq   1/1     Running   0          7m33s
pc-deployment-5d89bdfbf9-ttwgg   1/1     Running   0          7m35s
pc-deployment-5d89bdfbf9-v4wvc   1/1     Running   0          7m34s
pc-deployment-6c9f56fcfb-996rt   1/1     Running   0          3m31s
pc-deployment-6c9f56fcfb-j2gtj   1/1     Running   0          3m31s

# 确保更新的pod没问题了,继续更新
[root@k8s-master ~]# kubectl rollout resume deploy pc-deployment -n test
deployment.apps/pc-deployment resumed

# 查看最后的更新情况
[root@k8s-master ~]# kubectl get rs -n test -o wide
NAME                       DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         
pc-deployment-5d89bdfbf9   0         0         0       21m     nginx        nginx:1.17.1   
pc-deployment-675d469f8b   0         0         0       16m     nginx        nginx:1.17.2   
pc-deployment-6c9f56fcfb   4         4         4       5m11s   nginx        nginx:1.17.4   

[root@k8s-master ~]# kubectl get pods -n test
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-6c9f56fcfb-7bfwh   1/1     Running   0          37s
pc-deployment-6c9f56fcfb-996rt   1/1     Running   0          5m27s
pc-deployment-6c9f56fcfb-j2gtj   1/1     Running   0          5m27s
pc-deployment-6c9f56fcfb-rf84v   1/1     Running   0          37s

删除deploy

# 删除deployment,其下的rs和pod也将被删除

kubectl delete deploy deploy名 -n namespace
# 或
kubectl delete -f 配置清单文件名

你可能感兴趣的:(Kubernetes)