Jobs & CronJobs
Run batch workloads and scheduled tasks in Kubernetes — Jobs for one-off tasks, CronJobs for recurring schedules, and parallel processing patterns.
Kubernetes Jobs
Jobs run pods to completion — unlike Deployments which keep pods running indefinitely. A Job creates one or more pods and tracks successful completions. Use Jobs for batch processing, database migrations, and one-off tasks.
Jobs retry failed pods up to backoffLimit times. Set completions and parallelism for work queue patterns where multiple pods process items concurrently.
- restartPolicy must be Never or OnFailure for Jobs
- completions: 5 with parallelism: 2 runs 5 pods, 2 at a time
- Jobs are not restarted when they complete — create new ones
apiVersion: batch/v1
kind: Job
metadata:
name: data-migration
spec:
backoffLimit: 3
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: myapp:1.0.0
command: ["npm", "run", "migrate"]CronJobs
CronJobs create Jobs on a schedule using cron syntax. They manage Job history — successfulJobsHistoryLimit and failedJobsHistoryLimit control retention. Use CronJobs for backups, report generation, cache warming, and cleanup tasks.
CronJob schedules use standard cron format: "0 2 * * *" runs daily at 2 AM UTC. Specify timezone with timeZone field (Kubernetes 1.27+).
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-backup
spec:
schedule: "0 2 * * *"
timeZone: "America/New_York"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: backup-tool:latest
command: ["./backup.sh"]Parallel and Indexed Jobs
Parallel Jobs run multiple pods simultaneously with completions tracking total successes. Indexed Jobs assign each pod an index (0, 1, 2...) useful for partitioned work — processing data shards or running distributed tests.
Set completionMode: Indexed for indexed jobs. Pods access their index via JOB_COMPLETION_INDEX environment variable.
spec:
completions: 10
parallelism: 3
completionMode: Indexed
template:
spec:
containers:
- name: worker
image: worker:latest
env:
- name: SHARD_INDEX
valueFrom:
fieldRef:
fieldPath: metadata.annotations['batch.kubernetes.io/job-completion-index']Job Patterns and Best Practices
Use init containers in Job pods for setup tasks before the main container runs. Set activeDeadlineSeconds to kill jobs that run too long. Use ttlSecondsAfterFinished to auto-clean completed jobs.
For long-running batch workloads, consider Argo Workflows or Tekton Pipelines which provide DAG-based orchestration, artifact passing, and retry logic beyond basic Jobs.
spec:
activeDeadlineSeconds: 3600 # Kill after 1 hour
ttlSecondsAfterFinished: 86400 # Clean up after 24 hours
template:
spec:
initContainers:
- name: setup
image: setup:latest
command: ["./prepare-data.sh"]Monitoring Jobs
Monitor job success and failure rates with Prometheus metrics from kube-state-metrics. Alert on failed CronJobs and jobs exceeding deadline. Check job logs with kubectl logs job/job-name.
For CronJobs, a missed schedule (startingDeadlineSeconds exceeded) is logged but not retried. Monitor schedule adherence and job duration trends.
kubectl get jobs kubectl describe job data-migration kubectl logs job/data-migration kubectl get cronjobs kubectl create job --from=cronjob/nightly-backup manual-backup