|
| 1 | +--- |
| 2 | +title: Service ClusterIP 分配 |
| 3 | +content_type: concept |
| 4 | +weight: 120 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- |
| 8 | +reviewers: |
| 9 | +- sftim |
| 10 | +- thockin |
| 11 | +title: Service ClusterIP allocation |
| 12 | +content_type: concept |
| 13 | +weight: 120 |
| 14 | +--> |
| 15 | + |
| 16 | +<!-- overview --> |
| 17 | +<!-- |
| 18 | +In Kubernetes, [Services](/docs/concepts/services-networking/service/) are an abstract way to expose |
| 19 | +an application running on a set of Pods. Services |
| 20 | +can have a cluster-scoped virtual IP address (using a Service of `type: ClusterIP`). |
| 21 | +Clients can connect using that virtual IP address, and Kubernetes then load-balances traffic to that |
| 22 | +Service across the different backing Pods. |
| 23 | +--> |
| 24 | + |
| 25 | +在 Kubernetes 中,[Service](/zh-cn/docs/concepts/services-networking/service/) 是一种抽象的方式, |
| 26 | +用于公开在一组 Pod 上运行的应用。 |
| 27 | +Service 可以具有集群作用域的虚拟 IP 地址(使用 `type: ClusterIP` 的 Service)。 |
| 28 | +客户端可以使用该虚拟 IP 地址进行连接,Kubernetes 通过不同的后台 Pod 对该 Service 的流量进行负载均衡。 |
| 29 | +<!-- body --> |
| 30 | +<!-- |
| 31 | +## How Service ClusterIPs are allocated? |
| 32 | +When Kubernetes needs to assign a virtual IP address for a Service, |
| 33 | +that assignment happens one of two ways: |
| 34 | +
|
| 35 | +_dynamically_ |
| 36 | +: the cluster's control plane automatically picks a free IP address from within the configured IP range for `type: ClusterIP` Services. |
| 37 | +
|
| 38 | +_statically_ |
| 39 | +: you specify an IP address of your choice, from within the configured IP range for Services. |
| 40 | +
|
| 41 | +Across your whole cluster, every Service `ClusterIP` must be unique. |
| 42 | +Trying to create a Service with a specific `ClusterIP` that has already |
| 43 | +been allocated will return an error. |
| 44 | +--> |
| 45 | +## Service ClusterIP 是如何分配的? |
| 46 | +当 Kubernetes 需要为 Service 分配虚拟 IP 地址时,该分配会通过以下两种方式之一进行: |
| 47 | + |
| 48 | +**动态分配** |
| 49 | +: 集群的控制面自动从所配置的 IP 范围内为 `type: ClusterIP` 选择一个空闲 IP 地址。 |
| 50 | + |
| 51 | +**静态分配** |
| 52 | +: 根据为 Service 所配置的 IP 范围,选定并设置你的 IP 地址。 |
| 53 | + |
| 54 | +在整个集群中,每个 Service 的 `ClusterIP` 都必须是唯一的。 |
| 55 | +尝试使用已分配的 `ClusterIP` 创建 Service 将返回错误。 |
| 56 | + |
| 57 | +<!-- |
| 58 | +## Why do you need to reserve Service Cluster IPs? |
| 59 | +Sometimes you may want to have Services running in well-known IP addresses, so other components and |
| 60 | +users in the cluster can use them. |
| 61 | +The best example is the DNS Service for the cluster. As a soft convention, some Kubernetes installers assign the 10th IP address from |
| 62 | +the Service IP range to the DNS service. Assuming you configured your cluster with Service IP range |
| 63 | +10.96.0.0/16 and you want your DNS Service IP to be 10.96.0.10, you'd have to create a Service like |
| 64 | +this: |
| 65 | +--> |
| 66 | +## 为什么需要预留 Service 的 ClusterIP ? |
| 67 | + |
| 68 | +有时你可能希望 Services 在众所周知的 IP 上面运行,以便集群中的其他组件和用户可以使用它们。 |
| 69 | + |
| 70 | +最好的例子是集群的 DNS Service。作为一种非强制性的约定,一些 Kubernetes 安装程序 |
| 71 | +将 Service IP 范围中的第 10 个 IP 地址分配给 DNS 服务。假设将集群的 Service IP 范围配置为 |
| 72 | +10.96.0.0/16,并且希望 DNS Service IP 为 10.96.0.10,则必须创建如下 Service: |
| 73 | + |
| 74 | +```yaml |
| 75 | +apiVersion: v1 |
| 76 | +kind: Service |
| 77 | +metadata: |
| 78 | + labels: |
| 79 | + k8s-app: kube-dns |
| 80 | + kubernetes.io/cluster-service: "true" |
| 81 | + kubernetes.io/name: CoreDNS |
| 82 | + name: kube-dns |
| 83 | + namespace: kube-system |
| 84 | +spec: |
| 85 | + clusterIP: 10.96.0.10 |
| 86 | + ports: |
| 87 | + - name: dns |
| 88 | + port: 53 |
| 89 | + protocol: UDP |
| 90 | + targetPort: 53 |
| 91 | + - name: dns-tcp |
| 92 | + port: 53 |
| 93 | + protocol: TCP |
| 94 | + targetPort: 53 |
| 95 | + selector: |
| 96 | + k8s-app: kube-dns |
| 97 | + type: ClusterIP |
| 98 | +``` |
| 99 | +<!-- |
| 100 | +but as it was explained before, the IP address 10.96.0.10 has not been reserved; if other Services are created |
| 101 | +before or in parallel with dynamic allocation, there is a chance they can allocate this IP, hence, |
| 102 | +you will not be able to create the DNS Service because it will fail with a conflict error. |
| 103 | +--> |
| 104 | +但如前所述,IP 地址 10.96.0.10 尚未被保留。如果在 DNS 启动之前或同时采用动态分配机制创建其他 Service, |
| 105 | +则它们有可能被分配此 IP,因此,你将无法创建 DNS Service,因为它会因冲突错误而失败。 |
| 106 | +
|
| 107 | +<!-- |
| 108 | +## How can you avoid Service ClusterIP conflicts? {#avoid-ClusterIP-conflict} |
| 109 | +The allocation strategy implemented in Kubernetes to allocate ClusterIPs to Services reduces the |
| 110 | +risk of collision. |
| 111 | +The `ClusterIP` range is divided, based on the formula `min(max(16, cidrSize / 16), 256)`, |
| 112 | +described as _never less than 16 or more than 256 with a graduated step between them_. |
| 113 | +Dynamic IP assignment uses the upper band by default, once this has been exhausted it will |
| 114 | +use the lower range. This will allow users to use static allocations on the lower band with a low |
| 115 | +risk of collision. |
| 116 | +--> |
| 117 | + |
| 118 | +## 如何避免 Service ClusterIP 冲突?{#avoid-ClusterIP-conflict} |
| 119 | + |
| 120 | +Kubernetes 中用來将 ClusterIP 分配给 Service 的分配策略降低了冲突的风险。 |
| 121 | + |
| 122 | +`ClusterIP` 范围根据公式 `min(max(16, cidrSize / 16), 256)` 进行划分, |
| 123 | +描述为不小于 16 且不大于 256,并在二者之间有一个渐进的步长。 |
| 124 | + |
| 125 | +默认情况下,动态 IP 分配使用地址较高的一段,一旦用完,它将使用较低范围。 |
| 126 | +这将允许用户在冲突风险较低的较低地址段上使用静态分配。 |
| 127 | + |
| 128 | +<!-- |
| 129 | +## Examples {#allocation-examples} |
| 130 | +--> |
| 131 | +## 示例 {#allocation-examples} |
| 132 | + |
| 133 | +<!-- |
| 134 | +### Example 1 {#allocation-example-1} |
| 135 | +This example uses the IP address range: 10.96.0.0/24 (CIDR notation) for the IP addresses |
| 136 | +of Services. |
| 137 | +--> |
| 138 | +### 示例 1 {#allocation-example-1} |
| 139 | + |
| 140 | +此示例使用 IP 地址范围:10.96.0.0/24(CIDR 表示法)作为 Service 的 IP 地址。 |
| 141 | +<!-- |
| 142 | +Range Size: 2<sup>8</sup> - 2 = 254 |
| 143 | +Band Offset: `min(max(16, 256/16), 256)` = `min(16, 256)` = 16 |
| 144 | +Static band start: 10.96.0.1 |
| 145 | +Static band end: 10.96.0.16 |
| 146 | +Range end: 10.96.0.254 |
| 147 | + |
| 148 | +{{< mermaid >}} |
| 149 | +pie showData |
| 150 | + title 10.96.0.0/24 |
| 151 | + "Static" : 16 |
| 152 | + "Dynamic" : 238 |
| 153 | +{{< /mermaid >}} |
| 154 | +--> |
| 155 | +范围大小:2<sup>8</sup> - 2 = 254 |
| 156 | +带宽偏移量:`min(max(16, 256/16), 256)` = `min(16, 256)` = 16 |
| 157 | +静态带宽起始地址:10.96.0.1 |
| 158 | +静态带宽结束地址:10.96.0.16 |
| 159 | +范围结束地址:10.96.0.254 |
| 160 | + |
| 161 | +{{< mermaid >}} |
| 162 | +pie showData |
| 163 | + title 10.96.0.0/24 |
| 164 | + "静态分配" : 16 |
| 165 | + "动态分配" : 238 |
| 166 | +{{< /mermaid >}} |
| 167 | + |
| 168 | +<!-- |
| 169 | +### Example 2 {#allocation-example-2} |
| 170 | +This example uses the IP address range: 10.96.0.0/20 (CIDR notation) for the IP addresses |
| 171 | +of Services. |
| 172 | +--> |
| 173 | +### 示例 2 {#allocation-example-2} |
| 174 | + |
| 175 | +此示例使用 IP 地址范围 10.96.00/20(CIDR 表示法)作为 Service 的 IP 地址。 |
| 176 | + |
| 177 | +<!-- |
| 178 | +Range Size: 2<sup>12</sup> - 2 = 4094 |
| 179 | +Band Offset: `min(max(16, 4096/16), 256)` = `min(256, 256)` = 256 |
| 180 | +Static band start: 10.96.0.1 |
| 181 | +Static band end: 10.96.1.0 |
| 182 | +Range end: 10.96.15.254 |
| 183 | + |
| 184 | +{{< mermaid >}} |
| 185 | +pie showData |
| 186 | + title 10.96.0.0/20 |
| 187 | + "Static" : 256 |
| 188 | + "Dynamic" : 3838 |
| 189 | +{{< /mermaid >}} |
| 190 | +--> |
| 191 | + |
| 192 | +范围大小:2<sup>12</sup> - 2 = 4094 |
| 193 | +带宽偏移量:`min(max(16, 4096/16), 256)` = `min(256, 256)` = 256 |
| 194 | +静态带宽起始地址:10.96.0.1 |
| 195 | +静态带宽结束地址:10.96.1.0 |
| 196 | +范围结束地址:10.96.15.254 |
| 197 | + |
| 198 | +{{< mermaid >}} |
| 199 | +pie showData |
| 200 | + title 10.96.0.0/20 |
| 201 | + "静态分配" : 256 |
| 202 | + "动态分配" : 3838 |
| 203 | +{{< /mermaid >}} |
| 204 | + |
| 205 | +<!-- |
| 206 | +### Example 3 {#allocation-example-3} |
| 207 | +This example uses the IP address range: 10.96.0.0/16 (CIDR notation) for the IP addresses |
| 208 | +of Services. |
| 209 | +--> |
| 210 | +### 示例 3 {#allocation-example-3} |
| 211 | + |
| 212 | +此示例使用 IP 地址范围 10.96.0.0/16(CIDR 表示法)作为 Service 的 IP 地址。 |
| 213 | + |
| 214 | +<!-- |
| 215 | +Range Size: 2<sup>16</sup> - 2 = 65534 |
| 216 | +Band Offset: `min(max(16, 65536/16), 256)` = `min(4096, 256)` = 256 |
| 217 | +Static band start: 10.96.0.1 |
| 218 | +Static band ends: 10.96.1.0 |
| 219 | +Range end: 10.96.255.254 |
| 220 | + |
| 221 | +{{< mermaid >}} |
| 222 | +pie showData |
| 223 | + title 10.96.0.0/16 |
| 224 | + "Static" : 256 |
| 225 | + "Dynamic" : 65278 |
| 226 | +{{< /mermaid >}} |
| 227 | + |
| 228 | +--> |
| 229 | +范围大小:2<sup>16</sup> - 2 = 65534 |
| 230 | +带宽偏移量:`min(max(16, 65536/16), 256)` = `min(4096, 256)` = 256 |
| 231 | +静态带宽起始地址:10.96.0.1 |
| 232 | +静态带宽结束地址:10.96.1.0 |
| 233 | +范围结束地址:10.96.255.254 |
| 234 | + |
| 235 | +{{< mermaid >}} |
| 236 | +pie showData |
| 237 | + title 10.96.0.0/16 |
| 238 | + "静态分配" : 256 |
| 239 | + "动态分配" : 65278 |
| 240 | +{{< /mermaid >}} |
| 241 | + |
| 242 | +<!-- |
| 243 | +## {{% heading "whatsnext" %}} |
| 244 | +* Read about [Service External Traffic Policy](/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip) |
| 245 | +* Read about [Connecting Applications with Services](/docs/concepts/services-networking/connect-applications-service/) |
| 246 | +* Read about [Services](/docs/concepts/services-networking/service/) |
| 247 | +--> |
| 248 | +## {{% heading "whatsnext" %}} |
| 249 | + |
| 250 | +* 阅读[服务外部流量策略](/zh-cn/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip) |
| 251 | +* 阅读[应用程序与服务连接](/zh-cn/docs/concepts/services-networking/connect-applications-service/) |
| 252 | +* 阅读[服务](/zh-cn/docs/concepts/services-networking/service/) |
| 253 | + |
0 commit comments