- 扩展Deployment
- 当应用程序的访问量越来越大,使用3个pod已经无法继续支撑时,我们可以对
Pod的副本数进行修改
- 下面来看案例:
- 使用kubectl scale 进行修改,--replicas可以指定修改的副本数,--record=true可以使这条命令进行保留 [root@master1 ~]# kubectl scale deployment nginx-deployment --replicas=5 --record=true deployment.apps/nginx-deployment scaled [root@master1 ~]# kubectl rollout history deployment nginx-deployment deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 4 kubectl scale deployment nginx-deployment --replicas=3 --record=true 5 kubectl scale deployment nginx-deployment --replicas=5 --record=true [root@master1 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-6b75f4cbdd-7kn6x 1/1 Running 0 16s nginx-deployment-6b75f4cbdd-7m72m 1/1 Running 0 3m2s nginx-deployment-6b75f4cbdd-nh4rb 1/1 Running 0 2m58s nginx-deployment-6b75f4cbdd-ntqw4 1/1 Running 0 3m nginx-deployment-6b75f4cbdd-tvnxs 1/1 Running 0 16s
- Deployment暂停更新、恢复
- Deployment支持
暂停更新
,期间可以对Deployment进行多次修改
[root@master1 ~]# kubectl rollout pause deployment nginx-deployment #kubectl rollout pause暂停 deployment.apps/nginx-deployment paused #然后进行更新操作 [root@master1 ~]# kubectl set image deployment nginx-deployment nginx=nginxdemos/hello:plain-text deployment.apps/nginx-deployment image updated [root@master1 ~]# kubectl describe deployments.apps nginx-deployment #查看详细信息 Name: nginx-deployment Namespace: default CreationTimestamp: Thu, 23 Jun 2022 10:31:28 +0800 Labels: app=nginx Annotations: deployment.kubernetes.io/revision: 6 kubernetes.io/change-cause: kubectl scale deployment nginx-deployment --replicas=5 --record=true Selector: app=nginx Replicas: 5 desired | 0 updated | 5 total | 5 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginxdemos/hello:plain-text #成功更新 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing Unknown DeploymentPaused OldReplicaSets: nginx-deployment-6b75f4cbdd (5/5 replicas created) NewReplicaSet: nginx-deployment-5d6b8b588c (0/0 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 87m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 1 Normal ScalingReplicaSet 87m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 87m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 87m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 1 Normal ScalingReplicaSet 87m (x2 over 29h) deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 3 Normal ScalingReplicaSet 87m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 0 Normal ScalingReplicaSet 81m (x2 over 6h29m) deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 1 Normal ScalingReplicaSet 81m (x2 over 6h29m) deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 81m (x3 over 29h) deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 81m (x3 over 81m) deployment-controller (combined from similar events): Scaled down replica set nginx-deployment-6b75f4cbdd to 0 Normal ScalingReplicaSet 63m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 3 Normal ScalingReplicaSet 62m (x2 over 64m) deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 4 Normal ScalingReplicaSet 36m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 3 Normal ScalingReplicaSet 28m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 1 Normal ScalingReplicaSet 28m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 28m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 28m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 1 Normal ScalingReplicaSet 28m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 3 Normal ScalingReplicaSet 28m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 0 Normal ScalingReplicaSet 25m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 5
- 使用
kubectl rollout resume
可以恢复Deployment更新
[root@master1 ~]# kubectl rollout pause deployment nginx-deployment deployment.apps/nginx-deployment paused [root@master1 ~]# kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true deployment.apps/nginx-deployment image updated [root@master1 ~]# kubectl rollout resume deployment nginx-deployment [root@master1 ~]# kubectl describe deployments.apps nginx-deployment Name: nginx-deployment Namespace: default CreationTimestamp: Thu, 23 Jun 2022 10:31:28 +0800 Labels: app=nginx Annotations: deployment.kubernetes.io/revision: 7 kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.14-alpine --record=true Selector: app=nginx Replicas: 5 desired | 5 updated | 5 total | 5 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.14-alpine #镜像已经更换 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: nginx-deployment-6b75f4cbdd (5/5 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 57m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 3 Normal ScalingReplicaSet 49m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 1 Normal ScalingReplicaSet 49m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 49m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 49m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 1 Normal ScalingReplicaSet 49m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 3 Normal ScalingReplicaSet 49m deployment-controller Scaled down replica set nginx-deployment-5d6b8b588c to 0 Normal ScalingReplicaSet 46m deployment-controller Scaled up replica set nginx-deployment-6b75f4cbdd to 5 Normal ScalingReplicaSet 2m49s deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 3 Normal ScalingReplicaSet 2m49s deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 2 Normal ScalingReplicaSet 2m49s deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 4 Normal ScalingReplicaSet 2m48s deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 3 Normal ScalingReplicaSet 2m48s deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 4 Normal ScalingReplicaSet 2m47s deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 2 Normal ScalingReplicaSet 2m47s deployment-controller Scaled up replica set nginx-deployment-5d6b8b588c to 5 Normal ScalingReplicaSet 2m47s deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 1 Normal ScalingReplicaSet 2m46s deployment-controller Scaled down replica set nginx-deployment-6b75f4cbdd to 0 Normal ScalingReplicaSet 55s (x9 over 58s) deployment-controller (combined from similar events): Scaled down replica set nginx-deployment-5d6b8b588c to 0
- 更新Deployment的注意事项
- rollout版本保留策略:
在默认情况下,revision保留10个版本(RS)
,其余的将在后台进行垃圾回收,可以在spec.revisionHistoryLimit
设置保留版本(RS)的个数。当设置为0时,不保留历史记录
deployment更新策略:
- spec.strategy.type==Recreate:表示重建,先删掉旧的Pod再创建新的Pod
- spec.strategy.type==RollingUpdate:表示滚动更新,可以指定下面的``maxUnavailable和maxSurge`来控制滚动更新过程
- spec.strategy.rollingUpdate.maxUnavailable:指定在回滚更新时最大不可用的Pod数量,可选字段,默认为25%,可以设置为数字或百分比,如果maxSurge设置为0,则这个值也不能为0
- spec.strategy.rollingUpdate.maxSurge:可以超过期望值的最大Pod数,可选字段,默认为25%,可以设置成数字或百分比,如果maxUnavailable为0,则该值不能为0
- maxSurge: 25%:指定一次可以添加多少个Pod
- maxUnavailable: 25%:指定滚动更新期间最多有多少个Pod不可用