Kubernetes Jobs CronJobs Guide
Run batch workloads with Kubernetes Jobs and CronJobs. Parallel execution, completion tracking, failure handling, TTL cleanup, and scheduled tasks.
π‘ Quick Answer: A Job runs a pod to completion (exit 0) and tracks success. A CronJob creates Jobs on a schedule (cron syntax). Use
backoffLimitfor retry control,parallelismfor concurrent execution,ttlSecondsAfterFinishedfor auto-cleanup, andconcurrencyPolicyto control overlapping CronJob runs.
The Problem
Not all workloads are long-running services. Kubernetes needs to handle:
- Database backups on a schedule
- ETL pipelines and data processing
- Report generation
- Email campaigns
- Cleanup and maintenance tasks
- One-off migrations
The Solution
Basic Job
apiVersion: batch/v1
kind: Job
metadata:
name: db-backup
spec:
ttlSecondsAfterFinished: 3600 # Auto-delete after 1 hour
backoffLimit: 3 # Retry 3 times on failure
activeDeadlineSeconds: 600 # Timeout after 10 minutes
template:
spec:
containers:
- name: backup
image: postgres:16
command:
- pg_dump
- -h
- postgres-svc
- -U
- admin
- -d
- production
- -f
- /backup/dump.sql
volumeMounts:
- name: backup
mountPath: /backup
volumes:
- name: backup
persistentVolumeClaim:
claimName: backup-pvc
restartPolicy: OnFailureParallel Job
apiVersion: batch/v1
kind: Job
metadata:
name: image-processor
spec:
completions: 10 # Total pods to run successfully
parallelism: 3 # Run 3 pods at a time
completionMode: Indexed # Each pod gets $JOB_COMPLETION_INDEX
template:
spec:
containers:
- name: processor
image: myapp/processor:v1
command:
- python
- process.py
- --shard=$(JOB_COMPLETION_INDEX)
restartPolicy: NeverCronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-backup
spec:
schedule: "0 2 * * *" # 2 AM daily
timeZone: "Europe/Rome" # v1.27+ timezone support
concurrencyPolicy: Forbid # Don't overlap
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 5
startingDeadlineSeconds: 600 # Skip if 10min late
jobTemplate:
spec:
ttlSecondsAfterFinished: 86400
template:
spec:
containers:
- name: backup
image: backup-tool:v1
command: ["/backup.sh"]
restartPolicy: OnFailureConcurrency Policies
| Policy | Behavior |
|---|---|
Allow (default) | Multiple concurrent Jobs allowed |
Forbid | Skip new Job if previous still running |
Replace | Kill running Job, start new one |
Monitor Jobs
# List jobs
kubectl get jobs
kubectl get cronjobs
# Watch job pods
kubectl get pods -l job-name=db-backup -w
# Check job status
kubectl describe job db-backup
# View logs
kubectl logs job/db-backup
# Manually trigger a CronJob
kubectl create job manual-backup --from=cronjob/nightly-backupCommon Issues
Job pods remain after completion
Set ttlSecondsAfterFinished to auto-cleanup. Without it, completed pods stay until manually deleted or hit successfulJobsHistoryLimit.
CronJob creates duplicate runs
Using Allow concurrency with slow jobs. Switch to Forbid to prevent overlap.
Job never completes β stuck at 0/1
Pod is CrashLooping. Check backoffLimit β once exhausted, the Job fails permanently. Check pod logs for the error.
Best Practices
- Always set
ttlSecondsAfterFinishedβ prevent pod accumulation - Use
Forbidfor CronJobs that shouldnβt overlap - Set
activeDeadlineSecondsβ prevent runaway jobs - Use
startingDeadlineSecondson CronJobs β skip stale schedules after downtime restartPolicy: Neverfor debugging β keeps failed pods for log inspectionrestartPolicy: OnFailurefor production β automatic retry within the pod
Key Takeaways
- Jobs run pods to completion with retry and parallelism support
- CronJobs create Jobs on a cron schedule with overlap control
ttlSecondsAfterFinishedprevents completed pod accumulationconcurrencyPolicy: Forbidprevents overlapping scheduled runs- Use
Indexedcompletion mode for sharded parallel processing activeDeadlineSecondsis your timeout safety net

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
