如果 startingDeadlineSeconds 未设置(默认),并且 concurrencyPolicy 设置为 Allow,该参数默认为true,则作业可以并行运行。
root@k8s-master01:~# cat test-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
namespace: test-cronjob
spec:
schedule: "*/1 * * * *"
concurrencyPolicy: Allow
failedJobsHistoryLimit: 2
successfulJobsHistoryLimit: 2
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- sleep 100
restartPolicy: OnFailure
root@k8s-master01:~# kubectl get pod -n test-cronjob
NAME READY STATUS RESTARTS AGE
hello-27845595-nmtnc 1/1 Running 0 78s
hello-27845596-xz656 1/1 Running 0 18s
注意:
如果 startingDeadlineSeconds 的设置值低于 10 秒钟,CronJob 可能无法被调度。 这是因为 CronJob 控制器每 10 秒钟执行一次检查。
对于每个 CronJob,CronJob 控制器(Controller) 检查从上一次调度的时间点到现在所错过了调度次数。如果错过的调度次数超过 100 次, 那么它就不会启动这个任务,并记录这个错误:
上述所说为官网所说,本人亲测,1.25版本事件日志会抛出如下警告,但是依然会执行下一个任务。
Warning TooManyMissedTimes 4m13s (x6 over 9m13s) cronjob-controller (combined from similar events): too many missed start times: 115. Set or decrease .spec.startingDeadlineSeconds or check clock skew
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
namespace: test-cronjob
spec:
schedule: "*/1 * * * *"
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 2
successfulJobsHistoryLimit: 2
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- sleep 7000
restartPolicy: OnFailure
root@k8s-master01:~# kubectl get pod -n test-cronjob
NAME READY STATUS RESTARTS AGE
hello-27845626-vk6zj 0/1 Completed 0 120m
hello-27845742-tf8hv 1/1 Running 0 3m42s
root@k8s-master01:~# kubectl describe cronjob hello -n test-cronjob
Name: hello
Namespace: test-cronjob
Labels: <none>
Annotations: <none>
Schedule: */1 * * * *
Concurrency Policy: Forbid
Suspend: False
Successful Job History Limit: 2
Failed Job History Limit: 2
Starting Deadline Seconds: <unset>
Selector: <unset>
Parallelism: <unset>
Completions: <unset>
Pod Template:
Labels: <none>
Containers:
hello:
Image: busybox:1.28
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
sleep 7000
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: Sun, 11 Dec 2022 07:42:00 +0000
Active Jobs: hello-27845742
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning TooManyMissedTimes 13m cronjob-controller too many missed start times: 106. Set or decrease .spec.startingDeadlineSeconds or check clock skew
Normal JobAlreadyActive 9m13s (x110 over 118m) cronjob-controller Not starting job because prior execution is running and concurrency policy is Forbid
Warning TooManyMissedTimes 4m13s (x6 over 9m13s) cronjob-controller (combined from similar events): too many missed start times: 115. Set or decrease .spec.startingDeadlineSeconds or check clock skew
如果 startingDeadlineSeconds 字段非空,则控制器会统计从 startingDeadlineSeconds 设置的值到现在而不是从上一个计划时间到现在错过了多少次 Job。 例如,如果 startingDeadlineSeconds 是 300,则控制器会统计在过去 300 秒中错过了多少次 Job。按下述逻辑300s只错过了5次,所以不会抛出警告
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello1
namespace: test-cronjob1
spec:
schedule: "*/1 * * * *"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 300
failedJobsHistoryLimit: 2
successfulJobsHistoryLimit: 2
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- sleep 7000
restartPolicy: OnFailure
root@k8s-master01:~# kubectl get pod -n test-cronjob1
NAME READY STATUS RESTARTS AGE
hello1-27845635-52sk9 0/1 Completed 0 117m
hello1-27845751-6cpjk 1/1 Running 0 37s
设置拉参数startingDeadlineSeconds=300,events将不会抛出上述警告
root@k8s-master01:~# kubectl describe cronjob -n test-cronjob1
Name: hello1
Namespace: test-cronjob1
Labels: <none>
Annotations: <none>
Schedule: */1 * * * *
Concurrency Policy: Forbid
Suspend: False
Successful Job History Limit: 2
Failed Job History Limit: 2
Starting Deadline Seconds: 300s
Selector: <unset>
Parallelism: <unset>
Completions: <unset>
Pod Template:
Labels: <none>
Containers:
hello:
Image: busybox:1.28
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
sleep 7000
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: Sun, 11 Dec 2022 07:51:00 +0000
Active Jobs: hello1-27845751
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal JobAlreadyActive 2m (x116 over 117m) cronjob-controller Not starting job because prior execution is running and concurrency policy is Forbid