Kubernetes K8S之资源控制器RC、RS、Deployment详解            

Kubernetes的资源控制器ReplicationController(RC)、ReplicaSet(RS)、Deployment(Deploy)详解与示例

 

Kubernetes的资源控制器ReplicationController(RC)、ReplicaSet(RS)、Deployment(Deploy)详解与示例

 

主机配置规划
服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟)
k8s-master CentOS7.7 2C/4G/20G 172.16.1.110 10.0.0.110
k8s-node01 CentOS7.7 2C/4G/20G 172.16.1.111 10.0.0.111
k8s-node02 CentOS7.7 2C/4G/20G 172.16.1.112 10.0.0.112

 

什么是控制器

kubernetes中内建了很多controller(控制器),这些相当于一个状态机,用来控制pod的具体状态和行为。

部分控制器类型如下:

  • ReplicationController 和 ReplicaSet
  • Deployment
  • DaemonSet
  • StatefulSet
  • Job/CronJob
  • HorizontalPodAutoscaler

 

ReplicationController 和 ReplicaSet

ReplicationController (RC)用来确保容器应用的副本数始终保持在用户定义的副本数,即如果有容器异常退出,会自动创建新的pod来替代;而异常多出来的容器也会自动回收。

在新版的Kubernetes中建议使用ReplicaSet (RS)来取代ReplicationController。ReplicaSet跟ReplicationController没有本质的不同,只是名字不一样,但ReplicaSet支持集合式selector。

虽然 ReplicaSets 可以独立使用,但如今它主要被Deployments 用作协调 Pod 的创建、删除和更新的机制。当使用 Deployment 时,你不必担心还要管理它们创建的 ReplicaSet,Deployment 会拥有并管理它们的 ReplicaSet。

ReplicaSet 是下一代的 Replication Controller。 ReplicaSet 和 Replication Controller 的唯一区别是选择器的支持。ReplicaSet 支持新的基于集合的选择器需求,这在标签用户指南中有描述。而 Replication Controller 仅支持基于相等选择器的需求。

 

Deployments

Deployment 控制器为 Pods和 ReplicaSets提供描述性的更新方式。用来替代以前的ReplicationController以方便管理应用。

典型的应用场景包括:

  • 定义Deployment来创建Pod和ReplicaSet
  • 滚动升级和回滚应用
  • 扩容和缩容
  • 暂停和继续Deployment

 

ReplicaSet示例

yaml文件

 1 [root@k8s-master controller]# pwd 2 /root/k8s_practice/controller 3 [root@k8s-master controller]# cat ReplicaSet-01.yaml 
 4 apiVersion: apps/v1 5 kind: ReplicaSet 6 metadata: 7   name: frontend 8 spec: 9   replicas: 310   selector:11     matchLabels:12       tier: frontend13   template:14     metadata:15       labels:16         tier: frontend17     spec:18       containers:19       - name: nginx20         image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1721         imagePullPolicy: IfNotPresent22         ports:23         - name: httpd24           containerPort: 80

 

创建ReplicaSet,并查看rs状态与详情

 1 [root@k8s-master controller]# kubectl apply -f ReplicaSet-01.yaml 
 2 replicaset.apps/frontend created 3 [root@k8s-master controller]# kubectl get rs -o wide    # 查看状态 4 NAME       DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES                                                        SELECTOR 5 frontend   3         3         3       2m12s   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17   tier=frontend 6 [root@k8s-master controller]# 
 7 [root@k8s-master controller]# kubectl describe rs frontend   # 查看详情 8 Name:         frontend 9 Namespace:    default10 Selector:     tier=frontend11 Labels:12 Annotations:  kubectl.kubernetes.io/last-applied-configuration:13                 {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"name":"frontend","namespace":"default"},"spec":{"replicas":3,"se...14 Replicas:     3 current / 3 desired15 Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed16 Pod Template:17   Labels:  tier=frontend18   Containers:19    nginx:20     Image:        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1721     Port:         80/TCP22     Host Port:    0/TCP23     Environment:24     Mounts:25   Volumes:26 Events:27   Type    Reason            Age    From                   Message28   ----    ------            ----   ----                   -------29   Normal  SuccessfulCreate  10m    replicaset-controller  Created pod: frontend-kltwp30   Normal  SuccessfulCreate  10m    replicaset-controller  Created pod: frontend-76dbn31   Normal  SuccessfulCreate  10m    replicaset-controller  Created pod: frontend-jk8td

 

查看pod状态信息

1 [root@k8s-master controller]# kubectl get pod -o wide --show-labels2 NAME             READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES   LABELS3 frontend-76dbn   1/1     Running   0          5m15s   10.244.4.31   k8s-node01tier=frontend4 frontend-jk8td   1/1     Running   0          5m15s   10.244.2.35   k8s-node02tier=frontend5 frontend-kltwp   1/1     Running   0          5m15s   10.244.2.34   k8s-node02tier=frontend

 

删除一个pod,然后再次查看

1 [root@k8s-master controller]# kubectl delete pod frontend-kltwp2 pod "frontend-kltwp" deleted3 [root@k8s-master controller]# 
4 [root@k8s-master controller]# kubectl get pod -o wide --show-labels   # 可见重新创建了一个pod5 NAME             READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES   LABELS6 frontend-76dbn   1/1     Running   0          7m27s   10.244.4.31   k8s-node01tier=frontend7 frontend-jk8td   1/1     Running   0          7m27s   10.244.2.35   k8s-node02tier=frontend8 frontend-mf79k   1/1     Running   0          16s     10.244.4.32   k8s-node01tier=frontend

由上可见,rs又新建了一个pod,保证了pod数总是为3.

 

Deployment示例

创建 Deployment

yaml文件

 1 [root@k8s-master controller]# pwd 2 /root/k8s_practice/controller 3 [root@k8s-master controller]# cat nginx-deployment-1.17.1.yaml 
 4 apiVersion: apps/v1 5 kind: Deployment 6 metadata: 7   name: nginx-deployment 8   labels: 9     app: nginx10 spec:11   replicas: 312   # 重点关注该字段13   selector:14     matchLabels:15       app: nginx16   template:17     metadata:18       labels:19         app: nginx20     spec:21       containers:22       - name: nginx23         image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.124         ports:25         - containerPort: 8026 [root@k8s-master controller]# 
27 [root@k8s-master controller]# cat nginx-deployment-1.17.5.yaml 
28 apiVersion: apps/v129 kind: Deployment30 metadata:31   name: nginx-deployment32   labels:33     app: nginx34 spec:35   replicas: 336   # 重点关注该字段37   selector:38     matchLabels:39       app: nginx40   template:41     metadata:42       labels:43         app: nginx44     spec:45       containers:46       - name: nginx47         image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.548         ports:49         - containerPort: 80

selector 字段定义 Deployment 如何查找要管理的 Pods。 在这种情况下,会选择在 template(Pod)模板中定义的标签labels(app: nginx)。但更复杂的选择规则是可能的,只要 template (Pod) 模板本身满足规则。

 

刚启动时状态说明

启动deployment,并查看状态

1 [root@k8s-master controller]# kubectl apply -f nginx-deployment-1.17.1.yaml --record2 deployment.apps/nginx-deployment created3 [root@k8s-master controller]# 
4 [root@k8s-master controller]# kubectl get deployment -o wide5 NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                          SELECTOR6 nginx-deployment   2/3     3            2           10s   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1   app=nginx7 8 # --record 参数可以记录命令,通过 kubectl rollout history deployment/nginx-deployment 可查询

参数说明:

  • NAME:列出集群中 Deployments 的名称
  • READY:已就绪副本数/期望副本数
  • UP-TO-DATE:显示已更新和正在更新中的副本数
  • AVAILABLE:显示应用程序可供用户使用的副本数
  • AGE:显示运行的时间

 

查看ReplicaSet状态

1 [root@k8s-master controller]# kubectl get rs -o wide2 NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                          SELECTOR3 nginx-deployment-76b9d6bcf5   3         3         2       17s   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1   app=nginx,pod-template-hash=76b9d6bcf5

参数说明:

  • NAME:列出集群中 ReplicaSet的名称
  • DESIRED:期望副本数
  • CURRENT:当前副本数
  • READY:已就绪副本数
  • AGE:运行时间

 

查看pod状态

1 [root@k8s-master controller]# kubectl get pod -o wide2 NAME                                READY   STATUS              RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES3 nginx-deployment-76b9d6bcf5-ngpg5   1/1     Running             0          26s   10.244.2.43   k8s-node024 nginx-deployment-76b9d6bcf5-rw827   1/1     Running             0          26s   10.244.2.44   k8s-node025 nginx-deployment-76b9d6bcf5-ttf4j   0/1     ContainerCreating   0          26sk8s-node01

 

过一会儿状态说明

 1 [root@k8s-master controller]# kubectl get deployment -o wide --show-labels 2 NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                          SELECTOR    LABELS 3 nginx-deployment   3/3     3            3           23m   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1   app=nginx   app=nginx 4 [root@k8s-master controller]# 
 5 [root@k8s-master controller]# kubectl get rs -o wide --show-labels 6 NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                          SELECTOR                                 LABELS 7 nginx-deployment-76b9d6bcf5   3         3         3       23m   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1   app=nginx,pod-template-hash=76b9d6bcf5   app=nginx,pod-template-hash=76b9d6bcf5 8 [root@k8s-master controller]# 
 9 [root@k8s-master controller]# kubectl get pod -o wide --show-labels10 NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES   LABELS11 nginx-deployment-76b9d6bcf5-ngpg5   1/1     Running   0          23m   10.244.2.43   k8s-node02app=nginx,pod-template-hash=76b9d6bcf512 nginx-deployment-76b9d6bcf5-rw827   1/1     Running   0          23m   10.244.2.44   k8s-node02app=nginx,pod-template-hash=76b9d6bcf513 nginx-deployment-76b9d6bcf5-ttf4j   1/1     Running   0          23m   10.244.4.37   k8s-node01app=nginx,pod-template-hash=76b9d6bcf5

重点说明

1、ReplicaSet 的名称始终被格式化为[DEPLOYMENT-NAME]-[RANDOM-STRING]。随机字符串是随机生成,并使用 pod-template-hash 作为选择器和标签。

2、Deployment 控制器将 pod-template-hash 标签添加到 Deployment 创建或使用的每个 ReplicaSet 。此标签可确保 Deployment 的子 ReplicaSets 不重叠。因此不可修改。

3、注意Deployment、ReplicaSet和Pod三者的名称关系

 

更新 Deployment

Deployment 可确保在更新时仅关闭一定数量的 Pods。默认情况下,它确保至少 75%所需 Pods 运行(25%最大不可用)。

Deployment 更新过程中还确保仅创建一定数量的 Pods 且高于期望的 Pods 数。默认情况下,它可确保最多增加 25% 期望 Pods 数(25%最大增量)。

备注:实际操作中如果更新Deployment,那么最好通过yaml文件更新,这样回滚到任何版本都非常便捷,而且更容易追述;而不是通过命令行。

如下Deployment示例,由于只有3个副本。因此更新时不会先删除旧的pod,而是先新建一个pod。新pod运行时,才会删除对应老的pod。一切的前提都是为了满足上述的条件。

需求:更新 nginx Pods,从当前的1.17.1版本改为1.17.5版本。

 1 # 方式一 2 kubectl edit deployment/nginx-deployment    # 然后修改 image 镜像信息 【不推荐】 3 # 上述方法不会记录命令,通过kubectl rollout history deployment/nginx-deployment 无法查询 4  5 # 方式二如下【可使用】: 6 kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5 --record 
 7  8 # 方式三如下【推荐★★★★★】 9 kubectl apply -f nginx-deployment-1.17.5.yaml --record10 11 # --record 参数可以记录命令,通过 kubectl rollout history deployment/nginx-deployment 可查询

 

要查看更新状态

 1 [root@k8s-master controller]# kubectl rollout status deployment/nginx-deployment 2 # 如没有更新完成,则显示更新过程直到更新成功 3 Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... 4 Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... 5 Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... 6 Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... 7 Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... 8 Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... 9 Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...10 deployment "nginx-deployment" successfully rolled out11 # 如已更新完毕,直接显示更新成功12 deployment "nginx-deployment" successfully rolled out

 

更新中的Deployment、ReplicaSet、Pod信息

 1 [root@k8s-master controller]# kubectl get deployment -o wide --show-labels 2 NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                          SELECTOR    LABELS 3 nginx-deployment   3/3     1            3           12m   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5   app=nginx   app=nginx 4 [root@k8s-master controller]# 
 5 [root@k8s-master controller]# kubectl get rs -o wide --show-labels 6 NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                          SELECTOR                                 LABELS 7 nginx-deployment-56d78686f5   1         1         0       23s   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5   app=nginx,pod-template-hash=56d78686f5   app=nginx,pod-template-hash=56d78686f5 8 nginx-deployment-76b9d6bcf5   3         3         3       12m   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1   app=nginx,pod-template-hash=76b9d6bcf5   app=nginx,pod-template-hash=76b9d6bcf5 9 [root@k8s-master controller]# 
10 [root@k8s-master controller]# kubectl get pod -o wide --show-labels11 NAME                                READY   STATUS              RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES   LABELS12 nginx-deployment-56d78686f5-4kn4c   0/1     ContainerCreating   0          30sk8s-node02app=nginx,pod-template-hash=56d78686f513 nginx-deployment-76b9d6bcf5-7lcr9   1/1     Running             0          12m   10.244.4.41   k8s-node01app=nginx,pod-template-hash=76b9d6bcf514 nginx-deployment-76b9d6bcf5-jbb5h   1/1     Running             0          12m   10.244.2.48   k8s-node02app=nginx,pod-template-hash=76b9d6bcf515 nginx-deployment-76b9d6bcf5-rt4m7   1/1     Running             0          12m   10.244.4.42   k8s-node01app=nginx,pod-template-hash=76b9d6bcf5

 

更新成功后的Deployment、ReplicaSet、Pod信息

 1 [root@k8s-master controller]# kubectl get deployment -o wide --show-labels 2 NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                          SELECTOR    LABELS 3 nginx-deployment   3/3     3            3           15m   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5   app=nginx   app=nginx 4 [root@k8s-master controller]# 
 5 [root@k8s-master controller]# kubectl get rs -o wide --show-labels 6 NAME                          DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES                                                          SELECTOR                                 LABELS 7 nginx-deployment-56d78686f5   3         3         3       3m23s   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5   app=nginx,pod-template-hash=56d78686f5   app=nginx,pod-template-hash=56d78686f5 8 nginx-deployment-76b9d6bcf5   0         0         0       15m     nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1   app=nginx,pod-template-hash=76b9d6bcf5   app=nginx,pod-template-hash=76b9d6bcf5 9 [root@k8s-master controller]# 
10 [root@k8s-master controller]# kubectl get pod -o wide --show-labels11 NAME                                READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES   LABELS12 nginx-deployment-56d78686f5-4kn4c   1/1     Running   0          3m25s   10.244.2.49   k8s-node02app=nginx,pod-template-hash=56d78686f513 nginx-deployment-56d78686f5-khsnm   1/1     Running   0          100s    10.244.2.50   k8s-node02app=nginx,pod-template-hash=56d78686f514 nginx-deployment-56d78686f5-t24qw   1/1     Running   0          2m44s   10.244.4.43   k8s-node01app=nginx,pod-template-hash=56d78686f5

 

通过查询Deployment详情,知晓pod替换过程

 1 [root@k8s-master controller]# kubectl describe deploy nginx-deployment 2 Name:                   nginx-deployment 3 Namespace:              default 4 CreationTimestamp:      Thu, 28 May 2020 00:04:09 +0800 5 Labels:                 app=nginx 6 Annotations:            deployment.kubernetes.io/revision: 2 7                         kubectl.kubernetes.io/last-applied-configuration: 8                           {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=nginx-deploy... 9                         kubernetes.io/change-cause:10                           kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5 --record=true11 Selector:               app=nginx12 Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable13 StrategyType:           RollingUpdate14 MinReadySeconds:        015 RollingUpdateStrategy:  25% max unavailable, 25% max surge16 Pod Template:17   Labels:  app=nginx18   Containers:19    nginx:20     Image:        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.521     Port:         80/TCP22     Host Port:    0/TCP23     Environment:24     Mounts:25   Volumes:26 Conditions:27   Type           Status  Reason28   ----           ------  ------29   Available      True    MinimumReplicasAvailable30   Progressing    True    NewReplicaSetAvailable31 OldReplicaSets:32 NewReplicaSet:   nginx-deployment-56d78686f5 (3/3 replicas created)33 Events:34   Type    Reason             Age   From                   Message35   ----    ------             ----  ----                   -------36   Normal  ScalingReplicaSet  93s   deployment-controller  Scaled up replica set nginx-deployment-76b9d6bcf5 to 337   Normal  ScalingReplicaSet  38s   deployment-controller  Scaled up replica set nginx-deployment-56d78686f5 to 138   Normal  ScalingReplicaSet  37s   deployment-controller  Scaled down replica set nginx-deployment-76b9d6bcf5 to 239   Normal  ScalingReplicaSet  37s   deployment-controller  Scaled up replica set nginx-deployment-56d78686f5 to 240   Normal  ScalingReplicaSet  35s   deployment-controller  Scaled down replica set nginx-deployment-76b9d6bcf5 to 141   Normal  ScalingReplicaSet  35s   deployment-controller  Scaled up replica set nginx-deployment-56d78686f5 to 342   Normal  ScalingReplicaSet  34s   deployment-controller  Scaled down replica set nginx-deployment-76b9d6bcf5 to 0

 

多 Deployment 动态更新

当 Deployment 正在展开进行更新时,Deployment 会为每个更新创建一个新的 ReplicaSet 并开始向上扩展,之前的 ReplicaSet 会被添加到旧 ReplicaSets 队列并开始向下扩展。

例如,假设创建一个 Deployment 以创建 nginx:1.7.9 的 5 个副本,然后更新 Deployment 以创建 5 个 nginx:1.9.1 的副本,而此时只有 3 个nginx:1.7.9 的副本已创建。在这种情况下, Deployment 会立即开始杀死3个 nginx:1.7.9 Pods,并开始创建 nginx:1.9.1 Pods。它不等待 nginx:1.7.9 的 5 个副本完成后再更新为nginx:1.9.1。

 

回滚 Deployment

yaml文件方式

针对应用的每个镜像版本,都有对应deploy的yaml文件。不管是升级还是回滚都已轻松应对。如下

1 nginx-deployment-1.15.6.yaml2 nginx-deployment-1.17.yaml3 nginx-deployment-1.17.1.yaml4 nginx-deployment-1.17.5.yaml

yaml文件中的信息,参考上文即可。

 

命令行方式

问题产生

假设在更新 Deployment 时犯了一个拼写错误,将镜像名称命名为了 nginx:1.1710 而不是 nginx:1.17.10

1 [root@k8s-master controller]# kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710 --record2 deployment.apps/nginx-deployment image updated

 

查看Deployment、ReplicaSet、Pod信息

 1 [root@k8s-master controller]# kubectl get deploy -o wide --show-labels 2 NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                          SELECTOR    LABELS 3 nginx-deployment   3/3     1            3           14m   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710   app=nginx   app=nginx 4 [root@k8s-master controller]# 5 [root@k8s-master controller]# kubectl get rs -o wide --show-labels 6 NAME                          DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES                                                          SELECTOR                                 LABELS 7 nginx-deployment-55c7bdfb86   3         3         3       9m19s   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17     app=nginx,pod-template-hash=55c7bdfb86   app=nginx,pod-template-hash=55c7bdfb86 8 nginx-deployment-56d78686f5   0         0         0       12m     nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5   app=nginx,pod-template-hash=56d78686f5   app=nginx,pod-template-hash=56d78686f5 9 nginx-deployment-76b9d6bcf5   0         0         0       13m     nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1   app=nginx,pod-template-hash=76b9d6bcf5   app=nginx,pod-template-hash=76b9d6bcf510 nginx-deployment-844d7bbb7f   1         1         0       64s     nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710   app=nginx,pod-template-hash=844d7bbb7f   app=nginx,pod-template-hash=844d7bbb7f11 [root@k8s-master controller]#12 [root@k8s-master controller]# kubectl get pod -o wide --show-labels13 NAME                                READY   STATUS             RESTARTS   AGE    IP            NODE         NOMINATED NODE   READINESS GATES   LABELS14 nginx-deployment-55c7bdfb86-bwzk9   1/1     Running            0          10m    10.244.4.49   k8s-node01app=nginx,pod-template-hash=55c7bdfb8615 nginx-deployment-55c7bdfb86-cmvzg   1/1     Running            0          10m    10.244.2.55   k8s-node02app=nginx,pod-template-hash=55c7bdfb8616 nginx-deployment-55c7bdfb86-kjrrw   1/1     Running            0          10m    10.244.2.56   k8s-node02app=nginx,pod-template-hash=55c7bdfb8617 nginx-deployment-844d7bbb7f-pctwr   0/1     ImagePullBackOff   0          2m3s   10.244.4.51   k8s-node01app=nginx,pod-template-hash=844d7bbb7f

 

需求:回滚到以前稳定的 Deployment 版本。

操作步骤如下:

检查 Deployment 修改历史

1 [root@k8s-master controller]# kubectl rollout history deployment/nginx-deployment2 deployment.apps/nginx-deployment 
3 REVISION  CHANGE-CAUSE4 1         kubectl apply --filename=nginx-deployment.yaml --record=true5 2         kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5 --record=true6 3         kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 --record=true7 4         kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710 --record=true

 

查看修改历史的详细信息,运行

 1 [root@k8s-master controller]# kubectl rollout history deployment/nginx-deployment --revision=3 2 deployment.apps/nginx-deployment with revision #3 3 Pod Template: 4   Labels:    app=nginx 5     pod-template-hash=55c7bdfb86 6   Annotations:    kubernetes.io/change-cause: 7       kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 --record=true 8   Containers: 9    nginx:10     Image:    registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1711     Port:    80/TCP12     Host Port:    0/TCP13     Environment:14     Mounts:15   Volumes:

 

回滚到上一次修改(即版本 3)或指定版本

现在已决定撤消当前更新并回滚到以前的版本

1 # 回滚到上一版本2 [root@k8s-master controller]# kubectl rollout undo deployment/nginx-deployment3 deployment.apps/nginx-deployment rolled back4 # 回滚到指定历史版本5 [root@k8s-master controller]# kubectl rollout undo deployment/nginx-deployment --to-revision=26 deployment.apps/nginx-deployment rolled back

 

检查回滚是否成功、 Deployment 是否正在运行

1 [root@k8s-master controller]# kubectl get deploy -o wide --show-labels2 NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                          SELECTOR    LABELS3 nginx-deployment   3/3     3            3           17h   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5   app=nginx   app=nginx

 

获取 Deployment 描述信息

 1 [root@k8s-master controller]# kubectl describe deployment 2 Name:                   nginx-deployment 3 Namespace:              default 4 CreationTimestamp:      Thu, 28 May 2020 00:04:09 +0800 5 Labels:                 app=nginx 6 Annotations:            deployment.kubernetes.io/revision: 7 7                         kubectl.kubernetes.io/last-applied-configuration: 8                           {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=nginx-deploy... 9                         kubernetes.io/change-cause:10                           kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5 --record=true11 Selector:               app=nginx12 Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable13 StrategyType:           RollingUpdate14 MinReadySeconds:        015 RollingUpdateStrategy:  25% max unavailable, 25% max surge16 Pod Template:17   Labels:  app=nginx18   Containers:19    nginx:20     Image:        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.521     Port:         80/TCP22     Host Port:    0/TCP23     Environment:24     Mounts:25   Volumes:26 Conditions:27   Type           Status  Reason28   ----           ------  ------29   Available      True    MinimumReplicasAvailable30   Progressing    True    NewReplicaSetAvailable31 OldReplicaSets:32 NewReplicaSet:   nginx-deployment-56d78686f5 (3/3 replicas created)33 Events:34   Type    Reason             Age                 From                   Message35   ----    ------             ----                ----                   -------36 ………………37   Normal  ScalingReplicaSet  107s                deployment-controller  Scaled up replica set nginx-deployment-56d78686f5 to 138   Normal  ScalingReplicaSet  104s                deployment-controller  Scaled down replica set nginx-deployment-55c7bdfb86 to 239   Normal  ScalingReplicaSet  104s                deployment-controller  Scaled up replica set nginx-deployment-56d78686f5 to 240   Normal  ScalingReplicaSet  103s                deployment-controller  Scaled down replica set nginx-deployment-55c7bdfb86 to 141   Normal  ScalingReplicaSet  103s                deployment-controller  Scaled up replica set nginx-deployment-56d78686f5 to 342   Normal  ScalingReplicaSet  102s                deployment-controller  Scaled down replica set nginx-deployment-55c7bdfb86 to 0

 

扩容/缩容Deployment

操作过程如下

 1 [root@k8s-master controller]# kubectl scale deployment/nginx-deployment --replicas=10 2 deployment.apps/nginx-deployment scaled 3 [root@k8s-master controller]# kubectl get deploy -o wide --show-labels 4 NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                          SELECTOR    LABELS 5 nginx-deployment   10/10   10           10          17h   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5   app=nginx   app=nginx 6 [root@k8s-master controller]# 
 7 [root@k8s-master controller]# kubectl get rs -o wide --show-labels 8 NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                          SELECTOR                                 LABELS 9 nginx-deployment-55c7bdfb86   0         0         0       17h   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17     app=nginx,pod-template-hash=55c7bdfb86   app=nginx,pod-template-hash=55c7bdfb8610 nginx-deployment-56d78686f5   10        10        10      17h   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5   app=nginx,pod-template-hash=56d78686f5   app=nginx,pod-template-hash=56d78686f511 nginx-deployment-76b9d6bcf5   0         0         0       17h   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1   app=nginx,pod-template-hash=76b9d6bcf5   app=nginx,pod-template-hash=76b9d6bcf512 nginx-deployment-844d7bbb7f   0         0         0       17h   nginx        registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710   app=nginx,pod-template-hash=844d7bbb7f   app=nginx,pod-template-hash=844d7bbb7f13 [root@k8s-master controller]# 
14 [root@k8s-master controller]# kubectl get pod -o wide --show-labels15 NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES   LABELS16 nginx-deployment-56d78686f5-4v5mj   1/1     Running   0          44s   10.244.2.64   k8s-node02app=nginx,pod-template-hash=56d78686f517 nginx-deployment-56d78686f5-8m7mx   1/1     Running   0          44s   10.244.4.60   k8s-node01app=nginx,pod-template-hash=56d78686f518 nginx-deployment-56d78686f5-c7wlb   1/1     Running   0          44s   10.244.4.59   k8s-node01app=nginx,pod-template-hash=56d78686f519 nginx-deployment-56d78686f5-jg5lt   1/1     Running   0          44s   10.244.2.63   k8s-node02app=nginx,pod-template-hash=56d78686f520 nginx-deployment-56d78686f5-jj58d   1/1     Running   0          11m   10.244.4.56   k8s-node01app=nginx,pod-template-hash=56d78686f521 nginx-deployment-56d78686f5-k2kts   1/1     Running   0          11m   10.244.4.57   k8s-node01app=nginx,pod-template-hash=56d78686f522 nginx-deployment-56d78686f5-qltkv   1/1     Running   0          44s   10.244.2.61   k8s-node02app=nginx,pod-template-hash=56d78686f523 nginx-deployment-56d78686f5-r7vmm   1/1     Running   0          11m   10.244.2.60   k8s-node02app=nginx,pod-template-hash=56d78686f524 nginx-deployment-56d78686f5-rxlpm   1/1     Running   0          44s   10.244.2.62   k8s-node02app=nginx,pod-template-hash=56d78686f525 nginx-deployment-56d78686f5-vlzrf   1/1     Running   0          44s   10.244.4.58   k8s-node01app=nginx,pod-template-hash=56d78686f5

 

清理策略Policy

可以在 Deployment 中设置 .spec.revisionHistoryLimit,以指定保留多少该 Deployment 的 ReplicaSets数量。其余的将在后台进行垃圾回收。默认情况下,是10。

注意:此字段设置为 0 将导致清理 Deployment 的所有历史记录,因此 Deployment 将无法通过命令行回滚。

 

相关阅读

1、Kubernetes K8S之kubectl命令详解及常用示例

完毕!

 


 

———END———
如果觉得不错就关注下呗 (-^O^-) !