一、Deployment控制器
deployment控制器资源的主要职责同样是为了保证POD资源的健康运行、其大部分功能均可通过调用replicaset控制器来实现
同时还增添了部分特性:
1、事件和状态查看:必要时可以查看Deployment对象升级的详细进度和状态
2、回滚:升级操作完成后发现问题时、支持使用回滚机制将应用返回到前一个或由用户指定的历史记录中的版本上
3、版本记录:对Deployment对象的每一次操作都予以保存、以供后续可能执行的回滚操作使用
4、暂停和启动:对于每一次升级、都能够随时暂停和启动
5、多种自动更新方案:一个Recreate、即重建更新机制、全面停止、删除旧有的POD后用新版本替代;另一个RollingUpdate
即滚动升级机制、逐步替换旧有的pod至新的版本
二、 创建deployment
1、资源清单
[root@master chapter5]# cat myapp-deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: name: myapp-deploy spec: replicas: 3 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - containerPort: 80 name: http
2、创建运行
[root@master chapter5]# kubectl apply -f myapp-deploy.yaml --record deployment.apps/myapp-deploy created
3、验证效果
[root@master chapter5]# kubectl apply -f myapp-deploy.yaml --record deployment.apps/myapp-deploy created [root@master chapter5]# kubectl get ds myapp-deploy Error from server (NotFound): daemonsets.apps "myapp-deploy" not found [root@master chapter5]# kubectl get deployments myapp-deploy NAME READY UP-TO-DATE AVAILABLE AGE myapp-deploy 3/3 3 3 27s
三、更新策略
1、查看详细更新事件
[root@master chapter5]# kubectl describe deployments myapp-deploy Name: myapp-deploy Namespace: default CreationTimestamp: Fri, 12 Jun 2020 11:55:09 +0800 Labels:Annotations: deployment.kubernetes.io/revision: 1 kubernetes.io/change-cause: kubectl apply --filename=myapp-deploy.yaml --record=true Selector: app=myapp 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=myapp Containers: myapp: Image: ikubernetes/myapp:v1 Port: 80/TCP Host Port: 0/TCP Environment: Mounts: Volumes: Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: NewReplicaSet: myapp-deploy-5cbd66595b (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 3m23s deployment-controller Scaled up replica set myapp-deploy-5cbd66595b to 3
2、滚动更新图解
3、更新字段详解
[root@master ~]# kubectl explain deploy.spec.strategy.rollingUpdate KIND: Deployment VERSION: apps/v1 RESOURCE: rollingUpdate
4、maxSurge和maxUnavailable的作用方式
5、deployments的版本历史记录
[root@master ~]# kubectl explain deploy.spec.revisionHistoryLimit KIND: Deployment VERSION: apps/v1 FIELD: revisionHistoryLimitDESCRIPTION: #用户可安需回滚到指定的历史版本、控制器可保存的历史版本数量由"revisionHistoryLimit" 属性进行定义、当然、也只有保存于revision历史中的Replicaset版本可用于回滚、因此、用户要习惯性地在更新操作时指定保留旧版本 The number of old ReplicaSets to retain to allow rollback. This is a pointer to distinguish between explicit zero and not specified. Defaults to 10.
为了保存版本升级的历史、需要在创建deployments、replicas和strategy对象时于命令中使用"--record"选项
四、升级deployment
升级副本数
[root@master chapter5]# kubectl patch deployments myapp-deploy -p '{ "spec": {"minReadySeconds":5}}' deployment.apps/myapp-deploy patched
更改镜像 [root@master chapter5]# kubectl set image deployments myapp-deploy myapp=ikubernetes/myapp:v2 deployment.apps/myapp-deploy image updated
[root@master chapter5]# kubectl rollout status deployments myapp-deploy Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "myapp-deploy" rollout to finish: 1 old replicas are pending termination... deployment "myapp-deploy" successfully rolled out
验证效果 [root@master chapter5]# kubectl get deployments myapp-deploy --watch NAME READY UP-TO-DATE AVAILABLE AGE myapp-deploy 3/3 3 3 7m58s [root@master chapter5]# kubectl get pods -l app=myapp NAME READY STATUS RESTARTS AGE myapp-deploy-6685c8c7fc-2f5gz 1/1 Running 0 86s myapp-deploy-6685c8c7fc-58jcr 1/1 Running 0 79s myapp-deploy-6685c8c7fc-prmnc 1/1 Running 0 98s
测试 [root@master chapter5]# curl $(kubectl get pods myapp-deploy-6685c8c7fc-2f5gz -o go-template={{.status.podIP}}) Hello MyApp | Version: v2 | Pod Name
修改deployments控制器的和等字段minReadySeconds的值并不会触发pod资源的更新操作、因为它不属于模板的内嵌字段、对现在的pod对象不产生任何影响
五、金丝雀发布
为了尽可能降低对现有系统及其容量的影响,金丝雀发布过程通常建议采用
1、先添加、再删除
2、且可用pod资源对象总数不低于期望值的方式进行
首批添加1个Pod资源的方式:将deployments控制器的maxSurge属性的值设置为1,并将maxUnavailable属性的值设置为0
[root@master ~]# kubectl patch deployments myapp-deploy -p '{ "strategy": {"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}' deployment.apps/myapp-deploy patched (no change)
启动myapp-deploy 控制器的更新过程:在修改性用容器的镜像版本后立即暂停更新进度,它会在启动第一批新版本pod对象的创建操作之后转为暂停状态
[root@master ~]# kubectl set image deployments myapp-deploy myapp=ikubernetes/myapp:v3 \ && kubectl rollout pause deployments deployment.apps/myapp-deploy paused
通过其状态查看命令可以看到,在创建完一个新版本的pod资源后滚动更新操作"暂停"
[root@master ~]# kubectl rollout status deployments myapp-deploy deployment "myapp-deploy" successfully rolled out [root@master ~]# kubectl rollout resume deployments myapp-deploy deployment.apps/myapp-deploy resumed [root@master ~]# kubectl rollout status deployments myapp-deploy deployment "myapp-deploy" successfully rolled out
六、回滚deployment控制器下的应用发布
[root@master ~]# kubectl rollout undo deployments myapp-deploy deployment.apps/myapp-deploy rolled back
[root@master ~]# kubectl rollout history deployments myapp-deploy deployment.apps/myapp-deploy REVISION CHANGE-CAUSE 1 kubectl apply --filename=myapp-deploy.yaml --record=true 3 kubectl apply --filename=myapp-deploy.yaml --record=true 4 kubectl apply --filename=myapp-deploy.yaml --record=true