Kubernetes Jobs
A workload controller designed for finite, batch-style tasks that run to completion. Unlike Deployments or DaemonSets, a Job creates Pods that are expected to terminate successfully and then stop. Synthesized from CKA Day 12 — DaemonSet, Job & CronJob Explained.
What is a Job?
A Job creates one or more Pods and ensures that a specified number of them successfully terminate. Once the required number of completions is reached, the Job is marked as Complete. If a Pod fails, the Job controller restarts it (up to the backoffLimit) until success or failure is declared.
Key Insight: Deployments manage Pods that should run forever. Jobs manage Pods that should finish and exit.
Why Use a Job?
| Requirement | Deployment | Job |
|---|---|---|
| Run a task once to completion | ❌ (keeps restarting) | ✅ |
| Run multiple parallel workers | ❌ (not designed for batch) | ✅ (parallelism > 1) |
| Track completion count | ❌ | ✅ (completions field) |
| Automatic retry on failure | ❌ (unless readiness fails) | ✅ (backoffLimit) |
| Finite lifetime | ❌ | ✅ (Pods terminate, Job stays) |
Canonical Use Cases
- Database migrations and schema updates
- One-off data processing or ETL pipelines
- Batch report generation (monthly financial reports, analytics exports)
- CI/CD pipeline steps that execute inside the cluster
- Backup jobs (export data, compress, upload to object storage)
- Testing and validation suites run as ephemeral workloads
YAML Structure
apiVersion: batch/v1
kind: Job
metadata:
name: data-migration
spec:
completions: 1
parallelism: 1
backoffLimit: 4
activeDeadlineSeconds: 600
template:
metadata:
labels:
app: migration
spec:
restartPolicy: OnFailure
containers:
- name: migrator
image: myapp:latest
command: ["python", "migrate.py"]Key fields:
| Field | Default | Description |
|---|---|---|
completions | 1 | Total successful completions required |
parallelism | 1 | Number of Pods running concurrently |
backoffLimit | 6 | Retries before marking Job as failed |
activeDeadlineSeconds | unset | Max duration; Job is terminated if exceeded |
restartPolicy | Required | Must be OnFailure or Never; Always is invalid |
Parallel Execution Patterns
Sequential (default)
completions: 5, parallelism: 1 — run one Pod after another until 5 successes.
Parallel Workers
completions: 10, parallelism: 3 — run up to 3 Pods at a time until 10 total successes.
Work Queue (Indexed Job)
Set completionMode: Indexed so each Pod gets a unique index (0 to N-1) via the JOB_COMPLETION_INDEX environment variable. Useful for sharded or partitioned batch processing.
Job Lifecycle
| Phase | Description |
|---|---|
| Pending | Job created, Pods not yet scheduled |
| Active | At least one Pod is running |
| Complete | Required number of completions reached successfully |
| Failed | backoffLimit exhausted or activeDeadlineSeconds exceeded |
Essential Commands
| Command | Purpose |
|---|---|
kubectl get jobs | List Jobs |
kubectl describe job <name> | Events, completions, and failures |
kubectl logs job/<name> | Read logs from the Job’s Pod(s) |
kubectl delete job <name> | Delete Job and its Pods |
kubectl wait --for=condition=complete job/<name> | Block until Job finishes |
Important Restrictions
restartPolicy: Alwaysis invalid inside a Job template. The Job controller must detect termination, so the Pod must not restart automatically.- Deleting a Job does not delete completed Pods by default unless you set
ttlSecondsAfterFinished. However,kubectl delete jobwill delete the Job object and its active Pods. - A Job is not self-healing in the Deployment sense. If the Pod completes, the Job stays complete. To run it again, you must create a new Job.
CKA Exam Relevance
- Workloads & Scheduling (~15%): Create a Job from a given spec, or debug why a Job is stuck in
PendingorFailed. - Troubleshooting (~30%): Check
restartPolicyfirst. If it isAlways, the Job will fail validation. CheckbackoffLimitandactiveDeadlineSecondsfor premature termination. - Speed pattern:
kubectl create job my-job --image=busybox --dry-run=client -o yaml \ -- /bin/sh -c "echo hello" > job.yaml # Edit apiVersion: batch/v1, add restartPolicy: OnFailure
Sources
Related Pages
- Kubernetes CronJobs — wraps Jobs with a schedule
- Pod Fundamentals — the unit a Job creates
- Deployment, ReplicaSet & Replication Controller — long-running controllers
- Kubernetes Namespaces — Jobs are namespace-scoped
- Kubernetes Labels and Selectors — label-based Job selection and filtering
- CKA Certification — exam overview
- CKA Study Roadmap — 40-day plan
- Tech Tutorials with Piyush — course source
Tags: kubernetes job batch workload cka devops etl migration ci-cd