|
1 | 1 | ---
|
2 | 2 | title: "服务、负载均衡和联网"
|
3 | 3 | weight: 60
|
4 |
| -description: Kubernetes 网络背后的概念和资源。 |
| 4 | +description: > |
| 5 | + Kubernetes 网络背后的概念和资源。 |
5 | 6 | ---
|
| 7 | +<!-- |
| 8 | +title: "Services, Load Balancing, and Networking" |
| 9 | +weight: 60 |
| 10 | +description: > |
| 11 | + Concepts and resources behind networking in Kubernetes. |
| 12 | +--> |
6 | 13 |
|
7 | 14 | <!--
|
8 | 15 | ## The Kubernetes network model
|
9 | 16 |
|
10 |
| -Every [`Pod`](/docs/concepts/workloads/pods/) in a cluster gets its own unique cluster-wide IP address |
11 |
| -(one address per IP address family). |
12 |
| -This means you do not need to explicitly create links between `Pods` and you |
13 |
| -almost never need to deal with mapping container ports to host ports. |
14 |
| -This creates a clean, backwards-compatible model where `Pods` can be treated |
15 |
| -much like VMs or physical hosts from the perspectives of port allocation, |
16 |
| -naming, service discovery, [load balancing](/docs/concepts/services-networking/ingress/#load-balancing), |
17 |
| -application configuration, and migration. |
| 17 | +The Kubernetes network model is built out of several pieces: |
| 18 | +
|
| 19 | +* Each [pod](/docs/concepts/workloads/pods/) in a cluster gets its |
| 20 | + own unique cluster-wide IP address. |
| 21 | +
|
| 22 | + * A pod has its own private network namespace which is shared by |
| 23 | + all of the containers within the pod. Processes running in |
| 24 | + different containers in the same pod can communicate with each |
| 25 | + other over `localhost`. |
18 | 26 | -->
|
19 | 27 | ## Kubernetes 网络模型 {#the-kubernetes-network-model}
|
20 | 28 |
|
21 |
| -集群中每一个 [`Pod`](/zh-cn/docs/concepts/workloads/pods/) 都会获得自己的、 |
22 |
| -独一无二的 IP 地址(每个 IP 地址族一个地址), |
23 |
| -这就意味着你不需要显式地在 `Pod` 之间创建链接,你几乎不需要处理容器端口到主机端口之间的映射。 |
24 |
| -这将形成一个干净的、向后兼容的模型;在这个模型里,从端口分配、命名、服务发现、 |
25 |
| -[负载均衡](/zh-cn/docs/concepts/services-networking/ingress/#load-balancing)、 |
26 |
| -应用配置和迁移的角度来看,`Pod` 可以被视作虚拟机或者物理主机。 |
| 29 | +Kubernetes 网络模型由几个部分构成: |
| 30 | + |
| 31 | +* 集群中的每个 [Pod](/zh-cn/docs/concepts/workloads/pods/) |
| 32 | + 都会获得自己的、独一无二的集群范围 IP 地址。 |
| 33 | + |
| 34 | + * Pod 有自己的私有网络命名空间,Pod 内的所有容器共享这个命名空间。 |
| 35 | + 运行在同一个 Pod 中的不同容器的进程彼此之间可以通过 `localhost` 进行通信。 |
| 36 | + |
| 37 | +<!-- |
| 38 | +* The _pod network_ (also called a cluster network) handles communication |
| 39 | + between pods. It ensures that (barring intentional network segmentation): |
| 40 | +
|
| 41 | + * All pods can communicate with all other pods, whether they are |
| 42 | + on the same [node](/docs/concepts/architecture/nodes/) or on |
| 43 | + different nodes. Pods can communicate with each other |
| 44 | + directly, without the use of proxies or address translation (NAT). |
| 45 | +
|
| 46 | + On Windows, this rule does not apply to host-network pods. |
| 47 | +
|
| 48 | + * Agents on a node (such as system daemons, or kubelet) can |
| 49 | + communicate with all pods on that node. |
| 50 | +--> |
| 51 | +* **Pod 网络**(也称为集群网络)处理 Pod 之间的通信。它确保(除非故意进行网络分段): |
| 52 | + |
| 53 | + * 所有 Pod 可以与所有其他 Pod 进行通信, |
| 54 | + 无论它们是在同一个[节点](/zh-cn/docs/concepts/architecture/nodes/)还是在不同的节点上。 |
| 55 | + Pod 可以直接相互通信,而无需使用代理或地址转换(NAT)。 |
| 56 | + |
| 57 | + 在 Windows 上,这条规则不适用于主机网络 Pod。 |
| 58 | + |
| 59 | + * 节点上的代理(例如系统守护进程或 kubelet)可以与该节点上的所有 Pod 进行通信。 |
27 | 60 |
|
28 | 61 | <!--
|
29 |
| -Kubernetes imposes the following fundamental requirements on any networking |
30 |
| -implementation (barring any intentional network segmentation policies): |
| 62 | +* The [Service](/docs/concepts/services-networking/service/) API |
| 63 | + lets you provide a stable (long lived) IP address or hostname for a service implemented |
| 64 | + by one or more backend pods, where the individual pods making up |
| 65 | + the service can change over time. |
| 66 | +
|
| 67 | + * Kubernetes automatically manages |
| 68 | + [EndpointSlice](/docs/concepts/services-networking/endpoint-slices/) |
| 69 | + objects to provide information about the pods currently backing a Service. |
| 70 | +
|
| 71 | + * A service proxy implementation monitors the set of Service and |
| 72 | + EndpointSlice objects, and programs the data plane to route |
| 73 | + service traffic to its backends, by using operating system or |
| 74 | + cloud provider APIs to intercept or rewrite packets. |
31 | 75 | -->
|
32 |
| -Kubernetes 强制要求所有网络设施都满足以下基本要求(从而排除了有意隔离网络的策略): |
| 76 | +* [Service](/zh-cn/docs/concepts/services-networking/service/) API |
| 77 | + 允许你为由一个或多个后端 Pod 实现的服务提供一个稳定(长效)的 IP 地址或主机名, |
| 78 | + 其中组成服务的各个 Pod 可以随时变化。 |
| 79 | + |
| 80 | + * Kubernetes 会自动管理 |
| 81 | + [EndpointSlice](/zh-cn/docs/concepts/services-networking/endpoint-slices/) |
| 82 | + 对象,以提供有关当前用来提供 Service 的 Pod 的信息。 |
| 83 | + |
| 84 | + * 服务代理实现通过使用操作系统或云平台 API 来拦截或重写数据包, |
| 85 | + 监视 Service 和 EndpointSlice 对象集,并在数据平面编程将服务流量路由到其后端。 |
33 | 86 |
|
34 | 87 | <!--
|
35 |
| -* pods can communicate with all other pods on any other [node](/docs/concepts/architecture/nodes/) |
36 |
| - without NAT |
37 |
| -* agents on a node (e.g. system daemons, kubelet) can communicate with all |
38 |
| - pods on that node |
| 88 | +* The [Gateway](/docs/concepts/services-networking/gateway/) API |
| 89 | + (or its predecessor, [Ingress](/docs/concepts/services-networking/ingress/)) |
| 90 | + allows you to make Services accessible to clients that are outside the cluster. |
| 91 | +
|
| 92 | + * A simpler, but less-configurable, mechanism for cluster |
| 93 | + ingress is available via the Service API's |
| 94 | + [`type: LoadBalancer`](/docs/concepts/services-networking/service/#loadbalancer), |
| 95 | + when using a supported {{< glossary_tooltip term_id="cloud-provider">}}. |
| 96 | +
|
| 97 | +* [NetworkPolicy](/docs/concepts/services-networking/network-policies) is a built-in |
| 98 | + Kubernetes API that allows you to control traffic between pods, or between pods and |
| 99 | + the outside world. |
39 | 100 | -->
|
40 |
| -* Pod 能够与所有其他[节点](/zh-cn/docs/concepts/architecture/nodes/)上的 Pod 通信, |
41 |
| - 且不需要网络地址转译(NAT) |
42 |
| -* 节点上的代理(比如:系统守护进程、kubelet)可以和节点上的所有 Pod 通信 |
| 101 | +* [Gateway](/zh-cn/docs/concepts/services-networking/gateway/) API |
| 102 | + (或其前身 [Ingress](/zh-cn/docs/concepts/services-networking/ingress/) |
| 103 | + 使得集群外部的客户端能够访问 Service。 |
| 104 | + |
| 105 | + * 当使用受支持的 {{< glossary_tooltip term_id="cloud-provider">}} 时,通过 Service API 的 |
| 106 | + [`type: LoadBalancer`](/zh-cn/docs/concepts/services-networking/service/#loadbalancer) |
| 107 | + 可以使用一种更简单但可配置性较低的集群 Ingress 机制。 |
| 108 | + |
| 109 | +* [NetworkPolicy](/zh-cn/docs/concepts/services-networking/network-policies) |
| 110 | + 是一个内置的 Kubernetes API,允许你控制 Pod 之间的流量或 Pod 与外部世界之间的流量。 |
43 | 111 |
|
44 | 112 | <!--
|
45 |
| -For those platforms that support `Pods` running in the host network (such as Linux), when pods are attached to the host network of a node they can still communicate with all pods on all nodes without NAT. |
| 113 | +In older container systems, there was no automatic connectivity |
| 114 | +between containers on different hosts, and so it was often necessary |
| 115 | +to explicitly create links between containers, or to map container |
| 116 | +ports to host ports to make them reachable by containers on other |
| 117 | +hosts. This is not needed in Kubernetes; Kubernetes's model is that |
| 118 | +pods can be treated much like VMs or physical hosts from the |
| 119 | +perspectives of port allocation, naming, service discovery, load |
| 120 | +balancing, application configuration, and migration. |
46 | 121 | -->
|
47 |
| -对于支持在主机网络中运行 `Pod` 的平台(比如:Linux), |
48 |
| -当 Pod 挂接到节点的宿主网络上时,它们仍可以不通过 NAT 和所有节点上的 Pod 通信。 |
| 122 | +在早期的容器系统中,不同主机上的容器之间没有自动连通, |
| 123 | +因此通常需要显式创建容器之间的链路,或将容器端口映射到主机端口,以便其他主机上的容器能够访问。 |
| 124 | +在 Kubernetes 中并不需要如此操作;在 Kubernetes 的网络模型中, |
| 125 | +从端口分配、命名、服务发现、负载均衡、应用配置和迁移的角度来看,Pod 可以被视作虚拟机或物理主机。 |
49 | 126 |
|
50 | 127 | <!--
|
51 |
| -This model is not only less complex overall, but it is principally compatible |
52 |
| -with the desire for Kubernetes to enable low-friction porting of apps from VMs |
53 |
| -to containers. If your job previously ran in a VM, your VM had an IP and could |
54 |
| -talk to other VMs in your project. This is the same basic model. |
55 |
| -
|
56 |
| -Kubernetes IP addresses exist at the `Pod` scope - containers within a `Pod` |
57 |
| -share their network namespaces - including their IP address and MAC address. |
58 |
| -This means that containers within a `Pod` can all reach each other's ports on |
59 |
| -`localhost`. This also means that containers within a `Pod` must coordinate port |
60 |
| -usage, but this is no different from processes in a VM. This is called the |
61 |
| -"IP-per-pod" model. |
| 128 | +Only a few parts of this model are implemented by Kubernetes itself. |
| 129 | +For the other parts, Kubernetes defines the APIs, but the |
| 130 | +corresponding functionality is provided by external components, some |
| 131 | +of which are optional: |
| 132 | +
|
| 133 | +* Pod network namespace setup is handled by system-level software implementing the |
| 134 | + [Container Runtime Interface](/docs/concepts/architecture/cri.md). |
62 | 135 | -->
|
63 |
| -这个模型不仅不复杂,而且还和 Kubernetes 的实现从虚拟机向容器平滑迁移的初衷相符, |
64 |
| -如果你的任务开始是在虚拟机中运行的,你的虚拟机有一个 IP, |
65 |
| -可以和项目中其他虚拟机通信。这里的模型是基本相同的。 |
| 136 | +这个模型只有少部分是由 Kubernetes 自身实现的。 |
| 137 | +对于其他部分,Kubernetes 定义 API,但相应的功能由外部组件提供,其中一些是可选的: |
66 | 138 |
|
67 |
| -Kubernetes 的 IP 地址存在于 `Pod` 范围内 —— 容器共享它们的网络命名空间 —— |
68 |
| -包括它们的 IP 地址和 MAC 地址。 |
69 |
| -这就意味着 `Pod` 内的容器都可以通过 `localhost` 到达对方端口。 |
70 |
| -这也意味着 `Pod` 内的容器需要相互协调端口的使用,但是这和虚拟机中的进程似乎没有什么不同, |
71 |
| -这也被称为“一个 Pod 一个 IP”模型。 |
| 139 | +* Pod 网络命名空间的设置由实现[容器运行时接口(CRI)](/zh-cn/docs/concepts/architecture/cri.md)的系统层面软件处理。 |
72 | 140 |
|
73 | 141 | <!--
|
74 |
| -How this is implemented is a detail of the particular container runtime in use. |
75 |
| -
|
76 |
| -It is possible to request ports on the `Node` itself which forward to your `Pod` |
77 |
| -(called host ports), but this is a very niche operation. How that forwarding is |
78 |
| -implemented is also a detail of the container runtime. The `Pod` itself is |
79 |
| -blind to the existence or non-existence of host ports. |
| 142 | +* The pod network itself is managed by a |
| 143 | + [pod network implementation](/docs/concepts/cluster-administration/addons/#networking-and-network-policy). |
| 144 | + On Linux, most container runtimes use the |
| 145 | + {{< glossary_tooltip text="Container Networking Interface (CNI)" term_id="cni" >}} |
| 146 | + to interact with the pod network implementation, so these |
| 147 | + implementations are often called _CNI plugins_. |
| 148 | +
|
| 149 | +* Kubernetes provides a default implementation of service proxying, |
| 150 | + called {{< glossary_tooltip term_id="kube-proxy">}}, but some pod |
| 151 | + network implementations instead use their own service proxy that |
| 152 | + is more tightly integrated with the rest of the implementation. |
80 | 153 | -->
|
81 |
| -如何实现以上需求是所使用的特定容器运行时的细节。 |
| 154 | +* Pod 网络本身由 |
| 155 | + [Pod 网络实现](/zh-cn/docs/concepts/cluster-administration/addons/#networking-and-network-policy)管理。 |
| 156 | + 在 Linux 上,大多数容器运行时使用{{< glossary_tooltip text="容器网络接口 (CNI)" term_id="cni" >}} |
| 157 | + 与 Pod 网络实现进行交互,因此这些实现通常被称为 **CNI 插件**。 |
82 | 158 |
|
83 |
| -也可以在 `Node` 本身请求端口,并用这类端口转发到你的 `Pod`(称之为主机端口), |
84 |
| -但这是一个很特殊的操作。转发方式如何实现也是容器运行时的细节。 |
85 |
| -`Pod` 自己并不知道这些主机端口的存在。 |
| 159 | +* Kubernetes 提供了一个默认的服务代理实现,称为 {{< glossary_tooltip term_id="kube-proxy">}}, |
| 160 | + 但某些 Pod 网络实现使用其自己的服务代理,以便与实现的其余组件集成得更紧密。 |
86 | 161 |
|
87 | 162 | <!--
|
88 |
| -Kubernetes networking addresses four concerns: |
89 |
| -- Containers within a Pod [use networking to communicate](/docs/concepts/services-networking/dns-pod-service/) via loopback. |
90 |
| -- Cluster networking provides communication between different Pods. |
91 |
| -- The [Service](/docs/concepts/services-networking/service/) API lets you |
92 |
| - [expose an application running in Pods](/docs/tutorials/services/connect-applications-service/) |
93 |
| - to be reachable from outside your cluster. |
94 |
| - - [Ingress](/docs/concepts/services-networking/ingress/) provides extra functionality |
95 |
| - specifically for exposing HTTP applications, websites and APIs. |
96 |
| - - [Gateway API](/docs/concepts/services-networking/gateway/) is an {{<glossary_tooltip text="add-on" term_id="addons">}} |
97 |
| - that provides an expressive, extensible, and role-oriented family of API kinds for modeling service networking. |
98 |
| -- You can also use Services to |
99 |
| - [publish services only for consumption inside your cluster](/docs/concepts/services-networking/service-traffic-policy/). |
| 163 | +* NetworkPolicy is generally also implemented by the pod network |
| 164 | + implementation. (Some simpler pod network implementations don't |
| 165 | + implement NetworkPolicy, or an administrator may choose to |
| 166 | + configure the pod network without NetworkPolicy support. In these |
| 167 | + cases, the API will still be present, but it will have no effect.) |
| 168 | +
|
| 169 | +* There are many [implementations of the Gateway API](https://gateway-api.sigs.k8s.io/implementations/), |
| 170 | + some of which are specific to particular cloud environments, some more |
| 171 | + focused on "bare metal" environments, and others more generic. |
100 | 172 | -->
|
101 |
| -Kubernetes 网络解决四方面的问题: |
102 |
| - |
103 |
| -- 一个 Pod 中的容器之间[通过本地回路(loopback)通信](/zh-cn/docs/concepts/services-networking/dns-pod-service/)。 |
104 |
| -- 集群网络在不同 Pod 之间提供通信。 |
105 |
| -- [Service](/zh-cn/docs/concepts/services-networking/service/) API |
106 |
| - 允许你[向外暴露 Pod 中运行的应用](/zh-cn/docs/tutorials/services/connect-applications-service/), |
107 |
| - 以支持来自于集群外部的访问。 |
108 |
| - - [Ingress](/zh-cn/docs/concepts/services-networking/ingress/) |
109 |
| - 提供专门用于暴露 HTTP 应用程序、网站和 API 的额外功能。 |
110 |
| - - [Gateway API](/zh-cn/docs/concepts/services-networking/gateway/) |
111 |
| - 是一个{{<glossary_tooltip text="插件" term_id="addons">}}, |
112 |
| - 为服务网络建模提供富有表现力、可扩展和面向角色的 API 系列类别。 |
113 |
| -- 你也可以使用 Service |
114 |
| - 来[发布仅供集群内部使用的服务](/zh-cn/docs/concepts/services-networking/service-traffic-policy/)。 |
| 173 | +* NetworkPolicy 通常也由 Pod 网络实现提供支持。 |
| 174 | + (某些更简单的 Pod 网络实现不支持 NetworkPolicy,或者管理员可能会选择在不支持 NetworkPolicy |
| 175 | + 的情况下配置 Pod 网络。在这些情况下,API 仍然存在,但将没有效果。) |
| 176 | + |
| 177 | +* [Gateway API 的实现](https://gateway-api.sigs.k8s.io/implementations/)有很多, |
| 178 | + 其中一些特定于某些云环境,还有一些更专注于“裸金属”环境,而其他一些则更加通用。 |
| 179 | + |
| 180 | +## {{% heading "whatsnext" %}} |
115 | 181 |
|
116 | 182 | <!--
|
117 | 183 | The [Connecting Applications with Services](/docs/tutorials/services/connect-applications-service/)
|
|
0 commit comments