|
| 1 | +--- |
| 2 | +title: 边车容器 |
| 3 | +content_type: concept |
| 4 | +weight: 50 |
| 5 | +--- |
| 6 | +<!-- |
| 7 | +title: Sidecar Containers |
| 8 | +content_type: concept |
| 9 | +weight: 50 |
| 10 | +--> |
| 11 | + |
| 12 | +<!-- overview --> |
| 13 | +{{< feature-state for_k8s_version="v1.28" state="alpha" >}} |
| 14 | + |
| 15 | +<!-- |
| 16 | +Sidecar containers are the secondary containers that run along with the main |
| 17 | +application container within the same {{< glossary_tooltip text="Pod" term_id="pod" >}}. |
| 18 | +These containers are used to enhance or to extend the functionality of the main application |
| 19 | +container by providing additional services, or functionality such as logging, monitoring, |
| 20 | +security, or data synchronization, without directly altering the primary application code. |
| 21 | +--> |
| 22 | +边车容器是与主应用容器在同一个 {{< glossary_tooltip text="Pod" term_id="pod" >}} 中运行的辅助容器。 |
| 23 | +这些容器通过提供额外的服务或功能(如日志记录、监控、安全性或数据同步)来增强或扩展主应用容器的功能, |
| 24 | +而无需直接修改主应用代码。 |
| 25 | + |
| 26 | +<!-- body --> |
| 27 | + |
| 28 | +<!-- |
| 29 | +## Enabling sidecar containers |
| 30 | +
|
| 31 | +Starting with Kubernetes 1.28, a |
| 32 | +[feature gate](/docs/reference/command-line-tools-reference/feature-gates/) named |
| 33 | +`SidecarContainers` allows you to specify a `restartPolicy` for containers listed in a |
| 34 | +Pod's `initContainers` field. These restartable _sidecar_ containers are independent with |
| 35 | +other [init containers](/docs/concepts/workloads/pods/init-containers/) and main |
| 36 | +application container within the same pod. These can be started, stopped, or restarted |
| 37 | +without effecting the main application container and other init containers. |
| 38 | +--> |
| 39 | +## 启用边车容器 {#enabling-sidecar-containers} |
| 40 | + |
| 41 | +从 Kubernetes 1.28 开始,一个名为 `SidecarContainers` |
| 42 | +的[特性门控](/zh-cn/docs/reference/command-line-tools-reference/feature-gates/)允许你为 |
| 43 | +Pod 的 `initContainers` 字段中列出的容器指定 `restartPolicy`。这些可重启的**边车**容器与同一 |
| 44 | +Pod 内的其他 [Init 容器](/zh-cn/docs/concepts/workloads/pods/init-containers/)及主应用容器相互独立。 |
| 45 | +边车容器可以在不影响主应用容器和其他 Init 容器的情况下启动、停止或重启。 |
| 46 | + |
| 47 | +<!-- |
| 48 | +## Sidecar containers and Pod lifecycle |
| 49 | +
|
| 50 | +If an init container is created with its `restartPolicy` set to `Always`, it will |
| 51 | +start and remain running during the entire life of the Pod. This can be helpful for |
| 52 | +running supporting services separated from the main application containers. |
| 53 | +--> |
| 54 | +## 边车容器和 Pod 生命周期 {#sidecar-containers-and-pod-lifecyle} |
| 55 | + |
| 56 | +如果创建 Init 容器时将 `restartPolicy` 设置为 `Always`, |
| 57 | +则它将在整个 Pod 的生命周期内启动并持续运行。这对于运行与主应用容器分离的支持服务非常有帮助。 |
| 58 | + |
| 59 | +<!-- |
| 60 | +If a `readinessProbe` is specified for this init container, its result will be used |
| 61 | +to determine the `ready` state of the Pod. |
| 62 | +
|
| 63 | +Since these containers are defined as init containers, they benefit from the same |
| 64 | +ordering and sequential guarantees as other init containers, allowing them to |
| 65 | +be mixed with other init containers into complex Pod initialization flows. |
| 66 | +--> |
| 67 | +如果为此 Init 容器指定了 `readinessProbe`,其结果将用于确定 Pod 的 `ready` 状态。 |
| 68 | + |
| 69 | +由于这些容器被定义为 Init 容器,所以它们享有与其他 Init 容器相同的顺序和按序执行保证, |
| 70 | +可以将它们与其他 Init 容器混合在一起,形成复杂的 Pod 初始化流程。 |
| 71 | + |
| 72 | +<!-- |
| 73 | +Compared to regular init containers, sidecars defined within `initContainers` continue to |
| 74 | +run after they have started. This is important when there is more than one entry inside |
| 75 | +`.spec.initContainers` for a Pod. After a sidecar-style init container is running (the kubelet |
| 76 | +has set the `started` status for that init container to true), the kubelet then starts the |
| 77 | +next init container from the ordered `.spec.initContainers` list. |
| 78 | +That status either becomes true because there is a process running in the |
| 79 | +container and no startup probe defined, or as a result of its `startupProbe` succeeding. |
| 80 | +--> |
| 81 | +与常规 Init 容器相比,在 `initContainers` 中定义的边车容器在启动后继续运行。 |
| 82 | +当 Pod 的 `.spec.initContainers` 中有多个条目时,这一点非常重要。 |
| 83 | +在边车风格的 Init 容器运行后(kubelet 将该 Init 容器的 `started` 状态设置为 true), |
| 84 | +kubelet 启动 `.spec.initContainers` 这一有序列表中的下一个 Init 容器。 |
| 85 | +该状态要么因为容器中有一个正在运行的进程且没有定义启动探针而变为 true, |
| 86 | +要么是其 `startupProbe` 成功而返回的结果。 |
| 87 | + |
| 88 | +<!-- |
| 89 | +Here's an example of a Deployment with two containers, one of which is a sidecar: |
| 90 | +--> |
| 91 | +以下是一个具有两个容器的 Deployment 示例,其中一个是边车: |
| 92 | + |
| 93 | +{{% code_sample language="yaml" file="application/deployment-sidecar.yaml" %}} |
| 94 | + |
| 95 | +<!-- |
| 96 | +This feature is also useful for running Jobs with sidecars, as the sidecar |
| 97 | +container will not prevent the Job from completing after the main container |
| 98 | +has finished. |
| 99 | +
|
| 100 | +Here's an example of a Job with two containers, one of which is a sidecar: |
| 101 | +--> |
| 102 | +此特性也适用于带有边车的 Job,因为边车容器在主容器完成后不会阻止 Job 的完成。 |
| 103 | + |
| 104 | +以下是一个具有两个容器的 Job 示例,其中一个是边车: |
| 105 | + |
| 106 | +{{% code_sample language="yaml" file="application/job/job-sidecar.yaml" %}} |
| 107 | + |
| 108 | +<!-- |
| 109 | +By default, this feature is not available in Kubernetes. To avail this feature, you |
| 110 | +need to enable the [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) |
| 111 | +named `SidecarContainers`. |
| 112 | +--> |
| 113 | +Kubernetes 默认不提供此特性。要使用此特性,你需要启用名为 `SidecarContainers` |
| 114 | +的[特性门控](/zh-cn/docs/reference/command-line-tools-reference/feature-gates/)。 |
| 115 | + |
| 116 | +<!-- |
| 117 | +## Differences from regular containers |
| 118 | +
|
| 119 | +Sidecar containers run alongside regular containers in the same pod. However, they do not |
| 120 | +execute the primary application logic; instead, they provide supporting functionality to |
| 121 | +the main application. |
| 122 | +--> |
| 123 | +## 与常规容器的区别 {#differences-from-regular-containers} |
| 124 | + |
| 125 | +边车容器与同一 Pod 中的常规容器并行运行。不过边车容器不执行主应用逻辑,而是为主应用提供支持功能。 |
| 126 | + |
| 127 | +<!-- |
| 128 | +Sidecar containers have their own independent lifecycles. They can be started, stopped, |
| 129 | +and restarted independently of regular containers. This means you can update, scale, or |
| 130 | +maintain sidecar containers without affecting the primary application. |
| 131 | +
|
| 132 | +Sidecar containers share the same network and storage namespaces with the primary |
| 133 | +container This co-location allows them to interact closely and share resources. |
| 134 | +--> |
| 135 | +边车容器具有独立的生命周期。它们可以独立于常规容器启动、停止和重启。 |
| 136 | +这意味着你可以更新、扩展或维护边车容器,而不影响主应用。 |
| 137 | + |
| 138 | +边车容器与主容器共享相同的网络和存储命名空间。这种共存使它们能够紧密交互并共享资源。 |
| 139 | + |
| 140 | +<!-- |
| 141 | +## Differences from init containers |
| 142 | +
|
| 143 | +Sidecar containers work alongside the main container, extending its functionality and |
| 144 | +providing additional services. |
| 145 | +--> |
| 146 | +## 与 Init 容器的区别 {#differences-from-init-containers} |
| 147 | + |
| 148 | +边车容器与主容器并行工作,扩展其功能并提供附加服务。 |
| 149 | + |
| 150 | +<!-- |
| 151 | +Sidecar containers run concurrently with the main application container. They are active |
| 152 | +throughout the lifecycle of the pod and can be started and stopped independently of the |
| 153 | +main container. Unlike [init containers](/docs/concepts/workloads/pods/init-containers/), |
| 154 | +sidecar containers support [probes](/docs/concepts/workloads/pods/pod-lifecycle/#types-of-probe) to control their lifecycle. |
| 155 | +--> |
| 156 | +边车容器与主应用容器同时运行。它们在整个 Pod 的生命周期中都处于活动状态,并且可以独立于主容器启动和停止。 |
| 157 | +与 [Init 容器](/zh-cn/docs/concepts/workloads/pods/init-containers/)不同, |
| 158 | +边车容器支持[探针](/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#types-of-probe)来控制其生命周期。 |
| 159 | + |
| 160 | +<!-- |
| 161 | +These containers can interact directly with the main application containers, sharing |
| 162 | +the same network namespace, filesystem, and environment variables. They work closely |
| 163 | +together to provide additional functionality. |
| 164 | +
|
| 165 | +## Resource sharing within containers |
| 166 | +--> |
| 167 | +这些边车容器可以直接与主应用容器进行交互,共享相同的网络命名空间、文件系统和环境变量。 |
| 168 | +所有这些容器紧密合作,提供额外的功能。 |
| 169 | + |
| 170 | +## 容器内的资源共享 {#resource-sharing-within-containers} |
| 171 | + |
| 172 | +{{< comment >}} |
| 173 | +<!-- |
| 174 | +This section is also present in the [init containers](/docs/concepts/workloads/pods/init-containers/) page. |
| 175 | +If you're editing this section, change both places. |
| 176 | +--> |
| 177 | +这部分内容也出现在 [Init 容器](/zh-cn/docs/concepts/workloads/pods/init-containers/)页面上。 |
| 178 | +如果你正在编辑这部分内容,请同时修改两处。 |
| 179 | +{{< /comment >}} |
| 180 | + |
| 181 | +<!-- |
| 182 | +Given the order of execution for init, sidecar and app containers, the following rules |
| 183 | +for resource usage apply: |
| 184 | +--> |
| 185 | +假如执行顺序为 Init 容器、边车容器和应用容器,则关于资源用量适用以下规则: |
| 186 | + |
| 187 | +<!-- |
| 188 | +* The highest of any particular resource request or limit defined on all init |
| 189 | + containers is the *effective init request/limit*. If any resource has no |
| 190 | + resource limit specified this is considered as the highest limit. |
| 191 | +* The Pod's *effective request/limit* for a resource is the sum of |
| 192 | +[pod overhead](/docs/concepts/scheduling-eviction/pod-overhead/) and the higher of: |
| 193 | + * the sum of all non-init containers(app and sidecar containers) request/limit for a |
| 194 | + resource |
| 195 | + * the effective init request/limit for a resource |
| 196 | +* Scheduling is done based on effective requests/limits, which means |
| 197 | + init containers can reserve resources for initialization that are not used |
| 198 | + during the life of the Pod. |
| 199 | +* The QoS (quality of service) tier of the Pod's *effective QoS tier* is the |
| 200 | + QoS tier for all init, sidecar and app containers alike. |
| 201 | +--> |
| 202 | +* 所有 Init 容器上定义的任何特定资源的 limit 或 request 的最大值,作为 |
| 203 | + Pod **有效初始 request/limit**。 |
| 204 | + 如果任何资源没有指定资源限制,则被视为最高限制。 |
| 205 | +* Pod 对资源的 **有效 limit/request** 是如下两者中的较大者: |
| 206 | + * 所有应用容器对某个资源的 limit/request 之和 |
| 207 | + * Init 容器中对某个资源的有效 limit/request |
| 208 | +* 系统基于有效的 limit/request 完成调度,这意味着 Init 容器能够为初始化过程预留资源, |
| 209 | + 而这些资源在 Pod 的生命周期中不再被使用。 |
| 210 | +* Pod 的 **有效 QoS 级别**,对于 Init 容器和应用容器而言是相同的。 |
| 211 | + |
| 212 | +<!-- |
| 213 | +Quota and limits are applied based on the effective Pod request and |
| 214 | +limit. |
| 215 | +
|
| 216 | +Pod level control groups (cgroups) are based on the effective Pod request and |
| 217 | +limit, the same as the scheduler. |
| 218 | +--> |
| 219 | +配额和限制适用于 Pod 的有效请求和限制值。 |
| 220 | + |
| 221 | +Pod 级别的 cgroup 是基于 Pod 的有效请求和限制值,与调度器相同。 |
| 222 | + |
| 223 | +## {{% heading "whatsnext" %}} |
| 224 | + |
| 225 | +<!-- |
| 226 | +* Read a blog post on [native sidecar containers](/blog/2023/08/25/native-sidecar-containers/). |
| 227 | +* Read about [creating a Pod that has an init container](/docs/tasks/configure-pod-container/configure-pod-initialization/#create-a-pod-that-has-an-init-container). |
| 228 | +* Learn about the [types of probes](/docs/concepts/workloads/pods/pod-lifecycle/#types-of-probe): liveness, readiness, startup probe. |
| 229 | +* Learn about [pod overhead](/docs/concepts/scheduling-eviction/pod-overhead/). |
| 230 | +--> |
| 231 | +* 阅读关于[原生边车容器](/zh-cn/blog/2023/08/25/native-sidecar-containers/)的博文。 |
| 232 | +* 阅读[如何创建具有 Init 容器的 Pod](/zh-cn/docs/tasks/configure-pod-container/configure-pod-initialization/#create-a-pod-that-has-an-init-container)。 |
| 233 | +* 了解[探针类型](/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#types-of-probe): |
| 234 | + 存活态探针、就绪态探针、启动探针。 |
| 235 | +* 了解 [Pod 开销](/zh-cn/docs/concepts/scheduling-eviction/pod-overhead/)。 |
0 commit comments