类似于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 * * * *" --restart=OnFailure --image=busybox -- /bin/sh -c "date; echo Hello from the Kube

本文介绍了Kubernetes中的CronJob,用于执行定时任务。CronJob基于时间调度,与Linux Cron类似,它创建Job而非ReplicaSet。文中通过示例展示了CronJob的配置、状态检查、Job管理及重要字段解析,包括.spec.startingDeadlineSeconds、.spec.concurrencyPolicy和.spec.suspend等。
最低0.47元/天 解锁文章
1505

被折叠的 条评论
为什么被折叠?



