Sub topic 4
Retries and time limits
This chapter assumes you can write a Job manifest, you know what restartPolicy does in a Pod template, and you can read Pod status with kubectl. Here you will learn how a Job decides to retry failed work and when it gives up.
Two limits, two jobs
A Job has two settings that bound a run:
backoffLimitcaps the number of retries. The default is 6.activeDeadlineSecondscaps the total run time of the Job, measured from when it starts.
They protect against different failures. backoffLimit stops a task that keeps crashing. activeDeadlineSeconds stops a task that hangs or runs far too long.
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"]This Job retries a failed Pod up to 3 times and stops after 300 seconds, whichever limit is reached first.
How backoffLimit counts
The Job controller counts failed Pods. When the count reaches backoffLimit, the Job is marked Failed and its running Pods are terminated. The retry delay grows exponentially: about 10 seconds, then 20, then 40, capped at 6 minutes. The delay resets if no new failed Pods appear for 10 minutes.
How a retry happens depends on restartPolicy:
| restartPolicy | What happens on failure | Pod history |
|---|---|---|
| `Never` | The Job creates a new Pod. | One failed Pod stays per attempt, so logs from each try are kept. |
| `OnFailure` | The kubelet restarts the container inside the same Pod. | One Pod, with a rising restart count. Earlier container logs are lost after a restart. |
With OnFailure, each container restart counts toward backoffLimit, even though no new Pod is created. Use Never when you want to inspect each failed attempt.
How activeDeadlineSeconds works
The deadline applies to the whole Job, not to a single Pod. It includes all retries and the backoff waits between them. When the deadline passes, Kubernetes terminates all running Pods and marks the Job Failed with the reason DeadlineExceeded.
The deadline takes priority over backoffLimit. A Job with backoffLimit: 6 and activeDeadlineSeconds: 60 fails at 60 seconds even if only one retry has happened.
Watching a Job fail
Create a Job that always fails and watch what happens:
kubectl apply -f failing-job.yaml
kubectl get pods --selector=job-name=failing --watch
kubectl describe job failingIn describe, the Events section shows entries such as BackoffLimitExceeded or DeadlineExceeded. The reason tells you which limit stopped the Job.
Common errors
- Setting the deadline shorter than one normal run. The Job is killed even when nothing is wrong. Set it to several times the expected duration.
- Using a high
backoffLimitfor a permanent error. A bad image name or a wrong command will fail every time. Retries only delay the failure. Lower the limit or usepodFailurePolicyto fail fast on known exit codes. - Expecting retries to be instant. The exponential delay means 6 retries can take several minutes.
- Retrying work that is not safe to repeat. A retried Pod may redo part of the task. Design the task so a second attempt is harmless.
Exercise
A Job has backoffLimit: 5 and activeDeadlineSeconds: 120. Its Pod hangs forever without exiting. What happens, and which reason appears on the Job?
Show answer
The Pod never fails, so the retry count never rises and backoffLimit is never reached. After 120 seconds, the deadline passes. Kubernetes terminates the Pod and marks the Job Failed with the reason DeadlineExceeded.
Exercise
You want to keep the logs of every failed attempt. Which restartPolicy should you use, and why?
Show answer
Use Never. Each retry creates a new Pod, so every failed Pod and its logs remain until the Job is cleaned up. With OnFailure the container restarts inside the same Pod, and the logs of earlier attempts are lost.