Deployment(Deploy) 为了更好的解决服务编排的问题,kubernetes 在V1.2版本开始,引入了 Deployment控制器。值得一提的是,这种控制器并不直接管理pod,而是通过管理ReplicaSet来间接管理Pod,即:Deployment管理ReplicaSet,ReplicaSet管理Pod。所以Deployment比ReplicaSet功能更加强大
Deployment主要功能有下面几个:
支持ReplicaSet的所有功能
支持发布的停止、继续
支持滚动升级和回滚版本
Deployment的资源清单文件:
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 apiVersion: apps/v1 kind: Deployment metadata: name: namespace: labels: controller: deploy spec: replicas: 3 revisionHistoryLimit: 3 paused: false progressDeadlineSeconds: 600 strategy: type: RollingUpdate rollingUpdate: maxSurge: 30 % maxUnavailable: 30 % selector: matchLabels: app: nginx-pod matchExpressions: - {key: app , operator: In , values: [nginx-pod ]} template: metadata: labels: app: nginx-pod spec: containers: - name: nginx image: nginx:1.17.1 ports: - containerPort: 80
创建deployment 创建pc-deployment.yaml
,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root@master ~] deployment.apps/pc-deployment created [root@master ~] NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR pc-deployment 3/3 3 3 5m26s nginx nginx:1.17.1 app=nginx-pod [root@master ~] NAME DESIRED CURRENT READY AGE pc-deployment-5d89bdfbf9 3 3 3 5m46s [root@master ~] NAME READY STATUS RESTARTS AGE pc-deployment-5d89bdfbf9-6fd9w 1/1 Running 0 6m15s pc-deployment-5d89bdfbf9-b4g97 1/1 Running 0 6m15s pc-deployment-5d89bdfbf9-pvn8m 1/1 Running 0 6m15s
扩缩容 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 [root@master ~] deployment.apps/pc-deployment edited [root@master ~] NAME READY UP-TO-DATE AVAILABLE AGE pc-deployment 4/4 4 4 9m [root@master ~] NAME READY STATUS RESTARTS AGE pc-deployment-5d89bdfbf9-6fd9w 1/1 Running 0 9m46s pc-deployment-5d89bdfbf9-b4g97 1/1 Running 0 9m46s pc-deployment-5d89bdfbf9-j87kp 1/1 Running 0 12s pc-deployment-5d89bdfbf9-pvn8m 1/1 Running 0 9m46s [root@master ~] deployment.apps/pc-deployment scaled [root@master ~] NAME READY STATUS RESTARTS AGE pc-deployment-5d89bdfbf9-6fd9w 1/1 Running 0 10m pc-deployment-5d89bdfbf9-b4g97 1/1 Running 0 10m pc-deployment-5d89bdfbf9-j87kp 1/1 Running 0 63s pc-deployment-5d89bdfbf9-pvn8m 1/1 Running 0 10m pc-deployment-5d89bdfbf9-xg8mx 1/1 Running 0 33s
镜像更新 deployment支持两种更新策略:重建更新
和滚动更新
,可以通过strategy
指定策略类型,支持两个属性: 1)Recreate:在创建出新的Pod之前会先杀掉所有已存在的Pod 2)RollingUpdate:滚动更新,就是杀死一部分,就启动一部分,在更新过程中,存在两个版本Pod rollingUpdate:当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持两个属性:
maxUnavailable:用来指定在升级过程中不可用Pod的最大数量,默认为25%
maxSurge: 用来指定在升级过程中可以超过期望的Pod的最大数量,默认为25%
重建更新 1)新开一个窗口执行动态查看pod命令 kubectl get pods -n dev -w
,为观察做准备
2)使用 kubectl edit deploy pc-deployment -n dev
命令更改更新策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 spec: strategy: rollingUpdate: maxSurge: 25 % maxUnavailable: 25 % type: RollingUpdate template: spec: containers: - image: nginx:1.17.1 spec: strategy: type: Recreate template: spec: containers: - image: nginx:1.17.2
3)观察升级过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@master ~] NAME READY STATUS RESTARTS AGE pc-deployment-5d89bdfbf9-24ftm 1/1 Running 0 11s pc-deployment-5d89bdfbf9-8v9km 1/1 Running 0 11s pc-deployment-5d89bdfbf9-ttjvc 1/1 Running 0 11s pc-deployment-5d89bdfbf9-ttjvc 0/1 Terminating 0 25s pc-deployment-5d89bdfbf9-24ftm 0/1 Terminating 0 25s pc-deployment-5d89bdfbf9-8v9km 0/1 Terminating 0 26s pc-deployment-675d469f8b-4kszk 0/1 Pending 0 0s pc-deployment-675d469f8b-w97vm 0/1 Pending 0 0s pc-deployment-675d469f8b-ngskd 0/1 Pending 0 0s pc-deployment-675d469f8b-ngskd 1/1 Running 0 2s pc-deployment-675d469f8b-w97vm 1/1 Running 0 2s pc-deployment-675d469f8b-4kszk 1/1 Running 0 3s
滚动更新 1)新开一个窗口执行动态查看pod命令 kubectl get pods -n dev -w
,为观察做准备
2)使用 kubectl edit deploy pc-deployment -n dev
命令更改更新策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 spec: strategy: type: Recreate template: spec: containers: - image: nginx:1.17.2 spec: strategy: type: RollingUpdate rollingUpdate: maxSurge: 25 % maxUnavailable: 25 % template: spec: containers: - image: nginx:1.17.1
3)观察升级过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root@master ~] NAME READY STATUS RESTARTS AGE pc-deployment-675d469f8b-4kszk 1/1 Running 0 4m48s pc-deployment-675d469f8b-ngskd 1/1 Running 0 4m48s pc-deployment-675d469f8b-w97vm 1/1 Running 0 4m48s pc-deployment-5d89bdfbf9-fxdq8 0/1 Pending 0 0s pc-deployment-5d89bdfbf9-fxdq8 0/1 ContainerCreating 0 0s pc-deployment-5d89bdfbf9-fxdq8 1/1 Running 0 1s pc-deployment-675d469f8b-4kszk 1/1 Terminating 0 5m52s pc-deployment-5d89bdfbf9-xs54d 0/1 Pending 0 0s pc-deployment-5d89bdfbf9-xs54d 0/1 ContainerCreating 0 0s pc-deployment-5d89bdfbf9-xs54d 1/1 Running 0 1s pc-deployment-675d469f8b-w97vm 1/1 Terminating 0 5m53s pc-deployment-5d89bdfbf9-ldrwh 0/1 Pending 0 0s pc-deployment-5d89bdfbf9-ldrwh 0/1 ContainerCreating 0 0s pc-deployment-5d89bdfbf9-ldrwh 1/1 Running 0 1s pc-deployment-675d469f8b-w97vm 0/1 Terminating 0 5m54s
镜像更新中rs的变化 1 2 3 4 5 6 [root@master ~] NAME DESIRED CURRENT READY AGE pc-deployment-5d89bdfbf9 3 3 3 3h3m pc-deployment-675d469f8b 0 0 0 153m
版本回退 deployment支持版本升级过程中的暂停、继续功能以及版本回退等诸多功能
kubectl rollout: 版本升级相关功能,支持下面的选项:
status 显示当前升级状态
history 显示 升级历史记录
pause 暂停版本升级过程
resume 继续已经暂停的版本升级过程
restart 重启版本升级过程
undo 回滚到上一级版本(可以使用–to-revision回滚到指定版本)
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 [root@master ~] deployment "pc-deployment" successfully rolled out [root@master ~] 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 3 kubectl create --filename=pc-deployment.yaml --record=true [root@master ~] deployment.apps/pc-deployment rolled back [root@master ~] NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR pc-deployment 3/3 3 3 3h10m nginx nginx:1.17.1 app=nginx-pod
金丝雀部署 金丝雀部署的名字灵感来源于 17 世纪英国矿井工人使用金丝雀作为瓦斯检测指标的传统方法。金丝雀对瓦斯这种气体十分敏感,空气中哪怕有极其微量的瓦斯,金丝雀也会停止歌唱。而当瓦斯含量超过一定限度时,虽然人类毫无察觉,金丝雀却早已毒发身亡。在采矿设备相对简陋的条件下,工人们每次下井都会带上一只金丝雀作为“瓦斯检测指标”,以便在危险状况下紧急撤离。
金丝雀部署的核心思想是在实际运行环境中的一小部分用户或流量上测试新版本的软件,而大部分用户或流量仍然使用旧版本。通过对新版本进行有限范围的实时测试和监控,可以及早发现潜在的问题,并减少对整个系统的冲击。
Deployment控制器支持控制更新过程中的控制,如“暂停(pause)”或“继续(resume)”更新操作
比如有一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本
然后,再筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行
确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀部署实现方式
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 [root@master ~] deployment.apps/pc-deployment image updated [root@master ~] Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated... [root@master ~] NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES ...... pc-deployment-5d89bdfbf9 3 3 3 5m6s nginx nginx:1.17.1 ...... pc-deployment-675d469f8b 0 0 0 3m17s nginx nginx:1.17.2 ...... pc-deployment-7865c58bdf 1 1 1 4m37s nginx nginx:1.17.3 ...... [root@k8s-master01 ~] 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 [root@k8s-master01 ~] deployment.apps/pc-deployment resumed [root@k8s-master01 ~] 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@master ~] NAME READY STATUS RESTARTS AGE pc-deployment-5d89bdfbf9-8x8s4 1/1 Running 0 3m57s pc-deployment-5d89bdfbf9-cf7q7 1/1 Running 0 3m56s pc-deployment-5d89bdfbf9-lxbln 1/1 Running 0 3m58s pc-deployment-7865c58bdf-m96tm 1/1 Running 0 2m2s
删除Deployment 1 2 3 [root@master ~] deployment.apps "pc-deployment" deleted