-
Couldn't load subscription status.
- Fork 15.1k
Documentation change for container restart rules #51467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -236,7 +236,26 @@ To investigate the root cause of a `CrashLoopBackOff` issue, a user can: | |||||||||
| application code. Running this container image locally or in a development | ||||||||||
| environment can help diagnose application specific issues. | ||||||||||
|
|
||||||||||
| ### Container restart policy {#restart-policy} | ||||||||||
| ### Container restarts {#restart-policy} | ||||||||||
|
|
||||||||||
| When a container in your Pod stops, or experiences failure, Kubernetes can restart it. | ||||||||||
| A restart isn't always appropriate; for example, | ||||||||||
| {{< glossary_tooltip text="init containers" term_id="init-container" >}} run only once, | ||||||||||
| during Pod startup. | ||||||||||
| <!-- TODO reword when ContainerRestartRules graduates --> | ||||||||||
| You can configure restarts as a policy that applies to all Pods, or using container-level configuration (for example: when you define a | ||||||||||
| {{< glossary_tooltip text="sidecar container" term_id="sidecar-container" >}}). | ||||||||||
|
Comment on lines
+246
to
+247
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| #### Container restarts and resilience {#container-restart-resilience} | ||||||||||
|
|
||||||||||
| The Kubernetes project recommends following cloud-native principles, including resilient | ||||||||||
| design that accounts for unannounced or arbitrary restarts. You can achieve this either | ||||||||||
| by failing the Pod and relying on automatic | ||||||||||
| [replacement](/docs/concepts/workloads/controllers/), or you can design for container-level resilience. | ||||||||||
| Either approach helps to ensure that your overall workload remains available despite | ||||||||||
| partial failure. | ||||||||||
|
|
||||||||||
| #### Pod-level container restart policy | ||||||||||
|
|
||||||||||
| The `spec` of a Pod has a `restartPolicy` field with possible values Always, OnFailure, | ||||||||||
| and Never. The default value is Always. | ||||||||||
|
|
@@ -262,6 +281,104 @@ problems, the kubelet resets the restart backoff timer for that container. | |||||||||
| [Sidecar containers and Pod lifecycle](/docs/concepts/workloads/pods/sidecar-containers/#sidecar-containers-and-pod-lifecycle) | ||||||||||
| explains the behaviour of `init containers` when specify `restartpolicy` field on it. | ||||||||||
|
|
||||||||||
yuanwang04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| #### Individual container restart policy and rules {#container-restart-rules} | ||||||||||
|
|
||||||||||
| {{< feature-state | ||||||||||
| feature_gate_name="ContainerRestartRules" >}} | ||||||||||
|
Comment on lines
+286
to
+287
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Put the shortcode onto a single line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally yes |
||||||||||
|
|
||||||||||
| If your cluster has the feature gate `ContainerRestartRules` enabled, you can specify | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We really recommend hyperlinking the phrase "feature gates" to the list of feature gates. |
||||||||||
| `restartPolicy` and `restartPolicyRules` on _inidividual containers_ to override the Pod | ||||||||||
| restart policy. Container restart policy and rules applies to {{< glossary_tooltip text="app containers" term_id="app-container" >}} | ||||||||||
| in the Pod and to regular [init containers](/docs/concepts/workloads/pods/init-containers/). | ||||||||||
|
|
||||||||||
| A Kubernetes-native [sidecar container](/docs/concepts/workloads/pods/sidecar-containers/) | ||||||||||
| has its container-level `restartPolicy` set to `Always`, and does not support `restartPolicyRules`. | ||||||||||
|
|
||||||||||
| The container restarts will follow the same exponential backoff as pod restart policy described above. | ||||||||||
| Supported container restart policies: | ||||||||||
|
|
||||||||||
| * `Always`: Automatically restarts the container after any termination. | ||||||||||
| * `OnFailure`: Only restarts the container if it exits with an error (non-zero exit status). | ||||||||||
| * `Never`: Does not automatically restart the terminated container. | ||||||||||
|
|
||||||||||
| Additionally, _individual containers_ can specify `restartPolicyRules`. If the `restartPolicyRules` | ||||||||||
| field is specified, then container `restartPolicy` **must** also be specified. The `restartPolicyRules` | ||||||||||
| define a list of rules to apply on container exit. Each rule will consist of a condition | ||||||||||
| and an action. The supported condition is `exitCodes`, which compares the exit code of the container | ||||||||||
| with a list of given values. The supported action is `Restart`, which means the container will be | ||||||||||
| restarted. The rules will be evaluated in order. On the first match, the action will be applied. | ||||||||||
| If none of the rules’ conditions matched, Kubernetes fallback to container’s configured | ||||||||||
| `restartPolicy`. | ||||||||||
|
|
||||||||||
| For example, a Pod with OnFailure restart policy that have a `try-once` container. This allows | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| Pod to only restart certain containers: | ||||||||||
|
|
||||||||||
| ```yaml | ||||||||||
| apiVersion: v1 | ||||||||||
| kind: Pod | ||||||||||
| metadata: | ||||||||||
| name: on-failure-pod | ||||||||||
| spec: | ||||||||||
| restartPolicy: OnFailure | ||||||||||
| containers: | ||||||||||
| - name: try-once-container # This container will run only once because the restartPolicy is Never. | ||||||||||
| image: docker.io/library/busybox:1.28 | ||||||||||
| command: ['sh', '-c', 'echo "Only running once" && sleep 10 && exit 1'] | ||||||||||
| restartPolicy: Never | ||||||||||
| - name: on-failure-container # This container will be restarted on failure. | ||||||||||
| image: docker.io/library/busybox:1.28 | ||||||||||
| command: ['sh', '-c', 'echo "Keep restarting" && sleep 1800 && exit 1'] | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| A Pod with Always restart policy with an init container that only execute once. If the init | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Use a consistent style for those values |
||||||||||
| container fails, the Pod fails. This alllows the Pod to fail if the initialiaztion failed, | ||||||||||
| but also keep running once the initialization succeeds: | ||||||||||
|
|
||||||||||
| ```yaml | ||||||||||
| apiVersion: v1 | ||||||||||
| kind: Pod | ||||||||||
| metadata: | ||||||||||
| name: fail-pod-if-init-fails | ||||||||||
| spec: | ||||||||||
| restartPolicy: Always | ||||||||||
| initContainers: | ||||||||||
| - name: init-once # This init container will only try once. If it fails, the pod will fail. | ||||||||||
| image: docker.io/library/busybox:1.28 | ||||||||||
| command: ['sh', '-c', 'echo "Failing initialization" && sleep 10 && exit 1'] | ||||||||||
| restartPolicy: Never | ||||||||||
| containers: | ||||||||||
| - name: main-container # This container will always be restarted once initialization succeeds. | ||||||||||
| image: docker.io/library/busybox:1.28 | ||||||||||
| command: ['sh', '-c', 'sleep 1800 && exit 0'] | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| A Pod with Never restart policy with a container that ignores and restarts on specific exit codes. | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| This is useful to differentiate between restartable errors and non-restartable errors: | ||||||||||
|
|
||||||||||
| ```yaml | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this example is very synthetic. I do now know if it is useful to include, it looks more like a developer documentation, not the end user documentation. I would suggest to add a few "real life" examples where those overrides will be useful.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about a Pod with a true sidecar and a "lazy init" container that runs to completion during app container startup, writing out a config that the app container hot reloads when available? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added some suggested examples. Let me know would these be enough. |
||||||||||
| apiVersion: v1 | ||||||||||
| kind: Pod | ||||||||||
| metadata: | ||||||||||
| name: restart-on-exit-codes | ||||||||||
| spec: | ||||||||||
| restartPolicy: Never | ||||||||||
| containers: | ||||||||||
| - name: restart-on-exit-codes | ||||||||||
| image: docker.io/library/busybox:1.28 | ||||||||||
| command: ['sh', '-c', 'sleep 60 && exit 0'] | ||||||||||
| restartPolicy: Never # Container restart policy must be specified if rules are specified | ||||||||||
| restartPolicyRules: # Only restart the container if it exits with code 42 | ||||||||||
| - action: Restart | ||||||||||
| exitCodes: | ||||||||||
| operator: In | ||||||||||
| values: [42] | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
yuanwang04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| Restart rules can be used for many more advanced lifecycle management scenarios. Note, restart rules | ||||||||||
| are affected by the same inconsistencies as the regular restart policy. Kubelet restarts, container | ||||||||||
| runtime garbage collection, intermitted connectivity issues with the control plane may cause the state | ||||||||||
| loss and containers may be re-run even when you expect a container not to be restarted. | ||||||||||
|
|
||||||||||
| ### Reduced container restart delay | ||||||||||
|
|
||||||||||
| {{< feature-state | ||||||||||
|
|
||||||||||
yuanwang04 marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| title: ContainerRestartRules | ||
| content_type: feature_gate | ||
| _build: | ||
| list: never | ||
| render: false | ||
|
|
||
| stages: | ||
| - stage: alpha | ||
| defaultValue: false | ||
| fromVersion: "1.34" | ||
| --- | ||
| Enables the ability to configure container-level restart policy and restart rules. | ||
| See [Container Restart Policy and Rules](/docs/concepts/workloads/pods/pod-lifecycle/#container-restart-rules) for more details. |
Uh oh!
There was an error while loading. Please reload this page.