Pod的升级和回滚
Deployment的升级
apiVersion: apps/v1beta1 kind: Deployment metadata: name: nginx-deployment namespace: test spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
[root@lab-26 test]# kubectl get pod -n test NAME READY STATUS RESTARTS AGE nginx-deployment-5754944d6c-6x2zq 1/1 Running 0 59s nginx-deployment-5754944d6c-9cncj 1/1 Running 0 59s nginx-deployment-5754944d6c-wx5k4 1/1 Running 0 59s
方式1
现在Pod镜像需要被更新为Nginx:1.9.1,我们可以通过kubectl set image命令为Deployment设置新的镜像名称
[root@lab-26 test]# kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1 -n test deployment.extensions/nginx-deployment image updated [root@lab-26 test]# kubectl describe pod -n test | grep Image Image: nginx:1.9.1
方式2
[root@lab-26 test]# kubectl edit deployment/nginx-deployment -n test deployment.extensions/nginx-deployment edited
查看滚动状态
[root@lab-26 test]# kubectl rollout status deployment/nginx-deployment -n test deployment "nginx-deployment" successfully rolled out [root@lab-26 test]# kubectl describe pod -n test | grep Image Image: nginx:1.7.9 [root@lab-26 test]# kubectl get rs -n test NAME DESIRED CURRENT READY AGE nginx-deployment-5754944d6c 3 3 3 17m nginx-deployment-7448597cd5 0 0 0 11m
滚动更新时两个主要参数
- spec.strategy.rollingUpdate.maxUnavailable:用于指定 Deployment在更新过程中不可用状态的Pod数量的上限
- spec.strategy.rollingUpdate.maxSurge:用于指定在Deployment 更新Pod的过程中Pod总数超过Pod期望副本数部分的最大值
Deployment的回滚
假设在更新Deployment镜像时,将容器镜像名误设置成 Nginx:1.91(一个不存在的镜像)
[root@lab-26 test]# kubectl set image deployment/nginx-deployment nginx=nginx:1.91 -n test
查看创建的Pod,会发现新的ReplicaSet创建的1个Pod被卡在镜像拉取过程中
[root@lab-26 test]# kubectl get pod -n test NAME READY STATUS RESTARTS AGE nginx-deployment-5754944d6c-kmfg7 1/1 Running 0 55m nginx-deployment-5754944d6c-plgs9 1/1 Running 0 55m nginx-deployment-5754944d6c-qmd2f 1/1 Running 0 55m nginx-deployment-7ff84c8bc9-2z8hs 0/1 ErrImagePull 0 27s
回滚方式
首先,用kubectl rollout history命令检查这个Deployment部署的历史 记录
[root@lab-26 test]# kubectl rollout history deployment/nginx-deployment -n test deployment.extensions/nginx-deployment REVISION CHANGE-CAUSE 2 3 4 [root@lab-26 test]# kubectl rollout history deployment/nginx-deployment --revision=3 -n test deployment.extensions/nginx-deployment with revision #3 Pod Template: Labels: app=nginx pod-template-hash=5754944d6c Containers: nginx: Image: nginx:1.7.9 Port: 80/TCP Host Port: 0/TCP Environment: Mounts: Volumes:
注意,在创建Deployment时使用--record参数,就可以在CHANGE- CAUSE列看到每个版本使用的命令了。
默认回退到上个版本
[root@lab-26 test]# kubectl rollout undo deployment/nginx-deployment -n test deployment.extensions/nginx-deployment rolled back
指定版本
[root@lab-26 test]# kubectl rollout undo deployment/nginx-deployment --to-revision=3 -n test deployment.extensions/nginx-deployment rolled back
暂停和恢复Deployment的部署操作
对于一次复杂的Deployment配置修改,为了避免频繁触发 Deployment的更新操作,可以先暂停Deployment的更新操作,然后进行配置修改,再恢复Deployment,一次性触发完整的更新操作,就可以避 免不必要的Deployment更新操作了
通过kubectl rollout pause命令暂停Deployment的更新操作
[root@lab-26 test]# kubectl rollout pause deployment/nginx-deployment -n test deployment.extensions/nginx-deployment paused [root@lab-26 test]# kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1 -n test deployment.extensions/nginx-deployment image updated
查看Deployment的历史记录,发现并没有触发新的Deployment部署操作
[root@lab-26 test]# kubectl rollout history deployment/nginx-deployment -n test
在暂停Deployment部署之后,可以根据需要进行任意次数的配置更新。例如,再次更新容器的资源限制
[root@lab-26 test]# kubectl set resources deployment nginx-deployment -c=nginx --limits=cpu=200m,memory=512Mi -n test
最后,恢复这个Deployment的部署操作
[root@lab-26 test]# kubectl rollout resume deploy nginx-deployment -n test deployment.extensions/nginx-deployment resumed [root@lab-26 test]# kubectl describe deploy nginx-deployment -n test
DaemonSet的更新策略
目前DaemonSet的升级策略包括两种:OnDelete和RollingUpdate。
(1)OnDelete:DaemonSet的默认升级策略,与1.5及以前版本的 Kubernetes保持一致。当使用OnDelete作为升级策略时,在创建好新的 DaemonSet配置之后,新的Pod并不会被自动创建,直到用户手动删除 旧版本的Pod,才触发新建操作。
(2)RollingUpdate:从Kubernetes 1.6版本开始引入。当使用 RollingUpdate作为升级策略对DaemonSet进行更新时,旧版本的Pod将被 自动杀掉,然后自动创建新版本的DaemonSet Pod。整个过程与普通 Deployment的滚动升级一样是可控的。不过有两点不同于普通Pod的滚 动升级:一是目前Kubernetes还不支持查看和管理DaemonSet的更新历 史记录;二是DaemonSet的回滚(Rollback)并不能如同Deployment一 样直接通过kubectl rollback命令来实现,必须通过再次提交旧版本配置 的方式实现。
Pod的扩缩容
apiVersion: apps/v1beta1 kind: Deployment metadata: name: nginx-deployment namespace: test spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
通过kubectl scale命令可以将Pod副本数量从初始的3个更新为5个
扩容
[root@lab-26 test]# kubectl scale deploy nginx-deployment --replicas 5 -n test deployment.extensions/nginx-deployment scaled [root@lab-26 test]# kubectl get pod -n test NAME READY STATUS RESTARTS AGE nginx-deployment-7576c67d77-dfmhp 1/1 Running 0 17m nginx-deployment-7576c67d77-m6pbq 1/1 Running 0 53s nginx-deployment-7576c67d77-v82l9 1/1 Running 0 17m nginx-deployment-7576c67d77-z6v4h 1/1 Running 0 53s nginx-deployment-7576c67d77-z6xgj 1/1 Running 0 17m
缩容
[root@lab-26 test]# kubectl scale deploy nginx-deployment --replicas 2 -n test deployment.extensions/nginx-deployment scaled