一、认识CronJob控制器
CronJob控制器用于管理Job控制器资源的运行时间。Job控制器定义的作业任务在其控制器资源创建之后便会立即执行,但是CronJob可以以类似于Linux操作系统中的周期任务作业计划(crontab)的方式控制其运行的时间点及重复运行的方式,具体如下:
1)在未来某个时间点运行作业一次
2)在指定的时间点重复运行作业
CronJob对象支持使用时间格式类似于CronJob,略微有不同的是,CronJob控制器在指定的时间点时,"?“和”*"的意义相同,都表示任何可用的有效值。
二、创建CronJob对象
CronJob控制器的spec字段可嵌套使用以下字段:
1)jobTemplate:Job控制器模板,用于为CronJob控制器生成Job对象;必选字段;
2)schedule
3)concurrencyPolicy
4)failedJobHistoryLimit
5)successfulJobHistoryLimit
6)startingDeadlineSeconds
7)suspend
]# cat cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: crontab-example
labels:
app: mycronjob
spec:
schedule: "*/2 * * * *"
jobTemplate:
metadata:
labels:
app: mycronjob-jobs
spec:
parallelism: 2
template:
spec:
containers:
- name: myjob
image: alpine
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster; sleep 10
restartPolicy: OnFailure
]# kubectl apply -f cronjob.yaml
cronjob.batch/crontab-example created
2)查看CronJob控制器信息
]# kubectl get cronjob -o wide
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE CONTAINERS IMAGES SELECTOR
crontab-example */2 * * * * False 0 <none> 8s myjob alpine <none>
命令结果的显示:
SCHEDULE:是指其调度的时间点
SUSPEND:表示后续任务是否处于挂起状态,即暂停任务的调度和运行
ACTIVE:表示活动状态的Job对象的数量
LAST SCHEDULE:表示上次调度运行至此刻的时长
]# kubectl get pods -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
crontab-example-1597022880-bj7c8 0/1 Completed 0 71s 10.244.2.30 node2 <none> <none>
crontab-example-1597022880-nvc4r 0/1 Completed 0 71s 10.244.1.73 node1 <none> <none>
crontab-example-1597023000-rjfvw 0/1 Pending 0 0s <none> <none> <none> <none>
crontab-example-1597023000-g7r5q 0/1 Pending 0 0s <none> <none> <none> <none>
crontab-example-1597023000-rjfvw 0/1 Pending 0 0s <none> node2 <none> <none>
crontab-example-1597023000-g7r5q 0/1 Pending 0 0s <none> node1 <none> <none>
crontab-example-1597023000-g7r5q 0/1 ContainerCreating 0 0s <none> node1 <none> <none>
crontab-example-1597023000-rjfvw 0/1 ContainerCreating 0 0s <none> node2 <none> <none>
crontab-example-1597023000-g7r5q 1/1 Running 0 1s 10.244.1.74 node1 <none> <none>
crontab-example-1597023000-rjfvw 1/1 Running 0 1s 10.244.2.31 node2 <none> <none>
crontab-example-1597023000-rjfvw 0/1 Completed 0 11s 10.244.2.31 node2 <none> <none>
crontab-example-1597023000-g7r5q 0/1 Completed 0 12s 10.244.1.74 node1 <none> <none>
crontab-example-1597023120-r87rw 0/1 Pending 0 0s <none> <none> <none> <none>
crontab-example-1597023120-z9gbg 0/1 Pending 0 0s <none> <none> <none> <none>
crontab-example-1597023120-r87rw 0/1 Pending 0 0s <none> node2 <none> <none>
crontab-example-1597023120-z9gbg 0/1 Pending 0 0s <none> node1 <none> <none>
crontab-example-1597023120-r87rw 0/1 ContainerCreating 0 0s <none> node2 <none> <none>
crontab-example-1597023120-z9gbg 0/1 ContainerCreating 0 0s <none> node1 <none> <none>
crontab-example-1597023120-r87rw 1/1 Running 0 1s 10.244.2.32 node2 <none> <none>
crontab-example-1597023120-z9gbg 1/1 Running 0 1s 10.244.1.75 node1 <none> <none>
crontab-example-1597023120-r87rw 0/1 Completed 0 11s 10.244.2.32 node2 <none> <none>
crontab-example-1597023120-z9gbg 0/1 Completed 0 11s 10.244.1.75 node1 <none> <none>
]# kubectl get pods -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
crontab-example-1597022880-bj7c8 0/1 Completed 0 71s 10.244.2.30 node2 <none> <none>
crontab-example-1597022880-nvc4r 0/1 Completed 0 71s 10.244.1.73 node1 <none> <none>
crontab-example-1597023000-rjfvw 0/1 Pending 0 0s <none> <none> <none> <none>
crontab-example-1597023000-g7r5q 0/1 Pending 0 0s <none> <none> <none> <none>
crontab-example-1597023000-rjfvw 0/1 Pending 0 0s <none> node2 <none> <none>
crontab-example-1597023000-g7r5q 0/1 Pending 0 0s <none> node1 <none> <none>
crontab-example-1597023000-g7r5q 0/1 ContainerCreating 0 0s <none> node1 <none> <none>
crontab-example-1597023000-rjfvw 0/1 ContainerCreating 0 0s <none> node2 <none> <none>
crontab-example-1597023000-g7r5q 1/1 Running 0 1s 10.244.1.74 node1 <none> <none>
crontab-example-1597023000-rjfvw 1/1 Running 0 1s 10.244.2.31 node2 <none> <none>
crontab-example-1597023000-rjfvw 0/1 Completed 0 11s 10.244.2.31 node2 <none> <none>
crontab-example-1597023000-g7r5q 0/1 Completed 0 12s 10.244.1.74 node1 <none> <none>
crontab-example-1597023120-r87rw 0/1 Pending 0 0s <none> <none> <none> <none>
crontab-example-1597023120-z9gbg 0/1 Pending 0 0s <none> <none> <none> <none>
crontab-example-1597023120-r87rw 0/1 Pending 0 0s <none> node2 <none> <none>
crontab-example-1597023120-z9gbg 0/1 Pending 0 0s <none> node1 <none> <none>
crontab-example-1597023120-r87rw 0/1 ContainerCreating 0 0s <none> node2 <none> <none>
crontab-example-1597023120-z9gbg 0/1 ContainerCreating 0 0s <none> node1 <none> <none>
crontab-example-1597023120-r87rw 1/1 Running 0 1s 10.244.2.32 node2 <none> <none>
crontab-example-1597023120-z9gbg 1/1 Running 0 1s 10.244.1.75 node1 <none> <none>
crontab-example-1597023120-r87rw 0/1 Completed 0 11s 10.244.2.32 node2 <none> <none>
crontab-example-1597023120-z9gbg 0/1 Completed 0 11s 10.244.1.75 node1 <none> <none>
从观察CronJob控制器与Pod可以看到每隔两分钟都会创建两个并发执行的Pod任务,呆任务完成结束之后便退出状态变成Completed(完成状态)
3)查看CronJob控制器详细信息
]# kubectl describe cronjob crontab-example
Name: crontab-example
Namespace: default
Labels: app=mycronjob
Annotations: Schedule: */2 * * * *
Concurrency Policy: Allow
Suspend: False
Successful Job History Limit: 3
Failed Job History Limit: 1
Starting Deadline Seconds: <unset>
Selector: <unset>
Parallelism: 2
Completions: <unset>
Pod Template:
Labels: <none>
Containers:
myjob:
Image: alpine
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
date; echo Hello from the Kubernetes cluster; sleep 10
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: Mon, 10 Aug 2020 09:36:00 +0800
Active Jobs: crontab-example-1597023360
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 8m9s cronjob-controller Created job crontab-example-1597022880
Normal SawCompletedJob 7m49s cronjob-controller Saw completed job: crontab-example-1597022880, status: Complete
Normal SuccessfulCreate 6m9s cronjob-controller Created job crontab-example-1597023000
Normal SawCompletedJob 5m49s cronjob-controller Saw completed job: crontab-example-1597023000, status: Complete
Normal SuccessfulCreate 4m9s cronjob-controller Created job crontab-example-1597023120
Normal SawCompletedJob 3m49s cronjob-controller Saw completed job: crontab-example-1597023120, status: Complete
Normal SuccessfulCreate 2m9s cronjob-controller Created job crontab-example-1597023240
Normal SawCompletedJob 109s cronjob-controller Saw completed job: crontab-example-1597023240, status: Complete
Normal SuccessfulDelete 109s cronjob-controller Deleted job crontab-example-1597022880
Normal SuccessfulCreate 9s cronjob-controller Created job crontab-example-1597023360
三、CronJob的控制机制
CronJob控制器是一个更高级别的资源,它以Job控制器资源为其管控对象,并借助它管理Pod资源对象。可列出的Job对象的数量取决于CronJob资源的.spec.successfulJobsHistoryLimit的属性值,默认为3;
如果作业重复执行时指定的时间点接近,而作业执行时长(普遍或偶尔)跨过了其两次执行的时间长度,则会出现两个Job对象同时存在的情形。有些Job对象可能会存在无法或不能同时运行的情况,这个时候就要通过.spec.concurrencyPolicy属性控制作业并存的机制,其默认值为"Allow",即允许前后Job,甚至属于同一个CronJob的更多Job同时运行。其他两个可用值中,"Forbid"用于禁止前后两个Job同时运行,如果前一个尚未结束,后一个则不予启动(跳过),"Replace"用于让后一个Job取代前一个,即终止前一个启动后一个。