What is the difference between a Kubernetes Job and a CronJob?
Kubernetes Jobs and CronJobs are workload resources for running tasks to completion rather than running continuously like Deployments.
Kubernetes Job
A Job creates one or more pods and ensures a specified number of them successfully terminate. Once the required completions are reached, the Job is complete.
Use Cases
- Database migrations
- Batch data processing
- One-time setup tasks
- Report generation
Example Job
apiVersion: batch/v1
kind: Job
metadata:
name: db-migration
spec:
completions: 1
parallelism: 1
backoffLimit: 3
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: myapp:latest
command: ["python", "manage.py", "migrate"]Job Patterns
- Non-parallel: Single pod runs to completion
- Parallel with fixed count: Multiple pods, each does a portion
- Parallel with work queue: Pods process items from a queue
CronJob
A CronJob creates Jobs on a repeating schedule using standard Unix cron syntax.
Use Cases
- Nightly database backups
- Hourly report generation
- Periodic cleanup tasks
- Scheduled ETL pipelines
Example CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-backup
spec:
schedule: "0 2 * * *" # 2 AM every day
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: backup-tool:latest
command: ["/backup.sh"]Key Differences
| Feature | Job | CronJob |
|---|---|---|
| Trigger | Manual/one-time | Scheduled (cron) |
| Recurrence | Runs once | Repeats on schedule |
| Use case | Ad-hoc tasks | Recurring tasks |
| Creates | Pods directly | Jobs (which create pods) |