Kubernetes CronJob

Kubernetes CronJob

范例

cronjob.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

创建 CronJob

kubectl create -f cronjob.yaml

查看 CronJob

kubectl get cronjob hello

k8s_cronjob01

关注 Job 状态

kubectl get jobs --watch

k8s_cronjob02

删除 CronJob

kubectl delete cronjob hello

更多详细操作

集成

针对php项目容器化改造过程中,原有crontab任务较多的问题,在迁移到 k8s 集群时可无缝替换成 CronJob 方式管理。

后续可基于 CronJob 开发客户端工具,实现简单高效的分布式任务系统。