K8S-资源管理器
Posted guardwhy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K8S-资源管理器相关的知识,希望对你有一定的参考价值。
1-什么是资源控制器
1、Controller Manager(资源控制器) 由kube-controller-manager
和 cloud-controller-manager
组成, 是Kubernetes 的大脑,它通过 apiserver 监控整个集群的状态, 并确保集群处于预期的工作状态。
2、kube-controller-manager
kube-controller-manager运行管理控制器,它们是集群中处理常规任务的后台线程,逻辑上,每个控制器是一个单独的进程,但为了降低复杂性,它们都被编译成单个二进制文件,并在单个进程中运行。
这些控制器包括:
- 节点(Node)控制器。
- 副本(Replication)控制器:负责维护系统中每个副本中的pod。
- 端点(Endpoints)控制器:填充Endpoints对象(即连接Services&Pods)。
- Service Account和Token控制器:为新的Namespace 创建默认帐户访问API Token。
3、cloud-controller-manager
- 云控制器管理器负责与底层云提供商的平台交互。云控制器管理器是Kubernetes版本1.6中引入的,目前还是Alpha的功能。
- 云控制器管理器仅运行云提供商特定的(controller loops)控制器循环。可以通过将
--cloud-provider
flag设置为external。 - 启动
kube-controller-manager
来禁用控制器循环。
cloud-controller-manager 具体功能:
节点(Node Controller)控制器
路由(Route Controller)控制器
Service(Service Controller)控制器
卷(VolumeController)控制器
2- 常见Pod控制器
1、ReplicaSet:适合无状态的服务部署
用户创建指定数量的pod副本数量,确保pod副本数量符合预期状态,并且支持滚动式自动扩容和缩容功能。
ReplicaSet主要三个组件组成
- 用户期望的pod副本数量。
- 标签选择器,判断哪个pod归自己管理。
- 当现存的pod数量不足,会根据pod资源模板进行新建。
注意点:
帮助用户管理无状态的pod资源,精确反应用户定义的目标数量,但是RelicaSet不是直接使用的控制器,而是使用Deployment。
2、deployment:适合无状态的服务部署,工作在ReplicaSet之上,用于管理无状态应用,目前来说最好的控制器。
支持滚动更新和回滚功能,还提供声明式配置。
3、StatefullSet:适合有状态的服务部署。需要学完存储卷后进行系统学习。
4、DaemonSet:一次部署,所有的node节点都会部署,一些典型的应用场景:
- 运行集群存储 daemon,例如在每个Node上运行 glusterd、ceph。
- 在每个Node上运行日志收集 daemon,例如 fluentd、 logstash
- 在每个Node上运行监控 daemon,例如 Prometheus Node Exporter
- 注意点: 用于确保集群中的每一个节点只运行特定的pod副本,通常用于实现系统级后台任务。
- 特性:服务是无状态的服务必须是守护进程
5、Job:一次性的执行任务,只要完成就立即退出,不需要重启或重建。
6、Cronjob:周期性的执行任务,周期性任务控制,不需要持续后台运行。
3- Replication Controller
3.1 基本概念
replication controller简称RC,是kubernetes系统中的核心概念之一,简单来说,它其实定义了一个期望的场景,即声明某种pod的副本数量在任意时刻都复合某个预期值,所以RC的定义包含以下部分
- pod期待的副本数量。
- 用于筛选目标pod的Label Selector。
- 当pod的副本数量小于期望值时,用于创建新的pod的pod模板(template)。
3.2 ReplicaSet
1、基本简介
ReplicationController
用来确保容器应用的副本数始终保持在用户定义的副本数,即如果有容器异常退出,会自动创建新的Pod来替代,而如果异常多出来的容器也会自动回收。- 在新版本的Kubernetes中建议使用ReplicaSet来取代ReplicationController。ReplicaSet跟ReplicationController没有本质的不同,只是名字不一样,并且ReplicaSet支持集合式的selector。
- 虽然ReplicaSet可以独立使用,但一般还是建议使用 Deployment 来自动管理ReplicaSet,这样就无需担心跟其他机制的不兼容问题(比如ReplicaSet不支持rolling-update但Deployment支持)。
2、环境所需镜像
docker pull nginx:1.17.10-alpine
docker pull nginx:1.18.0-alpine
docker pull nginx:1.19.2-alpine
3、ReplicaSet模板说明
apiVersion: apps/v1 #api版本定义
kind: ReplicaSet #定义资源类型为ReplicaSet
metadata: #元数据定义
name: guardwhy
namespace: default
spec: #ReplicaSet的规格定义
replicas: 2 #定义副本数量为2个
selector: #标签选择器,定义匹配pod的标签
matchLabels:
app: guardwhy
release: canary
template: #pod的模板定义
metadata: #pod的元数据定义
name: guardwhy-pod #自定义pod的名称
labels: #定义pod的标签,需要和上面定义的标签一致,也可以多出其他标签
app: guardwhy
release: canary
environment: qa
spec: #pod的规格定义
containers: #容器定义
- name: guardwhy-container #容器名称
image: nginx:1.17.10-alpine #容器镜像
ports: #暴露端口
- name: http
containerPort: 80
3.3 部署ReplicaSet
1、在k8sdemo01
工程创建replicasetdemo.yml
文件
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicasetdemo
labels:
app: replicasetdemo
spec:
replicas: 1
template:
metadata:
name: replicasetdemo
labels:
app: replicasetdemo
spec:
containers:
- name: replicasetdemo
image: nginx:1.17.10-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
restartPolicy: Always
selector:
matchLabels:
app: replicasetdemo
2、通过idea的Remote Host快速将yml文件上传k8s集群进行测试
[root@k8s-master01 controller]# ls
replicasetdemo.yml
# 1、运行ReplicaSet
[root@k8s-master01 controller]# kubectl apply -f replicasetdemo.yml
replicaset.apps/replicasetdemo created
# 2、查看rs控制器
[root@k8s-master01 controller]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicasetdemo 1 1 1 25s
# 3、查看pod信息
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicasetdemo-2gllp 1/1 Running 0 34s
# 4、查看pod详细信息
[root@k8s-master01 controller]# kubectl describe pod replicasetdemo-2gllp
Name: replicasetdemo-2gllp
Namespace: default
Priority: 0
Node: k8s-node01/172.21.252.4
Start Time: Thu, 08 Jul 2021 22:43:10 +0800
Labels: app=replicasetdemo
Annotations: cni.projectcalico.org/podIP: 10.81.85.230/32
cni.projectcalico.org/podIPs: 10.81.85.230/32
Status: Running
IP: 10.81.85.230
IPs:
IP: 10.81.85.230
Controlled By: ReplicaSet/replicasetdemo
.....................
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned default/replicasetdemo-2gllp to k8s-node01
Normal Pulled 67s kubelet, k8s-node01 Container image "nginx:1.17.10-alpine" already present on machine
Normal Created 67s kubelet, k8s-node01 Created container replicasetdemo
Normal Started 67s kubelet, k8s-node01 Started container replicasetdemo
# 5、测试controller控制器下的pod删除、重新被controller控制器拉起
[root@k8s-master01 controller]# kubectl delete pod --all
pod "replicasetdemo-2gllp" deleted
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicasetdemo-6kbdn 1/1 Running 0 110s
[root@k8s-master01 controller]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
replicasetdemo-6kbdn 1/1 Running 0 2m2s 10.81.58.214 k8s-node02 <none> <none>
[root@k8s-master01 controller]# curl 10.81.58.214
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
..............
</html>
# 6、修改pod的副本数量:通过命令行方式
[root@k8s-master01 controller]# kubectl scale replicaset replicasetdemo --replicas=3
replicaset.apps/replicasetdemo scaled
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicasetdemo-6kbdn 1/1 Running 0 14m
replicasetdemo-g5664 1/1 Running 0 22s
replicasetdemo-t8ksq 1/1 Running 0 22s
# 7、修改pod的副本数量:通过资源清单方式
[root@k8s-master01 controller]# kubectl edit replicasets.apps replicasetdemo
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: ReplicaSet
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"labels":{"app":"replicasetdemo"},"name":"replicasetdemo","namespace":"default"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"replicasetdemo"}},"template":{"metadata":{"labels":{"app":"replicasetdemo"},"name":"replicasetdemo"},"spec":{"containers":[{"image":"nginx:1.17.10-alpine","imagePullPolicy":"IfNotPresent","name":"replicasetdemo","ports":[{"containerPort":80}]}],"restartPolicy":"Always"}}}}
creationTimestamp: "2021-07-08T14:43:10Z"
generation: 5
labels:
app: replicasetdemo
name: replicasetdemo
namespace: default
resourceVersion: "294582"
selfLink: /apis/apps/v1/namespaces/default/replicasets/replicasetdemo
"/tmp/kubectl-edit-6ttin.yaml" 52L, 1886C
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
replicasetdemo-47672 1/1 Running 0 11s
replicasetdemo-6kbdn 1/1 Running 0 19m
replicasetdemo-g5664 1/1 Running 0 5m39s
replicasetdemo-smddx 1/1 Running 0 11s
replicasetdemo-t8ksq 1/1 Running 0 5m39s
[root@k8s-master01 controller]#
3、显示Pod标签
# 1、显示pod的标签
[root@k8s-master01 controller]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicasetdemo-47672 1/1 Running 0 13m app=replicasetdemo
replicasetdemo-6kbdn 1/1 Running 0 32m app=replicasetdemo
replicasetdemo-g5664 1/1 Running 0 18m app=replicasetdemo
replicasetdemo-smddx 1/1 Running 0 13m app=replicasetdemo
replicasetdemo-t8ksq 1/1 Running 0 18m app=replicasetdemo
# 2、修改pod标签(label)
[root@k8s-master01 controller]# kubectl label pod replicasetdemo-g5664 app=guardwhy --overwrite=True
pod/replicasetdemo-g5664 labeled
# 3、再次显示pod的标签
[root@k8s-master01 controller]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicasetdemo-47672 1/1 Running 0 15m app=replicasetdemo
replicasetdemo-6kbdn 1/1 Running 0 34m app=replicasetdemo
replicasetdemo-g5664 1/1 Running 0 21m app=guardwhy
replicasetdemo-hsbx5 1/1 Running 0 4s app=replicasetdemo
replicasetdemo-smddx 1/1 Running 0 15m app=replicasetdemo
replicasetdemo-t8ksq 1/1 Running 0 21m app=replicasetdemo
# 4、查看rs控制器
[root@k8s-master01 controller]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicasetdemo 5 5 5 40m
# 4、查看控制器
[root@k8s-master01 controller]# kubectl delete rs replicasetdemo
replicaset.apps "replicasetdemo" deleted
[root@k8s-master01 controller]# kubectl get rs
No resources found in default namespace.
[root@k8s-master01 controller]#
3.4 ReplicaSet小结
1、kubectl命令行工具适用于RC的绝大部分命令同样适用于ReplicaSet,当前很少单独适用ReplicaSet
,它主要被Deployment
这个更高层的资源对象所使用,从而形成一整套Pod创建,删除,更新的编排机制,在使用Deployment
时无需关心它是如何维护和创建ReplicaSet
的,这一切都是自动发生的。
2、总结一下RC(ReplicaSet)
特性和作用
- 在绝大多数情况下,通过定义一个RC实现Pod的创建及副本数量的自动控制。
- 在RC里包括完整的Pod定义模板,RC通过Label Selector机制实现对Pod副本的自动控制。
- 通过改变RC里的Pod副本数量,可以实现Pod的扩容和缩容。
- 通过改变RC里Pod模板中的镜像版本,可以实现滚动升级。
4- Deployment
4.1 基本概念
1、Deployment是kubernetes在1.2版本中引入的新概念,用于更好的解决Pod的编排问题,keployment在内部使用了ReplicaSet来实现目的,可以把Deployment理解为ReplicaSet的一次升级。
2、Deployment的使用过程
- 创建一个
Deployment
对象来生成对应的ReplicaSet
并完成Pod副本的创建。 - 检查
Deployment
的状态来看部署动作是否完成(Pod副本数量是否达到了预期的值)。 - 更新
Deployment
以创建新的Pod(比如镜像升级),如果当前Deployment
不稳定,可以回滚到一个早先的Deployment
版本。 - 暂停
Deployment
以便于一次性修改多个PodTemplateSpec
的配置项,之后在恢复Deployment
,进行新的发布。 - 扩展
Deployment
以应对高负载,查看Deployment
的状态,以此作为发布是否成功的标志,清理不在需要的旧版本ReplicaSet
。
4.2 部署Deployment
1、可以通过kubectl
命令行方式获取更加详细的信息
kubectl explain deploy
kubectl explain deploy.spec
2、在k8sdemo01
工程创建deploymentdemo.yml
文件
Deployment除了API生命与Kind类型有区别,Deployment的定义与Replica Set的定义很类似。
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploymentdemo
labels:
app: deploymentdemo
spec:
replicas: 3
template:
metadata:
name: deploymentdemo
labels:
app: deploymentdemo
spec:
containers:
- name: deploymentdemo
image: nginx:1.17.10-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
restartPolicy: Always
selector:
matchLabels:
app: deploymentdemo
3、通过idea的Remote Host快速将yml文件上传k8s集群进行测试
[root@k8s-master01 controller]# ls
deploymentdemo.yml replicasetdemo.yml
# 1、运行Deployment
[root@k8s-master01 controller]# kubectl apply -f deploymentdemo.yml
deployment.apps/deploymentdemo created
# 2、查看deployment
[root@k8s-master01 controller]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
deploymentdemo 3/3 3 3 22s
# 3、查看rs控制器
[root@k8s-master01 controller]# kubectl get rs
NAME DESIRED CURRENT READY AGE
deploymentdemo-cff9d5c4d 3 3 3 35s
# 4、查看pod
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-cff9d5c4d-csq4d 1/1 Running 0 42s
deploymentdemo-cff9d5c4d-kdl6w 1/1 Running 0 42s
deploymentdemo-cff9d5c4d-zjhn9 1/1 Running 0 42s
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-cff9d5c4d-csq4d sh
/ # nginx -v
nginx version: nginx/1.17.10
/ # exit
[root@k8s-master01 controller]#
4.2 镜像更新升级
1、命令行方式
# 1、升级nginx镜像版本为1.18.0
[root@k8s-master01 controller]# kubectl set image deployment deploymentdemo deploymentdemo=nginx:1.18.0-alpine
deployment.apps/deploymentdemo image updated
# 2、查看pod
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-795cbd9bbd-d4vml 1/1 Running 0 2m2s
deploymentdemo-795cbd9bbd-nzs8w 1/1 Running 0 62s
deploymentdemo-795cbd9bbd-qlcvc 1/1 Running 0 94s
# 3、进去某一个pod内部,查看nginx升级版本信息
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-795cbd9bbd-d4vml sh
/ # nginx -v
nginx version: nginx/1.18.0
/ # exit
[root@k8s-master01 controller]#
2、yml文件方式
[root@k8s-master01 controller]# ls
deploymentdemo.yml replicasetdemo.yml
# 1、升级nginx镜像版本为1.19.2-alpine
[root@k8s-master01 controller]# kubectl edit deployments.apps deploymentdemo
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "2"
kubectl.kubernetes.io/last-applied-configuration: |
creationTimestamp: "2021-07-09T02:04:24Z"
..........
spec:
containers:
- image: nginx:1.19.2-alpine
imagePullPolicy: IfNotPresent
deployment.apps/deploymentdemo edited
# 2、查看pod升级情况
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 0 87s
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 0 43s
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 0 2s
deploymentdemo-795cbd9bbd-d4vml 0/1 Terminating 1 3h55m
deploymentdemo-795cbd9bbd-qlcvc 0/1 Terminating 1 3h54m
# 3、进去某一个pod内部,查看nginx升级版本信息
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-56fb57bf59-7lxp9 sh
/ # nginx -v
nginx version: nginx/1.19.2
/ # exit
[root@k8s-master01 controller]#
4.3 Deployment扩容
1、命令行方式
[root@k8s-master01 ~]# cd /home/data/controller/
[root@k8s-master01 controller]# ls
deploymentdemo.yml replicasetdemo.yml
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 1 137m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 1 137m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 1 136m
[root@k8s-master01 controller]# kubectl scale deployment deploymentdemo --replicas=6
deployment.apps/deploymentdemo scaled
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-576x6 1/1 Running 0 3s
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 1 138m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 1 137m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 1 136m
deploymentdemo-56fb57bf59-ptbxq 1/1 Running 0 3s
deploymentdemo-56fb57bf59-xb2zh 1/1 Running 0 3s
[root@k8s-master01 controller]#
2、yml文件方式
[root@k8s-master01 controller]# kubectl edit deployments.apps deploymentdemo
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"deploymentdemo"},"name":"deploymentdemo","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"deploymentdemo"}},"template":{"metadata":{"labels":{"app":"deploymentdemo"},"name":"deploymentdemo"},"spec":{"containers":[{"image":"nginx:1.17.10-alpine","imagePullPolicy":"IfNotPresent","name":"deploymentdemo","ports":[{"containerPort":80}]}],"restartPolicy":"Always"}}}}
creationTimestamp: "2021-07-09T02:04:24Z"
generation: 4
labels:
app: deploymentdemo
name: deploymentdemo
namespace: default
resourceVersion: "336158"
selfLink: /apis/apps/v1/namespaces/default/deployments/deploymentdemo
uid: 9d7ed144-738c-4e6f-a0e0-d1727a1e7c37
spec:
progressDeadlineSeconds: 600
replicas: 4
revisionHistoryLimit: 10
selector:
deployment.apps/deploymentdemo edited
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-576x6 1/1 Running 0 4m13s
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 1 142m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 1 141m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 1 141m
[root@k8s-master01 controller]#
4.4 滚动更新
1、基本概述
微服务部署种类:蓝绿部署、滚动部署、灰度发布、金丝雀发布。
- 蓝绿部署: 是不停老版本,部署新版本然后进行测试,确认以后,将流量切到新版本,然后老版本同时也升级到新版本。 蓝绿部署无需停机,并且风险较小。
- 滚动发布:一般是取出一个或者多个服务器停止服务,执行更新,并重新将其投入使用。周而复始,直到集群中所有的实例都更新成新版本。 这种部署方式相对于蓝绿部署,更加节约资源,它不需要运行两个集群、两倍的实例数。我们可以部分部署,例如每次只取出集群的20%进行升级。
- 灰度发布: 是指在黑与白之间,能够平滑过渡的一种发布方式。AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度,平常所说的金丝雀部署也就是灰度发布的一种方式。
2、金丝雀发布
Deployment控制器支持自定义控制更新过程中的滚动节奏,如暂停(pause)
或继续(resume)
更新操作。比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。再筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行,确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作这就是所谓的金丝雀发布(Canary Release
)
# 1、查看deployment
[root@k8s-master01 ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
deploymentdemo 4/4 4 4 10h
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-576x6 1/1 Running 1 3h18m
deploymentdemo-56fb57bf59-7lxp9 1/1 Running 2 5h36m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 2 5h35m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 2 5h35m
[root@k8s-master01 ~]# kubectl exec -it deploymentdemo-56fb57bf59-576x6 sh
/ # nginx -v
nginx version: nginx/1.19.2
/ # exit
[root@k8s-master01 controller]# ls
deploymentdemo.yml replicasetdemo.yml
# 2、更新deployment的nginx:1.18.0-alpine版本,并配置暂停deployment
[root@k8s-master01 controller]# kubectl set image deployment deploymentdemo deploymentdemo=nginx:1.18.0-alpine && kubectl rollout pause deployment deploymentdemo
deployment.apps/deploymentdemo image updated
deployment.apps/deploymentdemo paused
[root@k8s-master01 controller]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-576x6 1/1 Running 1 3h31m
deploymentdemo-56fb57bf59-9lcz7 1/1 Running 2 5h49m
deploymentdemo-56fb57bf59-cjzd2 1/1 Running 2 5h48m
deploymentdemo-795cbd9bbd-rxn9c 1/1 Running 0 40s
deploymentdemo-795cbd9bbd-xdlh5 1/1 Running 0 40s
^C
# 3、观察更新状态
[root@k8s-master01 controller]# kubectl rollout resume deployment deploymentdemo
deployment.apps/deploymentdemo resumed
# 4、监控更新的过程,可以看到已经新增了一个资源,但是并未删除一个旧的资源,就是因为使用了pause暂停命令
[root@k8s-master01 controller]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
deploymentdemo-795cbd9bbd-fs75g 1/1 Running 0 21s
deploymentdemo-795cbd9bbd-rxn9c 1/1 Running 0 6m46s
deploymentdemo-795cbd9bbd-vwvn4 1/1 Running 0 21s
deploymentdemo-795cbd9bbd-xdlh5 1/1 Running 0 6m46s
^C[root@k8s-master01 controller]#
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-795cbd9bbd-fs75g 1/1 Running 0 43s
deploymentdemo-795cbd9bbd-rxn9c 1/1 Running 0 7m8s
deploymentdemo-795cbd9bbd-vwvn4 1/1 Running 0 43s
deploymentdemo-795cbd9bbd-xdlh5 1/1 Running 0 7m8s
# 5、进去某一个pod内部,查看nginx更新版本信息
[root@k8s-master01 controller]# kubectl exec -it deploymentdemo-795cbd9bbd-fs75g sh
/ # nginx -v
nginx version: nginx/1.18.0
/ # exit
[root@k8s-master01 controller]#
4.5 Deployment版本回退
1、默认情况下,kubernetes 会在系统中保存前两次的 Deployment 的 rollout 历史记录,以便可以随时回退(可以修改 revision history limit
来更改保存的revision数)。
2、只要 Deployment 的 rollout
被触发就会创建一个revision
,当且仅当Deployment 的Pod template
被更改,例如更新template 中的 label 和容器镜像时,就会创建出一个新的 revision。
3、其他的更新,比如扩容 Deployment 不会创建 revision
。因此可以很方便的手动或者自动扩容。这意味着当回退到历史 revision 时,只有 Deployment 中的 Pod template
部分才会回退。
4、rollout常见命令
常见命令 | 功能具体说明 |
---|---|
history | 查看rollout操作历史。 |
pause | 将提供的资源设定为暂停状态。 |
restart | 重启某资源。 |
resume | 将某资源从暂停状态恢复正常。 |
status | 查看rollout操作状态。 |
undo | 回滚前一rollout。 |
# 1、history历史操作
[root@k8s-master01 controller]# kubectl rollout history deployment deploymentdemo
deployment.apps/deploymentdemo
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
# 2、回滚版本信息
[root@k8s-master01 controller]# kubectl rollout undo deployment deploymentdemo
deployment.apps/deploymentdemo rolled back
# 3、查看pod回滚情况
[root@k8s-master01 controller]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-729m2 1/1 Running 0 11s
deploymentdemo-56fb57bf59-fldk5 1/1 Running 0 12s
deploymentdemo-56fb57bf59-msl9r 1/1 Running 0 12s
deploymentdemo-56fb57bf59-wmrlw 1/1 Running 0 10s
deploymentdemo-795cbd9bbd-rxn9c 0/1 Terminating 0 43m
deploymentdemo-795cbd9bbd-rxn9c 0/1 Terminating 0 43m
deploymentdemo-795cbd9bbd-rxn9c 0/1 Terminating 0 43m
^C[root@k8s-master01 controller]#
[root@k8s-master01 controller]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploymentdemo-56fb57bf59-729m2 1/1 Running 0 30s
deploymentdemo-56fb57bf59-fldk5 1/1 Running 0 31s
deploymentdemo-56fb57bf59-msl9r 1/1 Running 以上是关于K8S-资源管理器的主要内容,如果未能解决你的问题,请参考以下文章