@@ -7,7 +7,8 @@ description: >-
7
7
---
8
8
<!--
9
9
reviewers:
10
- - davidopp
10
+ - jbelamaric
11
+ - bowei
11
12
- thockin
12
13
title: DNS for Services and Pods
13
14
content_type: concept
@@ -29,19 +30,20 @@ Kubernetes 为 Service 和 Pod 创建 DNS 记录。
29
30
<!-- body -->
30
31
31
32
<!--
32
- Kubernetes DNS schedules a DNS Pod and Service on the cluster, and configures
33
- the kubelets to tell individual containers to use the DNS Service's IP to
34
- resolve DNS names .
33
+ Kubernetes publishes information about Pods and Services which is used
34
+ to program DNS. Kubelet configures Pods' DNS so that running containers
35
+ can lookup Services by name rather than IP .
35
36
-->
36
- Kubernetes DNS 除了在集群上调度 DNS Pod 和 Service,
37
- 还配置 kubelet 以告知各个容器使用 DNS Service 的 IP 来解析 DNS 名称 。
37
+ Kubernetes 发布有关 Pod 和 Service 的信息,这些信息被用来对 DNS 进行编程。
38
+ Kubelet 配置 Pod 的 DNS,以便运行中的容器可以通过名称而不是 IP 来查找服务 。
38
39
39
40
<!--
40
- Every Service defined in the cluster (including the DNS server itself) is
41
- assigned a DNS name. By default, a client Pod's DNS search list includes the
42
- Pod's own namespace and the cluster's default domain.
41
+ Services defined in the cluster are assigned DNS names. By default, a
42
+ client Pod's DNS search list includes the Pod's own namespace and the
43
+ cluster's default domain.
43
44
-->
44
- 集群中定义的每个 Service (包括 DNS 服务器自身)都被赋予一个 DNS 名称。
45
+
46
+ 集群中定义的 Service 被赋予 DNS 名称。
45
47
默认情况下,客户端 Pod 的 DNS 搜索列表会包含 Pod 自身的名字空间和集群的默认域。
46
48
47
49
<!--
@@ -74,13 +76,14 @@ Pod 查询 `data.prod` 时则会返回预期的结果,因为查询中指定了
74
76
75
77
<!--
76
78
DNS queries may be expanded using the Pod's `/etc/resolv.conf`. Kubelet
77
- sets this file for each Pod. For example, a query for just `data` may be
79
+ configures this file for each Pod. For example, a query for just `data` may be
78
80
expanded to `data.test.svc.cluster.local`. The values of the `search` option
79
81
are used to expand queries. To learn more about DNS queries, see
80
- [the `resolv.conf` manual page.](https://www.man7.org/linux/man-pages/man5/resolv.conf.5.html)
82
+ [the `resolv.conf` manual page.](https://www.man7.org/linux/man-pages/man5/resolv.conf.5.html)
81
83
-->
82
- DNS 查询可以使用 Pod 中的 ` /etc/resolv.conf ` 展开。kubelet 会为每个 Pod
83
- 生成此文件。例如,对 ` data ` 的查询可能被展开为 ` data.test.svc.cluster.local ` 。
84
+ DNS 查询可以使用 Pod 中的 ` /etc/resolv.conf ` 展开。
85
+ Kubelet 为每个 Pod 配置此文件。
86
+ 例如,对 ` data ` 的查询可能被展开为 ` data.test.svc.cluster.local ` 。
84
87
` search ` 选项的取值会被用来展开查询。要进一步了解 DNS 查询,可参阅
85
88
[ ` resolv.conf ` 手册页面] ( https://www.man7.org/linux/man-pages/man5/resolv.conf.5.html ) 。
86
89
@@ -91,10 +94,10 @@ options ndots:5
91
94
```
92
95
93
96
<!--
94
- In summary, a Pod in the `test` namespace can successfully resolve either
97
+ In summary, a Pod in the _test_ namespace can successfully resolve either
95
98
`data.prod` or `data.prod.svc.cluster.local`.
96
99
-->
97
- 概括起来,名字空间 ` test ` 中的 Pod 可以成功地解析 ` data.prod ` 或者
100
+ 概括起来,名字空间 _ test _ 中的 Pod 可以成功地解析 ` data.prod ` 或者
98
101
` data.prod.svc.cluster.local ` 。
99
102
100
103
<!--
@@ -127,56 +130,55 @@ For more up-to-date specification, see
127
130
128
131
### A/AAAA records
129
132
130
- "Normal" (not headless) Services are assigned a DNS A or AAAA record ,
131
- depending on the IP family of the Service, for a name of the form
133
+ "Normal" (not headless) Services are assigned DNS A and/ or AAAA records ,
134
+ depending on the IP family or families of the Service, with a name of the form
132
135
`my-svc.my-namespace.svc.cluster-domain.example`. This resolves to the cluster IP
133
136
of the Service.
134
137
135
- " Headless" (without a cluster IP) Services are also assigned a DNS A or AAAA record,
136
- depending on the IP family of the Service, for a name of the form
137
- `my-svc.my-namespace.svc.cluster-domain.example`. Unlike normal
138
- Services, this resolves to the set of IPs of the Pods selected by the Service.
138
+ [ Headless Services](/docs/concepts/services-networking/service/#headless-services)
139
+ (without a cluster IP) Services are also assigned DNS A and/or AAAA records,
140
+ with a name of the form `my-svc.my-namespace.svc.cluster-domain.example`. Unlike normal
141
+ Services, this resolves to the set of IPs of all of the Pods selected by the Service.
139
142
Clients are expected to consume the set or else use standard round-robin
140
143
selection from the set.
141
144
-->
142
145
### Service
143
146
144
147
#### A/AAAA 记录 {#a-aaaa-records}
145
148
146
- “普通” Service(除了无头 Service)会以 ` my-svc.my-namespace.svc.cluster-domain.example `
147
- 这种名字的形式被分配一个 DNS A 或 AAAA 记录,取决于 Service 的 IP 协议族。
149
+ 除了无头 Service 之外的 “普通” Service 会被赋予一个形如 ` my-svc.my-namespace.svc.cluster-domain.example `
150
+ 的 DNS A 和/ 或 AAAA 记录,取决于 Service 的 IP 协议族(可能有多个)设置 。
148
151
该名称会解析成对应 Service 的集群 IP。
149
152
150
- “无头(Headless)” Service (没有集群 IP)也会以
151
- ` my-svc.my-namespace.svc.cluster-domain.example ` 这种名字的形式被指派一个 DNS A 或 AAAA 记录,
152
- 具体取决于 Service 的 IP 协议族。
153
+ 没有集群 IP 的[ 无头 Service] ( /zh-cn/docs/concepts/services-networking/service/#headless-services )
154
+ 也会被赋予一个形如 ` my-svc.my-namespace.svc.cluster-domain.example ` 的 DNS A 和/或 AAAA 记录。
153
155
与普通 Service 不同,这一记录会被解析成对应 Service 所选择的 Pod IP 的集合。
154
156
客户端要能够使用这组 IP,或者使用标准的轮转策略从这组 IP 中进行选择。
155
157
156
158
<!--
157
159
### SRV records
158
160
159
- SRV Records are created for named ports that are part of normal or [Headless
160
- Services](/docs/concepts/services-networking/service/#headless-services).
161
- For each named port, the SRV record would have the form
162
- `_my-port-name._my-port-protocol.my-svc.my-namespace.svc.cluster-domain.example`.
161
+ SRV Records are created for named ports that are part of normal or headless
162
+ services. For each named port, the SRV record has the form
163
+ `_port-name._port-protocol.my-svc.my-namespace.svc.cluster-domain.example`.
163
164
For a regular Service, this resolves to the port number and the domain name:
164
165
`my-svc.my-namespace.svc.cluster-domain.example`.
165
166
For a headless Service, this resolves to multiple answers, one for each Pod
166
167
that is backing the Service, and contains the port number and the domain name of the Pod
167
- of the form `auto-generated-name .my-svc.my-namespace.svc.cluster-domain.example`.
168
+ of the form `hostname .my-svc.my-namespace.svc.cluster-domain.example`.
168
169
-->
169
170
#### SRV 记录 {#srv-records}
170
171
171
- Kubernetes 根据普通 Service 或
172
- [ Headless Service] ( /zh-cn/docs/concepts/services-networking/service/#headless-services )
172
+ Kubernetes 根据普通 Service 或无头 Service
173
173
中的命名端口创建 SRV 记录。每个命名端口,
174
- SRV 记录格式为 ` _my-port- name._my-port -protocol.my-svc.my-namespace.svc.cluster-domain.example` 。
174
+ SRV 记录格式为 ` _port- name._port -protocol.my-svc.my-namespace.svc.cluster-domain.example` 。
175
175
普通 Service,该记录会被解析成端口号和域名:` my-svc.my-namespace.svc.cluster-domain.example ` 。
176
176
无头 Service,该记录会被解析成多个结果,及该服务的每个后端 Pod 各一个 SRV 记录,
177
- 其中包含 Pod 端口号和格式为 ` auto-generated-name .my-svc.my-namespace.svc.cluster-domain.example`
177
+ 其中包含 Pod 端口号和格式为 ` hostname .my-svc.my-namespace.svc.cluster-domain.example`
178
178
的域名。
179
179
180
+ <!--
181
+ ## Pods-->
180
182
## Pod
181
183
182
184
<!--
@@ -213,49 +215,63 @@ Any Pods exposed by a Service have the following DNS resolution available:
213
215
<!--
214
216
### Pod's hostname and subdomain fields
215
217
216
- Currently when a Pod is created, its hostname is the Pod's `metadata.name` value.
217
-
218
- The Pod spec has an optional `hostname` field, which can be used to specify the
219
- Pod's hostname. When specified, it takes precedence over the Pod's name to be
220
- the hostname of the Pod. For example, given a Pod with `hostname` set to
221
- "`my-host`", the Pod will have its hostname set to "`my-host`".
218
+ Currently when a Pod is created, its hostname (as observed from within the Pod)
219
+ is the Pod's `metadata.name` value.
220
+ -->
221
+ ### Pod 的 hostname 和 subdomain 字段 {#pod-s-hostname-and-subdomain-fields}
222
222
223
- The Pod spec also has an optional `subdomain` field which can be used to specify
224
- its subdomain. For example, a Pod with `hostname` set to "`foo`", and `subdomain`
225
- set to "`bar`", in namespace "`my-namespace`", will have the fully qualified
226
- domain name (FQDN) "`foo.bar.my-namespace.svc.cluster-domain.example`".
223
+ 当前,创建 Pod 时其主机名(从 Pod 内部观察)取自 Pod 的 ` metadata.name ` 值。
227
224
228
- Example:
225
+ <!--
226
+ The Pod spec has an optional `hostname` field, which can be used to specify a
227
+ different hostname. When specified, it takes precedence over the Pod's name to be
228
+ the hostname of the Pod (again, as observed from within the Pod). For example,
229
+ given a Pod with `spec.hostname` set to `"my-host"`, the Pod will have its
230
+ hostname set to `"my-host"`.
229
231
-->
230
- ### Pod 的 hostname 和 subdomain 字段 {#pod-s-hostname-and-subdomain-fields}
231
232
232
- 当前,创建 Pod 时其主机名取自 Pod 的 ` metadata.name ` 值。
233
+ Pod 规约中包含一个可选的 ` hostname ` 字段,可以用来指定一个不同的主机名。
234
+ 当这个字段被设置时,它将优先于 Pod 的名字成为该 Pod 的主机名(同样是从 Pod 内部观察)。
235
+ 举个例子,给定一个 ` spec.hostname ` 设置为 ` “my-host” ` 的 Pod,
236
+ 该 Pod 的主机名将被设置为 ` “my-host” ` 。
237
+
238
+ <!--
239
+ The Pod spec also has an optional `subdomain` field which can be used to indicate
240
+ that the pod is part of sub-group of the namespace. For example, a Pod with `spec.hostname`
241
+ set to `"foo"`, and `spec.subdomain` set to `"bar"`, in namespace `"my-namespace"`, will
242
+ have its hostname set to `"foo"` and its fully qualified domain name (FQDN) set to
243
+ `"foo.bar.my-namespace.svc.cluster.local"` (once more, as observed from within
244
+ the Pod).
245
+ -->
233
246
234
- Pod 规约中包含一个可选的 ` hostname ` 字段,可以用来指定 Pod 的主机名 。
235
- 当这个字段被设置时,它将优先于 Pod 的名字成为该 Pod 的主机名。
236
- 举个例子,给定一个 ` hostname ` 设置为 " ` my-host ` " 的 Pod,
237
- 该 Pod 的主机名将被设置为 " ` my-host ` " 。
247
+ Pod 规约还有一个可选的 ` subdomain ` 字段,可以用来表明该 Pod 是名字空间的子组的一部分 。
248
+ 举个例子,某 Pod 的 ` spec.hostname ` 设置为 ` “foo” ` , ` spec.subdomain ` 设置为 ` “bar” ` ,
249
+ 在名字空间 ` “my-namespace” ` 中,主机名称被设置成 ` “foo” ` 并且对应的完全限定域名(FQDN)为
250
+ “ ` foo.bar. my-namespace.svc.cluster-domain.example ` ”(还是从 Pod 内部观察) 。
238
251
239
- Pod 规约还有一个可选的 ` subdomain ` 字段,可以用来指定 Pod 的子域名。
240
- 举个例子,某 Pod 的 ` hostname ` 设置为 “` foo ` ”,` subdomain ` 设置为 “` bar ` ”,
241
- 在名字空间 “` my-namespace ` ” 中对应的完全限定域名(FQDN)为
242
- “` foo.bar.my-namespace.svc.cluster-domain.example ` ”。
252
+ <!--
253
+ If there exists a headless Service in the same namespace as the Pod, with
254
+ the same name as the subdomain, the cluster's DNS Server also returns A and/or AAAA
255
+ records for the Pod's fully qualified hostname.
256
+ Example:
257
+ -->
258
+ 如果 Pod 所在的名字空间中存在一个无头服务,其名称与子域相同,
259
+ 则集群的 DNS 服务器还会为 Pod 的完全限定主机名返回 A 和/或 AAAA 记录。
243
260
244
261
示例:
245
262
246
263
``` yaml
247
264
apiVersion : v1
248
265
kind : Service
249
266
metadata :
250
- name : default -subdomain
267
+ name : busybox -subdomain
251
268
spec :
252
269
selector :
253
270
name : busybox
254
271
clusterIP : None
255
272
ports :
256
273
- name : foo # 实际上不需要指定端口号
257
274
port : 1234
258
- targetPort : 1234
259
275
---
260
276
apiVersion : v1
261
277
kind : Pod
@@ -265,7 +281,7 @@ metadata:
265
281
name : busybox
266
282
spec :
267
283
hostname : busybox-1
268
- subdomain : default -subdomain
284
+ subdomain : busybox -subdomain
269
285
containers :
270
286
- image : busybox:1.28
271
287
command :
@@ -281,7 +297,7 @@ metadata:
281
297
name : busybox
282
298
spec :
283
299
hostname : busybox-2
284
- subdomain : default -subdomain
300
+ subdomain : busybox -subdomain
285
301
containers :
286
302
- image : busybox:1.28
287
303
command :
@@ -291,24 +307,16 @@ spec:
291
307
` ` `
292
308
293
309
<!--
294
- If there exists a headless Service in the same namespace as the Pod and with
295
- the same name as the subdomain, the cluster's DNS Server also returns an A or AAAA
296
- record for the Pod's fully qualified hostname.
297
- For example, given a Pod with the hostname set to "` busybox-1`" and the subdomain set to
298
- " `default-subdomain`" , and a headless Service named "`default-subdomain`" in
299
- the same namespace, the Pod will see its own FQDN as
300
- " `busybox-1.default-subdomain.my-namespace.svc.cluster-domain.example`" . DNS serves an
301
- A or AAAA record at that name, pointing to the Pod's IP. Both Pods "`busybox1`" and
302
- " `busybox2`" can have their distinct A or AAAA records.
310
+ Given the above Service ` " busybox-subdomain" ` and the Pods which set ` spec.subdomain`
311
+ to `"busybox-subdomain"`, the first Pod will see its own FQDN as
312
+ ` "busybox-1.busybox-subdomain.my-namespace.svc.cluster-domain.example"` . DNS serves
313
+ A and/or AAAA records at that name, pointing to the Pod's IP. Both Pods "`busybox1`" and
314
+ " `busybox2`" will have their own address records.
303
315
-->
304
- 如果某无头 Service 与某 Pod 在同一个名字空间中,且它们具有相同的子域名,
305
- 集群的 DNS 服务器也会为该 Pod 的全限定主机名返回 A 记录或 AAAA 记录。
306
- 例如,在同一个名字空间中,给定一个主机名为 “busybox-1”、
307
- 子域名设置为 “default-subdomain” 的 Pod,和一个名称为 “`default-subdomain`”
308
- 的无头 Service,Pod 将看到自己的 FQDN 为
309
- " `busybox-1.default-subdomain.my-namespace.svc.cluster-domain.example`" 。
310
- DNS 会为此名字提供一个 A 记录或 AAAA 记录,指向该 Pod 的 IP。
311
- “`busybox1`” 和 “`busybox2`” 这两个 Pod 分别具有它们自己的 A 或 AAAA 记录。
316
+ 鉴于上述服务 `“busybox-subdomain”` 和将 `spec.subdomain` 设置为 `“busybox-subdomain”` 的 Pod,
317
+ 第一个 Pod 将看到自己的 FQDN 为 `“busybox-1.busybox-subdomain.my-namespace.svc.cluster-domain.example”`。
318
+ DNS 会为此名字提供一个 A 记录和/或 AAAA 记录,指向该 Pod 的 IP。
319
+ Pod “`busybox1`” 和 “`busybox2`” 都将有自己的地址记录。
312
320
313
321
<!--
314
322
An {{<glossary_tooltip term_id="endpoint-slice" text="EndpointSlice">}} can specify
@@ -318,18 +326,18 @@ the DNS hostname for any endpoint addresses, along with its IP.
318
326
对象可以为任何端点地址及其 IP 指定 `hostname`。
319
327
320
328
<!--
321
- Because A or AAAA records are not created for Pod names, `hostname` is required for the Pod's A or AAAA
329
+ Because A and AAAA records are not created for Pod names, `hostname` is required for the Pod's A or AAAA
322
330
record to be created. A Pod with no `hostname` but with `subdomain` will only create the
323
- A or AAAA record for the headless Service (`default -subdomain.my-namespace.svc.cluster-domain.example`),
324
- pointing to the Pod's IP address . Also, Pod needs to become ready in order to have a
331
+ A or AAAA record for the headless Service (`busybox -subdomain.my-namespace.svc.cluster-domain.example`),
332
+ pointing to the Pods' IP addresses . Also, the Pod needs to be ready in order to have a
325
333
record unless `publishNotReadyAddresses=True` is set on the Service.
326
334
-->
327
335
{{< note >}}
328
- 由于不是为 Pod 名称创建 A 或 AAAA 记录的,因此 Pod 的 A 或 AAAA 需要 `hostname` 。
336
+ 由于 A 和 AAAA 记录不是基于 Pod 名称创建,因此需要设置了 `hostname` 才会生成 Pod 的 A 或 AAAA 记录 。
329
337
没有设置 `hostname` 但设置了 `subdomain` 的 Pod 只会为
330
- 无头 Service 创建 A 或 AAAA 记录(`default -subdomain.my-namespace.svc.cluster-domain.example`)
338
+ 无头 Service 创建 A 或 AAAA 记录(`busybox -subdomain.my-namespace.svc.cluster-domain.example`)
331
339
指向 Pod 的 IP 地址。
332
- 另外,除非在服务上设置了 `publishNotReadyAddresses=True`,否则只有 Pod 进入就绪状态
340
+ 另外,除非在服务上设置了 `publishNotReadyAddresses=True`,否则只有 Pod 准备就绪
333
341
才会有与之对应的记录。
334
342
{{< /note >}}
335
343
@@ -341,12 +349,16 @@ record unless `publishNotReadyAddresses=True` is set on the Service.
341
349
{{< feature-state for_k8s_version="v1.22" state="stable" >}}
342
350
343
351
<!--
344
- When a Pod is configured to have fully qualified domain name (FQDN), its hostname is the short hostname. For example, if you have a Pod with the fully qualified domain name `busybox-1.default-subdomain.my-namespace.svc.cluster-domain.example`, then by default the `hostname` command inside that Pod returns `busybox-1` and the `hostname --fqdn` command returns the FQDN.
352
+ When a Pod is configured to have fully qualified domain name (FQDN), its
353
+ hostname is the short hostname. For example, if you have a Pod with the fully
354
+ qualified domain name `busybox-1.busybox-subdomain.my-namespace.svc.cluster-domain.example`,
355
+ then by default the `hostname` command inside that Pod returns `busybox-1` and the
356
+ ` hostname --fqdn` command returns the FQDN.
345
357
346
358
When you set `setHostnameAsFQDN : true` in the Pod spec, the kubelet writes the Pod's FQDN into the hostname for that Pod's namespace. In this case, both `hostname` and `hostname --fqdn` return the Pod's FQDN.
347
359
-->
348
360
当 Pod 配置为具有全限定域名 (FQDN) 时,其主机名是短主机名。
349
- 例如,如果你有一个具有完全限定域名 `busybox-1.default -subdomain.my-namespace.svc.cluster-domain.example` 的 Pod,
361
+ 例如,如果你有一个具有完全限定域名 `busybox-1.busybox -subdomain.my-namespace.svc.cluster-domain.example` 的 Pod,
350
362
则默认情况下,该 Pod 内的 `hostname` 命令返回 `busybox-1`,而 `hostname --fqdn` 命令返回 FQDN。
351
363
352
364
当你在 Pod 规约中设置了 `setHostnameAsFQDN : true` 时,kubelet 会将 Pod
@@ -526,7 +538,7 @@ options ndots:2 edns0
526
538
```
527
539
528
540
<!--
529
- For IPv6 setup, search path and name server should be setup like this:
541
+ For IPv6 setup, search path and name server should be set up like this:
530
542
-->
531
543
对于 IPv6 设置,搜索路径和名称服务器应按以下方式设置:
532
544
0 commit comments