@@ -3,7 +3,8 @@ title: 服务(Service)
3
3
feature :
4
4
title : 服务发现与负载均衡
5
5
description : >
6
- 你无需修改应用来使用陌生的服务发现机制。Kubernetes 为每个 Pod 提供了自己的 IP 地址并为一组 Pod 提供一个 DNS 名称,并且可以在它们之间实现负载均衡。
6
+ 你无需修改应用来使用陌生的服务发现机制。Kubernetes 为每个 Pod 提供了自己的 IP 地址并为一组
7
+ Pod 提供一个 DNS 名称,并且可以在它们之间实现负载均衡。
7
8
description : >-
8
9
将在集群中运行的应用通过同一个面向外界的端点公开出去,即使工作负载分散于多个后端也完全可行。
9
10
content_type : concept
@@ -36,8 +37,7 @@ an older app you've containerized. You use a Service to make that set of Pods av
36
37
on the network so that clients can interact with it.
37
38
-->
38
39
Kubernetes 中 Service 的一个关键目标是让你无需修改现有应用以使用某种不熟悉的服务发现机制。
39
- 你可以在 Pod 集合中运行代码,无论该代码是为云原生环境设计的,
40
- 还是被容器化的老应用。
40
+ 你可以在 Pod 集合中运行代码,无论该代码是为云原生环境设计的,还是被容器化的老应用。
41
41
你可以使用 Service 让一组 Pod 可在网络上访问,这样客户端就能与之交互。
42
42
43
43
<!--
@@ -241,7 +241,7 @@ A Service can map _any_ incoming `port` to a `targetPort`. By default and
241
241
for convenience, the `targetPort` is set to the same value as the `port`
242
242
field.
243
243
-->
244
- Service 能够将任意入站 `port` 映射到某个 `targetPort`。
244
+ Service 能够将**任意**入站 `port` 映射到某个 `targetPort`。
245
245
默认情况下,出于方便考虑,`targetPort` 会被设置为与 `port` 字段相同的值。
246
246
{{< /note >}}
247
247
@@ -373,6 +373,31 @@ object manually. For example:
373
373
由于此 Service 没有选择算符,因此不会自动创建对应的 EndpointSlice(和旧版的 Endpoints)对象。
374
374
你可以通过手动添加 EndpointSlice 对象,将 Service 映射到该服务运行位置的网络地址和端口:
375
375
376
+ <!--
377
+ ` ` ` yaml
378
+ apiVersion: discovery.k8s.io/v1
379
+ kind: EndpointSlice
380
+ metadata:
381
+ name: my-service-1 # by convention, use the name of the Service
382
+ # as a prefix for the name of the EndpointSlice
383
+ labels:
384
+ # You should set the "kubernetes.io/service-name" label.
385
+ # Set its value to match the name of the Service
386
+ kubernetes.io/service-name: my-service
387
+ addressType: IPv4
388
+ ports:
389
+ - name: '' # empty because port 9376 is not assigned as a well-known
390
+ # port (by IANA)
391
+ appProtocol: http
392
+ protocol: TCP
393
+ port: 9376
394
+ endpoints:
395
+ - addresses:
396
+ - "10.4.5.6"
397
+ - addresses:
398
+ - "10.1.2.3"
399
+ ` ` `
400
+ -->
376
401
` ` ` yaml
377
402
apiVersion: discovery.k8s.io/v1
378
403
kind: EndpointSlice
@@ -898,22 +923,21 @@ spec:
898
923
-->
899
924
#### 预留 NodePort 端口范围以避免发生冲突 {#avoid-nodeport-collisions}
900
925
901
- {{< feature-state for_k8s_version="v1.28 " state="beta " >}}
926
+ {{< feature-state for_k8s_version="v1.29 " state="stable " >}}
902
927
903
928
<!--
904
929
The policy for assigning ports to NodePort services applies to both the auto-assignment and
905
930
the manual assignment scenarios. When a user wants to create a NodePort service that
906
931
uses a specific port, the target port may conflict with another port that has already been assigned.
907
- In this case, you can enable the feature gate ` ServiceNodePortStaticSubrange`, which allows you
908
- to use a different port allocation strategy for NodePort Services. The port range for NodePort services
909
- is divided into two bands. Dynamic port assignment uses the upper band by default, and it may use
910
- the lower band once the upper band has been exhausted. Users can then allocate from the lower band
911
- with a lower risk of port collision.
932
+
933
+ To avoid this problem, the port range for NodePort services is divided into two bands.
934
+ Dynamic port assignment uses the upper band by default, and it may use the lower band once the
935
+ upper band has been exhausted. Users can then allocate from the lower band with a lower risk of port collision.
912
936
-->
913
937
为 NodePort 服务分配端口的策略既适用于自动分配的情况,也适用于手动分配的场景。
914
938
当某个用于希望创建一个使用特定端口的 NodePort 服务时,该目标端口可能与另一个已经被分配的端口冲突。
915
- 这时,你可以启用特性门控 `ServiceNodePortStaticSubrange`,进而为 NodePort Service
916
- 使用不同的端口分配策略。 用于 NodePort 服务的端口范围被分为两段。
939
+
940
+ 为了避免这个问题, 用于 NodePort 服务的端口范围被分为两段。
917
941
动态端口分配默认使用较高的端口段,并且在较高的端口段耗尽时也可以使用较低的端口段。
918
942
用户可以从较低端口段中分配端口,降低端口冲突的风险。
919
943
@@ -1168,6 +1192,50 @@ Unprefixed names are reserved for end-users.
1168
1192
可以有选择地带有类似 "`internal-vip`" 或 "`example.com/internal-vip`" 这类前缀。
1169
1193
没有前缀的名字是保留给最终用户的。
1170
1194
1195
+ <!--
1196
+ # ### Specifying IPMode of load balancer status {#load-balancer-ip-mode}
1197
+ -->
1198
+ # ### 指定负载均衡器状态的 IPMode {#load-balancer-ip-mode}
1199
+
1200
+ {{< feature-state for_k8s_version="v1.29" state="alpha" >}}
1201
+
1202
+ <!--
1203
+ Starting as Alpha in Kubernetes 1.29,
1204
+ a [feature gate](/docs/reference/command-line-tools-reference/feature-gates/)
1205
+ named `LoadBalancerIPMode` allows you to set the `.status.loadBalancer.ingress.ipMode`
1206
+ for a Service with `type` set to `LoadBalancer`.
1207
+ The `.status.loadBalancer.ingress.ipMode` specifies how the load-balancer IP behaves.
1208
+ It may be specified only when the `.status.loadBalancer.ingress.ip` field is also specified.
1209
+ -->
1210
+ 这是从 Kubernetes 1.29 开始的一个 Alpha 级别特性,通过名为 `LoadBalancerIPMode`
1211
+ 的[特性门控](/zh-cn/docs/reference/command-line-tools-reference/feature-gates/)允许你为
1212
+ ` type` 为 `LoadBalancer` 的服务设置 `.status.loadBalancer.ingress.ipMode`。
1213
+ ` .status.loadBalancer.ingress.ipMode` 指定负载均衡器 IP 的行为方式。
1214
+ 此字段只能在 `.status.loadBalancer.ingress.ip` 字段也被指定时才能指定。
1215
+
1216
+ <!--
1217
+ There are two possible values for `.status.loadBalancer.ingress.ipMode` : " VIP" and "Proxy".
1218
+ The default value is "VIP" meaning that traffic is delivered to the node
1219
+ with the destination set to the load-balancer's IP and port.
1220
+ There are two cases when setting this to "Proxy", depending on how the load-balancer
1221
+ from the cloud provider delivers the traffics :
1222
+ -->
1223
+ ` .status.loadBalancer.ingress.ipMode` 有两个可能的值:"VIP" 和 "Proxy"。
1224
+ 默认值是 "VIP",意味着流量被传递到目的地设置为负载均衡器 IP 和端口的节点上。
1225
+ 将此字段设置为 "Proxy" 时会出现两种情况,具体取决于云驱动提供的负载均衡器如何传递流量:
1226
+
1227
+ <!--
1228
+ - If the traffic is delivered to the node then DNATed to the pod, the destination would be set to the node's IP and node port;
1229
+ - If the traffic is delivered directly to the pod, the destination would be set to the pod's IP and port.
1230
+ -->
1231
+ - 如果流量被传递到节点,然后 DNAT 到 Pod,则目的地将被设置为节点的 IP 和节点端口;
1232
+ - 如果流量被直接传递到 Pod,则目的地将被设置为 Pod 的 IP 和端口。
1233
+
1234
+ <!--
1235
+ Service implementations may use this information to adjust traffic routing.
1236
+ -->
1237
+ 服务实现可以使用此信息来调整流量路由。
1238
+
1171
1239
<!--
1172
1240
# ### Internal load balancer
1173
1241
@@ -1184,16 +1252,18 @@ depending on the cloud service provider you're using:
1184
1252
1185
1253
在混合环境中,有时有必要在同一(虚拟)网络地址段内路由来自 Service 的流量。
1186
1254
1187
- 在水平分割(Split-Horizon) DNS 环境中,你需要两个 Service 才能将内部和外部流量都路由到你的端点。
1255
+ 在水平分割(Split-Horizon)DNS 环境中,你需要两个 Service 才能将内部和外部流量都路由到你的端点。
1188
1256
1189
1257
如要设置内部负载均衡器,请根据你所使用的云平台,为 Service 添加以下注解之一:
1190
1258
1191
1259
{{< tabs name="service_tabs" >}}
1192
1260
{{% tab name="Default" %}}
1261
+
1193
1262
<!--
1194
1263
Select one of the tabs.
1195
1264
-->
1196
1265
选择一个标签。
1266
+
1197
1267
{{% /tab %}}
1198
1268
{{% tab name="GCP" %}}
1199
1269
@@ -1245,7 +1315,9 @@ metadata:
1245
1315
` ` `
1246
1316
1247
1317
{{% /tab %}}
1248
- <!--Baidu Cloud-->
1318
+ <!--
1319
+ Baidu Cloud
1320
+ -->
1249
1321
{{% tab name="百度云" %}}
1250
1322
1251
1323
` ` ` yaml
@@ -1256,7 +1328,9 @@ metadata:
1256
1328
` ` `
1257
1329
1258
1330
{{% /tab %}}
1259
- <!--Tencent Cloud-->
1331
+ <!--
1332
+ Tencent Cloud
1333
+ -->
1260
1334
{{% tab name="腾讯云" %}}
1261
1335
1262
1336
` ` ` yaml
@@ -1266,7 +1340,9 @@ metadata:
1266
1340
` ` `
1267
1341
1268
1342
{{% /tab %}}
1269
- <!--Alibaba Cloud-->
1343
+ <!--
1344
+ Alibaba Cloud
1345
+ -->
1270
1346
{{% tab name="阿里云" %}}
1271
1347
1272
1348
` ` ` yaml
@@ -1452,10 +1528,6 @@ finding a Service: environment variables and DNS.
1452
1528
When a Pod is run on a Node, the kubelet adds a set of environment variables
1453
1529
for each active Service. It adds `{SVCNAME}_SERVICE_HOST` and `{SVCNAME}_SERVICE_PORT` variables,
1454
1530
where the Service name is upper-cased and dashes are converted to underscores.
1455
- It also supports variables
1456
- (see [makeLinkVariables](https://github.com/kubernetes/kubernetes/blob/dd2d12f6dc0e654c15d5db57a5f9f6ba61192726/pkg/kubelet/envvars/envvars.go#L72))
1457
- that are compatible with Docker Engine's
1458
- " _[legacy container links](https://docs.docker.com/network/links/)_" feature.
1459
1531
1460
1532
For example, the Service `redis-primary` which exposes TCP port 6379 and has been
1461
1533
allocated cluster IP address 10.0.0.11, produces the following environment
@@ -1466,9 +1538,6 @@ variables:
1466
1538
当 Pod 运行在某 Node 上时,kubelet 会在其中为每个活跃的 Service 添加一组环境变量。
1467
1539
kubelet 会添加环境变量 `{SVCNAME}_SERVICE_HOST` 和 `{SVCNAME}_SERVICE_PORT`。
1468
1540
这里 Service 的名称被转为大写字母,横线被转换成下划线。
1469
- 它还支持与 Docker Engine 的 "**[legacy container links](https://docs.docker.com/network/links/)**"
1470
- 特性兼容的变量
1471
- (参阅 [makeLinkVariables](https://github.com/kubernetes/kubernetes/blob/dd2d12f6dc0e654c15d5db57a5f9f6ba61192726/pkg/kubelet/envvars/envvars.go#L72)) 。
1472
1541
1473
1542
例如,一个 Service `redis-primary` 公开了 TCP 端口 6379,
1474
1543
同时被分配了集群 IP 地址 10.0.0.11,这个 Service 生成的环境变量如下:
@@ -1594,7 +1663,7 @@ to control how Kubernetes routes traffic to healthy (“ready”) backends.
1594
1663
1595
1664
See [Traffic Policies](/docs/reference/networking/virtual-ips/#traffic-policies) for more details.
1596
1665
-->
1597
- # ## 流量策略
1666
+ # ## 流量策略 {#traffic-policies}
1598
1667
1599
1668
你可以设置 `.spec.internalTrafficPolicy` 和 `.spec.externalTrafficPolicy`
1600
1669
字段来控制 Kubernetes 如何将流量路由到健康(“就绪”)的后端。
0 commit comments