• Kubernetes(k8s)的Pod控制器Deployment详细讲解


    1. 概述

    Deployment控制器并不直接管理Pod,而是通过管理ReplicaSet来间接管理Pod。所以Deployment的功能比ReplicaSet强大。支持扩缩容、 发布的停止和继续、镜像版本滚动变更、ReplicaSet版本回退

    Deployment

    Deployment的资源清单文件模板

    apiVersion: apps/v1                    
    kind: Deployment                      
    metadata:                             
      name: pod-controller                # deploy名称 
      namespace: dev                      # deploy所属命名空间 
      labels:                             # 给deploy打标签 
        controller: deploy 
    spec:                                
      replicas: 3                         # pod副本数量。默认是1
      revisionHistoryLimit: 3             # 保留ReplicaSet历史版本数量,默认为10 
      paused: false                       # 暂停部署,默认是false 
      progressDeadlineSeconds: 600        # 部署超时时间(s),默认是600 
      strategy:                           # Pod更新策略,默认是RollingUpdate 
        type: RollingUpdate               # 滚动更新策略。另一种是Recreate,其没有子属性配置参数 
        rollingUpdate:                    # 当type为RollingUpdate的时候生效,为其配置参数
          maxSurge: 25%                   # 升级过程中可以超过期望的Pod的最大数量,可以为百分比,也可以为整数。默认是25%
          maxUnavailable: 25%             # 升级过程中最大不可用状态的Pod数量,可以为百分比,也可以为整数。默认是25% 
      selector:                           # 选择器,通过该控制器管理哪些pod 
        matchLabels:                      # Labels匹配规则。和matchExpressions类似
          app: nginx-pod 
        matchExpressions:                 # Expressions匹配规则。和matchLabels类似 
          - {key: app, operator: In, values: ["nginx-pod"]} 
      template:                           # pod副本创建模板。属性和Pod的属性一样
        metadata: 
          labels: 
            app: nginx-pod 
        spec: 
          containers: 
          - name: nginx 
            image: nginx:latest
            ports: 
            - name: nginx-port
              containerPort: 80
              protocol: TCP
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    2. 创建Deployment

    新建pod-controller.yaml,内容如下。并运行deployment

    [root@k8s-master ~]# cat pod-controller.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: pod-controller
      namespace: dev
      labels:
        controller: deploy 
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx-pod
      template:
        metadata:
          labels:
            app: nginx-pod
        spec:
          containers:
            - name: nginx
              image: nginx:latest
              ports:
                - name: nginx-port
                  containerPort: 80
                  protocol: TCP
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl apply -f pod-controller.yaml 
    deployment.apps/pod-controller created
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2.1 查看Deployment

    UP-TO-DATE表示更新时最新版本的Pod数量

    [root@k8s-master ~]# kubectl get deploy pod-controller -n dev
    NAME             READY   UP-TO-DATE   AVAILABLE   AGE
    pod-controller   3/3     3            3           2m13s
    [root@k8s-master ~]# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3. 扩缩容

    方式一:使用以下命令,编辑文本内容:spec: replicas: 4

    [root@k8s-master ~]# kubectl edit deploy pod-controller -n dev
    deployment.apps/pod-controller edited
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3

    方式二:直接使用命令

    [root@k8s-master ~]# kubectl scale deploy pod-controller --replicas=2 -n dev
    deployment.apps/pod-controller scaled
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3

    4. 镜像版本变更

    4.1 概述

    新的Pod替代旧的Pod支持两种镜像更新策略:

    • 重建更新Recreate:在创建出新的Pod之前会先杀掉所有已经存在的Pod
    • 滚动更新RollingUpdate(默认):先杀死一部分,再启动一部分。再杀死一部分,再启动一部分。如此循环。在更新过程中,存在两个版本的Pod

    通过strategy属性进行配置

    4.2 重建更新

    在spec属性下面新增strategy属性,最终的pod-controller.yaml内容如下。然后使yaml配置文件生效

    [root@k8s-master ~]# cat pod-controller.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: pod-controller
      namespace: dev
      labels:
        controller: deploy 
    spec:
      replicas: 3
      strategy:
        type: Recreate
      selector:
        matchLabels:
          app: nginx-pod
      template:
        metadata:
          labels:
            app: nginx-pod
        spec:
          containers:
            - name: nginx
              image: nginx:latest
              ports:
                - name: nginx-port
                  containerPort: 80
                  protocol: TCP
    [root@k8s-master ~]#
    [root@k8s-master ~]# kubectl apply -f pod-controller.yaml 
    deployment.apps/pod-controller configured
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    更新镜像版本

    [root@k8s-master ~]# kubectl set image deploy pod-controller nginx=nginx:1.21.6 -n dev
    deployment.apps/pod-controller image updated
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3

    持续查看Pod版本变更情况

    [root@k8s-master ~]# kubectl get pod -n dev -w
    NAME                              READY   STATUS              RESTARTS   AGE
    pod-controller-5bdd9d95bf-9lljw   0/1     ContainerCreating   0          11s
    pod-controller-5bdd9d95bf-9nph6   0/1     ContainerCreating   0          11s
    pod-controller-5bdd9d95bf-z7n5c   0/1     ContainerCreating   0          11s
    pod-controller-5bdd9d95bf-9nph6   1/1     Running             0          95s
    pod-controller-5bdd9d95bf-z7n5c   1/1     Running             0          106s
    pod-controller-5bdd9d95bf-9lljw   1/1     Running             0          113s
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.3 滚动更新

    在spec属性下面新增strategy属性,最终的pod-controller.yaml内容如下。然后使yaml配置文件生效

    [root@k8s-master ~]# cat pod-controller.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: pod-controller
      namespace: dev
      labels:
        controller: deploy 
    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:latest
              ports:
                - name: nginx-port
                  containerPort: 80
                  protocol: TCP
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl apply -f pod-controller.yaml 
    deployment.apps/pod-controller configured
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    更新镜像版本

    [root@k8s-master ~]# kubectl set image deploy pod-controller nginx=nginx:1.21 -n dev
    deployment.apps/pod-controller image updated
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3

    持续查看Pod版本变更情况。可以看到是滚动更新的

    [root@k8s-master ~]# kubectl get pod -n dev -w
    NAME                              READY   STATUS              RESTARTS   AGE
    pod-controller-5b899bbdb4-2w59n   1/1     Running             0          53s
    pod-controller-5b899bbdb4-9bfm9   1/1     Running             0          19s
    pod-controller-5b899bbdb4-fn5ck   1/1     Running             0          37s
    pod-controller-bcc5899bf-mcfnh    0/1     ContainerCreating   0          2s
    pod-controller-bcc5899bf-mcfnh    1/1     Running             0          16s
    pod-controller-5b899bbdb4-9bfm9   1/1     Terminating         0          33s
    pod-controller-bcc5899bf-w6vfb    0/1     Pending             0          0s
    pod-controller-bcc5899bf-w6vfb    0/1     ContainerCreating   0          1s
    pod-controller-bcc5899bf-w6vfb    1/1     Running             0          17s
    pod-controller-5b899bbdb4-fn5ck   1/1     Terminating         0          68s
    pod-controller-bcc5899bf-zkfkb    0/1     Pending             0          0s
    pod-controller-bcc5899bf-zkfkb    0/1     ContainerCreating   0          0s
    pod-controller-bcc5899bf-zkfkb    1/1     Running             0          16s
    pod-controller-5b899bbdb4-2w59n   1/1     Terminating         0          100s
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    查看ReplicaSet情况。可以看到原来的rs依旧存在,只是Pod的数量变为0。这就是Deployment可以进行版本回退的原因

    [root@k8s-master ~]# kubectl get rs -n dev
    NAME                        DESIRED   CURRENT   READY   AGE
    pod-controller-5b899bbdb4   0         0         0       18m
    pod-controller-5bdd9d95bf   0         0         0       13m
    pod-controller-bcc5899bf    3         3         3       5m54s
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5. 版本回退(镜像版本变更回退)

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

    版本回退的命令语法如下所示:

    [root@k8s-master ~]# kubetl rollout 参数 deploy deploy-name
    
    • 1

    支持的参数如下:

    • status:显示当前升级的状态
    • history:显示升级历史记录。kubectl apply -f deploy.yaml需要加上--record参数,CHANGE-CAUSE字段才有数据
    • pause:暂停版本升级过程
    • resume:继续已经暂停的版本升级过程
    • restart:重启版本升级过程
    • undo:回滚到上一个版本(可以使用–to-revision参数回滚到指定的版本,可通过history进行查看版本号)

    5.1 查看当前升级版本的状态

    [root@k8s-master ~]# kubectl rollout status deployment pod-controller -n dev
    deployment "pod-controller" successfully rolled out
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3

    5.2 查看升级历史记录:

    [root@k8s-master ~]# kubectl rollout history deployment pod-controller -n dev
    deployment.apps/pod-controller 
    REVISION  CHANGE-CAUSE
    1         
    2         
    3         
    
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5.3 版本回退:

    回退到最老的一个版本。回退的时候将当前版本的Pod数量降为0,然后将回退版本的Pod数量提升为目标数量

    [root@k8s-master ~]# kubectl rollout undo deployment pod-controller --to-revision=1 -n dev
    deployment.apps/pod-controller rolled back
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl get deployment pod-controller -n dev -o wide
    NAME             READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
    pod-controller   3/3     3            3           43m   nginx        nginx:latest   app=nginx-pod
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl get rs -n dev
    NAME                        DESIRED   CURRENT   READY   AGE
    pod-controller-5b899bbdb4   3         3         3       43m
    pod-controller-5bdd9d95bf   0         0         0       38m
    pod-controller-bcc5899bf    0         0         0       31m
    [root@k8s-master ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    6 金丝雀发布/灰度发布

    Deployment支持更新过程中的控制,如暂停版本变更(pause)或继续版本变更操作(resume)

    暂停版本变更操作,可以检查变更后的Pod能否正常提供服务。如不能则可以进行回退。如可以则继续版本变更操作

    变更Deployment的版本,并配置暂停Deployment

    [root@k8s-master ~]# kubectl get rs -n dev
    NAME                        DESIRED   CURRENT   READY   AGE
    pod-controller-5b899bbdb4   3         3         3       56m
    pod-controller-5bdd9d95bf   0         0         0       51m
    pod-controller-bcc5899bf    0         0         0       43m
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl set image deployment pod-controller nginx=nginx:1.20.2 -n dev && kubectl rollout pause deployment pod-controller -n dev
    deployment.apps/pod-controller image updated
    deployment.apps/pod-controller paused
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl get rs -n dev
    NAME                        DESIRED   CURRENT   READY   AGE
    pod-controller-59d5b49ffd   1         1         1       54s
    pod-controller-5b899bbdb4   3         3         3       56m
    pod-controller-5bdd9d95bf   0         0         0       51m
    pod-controller-bcc5899bf    0         0         0       43m
    [root@k8s-master ~]# 
    [root@k8s-master ~]# kubectl get pod -n dev
    NAME                              READY   STATUS    RESTARTS   AGE
    pod-controller-59d5b49ffd-tvqlt   1/1     Running   0          4m11s
    pod-controller-5b899bbdb4-bvfjn   1/1     Running   0          18m
    pod-controller-5b899bbdb4-vmbhb   1/1     Running   0          18m
    pod-controller-5b899bbdb4-xcxxs   1/1     Running   0          19m
    [root@k8s-master ~]# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    查看版本变更状态

    [root@k8s-master ~]# kubectl rollout status deployment pod-controller -n dev
    Waiting for deployment "pod-controller" rollout to finish: 1 out of 3 new replicas have been updated...
    
    
    • 1
    • 2
    • 3

    确保变更后的Pod没问题之后,继续更新

    [root@k8s-master ~]# kubectl rollout resume deployment pod-controller -n dev
    deployment.apps/pod-controller resumed
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3

    7. 删除Deployment

    Deployment管理的ReplicaSet和Pod也会一起被删除

    [root@k8s-master ~]# kubectl delete -f pod-controller.yaml 
    deployment.apps "pod-controller" deleted
    [root@k8s-master ~]#
    
    • 1
    • 2
    • 3
  • 相关阅读:
    CY3/CY5/CY7荧光标记甜菊糖Stevioside,三氯蔗糖Sucralose,D-(+)-海藻糖D-(+)-Trehalose Dihydrate
    Python是如何进行内存管理的?(图文讲解)
    【网络篇】如何搭建自己的DNS服务器
    数据在内存中的存储——练习3
    MySQL的主从同步原理
    java-php-python-红河旅游信息服务系统计算机毕业设计
    跨平台编译QWT、安装QWT(Windows、Linux、MacOS环境下编译与安装)
    TDengine3.0 踩坑实录
    绝活!十年高工带你详解Spring Cloud 架构
    面试官:group by 有哪些注意事项?
  • 原文地址:https://blog.csdn.net/yy8623977/article/details/124852928