Sub topic 2
The Pod template and restartPolicy
This chapter assumes you can read and apply YAML manifests with kubectl, and that you know what a Pod, a container, and a Pod's logs are. It covers the Pod template inside a Job and the restartPolicy field, which decides who retries a failed container.
What the Pod template is
A Job does not describe a container directly. It holds a Pod template in spec.template. The Job controller uses this template to create Pods, the same way a Deployment does. The template has the same shape as a Pod spec: containers, volumes, env, resource requests, and so on. It has no metadata.name, because the Job controller names each Pod itself.
Two things are specific to Jobs:
- The Job adds a
job-namelabel and abatch.kubernetes.io/job-namelabel to each Pod. You can use these to find the Pods later. - The template is immutable after creation. To change the image or command, delete the Job and create a new one.
restartPolicy is required and restricted
A Pod's restartPolicy normally defaults to Always. That value would restart a finished container forever, so a Job rejects it. You must set restartPolicy explicitly to one of two values:
| Value | What happens when a container fails | Where the retry happens |
|---|---|---|
| `Never` | The Pod is marked `Failed`. The Job controller creates a new Pod. | A new Pod |
| `OnFailure` | The kubelet restarts the container in the same Pod. | The same Pod |
If you leave the field out or set Always, the API server refuses the manifest with a validation error.
Choosing between Never and OnFailure
Use Never when you want to keep evidence of each failed attempt. Each attempt is a separate Pod, so you can run kubectl logs on every failed Pod and see what happened in each. The cost is more Pod objects.
Use OnFailure when you want fewer objects and the work can safely start again on the same node. The container restarts in place, so the Pod keeps its node, its emptyDir volumes, and its Pod IP. Old container logs are available only through kubectl logs --previous, and only for the last restart.
podFailurePolicy, covered in a later chapter, requires Never. If you expect to use it, start with Never.
Both policies count toward the Job's retry limit. The way they count differs slightly. With OnFailure, container restarts inside a Pod are added up. With Never, each failed Pod is one failure. Chapter "Retries and time limits" covers backoffLimit in detail.
A worked example
This Job runs one container and uses Never:
apiVersion: batch/v1
kind: Job
metadata:
name: template-demo
spec:
template:
spec:
restartPolicy: Never
containers:
- name: main
image: busybox:1.36
command: ["sh", "-c", "echo attempt on $(hostname); exit 1"]Apply it and watch the Pods:
kubectl apply -f template-demo.yaml
kubectl get pods --selector=job-name=template-demo --watchBecause the container always exits with code 1 and the policy is Never, you see several Pods with status Error. The Job controller creates a new Pod for each failure. It waits longer between attempts, using an exponential back-off, and stops after the default backoffLimit of 6.
Now change restartPolicy to OnFailure, delete the old Job, and apply it again. You see one Pod whose RESTARTS column grows, and its status moves between Error and CrashLoopBackOff.
Common errors
- Validation error on apply. The message says the
restartPolicyvalue is unsupported. Set it toNeverorOnFailure. - Edit rejected. The message says the
spec.templatefield is immutable. Delete the Job and re-create it. - Logs missing after failures. With
OnFailure, only the previous container's logs remain. Switch toNeverto keep one Pod per attempt.
Exercise
A Job's container sometimes fails because of a temporary network error. You want one separate Pod, with its own logs, for every failed attempt. Which restartPolicy do you choose, and why?
Show answer
Choose Never. The Pod is marked Failed and the Job controller creates a new Pod for the next attempt, so each attempt keeps its own Pod and logs. With OnFailure the kubelet restarts the container inside the same Pod, so you keep only the last restart's previous logs.