创建一个Cron作业
kubectl create -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/application/job/cronjob.yaml
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
或者通过kubectl run方式创建
kubectl run hello --schedule="*/1 * * * *" --restart=OnFailure --image=busybox -- /bin/sh -c "date; echo Hello from the Kubernetes cluster"
查看cronjob状态及结果
kubectl get cronjob hello
kubectl get jobs --watch
删除Cron作业
kubectl delete cronjob hello
参考文档:
https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/