CKAD — Understanding Jobs
What Is a Job in Kubernetes?
A Job creates one or more Pods and ensures they run to completion. Unlike Deployments (which keep Pods running forever), Jobs are designed for batch processing — tasks that start, do their work, and exit.
This topic falls under the Application Design and Build domain (20% of the CKAD exam).
Real-World Use Cases
| Use Case | Example |
|---|---|
| Database migrations | Apply schema changes before deploying a new app version |
| Data processing / ETL | Transform and load data from one system to another |
| Batch emails | Send weekly newsletter to all subscribers |
| Report generation | Generate monthly billing reports |
| Test suites | Run integration tests as part of CI/CD |
| Cleanup tasks | Delete old temporary files or expired records |
Jobs vs Deployments
| Feature | Job | Deployment |
|---|---|---|
| Purpose | Run to completion, then exit | Run forever |
| Restart behavior | Does not restart after success | Restarts if Pod dies |
| Pod lifecycle | Finite (has an end) | Infinite (runs continuously) |
| Use case | Batch processing, one-time tasks | Web servers, APIs, long-running services |
restartPolicy |
Never or OnFailure |
Always (default) |
Creating a Basic Job
Imperative (Fast for Exams)
kubectl create job backup-job --image=busybox:1.36 -- /bin/sh -c "echo 'Backup completed at $(date)'"
This is the fastest way to create a Job during the CKAD exam. The -- separates kubectl arguments from the container command.
Declarative (YAML)
apiVersion: batch/v1
kind: Job
metadata:
name: backup-job
spec:
template:
spec:
containers:
- name: backup
image: busybox:1.36
command: ["/bin/sh", "-c"]
args: ["echo 'Backup started' && sleep 5 && echo 'Backup completed'"]
restartPolicy: Never
kubectl apply -f backup-job.yaml
# Watch the Job progress
kubectl get jobs -w
# Expected:
# NAME COMPLETIONS DURATION AGE
# backup-job 0/1 5s 5s
# backup-job 1/1 10s 10s
# Check the logs
kubectl logs job/backup-job
# Expected:
# Backup started
# Backup completed
Job YAML Structure
apiVersion: batch/v1
kind: Job
metadata:
name: <job-name>
spec: ← Job spec
completions: 1 ← How many successful Pods needed
parallelism: 1 ← Max Pods running at once
backoffLimit: 6 ← Max retries on failure
activeDeadlineSeconds: 300 ← Total time limit
ttlSecondsAfterFinished: 60 ← Auto-delete after completion
template: ← Pod template (same as Deployments)
spec:
containers:
- name: <container>
image: <image>
command: [...]
restartPolicy: Never ← Required: Never or OnFailure
Important: Jobs require restartPolicy: Never or restartPolicy: OnFailure. You cannot use Always (the default) because the Pod needs to be able to exit.
Generating Job YAML Quickly
On the CKAD exam, generate a YAML template and edit it:
# Generate YAML without creating the Job
kubectl create job my-job --image=busybox:1.36 --dry-run=client -o yaml -- /bin/sh -c "echo hello" > job.yaml
# Edit as needed
vi job.yaml
# Apply
kubectl apply -f job.yaml
Exam tip: Always use kubectl create job ... --dry-run=client -o yaml to generate a starting template. It saves time compared to writing YAML from scratch.
Next: Learn about running multiple Pods with Completions & Parallelism →