How to Schedule a Job in Kubernetes: Simple Guide
To schedule a job in Kubernetes, use the
CronJob resource which runs jobs on a time-based schedule defined by a cron expression. You create a CronJob YAML manifest specifying the schedule and job template, then apply it with kubectl apply -f.Syntax
The CronJob resource uses a cron schedule string to define when the job runs. It contains a spec.schedule field for timing and a spec.jobTemplate that defines the job to run.
- apiVersion: Kubernetes API version for CronJob.
- kind: Must be
CronJob. - metadata: Name and labels for the CronJob.
- spec.schedule: Cron format string for job timing.
- spec.jobTemplate: Template for the job to run, including container specs.
yaml
apiVersion: batch/v1 kind: CronJob metadata: name: example-cronjob spec: schedule: "*/5 * * * *" jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster restartPolicy: OnFailure
Example
This example schedules a job to run every 5 minutes. The job runs a simple busybox container that prints the date and a message. The restartPolicy is set to OnFailure to retry if the job fails.
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 the Kubernetes cluster restartPolicy: OnFailure
Output
kubectl get cronjob hello-cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello-cronjob */5 * * * * False 0 <none> 1m
Common Pitfalls
- Incorrect cron syntax: The schedule must follow standard cron format with five fields (minute, hour, day of month, month, day of week).
- Missing restartPolicy: Always set
restartPolicytoOnFailureorNeverinside the pod spec. - Time zone confusion: Kubernetes uses UTC time for schedules, so adjust your cron expression accordingly.
- Resource limits: Not setting resource requests/limits can cause scheduling delays or failures.
yaml
apiVersion: batch/v1 kind: CronJob metadata: name: wrong-cronjob spec: schedule: "60 * * * *" # Invalid minute value jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - echo Hello restartPolicy: OnFailure --- # Corrected schedule apiVersion: batch/v1 kind: CronJob metadata: name: correct-cronjob spec: schedule: "0 * * * *" # Runs at minute 0 every hour jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - echo Hello restartPolicy: OnFailure
Quick Reference
Use this quick reference when creating Kubernetes CronJobs:
| Field | Description | Example |
|---|---|---|
| apiVersion | Kubernetes API version for CronJob | batch/v1 |
| kind | Resource type | CronJob |
| metadata.name | Name of the CronJob | hello-cronjob |
| spec.schedule | Cron schedule string (UTC time) | "*/5 * * * *" |
| spec.jobTemplate | Template for the job to run | Specifies pod and container details |
| spec.jobTemplate.spec.template.spec.containers | Containers to run | busybox with args |
| spec.jobTemplate.spec.template.spec.restartPolicy | Pod restart policy | OnFailure or Never |
Key Takeaways
Use the Kubernetes CronJob resource to schedule jobs with cron syntax.
Always set restartPolicy to OnFailure or Never inside the job pod spec.
CronJob schedules use UTC time, so adjust your cron expression accordingly.
Validate your cron syntax to avoid scheduling errors.
Apply your CronJob manifest with kubectl to create the scheduled job.