目录
一、什么是控制器
二、控制器常用类型
三、replicaset控制器
1、replicaset功能
2、replicaset参数说明
3、replicaset示例
四、deployment控制器
1、deployment控制器的功能
2、deployment控制器示例
(1)版本迭代
(2)版本回滚
(3)滚动更新策略
(4)暂停及恢复
五、daemonset控制器
1、daemonset功能
2、daemonset示例
六、job控制器
1、job控制器功能
2、job控制器示例
七、cronjob 控制器
1、cronjob控制器功能
2、cronjob控制器示例
一、什么是控制器
官方文档:
工作负载管理 | Kubernetes
控制器也是管理pod的一种手段
-
自主式pod:pod退出或意外关闭后不会被重新创建
-
控制器管理的 Pod:在控制器的生命周期里,始终要维持 Pod 的副本数目
Pod控制器是管理pod的中间层,使用Pod控制器之后,只需要告诉Pod控制器,想要多少个什么样的Pod就可以了,它会创建出满足条件的Pod并确保每一个Pod资源处于用户期望的目标状态。如果Pod资源在运行中出现故障,它会基于指定策略重新编排Pod
当建立控制器后,会把期望值写入etcd,k8s中的apiserver检索etcd中我们保存的期望状态,并对比pod的当前状态,如果出现差异代码自驱动立即恢复。
二、控制器常用类型
控制器名称 | 控制器用途 |
---|---|
Replication Controller | 比较原始的pod控制器,已经被废弃,由ReplicaSet替代 |
ReplicaSet | ReplicaSet 确保任何时间都有指定数量的 Pod 副本在运行 |
Deployment | 一个 Deployment 为 Pod 和 ReplicaSet 提供声明式的更新能力 |
DaemonSet | DaemonSet 确保全指定节点上运行一个 Pod 的副本 |
StatefulSet | StatefulSet 是用来管理有状态应用的工作负载 API 对象。 |
Job | 执行批处理任务,仅执行一次任务,保证任务的一个或多个Pod成功结束 |
CronJob | Cron Job 创建基于时间调度的 Jobs。 |
HPA全称Horizontal Pod Autoscaler | 根据资源利用率自动调整service中Pod数量,实现Pod水平自动缩放 |
三、replicaset控制器
1、replicaset功能
- ReplicaSet 是下一代的 Replication Controller,官方推荐使用ReplicaSet
- ReplicaSet和Replication Controller的唯一区别是选择器的支持,ReplicaSet支持新的基于集合的选择器需求
- ReplicaSet 确保任何时间都有指定数量的 Pod 副本在运行
- 虽然 ReplicaSets 可以独立使用,但今天它主要被Deployments 用作协调 Pod 创建、删除和更新的机制
2、replicaset参数说明
参数名称 | 字段类型 | 参数说明 |
---|---|---|
spec | Object | 详细定义对象,固定值就写Spec |
spec.replicas | integer | 指定维护pod数量 |
spec.selector | Object | Selector是对pod的标签查询,与pod数量匹配 |
spec.selector.matchLabels | string | 指定Selector查询标签的名称和值,以key:value方式指定 |
spec.template | Object | 指定对pod的描述信息,比如lab标签,运行容器的信息等 |
spec.template.metadata | Object | 指定pod属性 |
spec.template.metadata.labels | string | 指定pod标签 |
spec.template.spec | Object | 详细定义对象 |
spec.template.spec.containers | list | Spec对象的容器列表定义 |
spec.template.spec.containers.name | string | 指定容器名称 |
spec.template.spec.containers.image | string | 指定容器镜像 |
3、replicaset示例
[root@k8s-master ~]# kubectl create deployment replicaset --image reg.zx.org/library/myapp:v1 --dry-run=client -o yaml > replicaset.yml
[root@k8s-master ~]# vim replicaset.yml
[root@k8s-master ~]# cat replicaset.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
creationTimestamp: null
labels:
app: replicaset
name: replicaset #指定pod名称,一定小写,如果出现大写报错
spec:
replicas: 2 #指定维护pod数量为2
selector: #指定检测匹配方式
matchLabels: #指定匹配方式为匹配标签
app: myapp #指定匹配的标签为app=myapp
template: #模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
creationTimestamp: null
labels:
app: myapp
spec:
containers:
- image: reg.zx.org/library/myapp:v1
name: myapp
resources: {}
status: {}
[root@k8s-master ~]# kubectl apply -f replicaset.yml
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-kxqm2 1/1 Running 0 46s app=myapp
replicaset-scvxh 1/1 Running 0 46s app=myapp
#replicaset是通过标签匹配pod
[root@k8s-master ~]# kubectl label pod replicaset-kxqm2 app=new_myapp --overwrite
pod/replicaset-kxqm2 labeled
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-kxqm2 1/1 Running 0 7m48s app=new_myapp #新开启的pod
replicaset-nx9fx 1/1 Running 0 11s app=myapp
replicaset-scvxh 1/1 Running 0 7m48s app=myapp
[root@k8s-master ~]# kubectl label pod replicaset-kxqm2 app-
pod/replicaset-kxqm2 unlabeled
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-kxqm2 1/1 Running 0 8m42s <none>
replicaset-nx9fx 1/1 Running 0 65s app=myapp
replicaset-scvxh 1/1 Running 0 8m42s app=myapp
[root@k8s-master ~]# kubectl delete pods replicaset-kxqm2
#replicaset自动控制副本数量,pod可以自愈
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-nx9fx 1/1 Running 0 7m47s app=myapp
replicaset-scvxh 1/1 Running 0 15m app=myapp
[root@k8s-master ~]# kubectl delete pods replicaset-nx9fx
pod "replicaset-nx9fx" deleted
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-s9tf8 1/1 Running 0 3s app=myapp
replicaset-scvxh 1/1 Running 0 17m app=myapp
# 回收资源
[root@k8s-master ~]# kubectl delete -f replicaset.yml
replicaset.apps "replicaset" deleted
四、deployment控制器
1、deployment控制器的功能
- 为了更好的解决服务编排的问题,kubernetes在V1.2版本开始,引入了Deployment控制器。
- Deployment控制器并不直接管理pod,而是通过管理ReplicaSet来间接管理Pod
- Deployment管理ReplicaSet,ReplicaSet管理Pod
- Deployment 为 Pod 和 ReplicaSet 提供了一个申明式的定义方法
- 在Deployment中ReplicaSet相当于一个版本
典型的应用场景:
用来创建Pod和ReplicaSet
滚动更新和回滚
扩容和缩容
暂停与恢复
2、deployment控制器示例
[root@k8s-master ~]# kubectl create deployment deployment --image reg.zx.org/library/myapp:v1 --dry-run=client -o yaml > deployment.yml
[root@k8s-master ~]# vim deployment.yml
[root@k8s-master ~]# kubectl apply -f deployment.yml
deployment.apps/deployment created
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
deployment-6746c65b55-d2cdj 1/1 Running 0 9s app=myapp,pod-template-hash=6746c65b55
deployment-6746c65b55-jfcb8 1/1 Running 0 9s app=myapp,pod-template-hash=6746c65b55
deployment-6746c65b55-l9bws 1/1 Running 0 9s app=myapp,pod-template-hash=6746c65b55
deployment-6746c65b55-q2p68 1/1 Running 0 9s app=myapp,pod-template-hash=6746c65b55
[root@k8s-master ~]# kubectl get deployments.apps -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 4/4 4 4 2m17s myapp reg.zx.org/library/myapp:v1 app=myapp
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-6746c65b55-d2cdj 1/1 Running 0 2m43s
pod/deployment-6746c65b55-jfcb8 1/1 Running 0 2m43s
pod/deployment-6746c65b55-l9bws 1/1 Running 0 2m43s
pod/deployment-6746c65b55-q2p68 1/1 Running 0 2m43s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25h
service/readiness ClusterIP 10.101.55.183 <none> 80/TCP 91m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment 4/4 4 4 2m43s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-6746c65b55 4 4 4 2m43s
[root@k8s-master ~]# kubectl rollout history deployment deployment
deployment.apps/deployment
REVISION CHANGE-CAUSE
1 <none>
[root@k8s-master ~]# kubectl describe deployments.apps deployment
Name: deployment
Namespace: default
CreationTimestamp: Sat, 14 Sep 2024 21:44:53 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=myapp
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
(1)版本迭代
更新的过程是重新建立一个版本的RS,新版本的RS会把pod 重建,然后把老版本的RS回收
[root@k8s-master ~]# vim deployment.yml
[root@k8s-master ~]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
replicas: 4
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: reg.zx.org/library/myapp:v2 # 修改版本为2
name: myapp
[root@k8s-master ~]# kubectl apply -f deployment.yml
deployment.apps/deployment configured
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-6746c65b55-q2p68 0/1 Terminating 0 6m3s
pod/deployment-857cb5548b-6h4rb 1/1 Running 0 3s
pod/deployment-857cb5548b-vnkk7 1/1 Running 0 3s
pod/deployment-857cb5548b-wn5kg 1/1 Running 0 6s
pod/deployment-857cb5548b-zr28k 1/1 Running 0 5s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25h
service/readiness ClusterIP 10.101.55.183 <none> 80/TCP 94m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment 4/4 4 4 6m3s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-6746c65b55 0 0 0 6m3s # v1回收
replicaset.apps/deployment-857cb5548b 4 4 4 6s # v2打开
[root@k8s-master ~]# kubectl rollout history deployment deployment
deployment.apps/deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
[root@k8s-master ~]# kubectl get deployments.apps -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 4/4 4 4 13m myapp reg.zx.org/library/myapp:v2 app=myapp
# 暴漏端口
[root@k8s-master ~]# kubectl expose deployment deployment --port 80 --target-port 80 --dry-run=client -o yaml >> deployment.yml
[root@k8s-master ~]# vim deployment.yml # 中间添加“---”
[root@k8s-master ~]# kubectl apply -f deployment.yml
deployment.apps/deployment unchanged
service/deployment created
[root@k8s-master ~]# kubectl get svc deployment
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
deployment ClusterIP 10.100.147.145 <none> 80/TCP 22s
(2)版本回滚
[root@k8s-master ~]# vim deployment.yml
[root@k8s-master ~]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
replicas: 4
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: reg.zx.org/library/myapp:v1
name: myapp
[root@k8s-master ~]# kubectl apply -f deployment.yml
deployment.apps/deployment configured
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
deployment-6746c65b55-dxc5j 1/1 Running 0 9s 10.244.2.52 k8s-node2.zx.org <none> <none>
deployment-6746c65b55-kcd98 1/1 Running 0 9s 10.244.1.53 k8s-node1.zx.org <none> <none>
deployment-6746c65b55-td9xs 1/1 Running 0 12s 10.244.1.52 k8s-node1.zx.org <none> <none>
deployment-6746c65b55-w7wwf 1/1 Running 0 12s 10.244.2.51 k8s-node2.zx.org <none> <none>
[root@k8s-master ~]# kubectl get deployments.apps -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment 4/4 4 4 16m myapp reg.zx.org/library/myapp:v1 app=myapp
[root@k8s-master ~]# kubectl rollout history deployment deployment
deployment.apps/deployment
REVISION CHANGE-CAUSE
2 <none>
3 <none>
(3)滚动更新策略
[root@k8s-master ~]# vim deployment.yml
[root@k8s-master ~]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
minReadySeconds: 5 #最小就绪时间,指定pod每隔多久更新一次
replicas: 4
strategy: #指定更新策略
rollingUpdate:
maxSurge: 1 #比定义pod数量多几个
maxUnavailable: 0 #比定义pod个数少几个
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: reg.zx.org/library/myapp:v1
name: myapp
(4)暂停及恢复
在实际生产环境中我们做的变更可能不止一处,当修改了一处后,如果执行变更就直接触发了
我们期望的触发时当我们把所有修改都搞定后一次触发暂停,避免触发不必要的线上更新
[root@k8s-master ~]# kubectl rollout pause deployment deployment # 暂停
deployment.apps/deployment paused
[root@k8s-master ~]# vim deployment.yml
[root@k8s-master ~]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
minReadySeconds: 5
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
replicas: 6
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: reg.zx.org/library/myapp:v1
name: myapp
resources:
limits:
cpu: 0.5
memory: 200Mi
requests:
cpu: 0.5
memory: 200Mi
[root@k8s-master ~]# kubectl apply -f deployment.yml # 更新镜像,修改资源
deployment.apps/deployment configured
[root@k8s-master ~]# kubectl describe pods deployment-6746c65b55-
deployment-6746c65b55-2bhx4 deployment-6746c65b55-kcd98 deployment-6746c65b55-td9xs
deployment-6746c65b55-dxc5j deployment-6746c65b55-sr6m4 deployment-6746c65b55-w7wwf
[root@k8s-master ~]# kubectl describe pods deployment-6746c65b55-2bhx4 # 查看详细信息,但是更新镜像和修改资源并没有触发更新
[root@k8s-master ~]# kubectl rollout history deployment deployment # 查看历史版本,无新版本信息
deployment.apps/deployment
REVISION CHANGE-CAUSE
2 <none>
3 <none>
[root@k8s-master ~]# kubectl rollout resume deployment deployment # #恢复后开始触发更新
deployment.apps/deployment resumed
[root@k8s-master ~]# kubectl rollout history deployment deployment # 更新版本
deployment.apps/deployment
REVISION CHANGE-CAUSE
2 <none>
3 <none>
4 <none>
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-5c66f7bff5-6p5td 1/1 Running 0 67s
pod/deployment-5c66f7bff5-bwxxj 1/1 Running 0 81s
pod/deployment-5c66f7bff5-dzd5z 1/1 Running 0 89s
pod/deployment-5c66f7bff5-m9llf 1/1 Running 0 96s
pod/deployment-5c66f7bff5-wmmzt 1/1 Running 0 60s
pod/deployment-5c66f7bff5-zh7zv 1/1 Running 0 74s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/deployment ClusterIP 10.100.147.145 <none> 80/TCP 11h
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 37h
service/readiness ClusterIP 10.101.55.183 <none> 80/TCP 13h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment 6/6 6 6 11h
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-5c66f7bff5 6 6 6 96s # 更新
replicaset.apps/deployment-6746c65b55 0 0 0 11h
replicaset.apps/deployment-857cb5548b 0 0 0 11h
# 回收
[root@k8s-master ~]# kubectl delete -f deployment.yml
deployment.apps "deployment" deleted
五、daemonset控制器
1、daemonset功能
DaemonSet 确保全部(或者某些)节点上运行一个 Pod 的副本。当有节点加入集群时, 也会为他们新增一个 Pod ,当有节点从集群移除时,这些 Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod
DaemonSet 的典型用法:
-
在每个节点上运行集群存储 DaemonSet,例如 glusterd、ceph。
-
在每个节点上运行日志收集 DaemonSet,例如 fluentd、logstash。
-
在每个节点上运行监控 DaemonSet,例如 Prometheus Node Exporter、zabbix agent等
-
一个简单的用法是在所有的节点上都启动一个 DaemonSet,将被作为每种类型的 daemon 使用
-
一个稍微复杂的用法是单独对每种 daemon 类型使用多个 DaemonSet,但具有不同的标志, 并且对不同硬件类型具有不同的内存、CPU 要求
2、daemonset示例
[root@k8s-master ~]# cp deployment.yml dm.yml
[root@k8s-master ~]# vim dm.yml
[root@k8s-master ~]# cat dm.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: dm
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
tolerations: #对于污点节点的容忍
- effect: NoSchedule
operator: Exists
containers:
- image: reg.zx.org/library/myapp:v1
name: myapp
[root@k8s-master ~]# kubectl apply -f dm.yml
daemonset.apps/dm created
[root@k8s-master ~]# kubectl get daemonsets.apps dm
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
dm 3 3 3 3 3 <none> 24s
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dm-kh2gm 1/1 Running 0 68s 10.244.2.57 k8s-node2.zx.org <none> <none>
dm-pt44v 1/1 Running 0 68s 10.244.1.58 k8s-node1.zx.org <none> <none>
dm-pzgfg 1/1 Running 0 68s 10.244.0.6 k8s-master.zx.org <none> <none>
# 回收
[root@k8s-master ~]# kubectl delete -f dm.yml
daemonset.apps "dm" deleted
六、job控制器
1、job控制器功能
Job,主要用于负责批量处理(一次要处理指定数量任务)短暂的一次性(每个任务仅运行一次就结束)任务
Job特点如下:
- 当Job创建的pod执行成功结束时,Job将记录成功结束的pod数量
- 当成功结束的pod达到指定的数量时,Job将完成执行
关于重启策略设置的说明:
如果指定为OnFailure,则job会在pod出现故障时重启容器
而不是创建pod,failed次数不变
如果指定为Never,则job会在pod出现故障时创建新的pod
并且故障pod不会消失,也不会重启,failed次数加1
如果指定为Always的话,就意味着一直重启,意味着job任务会重复去执行了
2、job控制器示例
[root@k8s-master ~]# docker load -i perl-5.34.tar.gz
c9a63110150b: Loading layer 129.1MB/129.1MB
e019be289189: Loading layer 11.3MB/11.3MB
6e632f416458: Loading layer 19.31MB/19.31MB
327e42081bbe: Loading layer 156.5MB/156.5MB
3ffc178e6d86: Loading layer 537.7MB/537.7MB
89ab64e009d0: Loading layer 3.584kB/3.584kB
213cb8592bff: Loading layer 57.87MB/57.87MB
Loaded image: perl:5.34.0
[root@k8s-master ~]# docker tag perl:5.34.0 reg.zx.org/library/perl:5.34.0
[root@k8s-master ~]# docker push reg.zx.org/library/perl:5.34.0
[root@k8s-master ~]# kubectl create job job-test --image reg.zx.org/library/perl:5.34.0 --dry-run=client -o yaml > job-test.yml
[root@k8s-master ~]# vim job-test.yml
[root@k8s-master ~]# kubectl apply -f job-test.yml
job.batch/job-test created
[root@k8s-master ~]# cat job-test.yml
apiVersion: batch/v1
kind: Job
metadata:
name: job-test
spec:
completions: 6 #一共完成任务数为6
parallelism: 2 #每次并行完成2个
backoffLimit: 4 #运行失败后尝试4重新运行
template:
metadata:
creationTimestamp: null
spec:
containers:
- image: reg.zx.org/library/perl:5.34.0
name: job-test
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] #计算Π的后2000位
restartPolicy: Never #关闭后不自动重启
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/job-test-2xn5q 0/1 Completed 0 97s
pod/job-test-fz8td 0/1 Completed 0 97s
pod/job-test-kffnm 1/1 Running 0 4s
pod/job-test-wbgsb 1/1 Running 0 6s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/deployment ClusterIP 10.100.147.145 <none> 80/TCP 12h
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 37h
service/readiness ClusterIP 10.101.55.183 <none> 80/TCP 14h
NAME STATUS COMPLETIONS DURATION AGE
job.batch/job-test Running 2/6 97s 97s
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/job-test-2xn5q 0/1 Completed 0 3m27s
pod/job-test-fz8td 0/1 Completed 0 3m27s
pod/job-test-gcc69 0/1 Completed 0 100s
pod/job-test-kffnm 0/1 Completed 0 114s
pod/job-test-rg765 0/1 Completed 0 99s
pod/job-test-wbgsb 0/1 Completed 0 116s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/deployment ClusterIP 10.100.147.145 <none> 80/TCP 12h
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 37h
service/readiness ClusterIP 10.101.55.183 <none> 80/TCP 14h
NAME STATUS COMPLETIONS DURATION AGE
job.batch/job-test Complete 6/6 2m3s 3m27s
[root@k8s-master ~]# kubectl logs pods/job-test-2xn5q
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
# 回收
[root@k8s-master ~]# kubectl delete -f job-test.yml
job.batch "job-test" deleted
七、cronjob 控制器
1、cronjob控制器功能
-
Cron Job 创建基于时间调度的 Jobs。
-
CronJob控制器以Job控制器资源为其管控对象,并借助它管理pod资源对象,
-
CronJob可以以类似于Linux操作系统的周期性任务作业计划的方式控制其运行时间点及重复运行的方式。
-
CronJob可以在特定的时间点(反复的)去运行job任务。
2、cronjob控制器示例
每分钟开启一个pod,显示日期时间
[root@k8s-master ~]# cp job-test.yml cronjob-test.yml
[root@k8s-master ~]# vim cronjob-test.yml
[root@k8s-master ~]# kubectl apply -f cronjob-test.yml
[root@k8s-master ~]# cat cronjob-test.yml
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob-test
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- image: reg.zx.org/library/busyboxplus:latest
name: cronjob-test
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cronjob-test-28772809-jf9h8 0/1 Completed 0 114s
cronjob-test-28772810-tjpdc 0/1 Completed 0 54s
[root@k8s-master ~]# kubectl get cronjobs.batch cronjob-test
NAME SCHEDULE TIMEZONE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob-test * * * * * <none> False 0 17s 4m56s
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cronjob-test-28772811-nkx9v 0/1 Completed 0 2m49s
cronjob-test-28772812-krwgt 0/1 Completed 0 109s
cronjob-test-28772813-xpb7q 0/1 Completed 0 49s
[root@k8s-master ~]# kubectl logs pods/cronjob-test-28772814-6fvr9
Sun Sep 15 02:54:01 UTC 2024
Hello from the Kubernetes cluster
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cronjob-test-28772813-xpb7q 0/1 Completed 0 2m52s
cronjob-test-28772814-6fvr9 0/1 Completed 0 112s
cronjob-test-28772815-7z6b9 0/1 Completed 0 52s
[root@k8s-master ~]# kubectl logs pods/cronjob-test-28772815-7z6b9
Sun Sep 15 02:55:01 UTC 2024
Hello from the Kubernetes cluster
# 回收
[root@k8s-master ~]# kubectl delete -f cronjob-test.yml
cronjob.batch "cronjob-test" deleted