Sub topic 14
Fine-grained failure handling with podFailurePolicy
This chapter assumes you can write a Job manifest, know how backoffLimit and restartPolicy work, and can read Pod status with kubectl. By default, a Job treats every failed Pod the same way. Each failure counts against backoffLimit, and the Job retries until the limit is reached. podFailurePolicy lets you change that for specific failures.
Why the default is not always right
Two kinds of failure should not be handled the same way:
- A bug in your code, such as a bad configuration. Retrying will never help, so you want the Job to stop at once.
- An infrastructure event, such as a node drain or a preemption. The Pod did nothing wrong, so it should not use up a retry.
Without a policy, both failures consume backoffLimit. A Job with backoffLimit: 6 might fail because a node was drained six times, or it might retry a hopeless error six times.
The manifest
apiVersion: batch/v1
kind: Job
metadata:
name: failure-policy-demo
spec:
backoffLimit: 6
podFailurePolicy:
rules:
- action: FailJob
onExitCodes:
containerName: main
operator: In
values: [42]
- action: Ignore
onPodConditions:
- type: DisruptionTarget
template:
spec:
restartPolicy: Never
containers:
- name: main
image: busybox:1.36
command: ["sh", "-c", "exit 42"]The policy has a list of rules. Each rule has an action and a matcher.
- The first rule matches when the container named
mainexits with code 42.FailJobmarks the whole Job as failed immediately, with no more retries. - The second rule matches when the Pod has the
DisruptionTargetcondition. Kubernetes sets this condition when it deletes a Pod because of a disruption, such as a node drain or preemption.Ignoremeans the failure does not count againstbackoffLimit, and the Job creates a replacement Pod.
Rules are checked in order, and the first matching rule wins. A failed Pod that matches no rule is handled normally and counts against backoffLimit.
The available actions
| Action | Effect |
|---|---|
| `FailJob` | Marks the Job as failed and terminates its running Pods. |
| `Ignore` | Does not count the failure toward `backoffLimit`. |
| `Count` | Counts the failure toward `backoffLimit`, which is the default behavior. |
| `FailIndex` | Fails only that index in an Indexed Job. It needs `backoffLimitPerIndex`. |
Matchers
A rule uses exactly one of two matchers:
onExitCodescompares a container's exit code with a list of values. The operator isInorNotIn. SetcontainerNameto target one container. If you omit it, the rule applies to all containers.onPodConditionsmatches Pod conditions bytypeandstatus. The status defaults toTrue.
Checking the result
Apply the manifest and inspect the Job:
kubectl apply -f failure-policy-demo.yaml
kubectl describe job failure-policy-demo
kubectl get job failure-policy-demo -o jsonpath='{.status.conditions}'The Job should show a Failed condition with a reason of PodFailurePolicy. Only one Pod should have run, even though backoffLimit is 6. If the reason is BackoffLimitExceeded, no rule matched. Check that containerName matches the real container name and that the exit code is in values.
Design notes
- Use
FailJobfor exit codes that your program documents as permanent errors, such as invalid input. Retrying wastes cluster time. - Use
IgnorewithDisruptionTargeton clusters that use spot nodes or frequent drains. - Keep
backoffLimitfor the failures you cannot classify in advance. - Tasks must still be safe to run twice. Ignored failures create extra Pods, so see the idempotency chapter.
Exercise
A Job has backoffLimit: 4 and a podFailurePolicy with one rule: Ignore on the DisruptionTarget condition. A node drain evicts the Pod twice, then the container exits with code 1 four times. How many failures count against backoffLimit, and what happens to the Job?
Show answer
Only the four exit-code failures count, because the two evictions are ignored. The Job reaches its limit of 4 and fails with the reason BackoffLimitExceeded. The two evicted Pods were replaced without using any retries.
Exercise
You add a podFailurePolicy to a Job whose template uses restartPolicy: OnFailure. What happens, and how do you fix it?
Show answer
The API server rejects the Job, because podFailurePolicy requires restartPolicy: Never. Change the template to restartPolicy: Never.