
一、CronJob配置参数详解
下面以一个示例来说明下CronJob配置参数:·
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
run: hello
name: hello
namespace: default
spec:
schedule: '*/1 * * * *'
successfulJobsHistoryLimit: 3
suspend: false
concurrencyPolicy: Allow
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
spec:
template:
metadata:
labels:
run: hello
spec:
containers:
- args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
image: registry.cn-hangzhou.aliyuncs.com/zq-demo/nginx:1.14.2
imagePullPolicy: Always
name: hello
resources: {}
restartPolicy: OnFailure
securityContext: {}
其中配置参数说明如下:
-
apiVersion:1.21版本之前可以使用batch/v1beta1 ,1.25版本之后不能使用batch/v1beta1,1.21版本之后可以使用batch/v1
-
schedule:调度周期,和Linux一致,分别是分时日月周。
-
restartPolicy: OnFailure,容器以不为 0 的状态码终止,自动重启该容器。
-
concurrencyPolicy:并发调度策略。可选参数如下:
(1)Allow:允许同时运行多个任务。
(2)Forbid:不允许并发运行,如果之前的任务尚未完成,新的任务不会被创建。
(3)Replace:如果之前的任务尚未完成,新的任务会替换的之前的任务。
-
suspend:如果设置为true,则暂停后续的任务,默认为false。
-
successfulJobsHistoryLimit:保留多少已完成的任务,按需配置。
-
failedJobsHistoryLimit:保留多少失败的任务
关于上述配置文件分为三层,从上到下分别是CronJob、Job、Pod
CronJob这一层的配置如下:
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
run: hello
name: hello
namespace: default
Job这一层的配置如下:
spec:
schedule: '*/1 * * * *'
successfulJobsHistoryLimit: 3
suspend: false
concurrencyPolicy: Allow
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
spec:
template:
metadata:
labels:
run: hello
Pod这一层的配置如下:
spec:
containers:
- args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
image: registry.cn-hangzhou.aliyuncs.com/zq-demo/nginx:1.14.2
imagePullPolicy: Always
name: hello
resources: {}
restartPolicy: OnFailure
securityContext: {}
1.1 CronJob配置示例
1.定义一个名为 cronjob.yaml的yaml文件
[root@k8s-master01 JOB]# vim cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
run: hello
name: hello
namespace: default
spec:
schedule: '*/1 * * * *'
successfulJobsHistoryLimit: 3
suspend: false
concurrencyPolicy: Allow
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
spec:
template:
metadata:
labels:
run: hello
spec:
containers:
- args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
image: registry.cn-hangzhou.aliyuncs.com/zq-demo/nginx:1.14.2
imagePullPolicy: Always
name: hello
resources: {}
restartPolicy: OnFailure
securityContext: {}
上面参数说明如下:
-
concurrencyPolicy:并发调度策略,这里选择Allow,允许同时运行多个任务。
-
Allow:允许同时运行多个任务
-
Forbid:不允许并发运行,如果之前的任务尚未完成,新的任务不会被创建
-
Replace:如果之前的任务尚未完成,新的任务会替换的之前的任务
-
suspend:如果设置为true,则暂停后续的任务,默认为false。
-
schedule:调度周期,和Linux一致,分别是分时日月周。
-
restartPolicy: OnFailure,容器以不为 0 的状态码终止,自动重启该容器。
-
failedJobsHistoryLimit:可选字段,指定应保留多少已失败的任务。 默认设置为 1。将限制设置为
0代表相应类型的任务完成后不会保留。 -
successfulJobsHistoryLimit:可选字段,指定应保留多少已完成的任务。 默认设置为 3。将限制设置为
0代表相应类型的任务完成后不会保留。
2.部署
[root@k8s-master01 JOB]# kubectl create -f cronjob.yaml
3.查看CronJob、job、pod
[root@k8s-master01 JOB]# kubectl get cj
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 <none> 13s
[root@k8s-master01 JOB]# kubectl get job
NAME COMPLETIONS DURATION AGE
hello-27840055 1/1 5s 26s
[root@k8s-master01 JOB]# kubectl get po
NAME READY STATUS RESTARTS AGE
hello-27840055-zpx59 0/1 Completed 0 41s
4.查看pod日志
[root@k8s-master01 JOB]# kubectl logs -f hello-27840055-zpx59
Wed Dec 7 08:55:02 UTC 2022
Hello from the Kubernetes cluster
5.等待一分钟后,再次查看CronJob、job、pod
[root@k8s-master01 JOB]# kubectl get cj
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 22s 2m2s
[root@k8s-master01 JOB]# kubectl get job
NAME COMPLETIONS DURATION AGE
hello-27840055 1/1 5s 87s
hello-27840056 1/1 6s 27s
[root@k8s-master01 JOB]# kubectl get po
NAME READY STATUS RESTARTS AGE
hello-27840055-zpx59 0/1 Completed 0 99s
hello-27840056-j7gwk 0/1 Completed 0 39s
6.继续打开cronjob.yaml的yaml文件,将suspend参数设置为true,关闭CronJob
[root@k8s-master01 JOB]# vim cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
run: hello
name: hello
namespace: default
spec:
concurrencyPolicy: Allow
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
spec:
template:
metadata:
labels:
run: hello
spec:
containers:
- args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
image: registry.cn-hangzhou.aliyuncs.com/zq-demo/nginx:1.14.2
imagePullPolicy: Always
name: hello
resources: {}
restartPolicy: OnFailure
securityContext: {}
schedule: '*/1 * * * *'
successfulJobsHistoryLimit: 3
suspend: true
7.更新yaml
[root@k8s-master01 JOB]# kubectl apply -f cronjob.yaml
8.等待1分钟后,再次查看CronJob、job、pod。观察到CronJob已关闭
[root@k8s-master01 JOB]# kubectl get cj
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * True 0 62s 3m42s
[root@k8s-master01 JOB]# kubectl get job
NAME COMPLETIONS DURATION AGE
hello-27840055 1/1 5s 3m5s
hello-27840056 1/1 6s 2m5s






暂无评论内容