@@ -459,7 +459,44 @@ To investigate the root cause of a `CrashLoopBackOff` issue, a user can:
459
459
在本地或开发环境中运行此容器镜像有助于诊断应用程序的特定问题。
460
460
461
461
<!--
462
- ### Container restart policy {#restart-policy}
462
+ ### Container restarts {#restart-policy}
463
+
464
+ When a container in your Pod stops, or experiences failure, Kubernetes can restart it.
465
+ A restart isn't always appropriate; for example,
466
+ {{< glossary_tooltip text="init containers" term_id="init-container" >}} run only once,
467
+ during Pod startup.
468
+ -->
469
+ ### 容器重启 {#restart-policy}
470
+
471
+ 当 Pod 中的某个容器停止或发生故障时,Kubernetes 可以重新启动此容器。但重启并不总是合适的;例如,
472
+ {{< glossary_tooltip text="Init 容器" term_id="init-container" >}}只在 Pod 启动期间运行一次。
473
+ <!-- TODO reword when ContainerRestartRules graduates -->
474
+ <!--
475
+ You can configure restarts as a policy that applies to all Pods, or using container-level configuration (for example: when you define a
476
+ {{< glossary_tooltip text="sidecar container" term_id="sidecar-container" >}}).
477
+ -->
478
+ 你可以将重启配置为适用于所有 Pod 的策略,或者使用容器级别的配置(例如:
479
+ 在你定义{{< glossary_tooltip text="边车容器" term_id="sidecar-container" >}}时)。
480
+
481
+ <!--
482
+ #### Container restarts and resilience {#container-restart-resilience}
483
+
484
+ The Kubernetes project recommends following cloud-native principles, including resilient
485
+ design that accounts for unannounced or arbitrary restarts. You can achieve this either
486
+ by failing the Pod and relying on automatic
487
+ [replacement](/docs/concepts/workloads/controllers/), or you can design for container-level resilience.
488
+ Either approach helps to ensure that your overall workload remains available despite
489
+ partial failure.
490
+ -->
491
+ #### 容器重启与弹性 {#container-restart-resilience}
492
+
493
+ Kubernetes 项目建议遵循云原生原则,包括能够应对未预告或随意重启的弹性设计。
494
+ 你可以通过让 Pod 失败并依赖自动[ 替换] ( /zh-cn/docs/concepts/workloads/controllers/ ) ,
495
+ 或者通过容器级别的弹性设计来实现。
496
+ 无论哪种方式,都有助于确保即使在部分故障的情况下,你的整体工作负载依然保持可用。
497
+
498
+ <!--
499
+ #### Pod-level container restart policy
463
500
464
501
The `spec` of a Pod has a `restartPolicy` field with possible values Always, OnFailure,
465
502
and Never. The default value is Always.
@@ -476,7 +513,7 @@ the Pod level `restartPolicy` is either `OnFailure` or `Always`.
476
513
* `OnFailure`: Only restarts the container if it exits with an error (non-zero exit status).
477
514
* `Never`: Does not automatically restart the terminated container.
478
515
-->
479
- ### 容器重启策略 {#restart-policy}
516
+ ### Pod 级别容器重启策略
480
517
481
518
Pod 的 ` spec ` 中包含一个 ` restartPolicy ` 字段,其可能取值包括
482
519
Always、OnFailure 和 Never。默认值是 Always。
@@ -512,6 +549,160 @@ kubelet 就会重置该容器的重启延迟计时器。
512
549
[ Sidecar 容器和 Pod 生命周期] ( /zh-cn/docs/concepts/workloads/pods/sidecar-containers/#sidecar-containers-and-pod-lifecycle ) 中解释了
513
550
` init containers ` 在指定 ` restartpolicy ` 字段时的行为。
514
551
552
+ <!--
553
+ #### Individual container restart policy and rules {#container-restart-rules}
554
+ -->
555
+ #### 单个容器的重启策略与规则 {#container-restart-rules}
556
+
557
+ {{< feature-state feature_gate_name="ContainerRestartRules" >}}
558
+
559
+ <!--
560
+ If your cluster has the feature gate `ContainerRestartRules` enabled, you can specify
561
+ `restartPolicy` and `restartPolicyRules` on _inidividual containers_ to override the Pod
562
+ restart policy. Container restart policy and rules applies to {{< glossary_tooltip text="app containers" term_id="app-container" >}}
563
+ in the Pod and to regular [init containers](/docs/concepts/workloads/pods/init-containers/).
564
+
565
+ A Kubernetes-native [sidecar container](/docs/concepts/workloads/pods/sidecar-containers/)
566
+ has its container-level `restartPolicy` set to `Always`, and does not support `restartPolicyRules`.
567
+ -->
568
+ 如果你的集群启用了 ` ContainerRestartRules ` 特性门控,你可以针对** 单个容器** 指定
569
+ ` restartPolicy ` 和 ` restartPolicyRules ` 来覆盖 Pod 重启策略。容器重启策略和规则适用于 Pod
570
+ 中的{{< glossary_tooltip text="应用容器" term_id="app-container" >}}以及常规的
571
+ [ Init 容器] ( /zh-cn/docs/concepts/workloads/pods/init-containers/ ) 。
572
+
573
+ Kubernetes 原生的[ 边车容器] ( /zh-cn/docs/concepts/workloads/pods/sidecar-containers/ ) 将其容器级别的
574
+ ` restartPolicy ` 设置为 ` Always ` ,并且不支持 ` restartPolicyRules ` 。
575
+
576
+ <!--
577
+ The container restarts will follow the same exponential backoff as pod restart policy described above.
578
+ Supported container restart policies:
579
+
580
+ * `Always`: Automatically restarts the container after any termination.
581
+ * `OnFailure`: Only restarts the container if it exits with an error (non-zero exit status).
582
+ * `Never`: Does not automatically restart the terminated container.
583
+ -->
584
+ 容器重启会遵循与前文所述的 Pod 重启策略相同的指数回退机制。支持的容器重启策略有:
585
+
586
+ * ` Always ` :在任何原因的容器终止后都会自动重启容器。
587
+ * ` OnFailure ` :仅当容器因错误退出(非零退出状态)时才重启。
588
+ * ` Never ` :不自动重启已终止的容器。
589
+
590
+ <!--
591
+ Additionally, _individual containers_ can specify `restartPolicyRules`. If the `restartPolicyRules`
592
+ field is specified, then container `restartPolicy` **must** also be specified. The `restartPolicyRules`
593
+ define a list of rules to apply on container exit. Each rule will consist of a condition
594
+ and an action. The supported condition is `exitCodes`, which compares the exit code of the container
595
+ with a list of given values. The supported action is `Restart`, which means the container will be
596
+ restarted. The rules will be evaluated in order. On the first match, the action will be applied.
597
+ If none of the rules’ conditions matched, Kubernetes fallback to container’s configured
598
+ `restartPolicy`.
599
+ -->
600
+ 此外,** 单个容器** 可以指定 ` restartPolicyRules ` 。如果指定了 ` restartPolicyRules ` 字段,
601
+ 则** 必须** 同时指定容器的 ` restartPolicy ` 。` restartPolicyRules ` 定义了一系列在容器退出时应用的规则。
602
+ 每条规则由条件和动作组成。支持的条件是 ` exitCodes ` ,用于将容器的退出码与给定值列表进行比较。
603
+ 支持的动作是 ` Restart ` ,表示容器将被重启。这些规则会按顺序进行评估。一旦匹配成功,立即执行相应动作。
604
+ 如果没有任何规则的状况被匹配,Kubernetes 回退到容器配置的 ` restartPolicy ` 。
605
+
606
+ <!--
607
+ For example, a Pod with OnFailure restart policy that have a `try-once` container. This allows
608
+ Pod to only restart certain containers:
609
+ -->
610
+ 例如,重启策略为 OnFailure 的某个 Pod 包含一个 ` try-once ` 容器。
611
+ 这样可以让 Pod 仅重启某些容器:
612
+
613
+ <!--
614
+ # This container will run only once because the restartPolicy is Never.
615
+ # This container will be restarted on failure.
616
+ -->
617
+ ``` yaml
618
+ apiVersion : v1
619
+ kind : Pod
620
+ metadata :
621
+ name : on-failure-pod
622
+ spec :
623
+ restartPolicy : OnFailure
624
+ containers :
625
+ - name : try-once-container # 此容器只运行一次,因为 restartPolicy 设置为 Never。
626
+ image : docker.io/library/busybox:1.28
627
+ command : ['sh', '-c', 'echo "Only running once" && sleep 10 && exit 1']
628
+ restartPolicy : Never
629
+ - name : on-failure-container # 此容器将在失败时重启。
630
+ image : docker.io/library/busybox:1.28
631
+ command : ['sh', '-c', 'echo "Keep restarting" && sleep 1800 && exit 1']
632
+ ` ` `
633
+
634
+ <!--
635
+ A Pod with Always restart policy with an init container that only execute once. If the init
636
+ container fails, the Pod fails. This alllows the Pod to fail if the initialiaztion failed,
637
+ but also keep running once the initialization succeeds:
638
+ -->
639
+ 下面是一个重启策略为 Always 的 Pod,其中包含一个只执行一次的 Init 容器。
640
+ 如果 Init 容器失败,则 Pod 也会失败。
641
+ 这样可以在初始化失败时让 Pod 失败,但在初始化成功后保持 Pod 运行:
642
+
643
+ <!--
644
+ # This init container will only try once. If it fails, the pod will fail.
645
+ # This container will always be restarted once initialization succeeds.
646
+ -->
647
+ ` ` ` yaml
648
+ apiVersion : v1
649
+ kind : Pod
650
+ metadata :
651
+ name : fail-pod-if-init-fails
652
+ spec :
653
+ restartPolicy : Always
654
+ initContainers :
655
+ - name : init-once # 这个 Init 容器只尝试一次。如果失败,Pod 将失败。
656
+ image : docker.io/library/busybox:1.28
657
+ command : ['sh', '-c', 'echo "Failing initialization" && sleep 10 && exit 1']
658
+ restartPolicy : Never
659
+ containers :
660
+ - name : main-container # 一旦初始化成功,此容器会始终被重启。
661
+ image : docker.io/library/busybox:1.28
662
+ command : ['sh', '-c', 'sleep 1800 && exit 0']
663
+ ` ` `
664
+
665
+ <!--
666
+ A Pod with Never restart policy with a container that ignores and restarts on specific exit codes.
667
+ This is useful to differentiate between restartable errors and non-restartable errors:
668
+ -->
669
+ 下面是一个重启策略为 Never 的 Pod,其中包含的容器会在遇到特定的退出码时忽略之并重启。
670
+ 这种配置有助于区分可重启错误和不可重启错误:
671
+
672
+ <!--
673
+ # Container restart policy must be specified if rules are specified
674
+ # Only restart the container if it exits with code 42
675
+ -->
676
+ ` ` ` yaml
677
+ apiVersion : v1
678
+ kind : Pod
679
+ metadata :
680
+ name : restart-on-exit-codes
681
+ spec :
682
+ restartPolicy : Never
683
+ containers :
684
+ - name : restart-on-exit-codes
685
+ image : docker.io/library/busybox:1.28
686
+ command : ['sh', '-c', 'sleep 60 && exit 0']
687
+ restartPolicy : Never # 如果指定了规则,必须同时指定容器重启策略
688
+ restartPolicyRules : # 仅当退出码为 42 时才重启容器
689
+ - action : Restart
690
+ exitCodes :
691
+ operator : In
692
+ values : [42]
693
+ ` ` `
694
+
695
+ <!--
696
+ Restart rules can be used for many more advanced lifecycle management scenarios. Note, restart rules
697
+ are affected by the same inconsistencies as the regular restart policy. Kubelet restarts, container
698
+ runtime garbage collection, intermitted connectivity issues with the control plane may cause the state
699
+ loss and containers may be re-run even when you expect a container not to be restarted.
700
+ -->
701
+ 重启规则可用于许多其他高级的生命周期管理场景。
702
+ 需要注意的是,重启规则会受到不一致性影响,这一点上与常规的重启策略相同。
703
+ kubelet 重启、容器运行时垃圾收集、与控制平面的间歇性连接问题都可能导致状态丢失,
704
+ 容器可能会在你预期不应被重启的情况下被再次运行。
705
+
515
706
<!--
516
707
### Reduced container restart delay
517
708
0 commit comments