Cron Job
第一个CronJob资源清单
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:latest
command: ["date", "+%F %T"]
restartPolicy: Never
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
关键参数:
- restartPolicy: 任务执行失败不重启再次尝试
- successfulJobsHistoryLimit: 成功执行的pod只保存最近3个
- failedJobsHistoryLimit:执行失败的任务Pod只保存最近1个
查看执行情况
# kubectl get cj -o wide
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE CONTAINERS IMAGES SELECTOR
hello * * * * * False 1 7s 9m hello busybox:latest <none>
# kubectl describe cj hello
Name: hello
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"batch/v1beta1","kind":"CronJob","metadata":{"annotations":{},"name":"hello","namespace":"default"},"spec":{"failedJobsHistoryLimit":1,"j...
Schedule: * * * * *
Concurrency Policy: Allow
Suspend: False
Starting Deadline Seconds: <unset>
Selector: <unset>
Parallelism: <unset>
Completions: <unset>
Pod Template:
Labels: <none>
Containers:
hello:
Image: busybox:latest
Port: <none>
Host Port: <none>
Command:
date
+%F %T
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: Wed, 05 Sep 2018 16:47:00 +0800
Active Jobs: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 9m cronjob-controller Created job hello-1536136680
Normal SuccessfulCreate 8m cronjob-controller Created job hello-1536136740
Normal SuccessfulCreate 7m cronjob-controller Created job hello-1536136800
Normal SawCompletedJob 7m cronjob-controller Saw completed job: hello-1536136800
Normal SuccessfulCreate 6m cronjob-controller Created job hello-1536136860
Normal SawCompletedJob 6m cronjob-controller Saw completed job: hello-1536136860
Normal SuccessfulCreate 5m cronjob-controller Created job hello-1536136920
Normal SawCompletedJob 5m cronjob-controller Saw completed job: hello-1536136920
Normal SuccessfulCreate 4m cronjob-controller Created job hello-1536136980
Normal SawCompletedJob 4m cronjob-controller Saw completed job: hello-1536136980
Normal SuccessfulDelete 4m cronjob-controller Deleted job hello-1536136800
Normal SuccessfulCreate 3m cronjob-controller Created job hello-1536137040
Normal SawCompletedJob 3m cronjob-controller Saw completed job: hello-1536136680
Normal SuccessfulDelete 3m cronjob-controller Deleted job hello-1536136860
Normal SawCompletedJob 3m cronjob-controller Saw completed job: hello-1536137040
Normal SawCompletedJob 2m cronjob-controller Saw completed job: hello-1536136740
Normal SuccessfulDelete 2m cronjob-controller Deleted job hello-1536136680
Normal SuccessfulCreate 2m cronjob-controller Created job hello-1536137100
Normal SawCompletedJob 2m cronjob-controller Saw completed job: hello-1536137100
Normal SuccessfulDelete 2m cronjob-controller Deleted job hello-1536136920
Normal SuccessfulCreate 1m cronjob-controller Created job hello-1536137160
Normal SawCompletedJob 1m cronjob-controller Saw completed job: hello-1536137160
Normal SuccessfulDelete 1m cronjob-controller Deleted job hello-1536136980
Normal SuccessfulCreate 42s cronjob-controller (combined from similar events): Created job hello-1536137220
Normal SawCompletedJob 32s cronjob-controller (combined from similar events): Saw completed job: hello-1536137220
Normal SuccessfulDelete 32s cronjob-controller Deleted job hello-1536137040