Skip to content

Commit 50523c8

Browse files
authored
Merge pull request #39127 from Zhuzhenghao/dns-pod-service
[zh-cn] Resync dns-pod-service.md
2 parents d28b5b0 + cb1af5d commit 50523c8

File tree

1 file changed

+99
-87
lines changed

1 file changed

+99
-87
lines changed

content/zh-cn/docs/concepts/services-networking/dns-pod-service.md

Lines changed: 99 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ description: >-
77
---
88
<!--
99
reviewers:
10-
- davidopp
10+
- jbelamaric
11+
- bowei
1112
- thockin
1213
title: DNS for Services and Pods
1314
content_type: concept
@@ -29,19 +30,20 @@ Kubernetes 为 Service 和 Pod 创建 DNS 记录。
2930
<!-- body -->
3031

3132
<!--
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.
3536
-->
36-
Kubernetes DNS 除了在集群上调度 DNS Pod 和 Service
37-
还配置 kubelet 以告知各个容器使用 DNS Service 的 IP 来解析 DNS 名称
37+
Kubernetes 发布有关 Pod 和 Service 的信息,这些信息被用来对 DNS 进行编程。
38+
Kubelet 配置 Pod 的 DNS,以便运行中的容器可以通过名称而不是 IP 来查找服务
3839

3940
<!--
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.
4344
-->
44-
集群中定义的每个 Service (包括 DNS 服务器自身)都被赋予一个 DNS 名称。
45+
46+
集群中定义的 Service 被赋予 DNS 名称。
4547
默认情况下,客户端 Pod 的 DNS 搜索列表会包含 Pod 自身的名字空间和集群的默认域。
4648

4749
<!--
@@ -74,13 +76,14 @@ Pod 查询 `data.prod` 时则会返回预期的结果,因为查询中指定了
7476

7577
<!--
7678
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
7880
expanded to `data.test.svc.cluster.local`. The values of the `search` option
7981
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)
8183
-->
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`
8487
`search` 选项的取值会被用来展开查询。要进一步了解 DNS 查询,可参阅
8588
[`resolv.conf` 手册页面](https://www.man7.org/linux/man-pages/man5/resolv.conf.5.html)
8689

@@ -91,10 +94,10 @@ options ndots:5
9194
```
9295

9396
<!--
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
9598
`data.prod` or `data.prod.svc.cluster.local`.
9699
-->
97-
概括起来,名字空间 `test` 中的 Pod 可以成功地解析 `data.prod` 或者
100+
概括起来,名字空间 _test_ 中的 Pod 可以成功地解析 `data.prod` 或者
98101
`data.prod.svc.cluster.local`
99102

100103
<!--
@@ -127,56 +130,55 @@ For more up-to-date specification, see
127130
128131
### A/AAAA records
129132
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
132135
`my-svc.my-namespace.svc.cluster-domain.example`. This resolves to the cluster IP
133136
of the Service.
134137
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.
139142
Clients are expected to consume the set or else use standard round-robin
140143
selection from the set.
141144
-->
142145
### Service
143146

144147
#### A/AAAA 记录 {#a-aaaa-records}
145148

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 协议族(可能有多个)设置
148151
该名称会解析成对应 Service 的集群 IP。
149152

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 记录。
153155
与普通 Service 不同,这一记录会被解析成对应 Service 所选择的 Pod IP 的集合。
154156
客户端要能够使用这组 IP,或者使用标准的轮转策略从这组 IP 中进行选择。
155157

156158
<!--
157159
### SRV records
158160
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`.
163164
For a regular Service, this resolves to the port number and the domain name:
164165
`my-svc.my-namespace.svc.cluster-domain.example`.
165166
For a headless Service, this resolves to multiple answers, one for each Pod
166167
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`.
168169
-->
169170
#### SRV 记录 {#srv-records}
170171

171-
Kubernetes 根据普通 Service 或
172-
[Headless Service](/zh-cn/docs/concepts/services-networking/service/#headless-services)
172+
Kubernetes 根据普通 Service 或无头 Service
173173
中的命名端口创建 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`
175175
普通 Service,该记录会被解析成端口号和域名:`my-svc.my-namespace.svc.cluster-domain.example`
176176
无头 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`
178178
的域名。
179179

180+
<!--
181+
## Pods-->
180182
## Pod
181183

182184
<!--
@@ -213,49 +215,63 @@ Any Pods exposed by a Service have the following DNS resolution available:
213215
<!--
214216
### Pod's hostname and subdomain fields
215217
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}
222222

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` 值。
227224

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"`.
229231
-->
230-
### Pod 的 hostname 和 subdomain 字段 {#pod-s-hostname-and-subdomain-fields}
231232

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+
-->
233246

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 内部观察)
238251

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 记录。
243260

244261
示例:
245262

246263
```yaml
247264
apiVersion: v1
248265
kind: Service
249266
metadata:
250-
name: default-subdomain
267+
name: busybox-subdomain
251268
spec:
252269
selector:
253270
name: busybox
254271
clusterIP: None
255272
ports:
256273
- name: foo # 实际上不需要指定端口号
257274
port: 1234
258-
targetPort: 1234
259275
---
260276
apiVersion: v1
261277
kind: Pod
@@ -265,7 +281,7 @@ metadata:
265281
name: busybox
266282
spec:
267283
hostname: busybox-1
268-
subdomain: default-subdomain
284+
subdomain: busybox-subdomain
269285
containers:
270286
- image: busybox:1.28
271287
command:
@@ -281,7 +297,7 @@ metadata:
281297
name: busybox
282298
spec:
283299
hostname: busybox-2
284-
subdomain: default-subdomain
300+
subdomain: busybox-subdomain
285301
containers:
286302
- image: busybox:1.28
287303
command:
@@ -291,24 +307,16 @@ spec:
291307
```
292308
293309
<!--
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.
303315
-->
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`” 都将有自己的地址记录。
312320

313321
<!--
314322
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.
318326
对象可以为任何端点地址及其 IP 指定 `hostname`。
319327

320328
<!--
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
322330
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
325333
record unless `publishNotReadyAddresses=True` is set on the Service.
326334
-->
327335
{{< note >}}
328-
由于不是为 Pod 名称创建 A 或 AAAA 记录的,因此 Pod 的 A 或 AAAA 需要 `hostname`
336+
由于 A 和 AAAA 记录不是基于 Pod 名称创建,因此需要设置了 `hostname` 才会生成 Pod 的 A 或 AAAA 记录
329337
没有设置 `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`)
331339
指向 Pod 的 IP 地址。
332-
另外,除非在服务上设置了 `publishNotReadyAddresses=True`,否则只有 Pod 进入就绪状态
340+
另外,除非在服务上设置了 `publishNotReadyAddresses=True`,否则只有 Pod 准备就绪
333341
才会有与之对应的记录。
334342
{{< /note >}}
335343

@@ -341,12 +349,16 @@ record unless `publishNotReadyAddresses=True` is set on the Service.
341349
{{< feature-state for_k8s_version="v1.22" state="stable" >}}
342350

343351
<!--
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.
345357

346358
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.
347359
-->
348360
当 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,
350362
则默认情况下,该 Pod 内的 `hostname` 命令返回 `busybox-1`,而 `hostname --fqdn` 命令返回 FQDN。
351363

352364
当你在 Pod 规约中设置了 `setHostnameAsFQDN: true` 时,kubelet 会将 Pod
@@ -526,7 +538,7 @@ options ndots:2 edns0
526538
```
527539
528540
<!--
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:
530542
-->
531543
对于 IPv6 设置,搜索路径和名称服务器应按以下方式设置:
532544

0 commit comments

Comments
 (0)