How to Create a CronJob in Kubernetes: Step-by-Step Guide
To create a
CronJob in Kubernetes, define a YAML manifest specifying the schedule in cron format and the job template to run. Apply this manifest using kubectl apply -f to schedule recurring tasks automatically.Syntax
A Kubernetes CronJob manifest includes these key parts:
- apiVersion: The API version, usually
batch/v1. - kind: Set to
CronJob. - metadata: Name and labels for the CronJob.
- spec.schedule: The cron expression defining when the job runs.
- spec.jobTemplate: The job to run, including the pod template.
yaml
apiVersion: batch/v1 kind: CronJob metadata: name: example-cronjob spec: schedule: "*/5 * * * *" jobTemplate: spec: template: spec: containers: - name: example image: busybox args: - /bin/sh - -c - date; echo Hello from the Kubernetes CronJob restartPolicy: OnFailure
Example
This example creates a CronJob that runs every 5 minutes. It runs a busybox container that prints the current date and a message.
yaml
apiVersion: batch/v1 kind: CronJob metadata: name: hello-cronjob spec: schedule: "*/5 * * * *" jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - date; echo Hello from Kubernetes CronJob restartPolicy: OnFailure
Output
Every 5 minutes, a job runs and logs output like:
Thu Jun 1 12:00:00 UTC 2024
Hello from Kubernetes CronJob
Common Pitfalls
- Incorrect cron syntax: The schedule must follow standard cron format, or the CronJob won't run.
- Missing restartPolicy: Must be
OnFailureorNeverfor CronJobs. - Using
latestimage tag: Avoid it for production; use specific image versions. - Time zone confusion: Kubernetes uses UTC time for schedules.
yaml
Wrong example: apiVersion: batch/v1 kind: CronJob metadata: name: bad-cronjob spec: schedule: "60 * * * *" # Invalid cron expression jobTemplate: spec: template: spec: containers: - name: bad image: busybox args: - /bin/sh - -c - echo "This will not run" restartPolicy: Always # Invalid for CronJob Corrected example: apiVersion: batch/v1 kind: CronJob metadata: name: good-cronjob spec: schedule: "0 * * * *" jobTemplate: spec: template: spec: containers: - name: good image: busybox args: - /bin/sh - -c - echo "This will run every hour" restartPolicy: OnFailure
Quick Reference
Here is a quick cheat sheet for Kubernetes CronJob fields:
| Field | Description | Example |
|---|---|---|
| apiVersion | API version for CronJob | batch/v1 |
| kind | Resource type | CronJob |
| metadata.name | Name of the CronJob | my-cronjob |
| spec.schedule | Cron schedule expression | */5 * * * * |
| spec.jobTemplate | Template for the job to run | spec.template.spec.containers |
| spec.jobTemplate.spec.template.spec.containers[].image | Container image to run | busybox |
| spec.jobTemplate.spec.template.spec.restartPolicy | Pod restart policy | OnFailure or Never |
Key Takeaways
Define a CronJob YAML with schedule and jobTemplate, then apply it with kubectl.
Use valid cron syntax and set restartPolicy to OnFailure or Never.
Kubernetes CronJobs run in UTC time zone by default.
Avoid using the latest image tag for predictable runs.
Check logs of jobs to verify CronJob execution.