K8S(2)RC、RS和Deployment(下)

简介: K8S(2)RC、RS和Deployment(下)

- 扩展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不可用
相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
1月前
|
Kubernetes 应用服务中间件 nginx
【赵渝强老师】K8s中的Deployment控制器
Kubernetes中的Deployment用于部署无状态应用程序,管理Pod的数量、更新方式和资源限制。通过创建和管理ReplicaSet,Deployment可以实现Pod的自动扩缩容、滚动更新和回滚。本文介绍了Deployment的基本概念,并通过一个具体的示例演示了如何使用Deployment创建、更新和管理Pod。
|
1月前
|
存储 Kubernetes 调度
【赵渝强老师】K8s中Deployment控制器与StatefulSet控制器的区别
K8s中的Deployment控制器用于管理无状态应用程序,关注Pod数量、更新方式等;而StatefulSets控制器则管理有状态应用程序,提供持久存储和唯一标识符,适用于需要稳定网络标识符和持久化存储的场景。两者的主要区别在于是否维护状态和顺序。
|
4月前
|
Kubernetes 容器 Perl
在K8S中,Deployment⽀持扩容吗?它与HPA有什么区别?
在K8S中,Deployment⽀持扩容吗?它与HPA有什么区别?
|
4月前
|
存储 Kubernetes 网络协议
在K8S中,Deployment和Statefulset有何区别?
在K8S中,Deployment和Statefulset有何区别?
|
4月前
|
Kubernetes API 开发工具
在K8S中,Deployment的升级过程是什么?
在K8S中,Deployment的升级过程是什么?
|
4月前
|
存储 Kubernetes 调度
在K8S中,deployment的创建过程包括什么?
在K8S中,deployment的创建过程包括什么?
|
4月前
|
Kubernetes API 容器
在K8S中,deployment的yaml文件如何编写呢?
在K8S中,deployment的yaml文件如何编写呢?
|
存储 Kubernetes NoSQL
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(六)
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(六)
193 0
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(六)
|
监控 应用服务中间件 nginx
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(五)
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(五)
190 0
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(五)
|
数据可视化 Perl 容器
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(四)
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(四)
299 0
KUBERNETES03_命名空间、Pod是什么、Deployment多副本能力、扩缩容、自愈或故障转移、滚动更新、版本回退(四)
下一篇
DataWorks