VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 网络工程 > Linux 教程 >
  • pod控制器之deployment详解

Deployment介绍

为了更好地解决服务编排的问题,k8s在V1.2版本开始,引入了deployment控制器,值得一提的是,这种控制器并不直接管理pod,

而是通过管理replicaset来间接管理pod,即:deployment管理replicaset,replicaset管理pod。所以deployment比replicaset的功能更强大。

 

deployment的主要功能有下面几个:

  • 支持replicaset的所有功能
  • 支持发布的停止、继续
  • 支持版本的滚动更新和版本回退

deployment的资源清单文件

复制代码
apiVersion: apps/v1  #版本号
kind: Deployment  #类型
metadata:    #元数据
  name:    #rs名称
  namespace:   #所属命名空间
  labels:   #标签
    controller: deploy
spec:   #详情描述
  replicas:  #副本数量
  revisionHistoryLimit: #保留历史版本,默认是10
  paused: #暂停部署,默认是false
  progressDeadlineSeconds: #部署超时时间(s),默认是600
  strategy: #策略
    type: RollingUpdates  #滚动更新策略
    rollingUpdate:  #滚动更新
      maxSurge: #最大额外可以存在的副本数,可以为百分比,也可以为整数
      maxUnavaliable: #最大不可用状态的pod的最大值,可以为百分比,也可以为整数
  selector:  #选择器,通过它指定该控制器管理哪些pod
    matchLabels:   #Labels匹配规则
       app: nginx-pod
    matchExpressions:   #Expression匹配规则
      - {key: app, operator: In, values: [nginx-pod]}
  template:  #模板,当副本数量不足时,会根据下面的模板创建pod副本
    metadata:
        labels:
          app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80
复制代码

创建deployment

创建pc-deployment.yaml,内容如下:

复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pc-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
     app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
复制代码

 使用配置文件

[root@master ~]# kubectl create -f pc-deployment.yaml 
deployment.apps/pc-deployment created
[root@master ~]# kubectl get deploy -n dev -o wide
NAME            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
pc-deployment   3/3     3            3           16s   nginx        nginx:1.17.1   app=nginx-pod

查看deployment控制的rs和pod,发现rs是在deployment之后加了一段字符串,而pod是在rs之后加了一段字符串

复制代码
[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   3         3         3       2m13s
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-k8j9n   1/1     Running   0          2m42s
pc-deployment-5d89bdfbf9-vw87k   1/1     Running   0          2m42s
pc-deployment-5d89bdfbf9-x7nsm   1/1     Running   0          2m42s
复制代码

deployment功能

扩缩容

方式一:命令行

格式

kubectl scale deploy deploy名称 --replicas=pod数量 -n 命名空间

通过命令行变更pod数量为5个

复制代码
[root@master ~]# kubectl scale deploy pc-deployment --replicas=5 -n dev
deployment.apps/pc-deployment scaled
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-bhcns   1/1     Running   0          83s
pc-deployment-5d89bdfbf9-cfls7   1/1     Running   0          83s
pc-deployment-5d89bdfbf9-k8j9n   1/1     Running   0          8m54s
pc-deployment-5d89bdfbf9-vw87k   1/1     Running   0          8m54s
pc-deployment-5d89bdfbf9-x7nsm   1/1     Running   0          8m54s
复制代码

方式二:编辑deploy文件

格式

kubectl edit deploy deploy名字 -n 命名空间

通过编辑deploy文件编辑pod数量为3个

复制代码
[root@master ~]# kubectl edit deploy pc-deployment -n dev
找到replicas,将其数量改为3
spec:
  progressDeadlineSeconds: 600
  replicas: 3
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-k8j9n   1/1     Running   0          15m
pc-deployment-5d89bdfbf9-vw87k   1/1     Running   0          15m
pc-deployment-5d89bdfbf9-x7nsm   1/1     Running   0          15m
复制代码

镜像更新

deployment支持两种镜像更新策略:重建更新滚动更新(默认),可以通过strategy选项进行配置

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

重建更新

编辑pc-deployment.yaml,在spec节点下添加更新策略

spec:
  strategy:  #策略
    type: Recreate  #重建更新策略
[root@master ~]# vim pc-deployment.yaml 
[root@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

创建deploy进行验证

复制代码
#首先记录原本的pod名
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-bqf86   1/1     Running   0          8s
pc-deployment-5d89bdfbf9-kz6jt   1/1     Running   0          8s
pc-deployment-5d89bdfbf9-z7d9z   1/1     Running   0          8s

#更改pod镜像
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev
deployment.apps/pc-deployment image updated

#再次查看镜像
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-675d469f8b-b9rwd   1/1     Running   0          27s
pc-deployment-675d469f8b-kc7rr   1/1     Running   0          27s
pc-deployment-675d469f8b-kxgkq   1/1     Running   0          27s
复制代码

发现pod镜像已经改变了

滚动更新

编辑pc-deployment.yaml,在spec节点下添加滚动更新策略(也可以把strategy去掉,因为默认滚动更新策略)

strategy:
  type: RollingUpdate #滚动更新策略
  rollingUpdate:
    maxUnavailable: 25%
    maxSurge: 25%
[root@master ~]# vim pc-deployment.yaml 
[root@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

创建deploy进行验证

复制代码
#记录以前的pod
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-526wf   1/1     Running   0          61s
pc-deployment-5d89bdfbf9-b5x5v   1/1     Running   0          64s
pc-deployment-5d89bdfbf9-kc7hb   1/1     Running   0          59s
#更新镜像
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev
deployment.apps/pc-deployment image updated
#查看pod状态
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS              RESTARTS   AGE
pc-deployment-5d89bdfbf9-526wf   0/1     Terminating         0          2m2s
pc-deployment-5d89bdfbf9-b5x5v   1/1     Running             0          2m5s
pc-deployment-5d89bdfbf9-kc7hb   0/1     Terminating         0          2m
pc-deployment-675d469f8b-7vw6x   1/1     Running             0          3s
pc-deployment-675d469f8b-rzq82   0/1     ContainerCreating   0          2s
pc-deployment-675d469f8b-zk4fs   1/1     Running             0          5s
[root@master ~]# kubectl get pod -n dev
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-675d469f8b-7vw6x   1/1     Running   0          38s
pc-deployment-675d469f8b-rzq82   1/1     Running   0          37s
pc-deployment-675d469f8b-zk4fs   1/1     Running   0          40s
复制代码

发现pod是旧的一遍停止新的一边创建,最后全变成了新的

滚动更新的过程

 

 

镜像更新中rs的变化

前期准备:

复制代码
#重建deployment
[root@master ~]# kubectl delete -f pc-deployment.yaml 
deployment.apps "pc-deployment" deleted
#添加record参数,表明创建时记录
[root@master ~]# kubectl create -f pc-deployment.yaml --record
deployment.apps/pc-deployment created
[root@master ~]# kubectl get deploy,rs,pod -n dev
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/pc-deployment   3/3     3            3           81s

NAME                                       DESIRED   CURRENT   READY   AGE
replicaset.apps/pc-deployment-5d89bdfbf9   3         3         3       81s

NAME                                 READY   STATUS    RESTARTS   AGE
pod/pc-deployment-5d89bdfbf9-4bg2j   1/1     Running   0          81s
pod/pc-deployment-5d89bdfbf9-gbt95   1/1     Running   0          81s
pod/pc-deployment-5d89bdfbf9-tstlh   1/1     Running   0          81s
复制代码

新建两个xshell窗口,用于监听rs和pod

 

 在2窗口中监听rs,3窗口中监听pod

复制代码
#在2窗口中输入
[root@master ~]# kubectl get rs -n dev -w
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   3         3         3       6m18s
#在3窗口中输入
[root@master ~]# kubectl get pod -n dev -w
NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-5d89bdfbf9-4bg2j   1/1     Running   0          6m56s
pc-deployment-5d89bdfbf9-gbt95   1/1     Running   0          6m56s
pc-deployment-5d89bdfbf9-tstlh   1/1     Running   0          6m56s
复制代码

在1窗口中改变pod镜像

[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev
deployment.apps/pc-deployment image updated

查看3窗口中pod的变化,发现序号5开头的pod在逐渐暂停,序号6开头的pod在逐渐创建

查看2窗口中rs的变化,可以看见序号5开头的rs的pod数在减少,序号6开头的rs的pod数在增加

 

 在1窗口中查看最终rs变化,发现原来的rs依旧存在,只是pod数量变为了0,而后又新产生了一个rs,pod数量为3,其实这就是deployment能够进行版本回退的奥妙所在,后面会详细解释

[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   0         0         0       11m
pc-deployment-675d469f8b   3         3         3       3m12s

版本回退

deployment支持版本升级过程中的暂停,继续功能以及版本回退等诸多功能,下面具体来看

kubectl rollout:版本升级相关功能,支持下面的选项:

  • status:显示当前升级状态
  • history:显示升级历史记录
  • pause:暂停版本升级过程
  • resume:继续已经暂停的版本升级过程
  • restart:重启版本升级过程
  • undo:回滚到上一级版本(可以使用--to-revision回滚到指定版本)
复制代码
#查看升级状态
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out

#查看升级历史(注意:如果只显示版本号说明一开始使用yaml创建文件的时候没有加上--record命令)
[root@master ~]# kubectl rollout history deploy pc-deployment -n dev
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

#版本回滚
#这里使用--to-revision=1回滚到1版本,如果省略这个选项,则会回退到上个版本
[root@master ~]# kubectl rollout undo deploy pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back

#查看是否回滚成功,发现5序号开头的rs被启动了
[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   3         3         3       31m
pc-deployment-675d469f8b   0         0         0       22m
复制代码

金丝雀发布

deployment支持更新过程中的控制,如"暂停(pause)"或"继续(resume)"更新操作

比如有一批新的pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,再筛选一小部分的用户请求路由到新的pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。

复制代码
#更新deployment版本,并配置暂停deployment
[root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.2 -n dev && kubectl rollout pause deploy pc-deployment -n dev
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused

#查看rs,发现老版本rs没有减少,新版本rs增加一个
[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   3         3         3       44m
pc-deployment-675d469f8b   1         1         1       35m

#在窗口2中查看deploy状态,发现deploy正在等待更新且已经有1个更新好了
[root@master ~]# kubectl rollout status deploy pc-deployment -n dev
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...


#在窗口1中继续deploy的更新
[root@master ~]# kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed

#查看窗口2的状态
Waiting for deployment spec update to be observed...
Waiting for deployment spec update to be observed...
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "pc-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "pc-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "pc-deployment" successfully rolled out

#在窗口1查看rs更新结果,发现老版本均停止,新版本已经创建好
[root@master ~]# kubectl get rs -n dev
NAME                       DESIRED   CURRENT   READY   AGE
pc-deployment-5d89bdfbf9   0         0         0       49m
pc-deployment-675d469f8b   3         3         3       40m
复制代码

 出处:https://www.cnblogs.com/Ayanamidesu/p/15021812.html


相关教程