Skip to content

Commit fb64460

Browse files
committed
Add documentation for container restart rules
1 parent 91cd000 commit fb64460

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

content/en/docs/concepts/workloads/pods/pod-lifecycle.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ To investigate the root cause of a `CrashLoopBackOff` issue, a user can:
236236
application code. Running this container image locally or in a development
237237
environment can help diagnose application specific issues.
238238

239-
### Container restart policy {#restart-policy}
239+
### Pod restart policy {#restart-policy}
240240

241241
The `spec` of a Pod has a `restartPolicy` field with possible values Always, OnFailure,
242242
and Never. The default value is Always.
@@ -262,6 +262,50 @@ problems, the kubelet resets the restart backoff timer for that container.
262262
[Sidecar containers and Pod lifecycle](/docs/concepts/workloads/pods/sidecar-containers/#sidecar-containers-and-pod-lifecycle)
263263
explains the behaviour of `init containers` when specify `restartpolicy` field on it.
264264

265+
#### Container Restart Policy
266+
267+
{{< feature-state
268+
feature_gate_name="ContainerRestartRules" >}}
269+
270+
With the alpha feature gate `ContainerRestartRules` enabled, containers can specify
271+
`restartPolicy` and `restartPolicyRules` to override the Pod restart policy. Container
272+
restart policy and rules applies to {{< glossary_tooltip text="app containers" term_id="app-container" >}}
273+
in the Pod and to regular [init containers](/docs/concepts/workloads/pods/init-containers/),
274+
overriding the Pod restart policy. [Sidecar containers](/docs/concepts/workloads/pods/sidecar-containers/)
275+
ignore `restartPolicyRules`. The container restarts will follow the same exponential backoff
276+
as pod restart policy described above. Supported container restart policies:
277+
278+
* `Always`: Automatically restarts the container after any termination.
279+
* `OnFailure`: Only restarts the container if it exits with an error (non-zero exit status).
280+
* `Never`: Does not automatically restart the terminated container.
281+
282+
Additionally, containers can specify `restartPolicyRules`. If the `restartPolicyRules`
283+
is specified, then container `restartPolicy` must also be specified. The `restartPolicyRules`
284+
define a list of rules to apply on container exit. Each rule will consist of a condition
285+
(onExitCodes) and an action (Restart). The rules will be evaluated in order; if none
286+
of the rules’ conditions matched, the default action will fallback to container’s `restartPolicy`.
287+
288+
To specify a container to only restart with an exit code of 42, here is an example:
289+
290+
```yaml
291+
apiVersion: v1
292+
kind: Pod
293+
metadata:
294+
name: my-pod
295+
spec:
296+
restartPolicy: OnFailure # Pod restart policy will be overriden by container restart policy
297+
containers:
298+
- name: my-container
299+
image: nginx:latest
300+
restartPolicy: Never # Overrides the pod restart policy to Never
301+
restartPolicyRules: # Only restart the container if it exits with code 42
302+
- action: Restart
303+
when:
304+
exitCodes:
305+
operator: In
306+
values: [42]
307+
```
308+
265309
### Reduced container restart delay
266310
267311
{{< feature-state

0 commit comments

Comments
 (0)