Kubernetes CronJob
CronJob
CronJob用于执行常规的计划操作(如备份、报告生成等)。
格式
* * * * * 分时日月周
创建一个 Job
[root@master01 job]# kubectl create -f cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
查看执行结果
[root@master01 job]# kubectl get jobs --watch
NAME COMPLETIONS DURATION AGE
hello-1673882700 1/1 2s 94s
hello-1673882760 1/1 1s 33s
删除
kubectl delete cronjob hello
参数介绍
[root@master01 job]# kubectl get cj hello -oyaml
kubectl get cj hello -oyaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
labels:
run: hello
name: hello
namespace: default
spec:
concurrencyPolicy: Allow #并发调度策略:Allow运行同时运行过个任务。
# Forbid:不运行并发执行。
# Replace:替换之前的任务
failedJobsHistoryLimit: 1 # 保留失败的任务数。
jobTemplate:
metadata:
creationTimestamp: null
spec:
template:
metadata:
creationTimestamp: null
labels:
run: hello
spec:
containers:
- args:
- /bin/sh
- -c
- date
image: nginx
imagePullPolicy: IfNotPresent
name: hello
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: OnFailure
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
schedule: '*/1 * * * *' #调度的策略 分时日月周
successfulJobsHistoryLimit: 3 # 成功的Job保留的次数
suspend: false # 挂起,true:cronjob不会被执行。
status: {}