CronJob

Imagine a virtual timekeeper, ticking along to the Linux system's crontab, triggering particular tasks to run at the precise time designated. This is the idea behind 'CronJob'.

API Version Cheat Sheet

Kubernetes Version
Batch API Version
Activated By Default?

v1.5-v1.7

batch/v2alpha1

No

v1.8-v1.20

batch/v1beta1

Yes

v1.21+

batch/v1

Yes

A word of caution: when executing APIs that aren't activated by default, users must configure --runtime-config=batch/v2alpha1 in the kube-apiserver.

CronJob Specs

  • .spec.schedule outlines the schedule of task execution, akin to the Cron format.

  • .spec.jobTemplate lists the tasks that need running, and mirrors the Job format.

  • .spec.startingDeadlineSeconds specifies the deadline for initiating tasks.

  • .spec.concurrencyPolicy delineates the policy for task concurrency, providing three options: Allow, Forbid, and Replace.

apiVersion: batch/v1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

You can also use kubectl run to create a CronJob:

Additional Resources

最后更新于