Sub topic 1
What a Job is and when to use one
Prerequisites
This chapter assumes you can read and apply YAML manifests with kubectl, and that you know what a Pod, a container, a label, and a namespace are. You should also be able to view Pod logs. You don't need to know cron syntax yet.
What a Job does
A Job is a Kubernetes object that runs Pods until a set number of them finish successfully. When that number is reached, the Job is complete and creates no more Pods. The Job controller creates the Pods and watches them. If a Pod fails, the controller can replace it, within limits you configure.
This makes a Job different from the two things people often confuse it with:
| Object | Intent | After the container exits 0 |
|---|---|---|
| Bare Pod | Run one container set on one node | Pod stays in `Succeeded`. Nothing replaces it if the node is lost. |
| Deployment | Keep N Pods running forever | The Pod is restarted, because a Deployment expects it to run continuously. |
| Job | Run work until it succeeds | The Job records the success and stops. |
A bare Pod is not rescheduled if its node fails. A Deployment requires a restart policy of Always, so it can't run a task that is meant to end. A Job fits work with a defined end, such as a database migration, a data export, a report, or a batch of image conversions.
A minimal Job
apiVersion: batch/v1
kind: Job
metadata:
name: hello
spec:
backoffLimit: 3
activeDeadlineSeconds: 300
ttlSecondsAfterFinished: 600
template:
spec:
restartPolicy: Never
containers:
- name: hello
image: busybox:1.36
command: ["sh", "-c", "echo Hello from a Job && sleep 5"]The fields work as follows:
templateis a normal Pod template. The Job creates Pods from it.restartPolicymust beNeverorOnFailurein a Job. The defaultAlwaysis rejected.backoffLimit: 3allows up to 3 retries after failures.activeDeadlineSeconds: 300stops the Job after 300 seconds, however many retries remain.ttlSecondsAfterFinished: 600deletes the Job 10 minutes after it finishes.
The last three fields are covered in detail in later chapters.
Running and watching a Job
kubectl apply -f job.yaml
kubectl wait --for=condition=complete job/hello --timeout=60s
kubectl logs job/hello
kubectl describe job hello
kubectl get pods --selector=job-name=helloThe Job controller adds a job-name label to every Pod it creates, so the last command lists that Job's Pods. kubectl logs job/hello prints logs from one of those Pods. kubectl describe shows the Job's status counts and its events.
When to use a Job
Use a Job when the work has a clear finish and you want Kubernetes to retry it on failure. Don't use one for a long-running service, which belongs in a Deployment. Don't use a bare Pod for work you care about, because nothing retries it.
A finished Job and its Pods are kept after completion, so you can read their logs. They stay until you delete them or a TTL does.
Exercise
You need a nightly task that exports data and then exits. A teammate suggests a Deployment with one replica. Why is a Job a better fit, and which restartPolicy values can the Job use?
Show answer
A Deployment restarts a container that exits, so it would run the export again and again. A Job records the successful run and stops. The Job's Pod template must use restartPolicy: Never or OnFailure.