类似于Linux的Cron模块,CronJob用来运行定时性任务,或者周期性、重复性任务。注意CronJob启动的是kubernetes中的Job,不是ReplicaSet、DaemonSet等其它控制器类型。
示例:
以下CronJob每分钟运行一次,打印出当前时间与hello消息。
CronJob SPEC:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
以上内容,schedule: "*/1 * * * *"与Linux Cron意义相同,表示每分钟启动一次。
运行CronJob
$ kubectl create -f ./cronjob.yaml
cronjob "hello" created
或者通过祈使命令行方式:
$ kubectl run hello --schedule="*/1 *