Kubernetes m6S之资源同步控制器RC、RS、Deployment精解

 

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

 

主机配置规划

服务器名称(hostname)系统版本配置内网IP外网IP(模拟)
k8s-masterCentOS7.72C/4G/20G172.16.1.11010.0.0.110
k8s-node01CentOS7.72C/4G/20G172.16.1.11110.0.0.111
k8s-node02CentOS7.72C/4G/20G172.16.1.11210.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

5kind: ReplicaSet

6metadata:

7 name: frontend

8spec:

9 replicas: 3

10 selector:

11 matchLabels:

12 tier: frontend

13 template:

14 metadata:

15 labels:

16 tier: frontend

17 spec:

18 containers:

19 - name: nginx

20 image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17

21 imagePullPolicy: IfNotPresent

22 ports:

23 - name: httpd

24 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 # 查看状态

4NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR

5 frontend 333 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 # 查看详情

8Name: frontend

9Namespace: default

10 Selector: tier=frontend

11 Labels: <none>

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 desired

15 Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed

16Pod Template:

17 Labels: tier=frontend

18 Containers:

19 nginx:

20 Image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17

21 Port: 80/TCP

22 Host Port: 0/TCP

23 Environment: <none>

24 Mounts: <none>

25 Volumes: <none>

26Events:

27 Type Reason Age From Message

28 ---- ------ ---- ---- -------

29 Normal SuccessfulCreate 10m replicaset-controller Created pod: frontend-kltwp

30 Normal SuccessfulCreate 10m replicaset-controller Created pod: frontend-76dbn

31 Normal SuccessfulCreate 10m replicaset-controller Created pod: frontend-jk8td

 

查看pod状态信息

1 [root@k8s-master controller]# kubectl get pod -o wide --show-labels

2NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS

3 frontend-76dbn 1/1 Running 0 5m15s 10.244.4.31 k8s-node01 <none> <none> tier=frontend

4 frontend-jk8td 1/1 Running 0 5m15s 10.244.2.35 k8s-node02 <none> <none> tier=frontend

5 frontend-kltwp 1/1 Running 0 5m15s 10.244.2.34 k8s-node02 <none> <none> tier=frontend

 

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

1 [root@k8s-master controller]# kubectl delete pod frontend-kltwp

2 pod "frontend-kltwp" deleted

3 [root@k8s-master controller]#

4 [root@k8s-master controller]# kubectl get pod -o wide --show-labels # 可见重新创建了一个pod

5NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS

6 frontend-76dbn 1/1 Running 0 7m27s 10.244.4.31 k8s-node01 <none> <none> tier=frontend

7 frontend-jk8td 1/1 Running 0 7m27s 10.244.2.35 k8s-node02 <none> <none> tier=frontend

8 frontend-mf79k 1/1 Running 0 16s 10.244.4.32 k8s-node01 <none> <none> tier=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

5kind: Deployment

6metadata:

7 name: nginx-deployment

8 labels:

9 app: nginx

10spec:

11 replicas: 3

12 # 重点关注该字段

13 selector:

14 matchLabels:

15 app: nginx

16 template:

17 metadata:

18 labels:

19 app: nginx

20 spec:

21 containers:

22 - name: nginx

23 image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1

24 ports:

25 - containerPort: 80

26 [root@k8s-master controller]#

27 [root@k8s-master controller]# cat nginx-deployment-1.17.5.yaml

28 apiVersion: apps/v1

29kind: Deployment

30metadata:

31 name: nginx-deployment

32 labels:

33 app: nginx

34spec:

35 replicas: 3

36 # 重点关注该字段

37 selector:

38 matchLabels:

39 app: nginx

40 template:

41 metadata:

42 labels:

43 app: nginx

44 spec:

45 containers:

46 - name: nginx

47 image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5

48 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 --record

2 deployment.apps/nginx-deployment created

3 [root@k8s-master controller]#

4 [root@k8s-master controller]# kubectl get deployment -o wide

5 NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR

6 nginx-deployment 2/332 10s nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1 app=nginx

7

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 wide

2NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR

3 nginx-deployment-76b9d6bcf5 332 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 wide

2NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

3 nginx-deployment-76b9d6bcf5-ngpg5 1/1 Running 0 26s 10.244.2.43 k8s-node02 <none> <none>

4 nginx-deployment-76b9d6bcf5-rw827 1/1 Running 0 26s 10.244.2.44 k8s-node02 <none> <none>

5 nginx-deployment-76b9d6bcf5-ttf4j 0/1 ContainerCreating 0 26s <none> k8s-node01 <none> <none>

 

过一会儿状态说明

 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/333 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

6NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS

7 nginx-deployment-76b9d6bcf5 333 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-labels

10NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS

11 nginx-deployment-76b9d6bcf5-ngpg5 1/1 Running 0 23m 10.244.2.43 k8s-node02 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5

12 nginx-deployment-76b9d6bcf5-rw827 1/1 Running 0 23m 10.244.2.44 k8s-node02 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5

13 nginx-deployment-76b9d6bcf5-ttf4j 1/1 Running 0 23m 10.244.4.37 k8s-node01 <none> <none> app=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 --record

10

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 out

11# 如已更新完毕,直接显示更新成功

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/313 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

6NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS

7 nginx-deployment-56d78686f5 110 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 333 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-labels

11NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS

12 nginx-deployment-56d78686f5-4kn4c 0/1 ContainerCreating 0 30s <none> k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5

13 nginx-deployment-76b9d6bcf5-7lcr9 1/1 Running 0 12m 10.244.4.41 k8s-node01 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5

14 nginx-deployment-76b9d6bcf5-jbb5h 1/1 Running 0 12m 10.244.2.48 k8s-node02 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5

15 nginx-deployment-76b9d6bcf5-rt4m7 1/1 Running 0 12m 10.244.4.42 k8s-node01 <none> <none> app=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/333 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

6NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS

7 nginx-deployment-56d78686f5 333 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 000 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-labels

11NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS

12 nginx-deployment-56d78686f5-4kn4c 1/1 Running 0 3m25s 10.244.2.49 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5

13 nginx-deployment-56d78686f5-khsnm 1/1 Running 0 100s 10.244.2.50 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5

14 nginx-deployment-56d78686f5-t24qw 1/1 Running 0 2m44s 10.244.4.43 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5

 

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

 1 [root@k8s-master controller]# kubectl describe deploy nginx-deployment

2 Name: nginx-deployment

3Namespace: default

4 CreationTimestamp: Thu, 28 May 202000: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=true

11 Selector: app=nginx

12 Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable

13StrategyType: RollingUpdate

14 MinReadySeconds: 0

15 RollingUpdateStrategy: 25% max unavailable, 25% max surge

16Pod Template:

17 Labels: app=nginx

18 Containers:

19 nginx:

20 Image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5

21 Port: 80/TCP

22 Host Port: 0/TCP

23 Environment: <none>

24 Mounts: <none>

25 Volumes: <none>

26Conditions:

27 Type Status Reason

28 ---- ------ ------

29 Available True MinimumReplicasAvailable

30 Progressing True NewReplicaSetAvailable

31 OldReplicaSets: <none>

32 NewReplicaSet: nginx-deployment-56d78686f5 (3/3 replicas created)

33Events:

34 Type Reason Age From Message

35 ---- ------ ---- ---- -------

36 Normal ScalingReplicaSet 93s deployment-controller Scaled up replica set nginx-deployment-76b9d6bcf5 to 3

37 Normal ScalingReplicaSet 38s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to 1

38 Normal ScalingReplicaSet 37s deployment-controller Scaled down replica set nginx-deployment-76b9d6bcf5 to 2

39 Normal ScalingReplicaSet 37s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to 2

40 Normal ScalingReplicaSet 35s deployment-controller Scaled down replica set nginx-deployment-76b9d6bcf5 to 1

41 Normal ScalingReplicaSet 35s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to 3

42 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.yaml

2 nginx-deployment-1.17.yaml

3 nginx-deployment-1.17.1.yaml

4 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 --record

2 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/313 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

6NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS

7 nginx-deployment-55c7bdfb86 333 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 000 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 000 13m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1 app=nginx,pod-template-hash=76b9d6bcf5 app=nginx,pod-template-hash=76b9d6bcf5

10 nginx-deployment-844d7bbb7f 110 64s nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710 app=nginx,pod-template-hash=844d7bbb7f app=nginx,pod-template-hash=844d7bbb7f

11 [root@k8s-master controller]#

12 [root@k8s-master controller]# kubectl get pod -o wide --show-labels

13NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS

14 nginx-deployment-55c7bdfb86-bwzk9 1/1 Running 0 10m 10.244.4.49 k8s-node01 <none> <none> app=nginx,pod-template-hash=55c7bdfb86

15 nginx-deployment-55c7bdfb86-cmvzg 1/1 Running 0 10m 10.244.2.55 k8s-node02 <none> <none> app=nginx,pod-template-hash=55c7bdfb86

16 nginx-deployment-55c7bdfb86-kjrrw 1/1 Running 0 10m 10.244.2.56 k8s-node02 <none> <none> app=nginx,pod-template-hash=55c7bdfb86

17 nginx-deployment-844d7bbb7f-pctwr 0/1 ImagePullBackOff 0 2m3s 10.244.4.51 k8s-node01 <none> <none> app=nginx,pod-template-hash=844d7bbb7f

 

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

操作步骤如下:

检查 Deployment 修改历史

1 [root@k8s-master controller]# kubectl rollout history deployment/nginx-deployment

2 deployment.apps/nginx-deployment

3 REVISION CHANGE-CAUSE

41 kubectl apply --filename=nginx-deployment.yaml --record=true

52 kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5 --record=true

63 kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 --record=true

74 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

3Pod 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.17

11 Port: 80/TCP

12 Host Port: 0/TCP

13 Environment: <none>

14 Mounts: <none>

15 Volumes: <none>

 

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

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

1# 回滚到上一版本

2 [root@k8s-master controller]# kubectl rollout undo deployment/nginx-deployment

3 deployment.apps/nginx-deployment rolled back

4# 回滚到指定历史版本

5 [root@k8s-master controller]# kubectl rollout undo deployment/nginx-deployment --to-revision=2

6 deployment.apps/nginx-deployment rolled back

 

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

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/333 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

3Namespace: default

4 CreationTimestamp: Thu, 28 May 202000: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=true

11 Selector: app=nginx

12 Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable

13StrategyType: RollingUpdate

14 MinReadySeconds: 0

15 RollingUpdateStrategy: 25% max unavailable, 25% max surge

16Pod Template:

17 Labels: app=nginx

18 Containers:

19 nginx:

20 Image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5

21 Port: 80/TCP

22 Host Port: 0/TCP

23 Environment: <none>

24 Mounts: <none>

25 Volumes: <none>

26Conditions:

27 Type Status Reason

28 ---- ------ ------

29 Available True MinimumReplicasAvailable

30 Progressing True NewReplicaSetAvailable

31 OldReplicaSets: <none>

32 NewReplicaSet: nginx-deployment-56d78686f5 (3/3 replicas created)

33Events:

34 Type Reason Age From Message

35 ---- ------ ---- ---- -------

36………………

37 Normal ScalingReplicaSet 107s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to 1

38 Normal ScalingReplicaSet 104s deployment-controller Scaled down replica set nginx-deployment-55c7bdfb86 to 2

39 Normal ScalingReplicaSet 104s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to 2

40 Normal ScalingReplicaSet 103s deployment-controller Scaled down replica set nginx-deployment-55c7bdfb86 to 1

41 Normal ScalingReplicaSet 103s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to 3

42 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/101010 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

8NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS

9 nginx-deployment-55c7bdfb86 000 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 app=nginx,pod-template-hash=55c7bdfb86 app=nginx,pod-template-hash=55c7bdfb86

10 nginx-deployment-56d78686f5 101010 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.5 app=nginx,pod-template-hash=56d78686f5 app=nginx,pod-template-hash=56d78686f5

11 nginx-deployment-76b9d6bcf5 000 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.1 app=nginx,pod-template-hash=76b9d6bcf5 app=nginx,pod-template-hash=76b9d6bcf5

12 nginx-deployment-844d7bbb7f 000 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710 app=nginx,pod-template-hash=844d7bbb7f app=nginx,pod-template-hash=844d7bbb7f

13 [root@k8s-master controller]#

14 [root@k8s-master controller]# kubectl get pod -o wide --show-labels

15NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS

16 nginx-deployment-56d78686f5-4v5mj 1/1 Running 0 44s 10.244.2.64 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5

17 nginx-deployment-56d78686f5-8m7mx 1/1 Running 0 44s 10.244.4.60 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5

18 nginx-deployment-56d78686f5-c7wlb 1/1 Running 0 44s 10.244.4.59 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5

19 nginx-deployment-56d78686f5-jg5lt 1/1 Running 0 44s 10.244.2.63 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5

20 nginx-deployment-56d78686f5-jj58d 1/1 Running 0 11m 10.244.4.56 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5

21 nginx-deployment-56d78686f5-k2kts 1/1 Running 0 11m 10.244.4.57 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5

22 nginx-deployment-56d78686f5-qltkv 1/1 Running 0 44s 10.244.2.61 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5

23 nginx-deployment-56d78686f5-r7vmm 1/1 Running 0 11m 10.244.2.60 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5

24 nginx-deployment-56d78686f5-rxlpm 1/1 Running 0 44s 10.244.2.62 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5

25 nginx-deployment-56d78686f5-vlzrf 1/1 Running 0 44s 10.244.4.58 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5

 

清理策略Policy

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

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

 

相关阅读

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

完毕!

 


 

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

 

以上是 Kubernetes m6S之资源同步控制器RC、RS、Deployment精解 的全部内容, 来源链接: utcz.com/a/50475.html

回到顶部