• K8S(2)RC、RS和Deployment


    一、RC 与 RS

    • RC全称为ReplicationController(复制控制器),RS全称为ReplicaSet(复制集)
    • RC和RS是K8S中两种简单部署pod的方式,但是由于在生产环境中,主要使用更高级的Deployment等方式对pod进行部署、运行、管理,所以这里只对RC和RS进行简单介绍

    - Replication Controller(RC)

    • 使用RC可以使Pod副本数达到指定数量,确保一个Pod或者一组同类Pod总是可用的

    • 如果存在的Pod大于了设置的数量,那么RC将会终止多余的Pod,相反,如果小于设置的数量,RC则会启动一些Pod以达到设置的数量

    • 和手动创建Pod不同,使用RC维护的Pod在失败、删除、终止时会自动替换,因此就算所需的应用程序只需要一个Pod,也应该使用RC或者其他的方式进行管理

    • RC类似于进程管理程序,但是RC不是监视单个节点上的各个进程,而是监视多个节点上的多个Pod

    • 定义一个RC的示例:

    apiVersion: v1
    kind: ReplicationController
    metadata:
     name: nginx
    spec:
     replicas: 3
     selector:
      app: nginx
     template:
      metadata:
       name: nginx
       labels:
         app: nginx
      spec:
       containers:
       - name: nginx
         image: nginx
         ports:
         - containerPort: 80
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    - ReplicaSet(RS)

    • RS是支持基于集合的标签选择器的新一代RC,RS主要作用于Deployment协调创建、删除、更新Pod,和RC的区别是,RS支持标签选择器

    • 在实际应用中,RS可以单独使用,但是一般使用Deployment来自动管理RS,除非自定义的Pod不需要更新或者其他编排等

    • 定义一个RS的示例:

    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
      name: frontend
      labels:
        app: test
    sepc:
      replicas: 3
      selector:
        matchLabels:
          app: test
        matchExpressions:
        - {key: tier,operator: ln,values: [frontend]}
      template:
        metadata:
          labels: 
          app: test
        sepc:
          containers:
          - name: redis
            images: redis:v1.0
            resources:
              requests:
                cpu: 100m
                memory: 100Mi
            env:
            - name: GET_HOSTS_FROM
              value: dns
            ports:
            - containerPort: 6379
    
    • 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
    • RC和RS的创建删除和Pod并没有太大的区别,并且RC在实际生产环境基本已经不再使用,RS也很少单独使用,一般都会根据需求使用更高级的Deployment、DaemonSet、StatefulSet来管理Pod

    二、Deployment

    • Deployment主要用于部署无状态的服务,是最常用的Pod控制器,一般用于管理公司中无状态的微服务,例如Config-Server注册中心、Zuul网关、SpringBoot微服务框架等,Deployment可以管理多个副本的Pod,实现无缝迁移、自动扩容缩容、自动灾难恢复、一键回滚功能等

    • 注释:有状态服务与无状态服务:

      • 有状态服务
      1. 服务本身依赖或者存在局部的状态数据,这些数据需要自身持久化或者可以通过其他节点恢复。
      2. 一个请求只能被某个节点(或者同等状态下的节点)处理。
      3. 存储状态数据,实例的拓展需要整个系统参与状态的迁移。
      4. 在一个封闭的系统中,存在多个数据闭环,需要考虑这些闭环的数据一致性问题。
      5. 有状态服务通常存在于分布式架构中。
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 无状态服务
      1. 服务不依赖自身的状态,实例的状态数据可以维护在内存中。
      2. 任何一个请求都可以被任意一个实例处理。
      3. 不存储状态数据,实例可以水平拓展,通过负载均衡将请求分发到各个节点。
      4. 在一个封闭的系统中,只存在一个数据闭环。
      5.无状态服务通常存在于单体架构的集群中。
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 虽然RS可以确保在任何给定时间运行的Pod副本数达到指定数量,但是Deployment是一个更高级的概念,它会管理RS,并且为Pod喝RS提供声明性更新,以及许多其他功能呢,一般实际工作中,会使用Deployment代替RS

    • 如果在Deployment对象中描述了所需要的状态,那么Deployment控制器就会以可控制的速率将实际状态更改为期望状态,也可以在Deployment中创建RS,或者删除现有的Deployment并使用新的Deployment部署所需要的资源

    - 创建Deployment

    [root@master1 ~]# kubectl create deployment nginx --image=nginx   #创建deployment
    deployment.apps/nginx created
    [root@master1 ~]# kubectl get pods
    NAME                    READY   STATUS              RESTARTS   AGE
    nginx-f89759699-fjwr8   0/1     ContainerCreating   0          3s
    [root@master1 ~]# kubectl get deployment   #查看deployment的pod
    NAME    READY   UP-TO-DATE   AVAILABLE   AGE
    nginx   1/1     1            1           39s
    [root@master1 ~]# kubectl get deployment nginx -o yaml > nginx.yaml  #导出deployment的yaml文件,用于修改
    [root@master1 ~]# ll
    total 8
    -rw-------. 1 root root 1259 Feb 23 17:13 anaconda-ks.cfg
    -rw-r--r--  1 root root 3703 Jun 22 09:59 nginx.yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 使用kubectl replace命令,参数-f指定yaml或json文件,替换资源(类似于重载配置)
    [root@master1 ~]# kubectl replace -f nginx.yaml  
    deployment.apps/nginx replaced
    
    • 1
    • 2
    • 使用kubectl edit命令,可以直接编辑、修改资源,edit命令会打开KUBE_EDITORGIT_EDITOR或者EDITOR环境变量定义的编辑器,可以同时编辑多个资源,但是编辑过的资源只会一次性提交,默认是YAML格式,加参数-o json可以以JSON格式编辑
    [root@master1 ~]# kubectl edit deployment nginx  #进入编辑模式后与vim类似,修改完后会直接生效
    Edit cancelled, no changes made.
    
    • 1
    • 2
    • 下面来创建一个Deployment
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14-alpine
            ports:
            - containerPort: 80
            
    #注释:
    示例解析:
    kind:指定类型为Deployment的控制器
    第一个metadata是定义Deployment的名称与标签
    第一个sepc定义Deployment的资源
    replicas:定义pod的副本数
    template定义pod的详细信息
    template下的spec定义容器的详细信息
    
    • 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
    • 创建此Deployment
    [root@master1 ~]# kubectl create -f nginx.yaml
    deployment.apps/nginx-deployment created
    [root@master1 ~]# kubectl get pods
    NAME                                READY   STATUS    RESTARTS   AGE
    nginx-deployment-6b75f4cbdd-8nmlv   1/1     Running   0          42m
    nginx-deployment-6b75f4cbdd-f56z8   1/1     Running   0          39m
    nginx-deployment-6b75f4cbdd-llhht   1/1     Running   0          42m
    [root@master1 ~]# kubectl get deployment
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment   3/3     3            3           42m
    [root@master1 ~]# kubectl get deploy -o wide  #查看deployment的信息
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES              SELECTOR
    nginx-deployment   3/3     3            3           43m   nginx        nginx:1.14-alpine   app=nginx
    
    #注释:
    name:集群中deployment的名称
    ready:pod的状态,已经ready的pod个数
    up-to-date:显示已经达到期望状态的副本数
    available:显示用户可以使用的应用程序副本数
    age:显示应用程序运行的时间
    containers:容器的名称
    images:镜像名称
    selector:管理的pod标签
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 查看pod标签
    [root@master1 ~]# kubectl get pod --show-labels
    NAME                                READY   STATUS    RESTARTS   AGE   LABELS
    nginx-deployment-6b75f4cbdd-8nmlv   1/1     Running   0          50m   app=nginx,pod-template-hash=6b75f4cbdd
    nginx-deployment-6b75f4cbdd-f56z8   1/1     Running   0          47m   app=nginx,pod-template-hash=6b75f4cbdd
    nginx-deployment-6b75f4cbdd-llhht   1/1     Running   0          50m   app=nginx,pod-template-hash=6b75f4cbdd
    
    #注释
    标签中'pod-template-hash=6b75f4cbdd'的这个值,表示RS的hash哈希值
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 查看RS
    [root@master1 ~]# kubectl get rs
    NAME                          DESIRED   CURRENT   READY   AGE
    nginx-deployment-6b75f4cbdd   3         3         3       52m
    
    • 1
    • 2
    • 3
    • 看此时Deployment rollout的状态:
    [root@master1 ~]# kubectl rollout status deployment/nginx-deployment   #查看资源状态
    deployment "nginx-deployment" successfully rolled out
    [root@master1 ~]# kubectl get deploy  #查看deployment
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment   3/3     3            3           3h12m
    [root@master1 ~]# kubectl get rs   #查看rs
    NAME                          DESIRED   CURRENT   READY   AGE
    nginx-deployment-6b75f4cbdd   3         3         3       3h12m
    [root@master1 ~]# kubectl get pods --show-labels   #查看deployment创建的pod
    NAME                                READY   STATUS    RESTARTS   AGE     LABELS
    nginx-deployment-6b75f4cbdd-8nmlv   1/1     Running   0          3h12m   app=nginx,pod-template-hash=6b75f4cbdd
    nginx-deployment-6b75f4cbdd-f56z8   1/1     Running   0          3h10m   app=nginx,pod-template-hash=6b75f4cbdd
    nginx-deployment-6b75f4cbdd-llhht   1/1     Running   0          3h12m   app=nginx,pod-template-hash=6b75f4cbdd
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    - 更新Deployment

    • 通常对应用程序的升级、版本迭代都会通过Deployment对Pod进行滚动更新
    • 下面来看示例:
    [root@master1 ~]# kubectl get deploy  #可以使用缩写进行查看
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment   3/3     3            3           23h
    [root@master1 ~]# kubectl get deployment
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment   3/3     3            3           23h
    
    #修改此deployment的镜像
    [root@master1 ~]# kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record
    ————————————————————————————————
    #除了使用set image进行修改,也可以使用kubectl edit deployment nginx直接对yaml文件进行编辑
    ————————————————————————————————
    deployment.apps/nginx-deployment image updated
    [root@master1 ~]# kubectl rollout status deployment.v1.apps/nginx-deployment  #查看状态
    Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
    Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
    Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
    Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
    Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
    Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
    Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
    Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
    deployment "nginx-deployment" successfully rolled out
    [root@master1 ~]# kubectl get rs  #查看rs
    NAME                          DESIRED   CURRENT   READY   AGE
    nginx-deployment-5d6b8b588c   3         3         3       3m58s
    nginx-deployment-6b75f4cbdd   0         0         0       23h
    
    [root@master1 ~]# kubectl describe deployments.apps nginx-deployment  #使用describe查看deployment的详细信息
    Name:                   nginx-deployment
    Namespace:              default
    CreationTimestamp:      Thu, 23 Jun 2022 10:31:28 +0800
    Labels:                 app=nginx
    Annotations:            deployment.kubernetes.io/revision: 2
                            kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true
    Selector:               app=nginx
    Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=nginx
      Containers:
       nginx:
        Image:        nginxdemos/hello:plain-text
        Port:         80/TCP
        Host Port:    0/TCP
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  <none>
    NewReplicaSet:   nginx-deployment-5d6b8b588c (3/3 replicas created)
    Events:
      Type    Reason             Age                  From                   Message
      ----    ------             ----                 ----                   -------
      Normal  ScalingReplicaSet  7m8s                 deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 1
      Normal  ScalingReplicaSet  6m47s (x2 over 23h)  deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  6m47s                deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  6m46s                deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 1
      Normal  ScalingReplicaSet  6m46s                deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 3
      Normal  ScalingReplicaSet  6m45s                deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 0
    
    #注释:
    events的过程可以看到:
    1、创建了一个新的deployment为'nginx-deployment-5d6b8b588c',并且增加了一个RS,现在新的deploymentRS数量为1个
    2、旧的deployment ' nginx-deployment-6b75f4cbdd'减少1个RS,现在旧的deploymentRS数量为2两个
    3、新的deploymentRS数量再次增加,增加1个,现在新的deploymentRS数量为2两个
    4、相应的旧的deploymentRS数量减少一个,数量为1个
    5、新的deploymentRS再次增加,增加一个,现在为3个
    6、旧的deploymentRS再次减少,减少一个,数量为0,同时也完成滚动更新
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 从上面的kubectl describe命令,可以看到deployment更新后的过程,是滚动更新

    - 回滚Deployment

    • 如果Deployment在更新后出现了问题,可以对其进行回滚操作,默认情况下,所有Deployment的rollout(回退)历史都保存在系统中,可以随时进行回滚
    • 下面来看案例:
    [root@master1 ~]# kubectl rollout history deployment nginx-deployment  #使用kubectl rollout history可以查看修改记录
    deployment.apps/nginx-deployment
    REVISION  CHANGE-CAUSE
    1         <none>
    2         kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true
    
    [root@master1 ~]# kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record  #再次修改镜像,加--record可以记录到history中
    deployment.apps/nginx-deployment image updated
    
    [root@master1 ~]# kubectl describe deployments.apps nginx-deployment  #查看详细信息
    Name:                   nginx-deployment
    Namespace:              default
    CreationTimestamp:      Thu, 23 Jun 2022 10:31:28 +0800
    Labels:                 app=nginx
    Annotations:            deployment.kubernetes.io/revision: 3
                            kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true
    Selector:               app=nginx
    Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=nginx
      Containers:
       nginx:
        Image:        nginx:1.14-alpine  #可以看到镜像已经更换
        Port:         80/TCP
        Host Port:    0/TCP
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  <none>
    NewReplicaSet:   nginx-deployment-6b75f4cbdd (3/3 replicas created)
    Events:
      Type    Reason             Age                From                   Message
      ----    ------             ----               ----                   -------
      Normal  ScalingReplicaSet  34s                deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 1
      Normal  ScalingReplicaSet  28s                deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  28s                deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  27s (x2 over 28h)  deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 3
      Normal  ScalingReplicaSet  27s                deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 1
      Normal  ScalingReplicaSet  25s                deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 0
    
    [root@master1 ~]# kubectl rollout history deployment nginx-deployment  #查看历史记录,发现增加了一条
    deployment.apps/nginx-deployment
    REVISION  CHANGE-CAUSE
    2         kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true
    3         kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true
    
    [root@master1 ~]# kubectl rollout history deployment nginx-deployment --revision=2  #查看编号2记录的详细信息,使用--revision可以指定版本查看详细信息
    deployment.apps/nginx-deployment with revision #2
    Pod Template:
      Labels:       app=nginx
            pod-template-hash=5d6b8b588c
      Annotations:  kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true
      Containers:
       nginx:
        Image:      nginxdemos/hello:plain-text
        Port:       80/TCP
        Host Port:  0/TCP
        Environment:        <none>
        Mounts:     <none>
      Volumes:      <none>
    
    [root@master1 ~]# kubectl rollout undo deployment nginx-deployment  #回滚到上一个版本
    deployment.apps/nginx-deployment rolled back
    
    [root@master1 ~]# kubectl describe deployments.apps nginx-deployment  #再次查看详细信息
    Name:                   nginx-deployment
    Namespace:              default
    CreationTimestamp:      Thu, 23 Jun 2022 10:31:28 +0800
    Labels:                 app=nginx
    Annotations:            deployment.kubernetes.io/revision: 4
                            kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text --record=true
    Selector:               app=nginx
    Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=nginx
      Containers:
       nginx:
        Image:        nginxdemos/hello:plain-text   #可以发现镜像已经变成了上一个版本的镜像
        Port:         80/TCP
        Host Port:    0/TCP
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  <none>
    NewReplicaSet:   nginx-deployment-5d6b8b588c (3/3 replicas created)
    Events:
      Type    Reason             Age                From                   Message
      ----    ------             ----               ----                   -------
      Normal  ScalingReplicaSet  6m7s               deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 1
      Normal  ScalingReplicaSet  6m1s               deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  6m1s               deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  6m                 deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 1
      Normal  ScalingReplicaSet  6m (x2 over 28h)   deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 3
      Normal  ScalingReplicaSet  5m58s              deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 0
      Normal  ScalingReplicaSet  4s (x2 over 5h8m)  deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 1
      Normal  ScalingReplicaSet  3s (x2 over 5h8m)  deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  3s (x3 over 28h)   deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  1s (x3 over 2s)    deployment-controller  (combined from similar events): Scaled down replica set nginx-deployment-6b75f4cbdd to 0
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 使用–to-revision返回到指定版本:
    [root@master1 ~]# kubectl rollout history deployment nginx-deployment
    deployment.apps/nginx-deployment
    REVISION  CHANGE-CAUSE
    3         kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true
    4         kubectl scale deployment nginx-deployment --replicas=3 --record=true
    
    [root@master1 ~]# kubectl rollout undo deployment nginx-deployment --to-revision=3  #返回3版本
    deployment.apps/nginx-deployment rolled back
    [root@master1 ~]# kubectl rollout history deployment nginx-deployment
    deployment.apps/nginx-deployment
    REVISION  CHANGE-CAUSE
    4         kubectl scale deployment nginx-deployment --replicas=3 --record=true
    5         kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    - 扩展Deployment

    • 当应用程序的访问量越来越大,使用3个pod已经无法继续支撑时,我们可以对Pod的副本数进行修改
    • 下面来看案例:
    - 使用kubectl scale 进行修改,--replicas可以指定修改的副本数,--record=true可以使这条命令进行保留
    [root@master1 ~]# kubectl scale deployment nginx-deployment --replicas=5 --record=true  
    deployment.apps/nginx-deployment scaled
    [root@master1 ~]# kubectl rollout history deployment nginx-deployment
    deployment.apps/nginx-deployment
    REVISION  CHANGE-CAUSE
    4         kubectl scale deployment nginx-deployment --replicas=3 --record=true
    5         kubectl scale deployment nginx-deployment --replicas=5 --record=true
    [root@master1 ~]# kubectl get pods
    NAME                                READY   STATUS    RESTARTS   AGE
    nginx-deployment-6b75f4cbdd-7kn6x   1/1     Running   0          16s
    nginx-deployment-6b75f4cbdd-7m72m   1/1     Running   0          3m2s
    nginx-deployment-6b75f4cbdd-nh4rb   1/1     Running   0          2m58s
    nginx-deployment-6b75f4cbdd-ntqw4   1/1     Running   0          3m
    nginx-deployment-6b75f4cbdd-tvnxs   1/1     Running   0          16s
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    - Deployment暂停更新、恢复

    • Deployment支持暂停更新,期间可以对Deployment进行多次修改
    [root@master1 ~]# kubectl rollout pause deployment nginx-deployment  #kubectl rollout pause暂停
    deployment.apps/nginx-deployment paused
    
    #然后进行更新操作
    [root@master1 ~]# kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text
    deployment.apps/nginx-deployment image updated
    [root@master1 ~]# kubectl describe deployments.apps nginx-deployment  #查看详细信息
    Name:                   nginx-deployment
    Namespace:              default
    CreationTimestamp:      Thu, 23 Jun 2022 10:31:28 +0800
    Labels:                 app=nginx
    Annotations:            deployment.kubernetes.io/revision: 6
                            kubernetes.io/change-cause: kubectl scale deployment nginx-deployment --replicas=5 --record=true
    Selector:               app=nginx
    Replicas:               5 desired | 0 updated | 5 total | 5 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=nginx
      Containers:
       nginx:
        Image:        nginxdemos/hello:plain-text  #成功更新
        Port:         80/TCP 
        Host Port:    0/TCP
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Conditions:
      Type           Status   Reason
      ----           ------   ------
      Available      True     MinimumReplicasAvailable
      Progressing    Unknown  DeploymentPaused
    OldReplicaSets:  nginx-deployment-6b75f4cbdd (5/5 replicas created)
    NewReplicaSet:   nginx-deployment-5d6b8b588c (0/0 replicas created)
    Events:
      Type    Reason             Age                  From                   Message
      ----    ------             ----                 ----                   -------
      Normal  ScalingReplicaSet  87m                  deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 1
      Normal  ScalingReplicaSet  87m                  deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  87m                  deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  87m                  deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 1
      Normal  ScalingReplicaSet  87m (x2 over 29h)    deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 3
      Normal  ScalingReplicaSet  87m                  deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 0
      Normal  ScalingReplicaSet  81m (x2 over 6h29m)  deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 1
      Normal  ScalingReplicaSet  81m (x2 over 6h29m)  deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  81m (x3 over 29h)    deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  81m (x3 over 81m)    deployment-controller  (combined from similar events): Scaled down replica set nginx-deployment-6b75f4cbdd to 0
      Normal  ScalingReplicaSet  63m                  deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 3
      Normal  ScalingReplicaSet  62m (x2 over 64m)    deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 4
      Normal  ScalingReplicaSet  36m                  deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 3
      Normal  ScalingReplicaSet  28m                  deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 1
      Normal  ScalingReplicaSet  28m                  deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  28m                  deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  28m                  deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 1
      Normal  ScalingReplicaSet  28m                  deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 3
      Normal  ScalingReplicaSet  28m                  deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 0
      Normal  ScalingReplicaSet  25m                  deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 5
    
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 使用kubectl rollout resume可以恢复Deployment更新
    [root@master1 ~]# kubectl rollout pause deployment nginx-deployment
    deployment.apps/nginx-deployment paused
    [root@master1 ~]# kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true
    deployment.apps/nginx-deployment image updated
    [root@master1 ~]# kubectl rollout  resume deployment nginx-deployment
    [root@master1 ~]# kubectl describe deployments.apps nginx-deployment
    Name:                   nginx-deployment
    Namespace:              default
    CreationTimestamp:      Thu, 23 Jun 2022 10:31:28 +0800
    Labels:                 app=nginx
    Annotations:            deployment.kubernetes.io/revision: 7
                            kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true
    Selector:               app=nginx
    Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=nginx
      Containers:
       nginx:
        Image:        nginx:1.14-alpine  #镜像已经更换
        Port:         80/TCP
        Host Port:    0/TCP
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  <none>
    NewReplicaSet:   nginx-deployment-6b75f4cbdd (5/5 replicas created)
    Events:
      Type    Reason             Age                From                   Message
      ----    ------             ----               ----                   -------
      Normal  ScalingReplicaSet  57m                deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 3
      Normal  ScalingReplicaSet  49m                deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 1
      Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  49m                deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 1
      Normal  ScalingReplicaSet  49m                deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 3
      Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set nginx-deployment-5d6b8b588c to 0
      Normal  ScalingReplicaSet  46m                deployment-controller  Scaled up replica set nginx-deployment-6b75f4cbdd to 5
      Normal  ScalingReplicaSet  2m49s              deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 3
      Normal  ScalingReplicaSet  2m49s              deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 2
      Normal  ScalingReplicaSet  2m49s              deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 4
      Normal  ScalingReplicaSet  2m48s              deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 3
      Normal  ScalingReplicaSet  2m48s              deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 4
      Normal  ScalingReplicaSet  2m47s              deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 2
      Normal  ScalingReplicaSet  2m47s              deployment-controller  Scaled up replica set nginx-deployment-5d6b8b588c to 5
      Normal  ScalingReplicaSet  2m47s              deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 1
      Normal  ScalingReplicaSet  2m46s              deployment-controller  Scaled down replica set nginx-deployment-6b75f4cbdd to 0
      Normal  ScalingReplicaSet  55s (x9 over 58s)  deployment-controller  (combined from similar events): Scaled down replica set nginx-deployment-5d6b8b588c to 0
    
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    - 更新Deployment的注意事项

    • rollout版本保留策略:

      在默认情况下,revision保留10个版本(RS),其余的将在后台进行垃圾回收,可以在spec.revisionHistoryLimit设置保留版本(RS)的个数。当设置为0时,不保留历史记录

    • deployment更新策略:

      • spec.strategy.type==Recreate:表示重建,先删掉旧的Pod再创建新的Pod
      • spec.strategy.type==RollingUpdate:表示滚动更新,可以指定下面的``maxUnavailablemaxSurge`来控制滚动更新过程
      • spec.strategy.rollingUpdate.maxUnavailable:指定在回滚更新时最大不可用的Pod数量,可选字段,默认为25%,可以设置为数字或百分比,如果maxSurge设置为0,则这个值也不能为0
      • spec.strategy.rollingUpdate.maxSurge:可以超过期望值的最大Pod数,可选字段,默认为25%,可以设置成数字或百分比,如果maxUnavailable为0,则该值不能为0
      • maxSurge: 25%:指定一次可以添加多少个Pod
      • maxUnavailable: 25%:指定滚动更新期间最多有多少个Pod不可用
  • 相关阅读:
    Mac平台文件传输工具Transmit 5
    布兰德 • 斯奈德节拍表
    Kafaka核心设计与实践原理(第一部分)
    JSD-2204-HTML-CSS-JavaScript-Day04
    企业架构LNMP学习笔记9
    STM32实战总结:HAL之I2C
    Stata 数据处理系列:日期与时间数据
    外国电影字幕翻译,怎么把英文字幕翻译成中文字幕?
    hadoop四种集群模式
    学生python编程--学习列表绘制彩虹
  • 原文地址:https://blog.csdn.net/rzy1248873545/article/details/125875697