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文件

 [root@k8s-master controller]# pwd
/root/k8s_practice/controller
[root@k8s-master controller]# cat ReplicaSet-.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
spec:
replicas:
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginx
image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17
imagePullPolicy: IfNotPresent
ports:
- name: httpd
containerPort:

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

 [root@k8s-master controller]# kubectl apply -f ReplicaSet-.yaml
replicaset.apps/frontend created
[root@k8s-master controller]# kubectl get rs -o wide # 查看状态
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
frontend 2m12s nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 tier=frontend
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl describe rs frontend # 查看详情
Name: frontend
Namespace: default
Selector: tier=frontend
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"name":"frontend","namespace":"default"},"spec":{"replicas":,"se...
Replicas: current / desired
Pods Status: Running / Waiting / Succeeded / Failed
Pod Template:
Labels: tier=frontend
Containers:
nginx:
Image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17
Port: /TCP
Host Port: /TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 10m replicaset-controller Created pod: frontend-kltwp
Normal SuccessfulCreate 10m replicaset-controller Created pod: frontend-76dbn
Normal SuccessfulCreate 10m replicaset-controller Created pod: frontend-jk8td

查看pod状态信息

 [root@k8s-master controller]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
frontend-76dbn / Running 5m15s 10.244.4.31 k8s-node01 <none> <none> tier=frontend
frontend-jk8td / Running 5m15s 10.244.2.35 k8s-node02 <none> <none> tier=frontend
frontend-kltwp / Running 5m15s 10.244.2.34 k8s-node02 <none> <none> tier=frontend

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

 [root@k8s-master controller]# kubectl delete pod frontend-kltwp
pod "frontend-kltwp" deleted
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get pod -o wide --show-labels # 可见重新创建了一个pod
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
frontend-76dbn / Running 7m27s 10.244.4.31 k8s-node01 <none> <none> tier=frontend
frontend-jk8td / Running 7m27s 10.244.2.35 k8s-node02 <none> <none> tier=frontend
frontend-mf79k / Running 16s 10.244.4.32 k8s-node01 <none> <none> tier=frontend

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

Deployment示例

创建 Deployment

yaml文件

 [root@k8s-master controller]# pwd
/root/k8s_practice/controller
[root@k8s-master controller]# cat nginx-deployment-1.17..yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas:
# 重点关注该字段
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.
ports:
- containerPort:
[root@k8s-master controller]#
[root@k8s-master controller]# cat nginx-deployment-1.17..yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas:
# 重点关注该字段
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.
ports:
- containerPort:

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

刚启动时状态说明

启动deployment,并查看状态

 [root@k8s-master controller]# kubectl apply -f nginx-deployment-1.17..yaml --record
deployment.apps/nginx-deployment created
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deployment / 10s nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx # --record 参数可以记录命令,通过 kubectl rollout history deployment/nginx-deployment 可查询

参数说明:

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

查看ReplicaSet状态

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

参数说明:

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

查看pod状态

 [root@k8s-master controller]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-76b9d6bcf5-ngpg5 / Running 26s 10.244.2.43 k8s-node02 <none> <none>
nginx-deployment-76b9d6bcf5-rw827 / Running 26s 10.244.2.44 k8s-node02 <none> <none>
nginx-deployment-76b9d6bcf5-ttf4j / ContainerCreating 26s <none> k8s-node01 <none> <none>

过一会儿状态说明

 [root@k8s-master controller]# kubectl get deployment -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment / 23m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx app=nginx
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get rs -o wide --show-labels
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment-76b9d6bcf5 23m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=76b9d6bcf5 app=nginx,pod-template-hash=76b9d6bcf5
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx-deployment-76b9d6bcf5-ngpg5 / Running 23m 10.244.2.43 k8s-node02 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5
nginx-deployment-76b9d6bcf5-rw827 / Running 23m 10.244.2.44 k8s-node02 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5
nginx-deployment-76b9d6bcf5-ttf4j / Running 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版本。

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

要查看更新状态

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

更新中的Deployment、ReplicaSet、Pod信息

 [root@k8s-master controller]# kubectl get deployment -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment / 12m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx app=nginx
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get rs -o wide --show-labels
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment-56d78686f5 23s nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=56d78686f5 app=nginx,pod-template-hash=56d78686f5
nginx-deployment-76b9d6bcf5 12m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=76b9d6bcf5 app=nginx,pod-template-hash=76b9d6bcf5
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx-deployment-56d78686f5-4kn4c / ContainerCreating 30s <none> k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-76b9d6bcf5-7lcr9 / Running 12m 10.244.4.41 k8s-node01 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5
nginx-deployment-76b9d6bcf5-jbb5h / Running 12m 10.244.2.48 k8s-node02 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5
nginx-deployment-76b9d6bcf5-rt4m7 / Running 12m 10.244.4.42 k8s-node01 <none> <none> app=nginx,pod-template-hash=76b9d6bcf5

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

 [root@k8s-master controller]# kubectl get deployment -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment / 15m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx app=nginx
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get rs -o wide --show-labels
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment-56d78686f5 3m23s nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=56d78686f5 app=nginx,pod-template-hash=56d78686f5
nginx-deployment-76b9d6bcf5 15m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=76b9d6bcf5 app=nginx,pod-template-hash=76b9d6bcf5
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx-deployment-56d78686f5-4kn4c / Running 3m25s 10.244.2.49 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-khsnm / Running 100s 10.244.2.50 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-t24qw / Running 2m44s 10.244.4.43 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5

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

 [root@k8s-master controller]# kubectl describe deploy nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Thu, May :: +
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision:
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=nginx-deploy...
kubernetes.io/change-cause:
kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. --record=true
Selector: app=nginx
Replicas: desired | updated | total | available | unavailable
StrategyType: RollingUpdate
MinReadySeconds:
RollingUpdateStrategy: % max unavailable, % max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.
Port: /TCP
Host Port: /TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-56d78686f5 (/ replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 93s deployment-controller Scaled up replica set nginx-deployment-76b9d6bcf5 to
Normal ScalingReplicaSet 38s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to
Normal ScalingReplicaSet 37s deployment-controller Scaled down replica set nginx-deployment-76b9d6bcf5 to
Normal ScalingReplicaSet 37s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to
Normal ScalingReplicaSet 35s deployment-controller Scaled down replica set nginx-deployment-76b9d6bcf5 to
Normal ScalingReplicaSet 35s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to
Normal ScalingReplicaSet 34s deployment-controller Scaled down replica set nginx-deployment-76b9d6bcf5 to

多 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文件。不管是升级还是回滚都已轻松应对。如下

 nginx-deployment-1.15..yaml
nginx-deployment-1.17.yaml
nginx-deployment-1.17..yaml
nginx-deployment-1.17..yaml

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

命令行方式

问题产生

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

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

查看Deployment、ReplicaSet、Pod信息

 [root@k8s-master controller]# kubectl get deploy -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment / 14m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710 app=nginx app=nginx
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get rs -o wide --show-labels
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment-55c7bdfb86 9m19s nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 app=nginx,pod-template-hash=55c7bdfb86 app=nginx,pod-template-hash=55c7bdfb86
nginx-deployment-56d78686f5 12m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=56d78686f5 app=nginx,pod-template-hash=56d78686f5
nginx-deployment-76b9d6bcf5 13m nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=76b9d6bcf5 app=nginx,pod-template-hash=76b9d6bcf5
nginx-deployment-844d7bbb7f 64s nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710 app=nginx,pod-template-hash=844d7bbb7f app=nginx,pod-template-hash=844d7bbb7f
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx-deployment-55c7bdfb86-bwzk9 / Running 10m 10.244.4.49 k8s-node01 <none> <none> app=nginx,pod-template-hash=55c7bdfb86
nginx-deployment-55c7bdfb86-cmvzg / Running 10m 10.244.2.55 k8s-node02 <none> <none> app=nginx,pod-template-hash=55c7bdfb86
nginx-deployment-55c7bdfb86-kjrrw / Running 10m 10.244.2.56 k8s-node02 <none> <none> app=nginx,pod-template-hash=55c7bdfb86
nginx-deployment-844d7bbb7f-pctwr / ImagePullBackOff 2m3s 10.244.4.51 k8s-node01 <none> <none> app=nginx,pod-template-hash=844d7bbb7f
需求:回滚到以前稳定的 Deployment 版本。

操作步骤如下:

检查 Deployment 修改历史

 [root@k8s-master controller]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
kubectl apply --filename=nginx-deployment.yaml --record=true
kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. --record=true
kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 --record=true
kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710 --record=true

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

 [root@k8s-master controller]# kubectl rollout history deployment/nginx-deployment --revision=
deployment.apps/nginx-deployment with revision #
Pod Template:
Labels: app=nginx
pod-template-hash=55c7bdfb86
Annotations: kubernetes.io/change-cause:
kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 --record=true
Containers:
nginx:
Image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17
Port: /TCP
Host Port: /TCP
Environment: <none>
Mounts: <none>
Volumes: <none>

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

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

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

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

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

获取 Deployment 描述信息

 [root@k8s-master controller]# kubectl describe deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Thu, May :: +
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision:
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=nginx-deploy...
kubernetes.io/change-cause:
kubectl set image deployment/nginx-deployment nginx=registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. --record=true
Selector: app=nginx
Replicas: desired | updated | total | available | unavailable
StrategyType: RollingUpdate
MinReadySeconds:
RollingUpdateStrategy: % max unavailable, % max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17.
Port: /TCP
Host Port: /TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-56d78686f5 (/ replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
………………
Normal ScalingReplicaSet 107s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to
Normal ScalingReplicaSet 104s deployment-controller Scaled down replica set nginx-deployment-55c7bdfb86 to
Normal ScalingReplicaSet 104s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to
Normal ScalingReplicaSet 103s deployment-controller Scaled down replica set nginx-deployment-55c7bdfb86 to
Normal ScalingReplicaSet 103s deployment-controller Scaled up replica set nginx-deployment-56d78686f5 to
Normal ScalingReplicaSet 102s deployment-controller Scaled down replica set nginx-deployment-55c7bdfb86 to

扩容/缩容Deployment

操作过程如下

 [root@k8s-master controller]# kubectl scale deployment/nginx-deployment --replicas=
deployment.apps/nginx-deployment scaled
[root@k8s-master controller]# kubectl get deploy -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment / 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx app=nginx
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get rs -o wide --show-labels
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-deployment-55c7bdfb86 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 app=nginx,pod-template-hash=55c7bdfb86 app=nginx,pod-template-hash=55c7bdfb86
nginx-deployment-56d78686f5 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=56d78686f5 app=nginx,pod-template-hash=56d78686f5
nginx-deployment-76b9d6bcf5 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17. app=nginx,pod-template-hash=76b9d6bcf5 app=nginx,pod-template-hash=76b9d6bcf5
nginx-deployment-844d7bbb7f 17h nginx registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.1710 app=nginx,pod-template-hash=844d7bbb7f app=nginx,pod-template-hash=844d7bbb7f
[root@k8s-master controller]#
[root@k8s-master controller]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx-deployment-56d78686f5-4v5mj / Running 44s 10.244.2.64 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-8m7mx / Running 44s 10.244.4.60 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-c7wlb / Running 44s 10.244.4.59 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-jg5lt / Running 44s 10.244.2.63 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-jj58d / Running 11m 10.244.4.56 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-k2kts / Running 11m 10.244.4.57 k8s-node01 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-qltkv / Running 44s 10.244.2.61 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-r7vmm / Running 11m 10.244.2.60 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-rxlpm / Running 44s 10.244.2.62 k8s-node02 <none> <none> app=nginx,pod-template-hash=56d78686f5
nginx-deployment-56d78686f5-vlzrf / Running 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 K8S之资源控制器RC、RS、Deployment详解的更多相关文章

  1. Kubernetes K8S之资源控制器Job和CronJob详解

    Kubernetes的资源控制器Job和CronJob详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7.7 2 ...

  2. Kubernetes K8S之资源控制器StatefulSets详解

    Kubernetes的资源控制器StatefulSet详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7.7 2 ...

  3. Kubernetes K8S之资源控制器Daemonset详解

    Kubernetes的资源控制器Daemonset详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7.7 2C/ ...

  4. Kubernetes K8S之Taints污点与Tolerations容忍详解

    Kubernetes K8S之Taints污点与Tolerations容忍详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master C ...

  5. Kubernetes K8S之affinity亲和性与反亲和性详解与示例

    Kubernetes K8S之Node节点亲和性与反亲和性以及Pod亲和性与反亲和性详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-mas ...

  6. k8s学习-资源控制器

    4.3.资源控制器 4.3.1.概念 Kubernetes中内建了很多种controller(控制器),这些相当于一个状态机,用来控制Pod的具体状态和行为. 4.3.2.分类 Replication ...

  7. kubernetes进阶(一) kubectl工具使用详解

    管理k8s核心资源的三种基本方法: 一.陈述式-主要依赖命令行工具  --可以满足90%以上的使用场景,但是缺点也很明显: 命令冗长,复杂,难以记忆 特定场景下,无法实现管理需求 对资源的增.删.查操 ...

  8. Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解

    一.控制器定义 控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现. 控制器解析用户的请求并将其转换为一个模型.在Spring MVC中一个控制器可以包含多个Action(动作. ...

  9. kubernetes之副本控制器(RC/RS)

    1.了解ReplicationController ReplicationController是一种kubernetes资源,可确保它的pod始终保持运行状态. 如果pod因任何原因消失(例如节点从集 ...

随机推荐

  1. 将形如 5D, 30s 的字符串转为秒

    import sys def convert_to_seconds(time_str): # write code here if 's' in time_str: return float(time ...

  2. FPGA内部IP核DDS

    项目当中需要正弦信号与余弦信号,首先想到了DDS芯片,例如AD9833.AD9834.由于还需要用FPGA   做一些数据处理,后来干脆直接用FPGA 内部的DDSIP核,同时根据IP核内部的相位累加 ...

  3. Python execfile() 函数

    描述 execfile() 函数可以用来执行一个文件.每组词 www.cgewang.com 语法 以下是 execfile() 方法的语法: execfile(filename[, globals[ ...

  4. Python File next() 方法

    概述 next() 方法在文件使用迭代器时会使用到,在循环中,next()方法会在每次循环中调用,该方法返回文件的下一行,如果到达结尾(EOF),则触发 StopIteration高佣联盟 www.c ...

  5. 4.26 省选模拟赛 T3 状压dp 差分求答案

    LINK:T3 比较好的题目 考试的时候被毒瘤的T2给搞的心态爆炸 这道题连正解的思路都没有想到. 一看到题求删除点的最少个 可以使得不连通. 瞬间想到最小割 发现对于10分直接跑最小割即可. 不过想 ...

  6. luogu P3409 值日班长值周班长 exgcd

    LINK:值日班长值周班长 题目描述非常垃圾. 题意:一周5天 每周有一个值周班长 每天有一个值日班长 值日班长日换 值周班长周换. 共n个值日班长 m个值周班长 A是第p个值日班长 B是第q个值日班 ...

  7. 数据分析First week(7.15~7.21)

    描述统计学 当我们面对大量信息的时候,经常会出现数据越多,事实越模糊的情况,因此我们需要对数据进行简化,描述统计学就是用几个关键的数字来描述数据集的整体情况. 1.集中趋势 1.1 众数 众数是样本观 ...

  8. Spring JdbcTemplate中关于RowMapper的使用实例

    在spring boot 集成使用jdbctemplate,首先在pom文件中引入相应的依赖 <dependency> <groupId>org.springframework ...

  9. CSS 定位总结

    目录 元素显示模式 元素模式 元素显示模式转换 CSS定位机制 静态定位static 相对定位relative 绝对定位absolute 固定定位fixed 粘性定位sticky 定位小结一图流 CS ...

  10. 2020-03-27:分布式锁的问题,假如a线程在获得锁的情况下 网络波动 极端情况是断网了,这种情况是怎么处理的

    福哥答案2020-04-04:超时释放锁.