03.kubernetes笔记|03.kubernetes笔记 Pod控制器(二) 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#资源名称,在作用域中要唯一 namespace#名称空间; Deployment隶属名称空间级别 spec: minReadySeconds #Pod就绪后多少秒内任一容器无crash方可视为“就绪” replicas #期望的Pod副本数,默认为1 selector #标签选择器,必须匹配template字段中Pod模板中的标签 template #Pod模板对象 revisionHistoryLimit #滚动更新历史记录数量,默认为10 strategy <0bject> #滚动更新策略 type#滚动更新类型,可用值有Recreate和Rollingupdate; Recreate 重建更新一次性删除所有Pod; Rollingupdate滚动更新默认策略 rollingUpdate <0bject> #滚动更新参数,专用于RollingUpdate类型 maxSurge#更新期间可比期望的Pod数量多出的数量或比例 maxUnavailable#更新期间可比期望的Pod数量缺少的数量或比例 progressDeadlineSeconds # 滚动更新故障超时时长,默认为600秒 paused #是否暂停部暑过程
示例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 NAMEREADYUP-TO-DATEAVAILABLEAGE deployment-demo4/44454s#可以看到创建了同名的rs deployment实际管理的是rs而不是Pod #名称中的 fb544c5d8 是通过配置文的生成的hash值,当检查到配置文件hash值变化就会启到更新[root@k8s-master PodControl]# kubectl get rs NAMEDESIREDCURRENTREADYAGE deployment-demo-fb544c5d844456s[root@k8s-master PodControl]# kubectl get pod NAMEREADYSTATUSRESTARTSAGE deployment-demo-fb544c5d8-kz8fn1/1Running058s deployment-demo-fb544c5d8-p6q4v1/1Running058s deployment-demo-fb544c5d8-ttjmt1/1Running058s deployment-demo-fb544c5d8-x2lqw1/1Running058s[root@k8s-master PodControl]# kubectl describe deployment/deployment-demo Name:deployment-demo Namespace:default CreationTimestamp:Mon, 16 Aug 2021 16:17:28 +0800 Labels: 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 *statefulsetsExamples: # Rollback to the previous deployment kubectl rollout undo deployment/abc# Check the rollout status of a daemonset kubectl rollout status daemonset/fooAvailable Commands: historyView rollout history#历史记录 pauseMark the provided resource as paused#暂停 restartRestart a resource#重启 resumeResume a paused resource#恢复暂时资源 statusShow the status of the rollout#查看资源更新状态 undoUndo 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 NAMEREADYUP-TO-DATEAVAILABLEAGE deployment-demo4/4442[root@k8s-master PodControl]# kubectl get rs NAMEDESIREDCURRENTREADYAGE deployment-demo-867c7d9d554446m5s deployment-demo-fb544c5d800025m#旧版本副本数为0[root@k8s-master PodControl]# kubectl get pod -o wide NAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATES deployment-demo-867c7d9d55-d6xvt1/1Running06m55s10.244.1.229k8s-node1 deployment-demo-867c7d9d55-l65z91/1Running06m47s10.244.2.209k8s-node2 deployment-demo-867c7d9d55-vhs6t1/1Running06m55s10.244.3.56k8s-node3 deployment-demo-867c7d9d55-zc8dm1/1Running06m43s10.244.1.230k8s-node1 [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!

  • 【03.kubernetes笔记|03.kubernetes笔记 Pod控制器(二) Deployment】在次更新使用 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 NAMEDESIREDCURRENTREADYAGE deployment-demo-77d46c47944442m25s deployment-demo-867c7d9d5500010m deployment-demo-fb544c5d800029m#验证 [root@k8s-master PodControl]# kubectl get pod -o wide NAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATES deployment-demo-77d46c4794-fbt5l1/1Running02m44s10.244.2.210k8s-node2 deployment-demo-77d46c4794-kdvn21/1Running03m20s10.244.1.231k8s-node1 deployment-demo-77d46c4794-lcm281/1Running03m20s10.244.3.57k8s-node3 deployment-demo-77d46c4794-x52sh1/1Running02m40s10.244.1.232k8s-node1 [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 REVISIONCHANGE-CAUSE 1#更新过3次 3个不同的版本回滚时可以 2 3[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 REVISIONCHANGE-CAUSE 1 3 4#版本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 REVISIONCHANGE-CAUSE 3 4 5[root@k8s-master PodControl]# kubectl get rs NAMEDESIREDCURRENTREADYAGE deployment-demo-77d46c479400017m deployment-demo-867c7d9d5500026m deployment-demo-fb544c5d844445m#只有最开始的rs副本数为4 其它都为0[root@k8s-master PodControl]# kubectl get pod -o wide NAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATES deployment-demo-867c7d9d55-b65cm0/1Terminating04m2s10.244.1.234k8s-node1 deployment-demo-867c7d9d55-pv8fp1/1Terminating04m10s10.244.3.58k8s-node3 deployment-demo-867c7d9d55-tnhsg1/1Terminating04m14s10.244.1.233k8s-node1 deployment-demo-867c7d9d55-wcvl71/1Terminating04m14s10.244.2.211k8s-node2 deployment-demo-fb544c5d8-5f9lp1/1Running032s10.244.1.235k8s-node1 deployment-demo-fb544c5d8-d5lr91/1Running022s10.244.1.236k8s-node1 deployment-demo-fb544c5d8-lj5gt1/1Running029s10.244.3.59k8s-node3 deployment-demo-fb544c5d8-t9swj1/1Running032s10.244.2.212k8s-node2[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 statusdeployment/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 NAMEDESIREDCURRENTREADYAGE deployment-demo-58c8475bcc0005m42s deployment-demo-77d46c479422136m deployment-demo-867c7d9d5500044m deployment-demo-fb544c5d833363m[root@k8s-master ~]# kubectl get svc NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE demoapp-deployClusterIP10.107.174.16980/TCP18s#让流量[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 NAMEREADYUP-TO-DATEAVAILABLEAGE deployment-demo5/42590m deployment-demo-with-strategy4/44449s[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: 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 NAMEREADYUP-TO-DATEAVAILABLEAGE deployment-demo5/42593m deployment-demo-with-strategy4/4443m58s[root@k8s-master PodControl]# kubectl get pod NAMEREADYSTATUSRESTARTSAGE deployment-demo-77d46c4794-fhz4l1/1Running026m deployment-demo-77d46c4794-kmrhn1/1Running026m deployment-demo-fb544c5d8-5f9lp1/1Running046m deployment-demo-fb544c5d8-lj5gt1/1Running046m deployment-demo-fb544c5d8-t9swj1/1Running046m deployment-demo-with-strategy-57b96bdc87-8mjzw1/1Running039s deployment-demo-with-strategy-57b96bdc87-k6rxx1/1Running038s deployment-demo-with-strategy-57b96bdc87-v4tdj1/1Running038s deployment-demo-with-strategy-57b96bdc87-zdvcp1/1Running038s#查看更新状态 先减后增 [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

    推荐阅读