A Cron Job creates Jobs on a time-based schedule.
A CronJob object is just like an entry in crontab in Unix/Linux. It runs a job periodically on a given schedule.
You need a working Kubernetes cluster at version >= 1.8 (for CronJob).
For previous versions of the cluster (< 1.8) you need to explicitly enable batch/v2alpha1 API by passing –runtime-config=batch/v2alpha1=true to the API server.
Here is the manifest for Cronjob
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
Creating a Cron Job
$ kubectl create -f cronjob.yaml cronjob.batch "hello" created
$ kubectl get cronjobs hello NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE hello */1 * * * * False 0 < none > 28s
$ kubectl get jobs --watch NAME DESIRED SUCCESSFUL AGE hello-1550406120 1 1 35s hello-1550406180 1 0 0s hello-1550406180 1 0 0s hello-1550406180 1 1 2s hello-1550406240 1 0 0s hello-1550406240 1 0 0s hello-1550406240 1 1 1s hello-1550406300 1 0 0s hello-1550406300 1 0 0s hello-1550406300 1 1 1s hello-1550406120 1 1 3m
Deleting a Cron Job
$ kubectl delete cronjob hello cronjob "hello" deleted
Writing a Cron Job Spec
As with all other Kubernetes configs, a cron job needs apiVersion, kind, and metadata fields.
Schedule
The .spec.schedule is a required field of the .spec. It takes a Cron format string, such as 0 * * * * or @hourly, as schedule time of its jobs to be created and executed.