美文网首页K8s
03.kubernetes笔记 Pod控制器(二) Deploy

03.kubernetes笔记 Pod控制器(二) Deploy

作者: Bigyong | 来源:发表于2021-09-08 23:12 被阅读0次

    目录
    Deployment简介
    Deployment 字段格式
    示例1: 创建Deployment控制器
    deployment rollout命令查看状态
    示例2: Deployment版本回滚
    示例3: 使用 pause 模拟金丝雀发布
    示例4: 修改Deployment滚动更新策略


    Deployment简介

    Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义 (declarative) 方法,用来替代以前的ReplicationController 来方便的管理应用。典型的应用场景包括;定义 Deployment 来创建 Pod 和 ReplicaSet

    1. 滚动升级和回滚应用
    2. 扩容和缩容
    3. 暂停和继续 Deploymen

    Deployment 无状态服务最常用的Pod管理器
    Deployment 实际是管理ReplicaSet Deployment默认会保存10个ReplicaSet历史版本
    Deployment 会把ReplicaSet之前个版本的副本数修改为0

    Deployment 字段格式
    apiVersion: apps/v1 #API群组及版本
    kind: Deployment #资源类型特有标识
    metadata:
      name <string>  #资源名称,在作用域中要唯一
      namespace <string>  #名称空间;Deployment隶属名称空间级别
    spec:  
      minReadySeconds <integer> #Pod就绪后多少秒内任一容器无crash方可视为“就绪”
      replicas <integer>  #期望的Pod副本数,默认为1
      selector <object> #标签选择器,必须匹配template字段中Pod模板中的标签
      template <object> #Pod模板对象
      revisionHistoryLimit <integer> #滚动更新历史记录数量,默认为10
      strategy <0bject> #滚动更新策略
        type <string>  #滚动更新类型,可用值有Recreate和Rollingupdate; Recreate 重建更新一次性删除所有Pod;Rollingupdate  滚动更新默认策略
        rollingUpdate <0bject> #滚动更新参数,专用于RollingUpdate类型
        maxSurge <string> #更新期间可比期望的Pod数量多出的数量或比例
        maxUnavailable <string> #更新期间可比期望的Pod数量缺少的数量或比例
      progressDeadlineSeconds <integer> # 滚动更新故障超时时长,默认为600秒
      paused <boolean> #是否暂停部暑过程
    
    示例1: 创建Deployment控制器 Pod副本数为4
    [root@k8s-master PodControl]# cat deployment-demo.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment-demo
    spec:
      replicas: 4  #副本数来4
      selector:
        matchLabels:
          app: demoapp
          release: stable
      template:
        metadata:
          labels :
            app: demoapp
            release: stable
        spec:
          containers:
          - name: demoapp
            image: ikubernetes/demoapp:${VERSION}
            ports:
            - containerPort: 80
              name: http
    
    [root@k8s-master PodControl]# kubectl apply -f deployment-demo.yaml 
    deployment.apps/deployment-demo created
    
    [root@k8s-master PodControl]# VERSION=v1.0 envsubst < deployment-demo.yaml |kubectl apply -f  -
    deployment.apps/deployment-demo created
    
    [root@k8s-master PodControl]# kubectl get deployment
    NAME              READY   UP-TO-DATE   AVAILABLE   AGE
    deployment-demo   4/4     4            4           54s
    
    #可以看到创建了同名的rs deployment实际管理的是rs而不是Pod
    #名称中的 fb544c5d8 是通过配置文的生成的hash值,当检查到配置文件hash值变化就会启到更新
    
    [root@k8s-master PodControl]# kubectl get rs
    NAME                        DESIRED   CURRENT   READY   AGE
    deployment-demo-fb544c5d8   4         4         4       56s    
    
    [root@k8s-master PodControl]# kubectl get pod
    NAME                              READY   STATUS    RESTARTS   AGE
    deployment-demo-fb544c5d8-kz8fn   1/1     Running   0          58s
    deployment-demo-fb544c5d8-p6q4v   1/1     Running   0          58s
    deployment-demo-fb544c5d8-ttjmt   1/1     Running   0          58s
    deployment-demo-fb544c5d8-x2lqw   1/1     Running   0          58s
    
    
    [root@k8s-master PodControl]# kubectl describe deployment/deployment-demo
    Name:                   deployment-demo
    Namespace:              default
    CreationTimestamp:      Mon, 16 Aug 2021 16:17:28 +0800
    Labels:                 <none>
    Annotations:            deployment.kubernetes.io/revision: 1
    Selector:               app=demoapp,release=stable
    Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
    StrategyType:           RollingUpdate   #可以看到默认的更新策略
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge  #默认的更新比例为 25%
    
    
    更新deployment rollout命令查看状态
    [root@k8s-master PodControl]# kubectl rollout --help  #查看张帮助选项
    Manage the rollout of a resource.
      
     Valid resource types include:  #可以更新的资源类型
    
      *  deployments
      *  daemonsets
      *  statefulsets
    
    Examples:
      # Rollback to the previous deployment
      kubectl rollout undo deployment/abc
      
      # Check the rollout status of a daemonset
      kubectl rollout status daemonset/foo
    
    Available Commands:
      history     View rollout history   #历史记录
      pause       Mark the provided resource as paused  #暂停
      restart     Restart a resource  #重启
      resume      Resume a paused resource  #恢复暂时资源
      status      Show the status of the rollout  #查看资源更新状态
      undo        Undo a previous rollout  #版本回退
    
    
    [root@k8s-master PodControl]# VERSION=v1.1 envsubst < deployment-demo.yaml |kubectl apply -f -
    deployment.apps/deployment-demo configured
    
    
    [root@k8s-master PodControl]# kubectl rollout status deployment/deployment-demo
    deployment "deployment-demo" successfully rolled out   #查看更新状态成功
    
    
    [root@k8s-master PodControl]# kubectl get deployment
    NAME              READY   UP-TO-DATE   AVAILABLE   AGE
    deployment-demo   4/4     4            4           2
    
    [root@k8s-master PodControl]# kubectl get rs
    NAME                         DESIRED   CURRENT   READY   AGE
    deployment-demo-867c7d9d55   4         4         4       6m5s
    deployment-demo-fb544c5d8    0         0         0       25m   #旧版本副本数为0
    
    [root@k8s-master PodControl]# kubectl get pod -o wide
    NAME                               READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
    deployment-demo-867c7d9d55-d6xvt   1/1     Running   0          6m55s   10.244.1.229   k8s-node1   <none>           <none>
    deployment-demo-867c7d9d55-l65z9   1/1     Running   0          6m47s   10.244.2.209   k8s-node2   <none>           <none>
    deployment-demo-867c7d9d55-vhs6t   1/1     Running   0          6m55s   10.244.3.56    k8s-node3   <none>           <none>
    deployment-demo-867c7d9d55-zc8dm   1/1     Running   0          6m43s   10.244.1.230   k8s-node1   <none>           <none>
    [root@k8s-master PodControl]# curl 10.244.1.229
    iKubernetes demoapp v1.1 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-867c7d9d55-d6xvt, ServerIP: 10.244.1.229!
    
    • 在次更新使用 kubectl rollout status查看更新状态状
    [root@k8s-master PodControl]# VERSION=v1.2 envsubst < deployment-demo.yaml |kubectl apply -f - && kubectl rollout status deployment/deployment-demo
    deployment.apps/deployment-demo configured
    Waiting for deployment spec update to be observed...
    Waiting for deployment "deployment-demo" rollout to finish: 1 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 1 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 1 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 2 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 2 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 2 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 3 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo" rollout to finish: 1 old replicas are pending termination...
    Waiting for deployment "deployment-demo" rollout to finish: 1 old replicas are pending termination...
    Waiting for deployment "deployment-demo" rollout to finish: 1 old replicas are pending termination...
    Waiting for deployment "deployment-demo" rollout to finish: 3 of 4 updated replicas are available...
    deployment "deployment-demo" successfully rolled out
    
    [root@k8s-master PodControl]# kubectl get rs    #可以看到rs又多了一个版本副本数为0
    NAME                         DESIRED   CURRENT   READY   AGE
    deployment-demo-77d46c4794   4         4         4       2m25s
    deployment-demo-867c7d9d55   0         0         0       10m
    deployment-demo-fb544c5d8    0         0         0       29m
    
    #验证
    [root@k8s-master PodControl]# kubectl get pod -o wide
    NAME                               READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
    deployment-demo-77d46c4794-fbt5l   1/1     Running   0          2m44s   10.244.2.210   k8s-node2   <none>           <none>
    deployment-demo-77d46c4794-kdvn2   1/1     Running   0          3m20s   10.244.1.231   k8s-node1   <none>           <none>
    deployment-demo-77d46c4794-lcm28   1/1     Running   0          3m20s   10.244.3.57    k8s-node3   <none>           <none>
    deployment-demo-77d46c4794-x52sh   1/1     Running   0          2m40s   10.244.1.232   k8s-node1   <none>           <none>
    [root@k8s-master PodControl]# curl 10.244.2.210
    iKubernetes demoapp v1.2 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-77d46c4794-fbt5l, ServerIP: 10.244.2.210!
    
    示例2: Deployment版本回滚
    • 回滚 回滚时可以指定回滚版本,默认回滚到前一个版本,
    • 这里需要注意的是,假设当前版本为A,前一个版本为B,回滚后当前版本A变成前一版本,再次回滚后A从前一版本回滚到最新版本,所以如果不指定版本,回滚只在最后两个版本间滚动
    [root@k8s-master PodControl]# kubectl rollout history deployment/deployment-demo
    deployment.apps/deployment-demo 
    REVISION  CHANGE-CAUSE
    1         <none>   #更新过3次 3个不同的版本  回滚时可以
    2         <none>
    3         <none>
    
    [root@k8s-master PodControl]# kubectl rollout undo deployment/deployment-demo
    deployment.apps/deployment-demo rolled back
    [root@k8s-master PodControl]# kubectl rollout history deployment/deployment-demo
    deployment.apps/deployment-demo 
    REVISION  CHANGE-CAUSE
    1         <none>
    3         <none>
    4         <none>   #版本2变成4为最新版本 序列号只会递增
    
    [root@k8s-master PodControl]# kubectl rollout undo --dry-run=server deployment/deployment-demo  #不执行测试是否能成功
    deployment.apps/deployment-demo rolled back (server dry run)
    
    [root@k8s-master PodControl]# kubectl rollout undo deployment/deployment-demo  --to-revision=1  #回滚到指定版本
    deployment.apps/deployment-demo rolled back
    [root@k8s-master PodControl]# kubectl rollout history deployment/deployment-demo
    deployment.apps/deployment-demo 
    REVISION  CHANGE-CAUSE
    3         <none>
    4         <none>
    5         <none>
    
    
    [root@k8s-master PodControl]# kubectl get rs
    NAME                         DESIRED   CURRENT   READY   AGE
    deployment-demo-77d46c4794   0         0         0       17m
    deployment-demo-867c7d9d55   0         0         0       26m
    deployment-demo-fb544c5d8    4         4         4       45m   #只有最开始的rs副本数为4 其它都为0
    
    [root@k8s-master PodControl]# kubectl get pod -o wide
    NAME                               READY   STATUS        RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
    deployment-demo-867c7d9d55-b65cm   0/1     Terminating   0          4m2s    10.244.1.234   k8s-node1   <none>           <none>
    deployment-demo-867c7d9d55-pv8fp   1/1     Terminating   0          4m10s   10.244.3.58    k8s-node3   <none>           <none>
    deployment-demo-867c7d9d55-tnhsg   1/1     Terminating   0          4m14s   10.244.1.233   k8s-node1   <none>           <none>
    deployment-demo-867c7d9d55-wcvl7   1/1     Terminating   0          4m14s   10.244.2.211   k8s-node2   <none>           <none>
    deployment-demo-fb544c5d8-5f9lp    1/1     Running       0          32s     10.244.1.235   k8s-node1   <none>           <none>
    deployment-demo-fb544c5d8-d5lr9    1/1     Running       0          22s     10.244.1.236   k8s-node1   <none>           <none>
    deployment-demo-fb544c5d8-lj5gt    1/1     Running       0          29s     10.244.3.59    k8s-node3   <none>           <none>
    deployment-demo-fb544c5d8-t9swj    1/1     Running       0          32s     10.244.2.212   k8s-node2   <none>           <none>
    
    
    
    [root@k8s-master PodControl]# curl 10.244.2.212  #测试回滚版本
    iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-t9swj, ServerIP: 10.244.2.212!
    
    
    示例3: 使用 pause 模拟金丝雀发布
    
    #指定回滚版本或更新版本后,马上暂停,这样只回滚或更新第一批Pod,模拟金丝雀发布让 用一小部分流量测试服务是否正常
    [root@k8s-master PodControl]# kubectl rollout undo deployment/deployment-demo  --to-revision=3 && kubectl rollout pause deployment/deployment-demo  
    deployment.apps/deployment-demo rolled back
    deployment.apps/deployment-demo paused
    
    #只更新了第一批就阻塞住了
    [root@k8s-master PodControl]# kubectl rollout status  deployment/deployment-demo
    Waiting for deployment "deployment-demo" rollout to finish: 2 out of 4 new replicas have been updated...
    
    [root@k8s-master PodControl]# kubectl get rs
    NAME                         DESIRED   CURRENT   READY   AGE
    deployment-demo-58c8475bcc   0         0         0       5m42s
    deployment-demo-77d46c4794   2         2         1       36m
    deployment-demo-867c7d9d55   0         0         0       44m
    deployment-demo-fb544c5d8    3         3         3       63m
    
    
    [root@k8s-master ~]# kubectl get svc
    NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
    demoapp-deploy   ClusterIP   10.107.174.169   <none>        80/TCP    18s
    
    #让流量
    
    [root@k8s-master ~]# while true; do curl --connect-timeout 1 10.107.174.169; sleep .2;done   #新旧版本同时存在
    iKubernetes demoapp v1.2 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-77d46c4794-fhz4l, ServerIP: 10.244.1.238!
    iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-lj5gt, ServerIP: 10.244.3.59!
    iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-t9swj, ServerIP: 10.244.2.212!
    iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-5f9lp, ServerIP: 10.244.1.235!
    iKubernetes demoapp v1.2 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-77d46c4794-kmrhn, ServerIP: 10.244.3.60!
    iKubernetes demoapp v1.2 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-77d46c4794-fhz4l, ServerIP: 10.244.1.238!
    iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-lj5gt, ServerIP: 10.244.3.59!
    iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-t9swj, ServerIP: 10.244.2.212!
    iKubernetes demoapp v1.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-fb544c5d8-5f9lp, ServerIP: 10.244.1.235!
    
    
    示例4: 修改Deployment滚动更新策略
    • 更新策略需要根据自己的实际情况配置 比如流量较大,要保证一定的Pod数量,那就要保证更新时Pod的数量不能少
    • 如果系统资源比较紧张,那更新时Pod的数量要保持不变,以免增大对系统资源的负担
    [root@k8s-master PodControl]# cat deployment-demo-with-strategy.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment-demo-with-strategy
    spec:
      replicas: 4
      selector:
        matchLabels:
          app: demoapp
          release: stable
          strategy: ture
      template:
        metadata:
          labels :
            app: demoapp
            release: stable
            strategy: ture
        spec:
          containers:
          - name: demoapp
            image: ikubernetes/demoapp:${VERSION}
            ports:
            - containerPort: 80
              name: http
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 0   #总的Pod数量不能多
          maxUnavailable: 1   #总的数量可以减少1个  先减在加  在系统资源紧缺的时候使用
    
    
    [root@k8s-master PodControl]# VERSION=v1.0 envsubst < deployment-demo-with-strategy.yaml |kubectl apply -f -
    deployment.apps/deployment-demo-with-strategy created
    
    [root@k8s-master PodControl]# kubectl get deployment
    NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
    deployment-demo                 5/4     2            5           90m
    deployment-demo-with-strategy   4/4     4            4           49s
    
    [root@k8s-master PodControl]# kubectl describe deployment/deployment-demo-with-strategy
    Name:                   deployment-demo-with-strategy
    Namespace:              default
    CreationTimestamp:      Mon, 16 Aug 2021 17:46:52 +0800
    Labels:                 <none>
    Annotations:            deployment.kubernetes.io/revision: 1
    Selector:               app=demoapp,release=stable,strategy=ture
    Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  1 max unavailable, 0 max surge   #更新策略生效
    
    [root@k8s-master PodControl]# kubectl get deployment
    NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
    deployment-demo                 5/4     2            5           93m
    deployment-demo-with-strategy   4/4     4            4           3m58s
    
    [root@k8s-master PodControl]# kubectl get pod
    NAME                                             READY   STATUS    RESTARTS   AGE
    deployment-demo-77d46c4794-fhz4l                 1/1     Running   0          26m
    deployment-demo-77d46c4794-kmrhn                 1/1     Running   0          26m
    deployment-demo-fb544c5d8-5f9lp                  1/1     Running   0          46m
    deployment-demo-fb544c5d8-lj5gt                  1/1     Running   0          46m
    deployment-demo-fb544c5d8-t9swj                  1/1     Running   0          46m
    deployment-demo-with-strategy-57b96bdc87-8mjzw   1/1     Running   0          39s
    deployment-demo-with-strategy-57b96bdc87-k6rxx   1/1     Running   0          38s
    deployment-demo-with-strategy-57b96bdc87-v4tdj   1/1     Running   0          38s
    deployment-demo-with-strategy-57b96bdc87-zdvcp   1/1     Running   0          38s
    
    #查看更新状态 先减后增
    [root@k8s-master PodControl]# VERSION=v1.1 envsubst < deployment-demo-with-strategy.yaml |kubectl apply -f - && kubectl rollout status deployment/deployment-demo-with-strategy
    deployment.apps/deployment-demo-with-strategy configured
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 0 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 0 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 1 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 1 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 1 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 2 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 2 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 2 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 3 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 3 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 3 out of 4 new replicas have been updated...
    Waiting for deployment "deployment-demo-with-strategy" rollout to finish: 3 of 4 updated replicas are available...
    deployment "deployment-demo-with-strategy" successfully rolled out
    

    相关文章

      网友评论

        本文标题:03.kubernetes笔记 Pod控制器(二) Deploy

        本文链接:https://www.haomeiwen.com/subject/nzxvwltx.html