@@ -3,12 +3,23 @@ title: Pod 与 Service 的 DNS
3
3
content_type : concept
4
4
weight : 20
5
5
---
6
+ <!--
7
+ reviewers:
8
+ - davidopp
9
+ - thockin
10
+ title: DNS for Services and Pods
11
+ content_type: concept
12
+ weight: 20
13
+ -->
14
+
6
15
<!-- overview -->
7
16
8
17
<!--
9
- This page provides an overview of DNS support by Kubernetes.
18
+ Kubernetes creates DNS records for services and pods. You can contact
19
+ services with consistent DNS names instead of IP addresses.
10
20
-->
11
- 本页面提供 Kubernetes 对 DNS 的支持的概述。
21
+ Kubernetes 为服务和 Pods 创建 DNS 记录。
22
+ 你可以使用一致的 DNS 名称而非 IP 地址来访问服务。
12
23
13
24
<!-- body -->
14
25
@@ -21,40 +32,93 @@ resolve DNS names.
21
32
-->
22
33
## 介绍
23
34
24
- Kubernetes DNS 在群集上调度 DNS Pod 和服务,并配置 kubelet 以告知各个容器使用 DNS 服务的 IP 来解析 DNS 名称。
35
+ Kubernetes DNS 在集群上调度 DNS Pod 和服务,并配置 kubelet 以告知各个容器
36
+ 使用 DNS 服务的 IP 来解析 DNS 名称。
25
37
26
38
<!--
27
- ### What things get DNS names?
28
-
29
39
Every Service defined in the cluster (including the DNS server itself) is
30
- assigned a DNS name. By default, a client Pod's DNS search list will
31
- include the Pod's own namespace and the cluster's default domain. This is best
32
- illustrated by example:
40
+ assigned a DNS name. By default, a client Pod's DNS search list includes the
41
+ Pod's own namespace and the cluster's default domain.
42
+ -->
43
+ 集群中定义的每个 Service (包括 DNS 服务器自身)都被赋予一个 DNS 名称。
44
+ 默认情况下,客户端 Pod 的 DNS 搜索列表会包含 Pod 自身的名字空间和集群
45
+ 的默认域。
46
+
47
+ <!--
48
+ ### Namespaces of Services
49
+
50
+ A DNS query may return different results based on the namespace of the pod making
51
+ it. DNS queries that don't specify a namespace are limited to the pod's
52
+ namespace. Access services in other namespaces by specifying it in the DNS query.
33
53
34
- Assume a Service named `foo` in the Kubernetes namespace `bar`. A Pod running
35
- in namespace `bar` can look up this service by simply doing a DNS query for
36
- `foo`. A Pod running in namespace `quux` can look up this service by doing a
37
- DNS query for `foo.bar`.
54
+ For example, consider a pod in a `test` namespace. A `data` service is in
55
+ the `prod` namespace.
38
56
39
- The following sections detail the supported record types and layout that is
57
+ A query for `data` returns no results, because it uses the pod's `test` namespace.
58
+
59
+ A query for `data.prod` returns the intended result, because it specifies the
60
+ namespace.
61
+ -->
62
+ ### Service 的名字空间
63
+
64
+ DNS 查询可能因为执行查询的 Pod 所在的名字空间而返回不同的结果。
65
+ 不指定名字空间的 DNS 查询会被限制在 Pod 所在的名字空间内。
66
+ 要访问其他名字空间中的服务,需要在 DNS 查询中给出名字空间。
67
+
68
+ 例如,假定名字空间 ` test ` 中存在一个 Pod,` prod ` 名字空间中存在一个服务
69
+ ` data ` 。
70
+
71
+ Pod 查询 ` data ` 时没有返回结果,因为使用的是 Pod 的名字空间 ` test ` 。
72
+
73
+ Pod 查询 ` data.prod ` 时则会返回预期的结果,因为查询中指定了名字空间。
74
+
75
+ <!--
76
+ 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
78
+ expanded to `data.test.cluster.local`. The values of the `search` option
79
+ 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)
81
+ -->
82
+ DNS 查询可以使用 Pod 中的 ` /etc/resolv.conf ` 展开。kubelet 会为每个 Pod
83
+ 生成此文件。例如,对 ` data ` 的查询可能被展开为 ` data.test.cluster.local ` 。
84
+ ` search ` 选项的取值会被用来展开查询。要进一步了解 DNS 查询,可参阅
85
+ [ ` resolv.conf ` 手册页面] ( https://www.man7.org/linux/man-pages/man5/resolv.conf.5.html ) 。
86
+
87
+ ```
88
+ nameserver 10.32.0.10
89
+ search <namespace>.svc.cluster.local svc.cluster.local cluster.local
90
+ options ndots:5
91
+ ```
92
+
93
+ <!--
94
+ In summary, a pod in the _test_ namespace can successfully resolve either
95
+ `data.prod` or `data.prod.cluster.local`.
96
+ -->
97
+ 概括起来,名字空间 ` test ` 中的 Pod 可以成功地解析 ` data.prod ` 或者
98
+ ` data.prod.cluster.local ` 。
99
+
100
+ <!--
101
+ ### DNS Records
102
+
103
+ What objects get DNS records?
104
+ -->
105
+ ### DNS 记录 {#dns-records}
106
+
107
+ 哪些对象会获得 DNS 记录呢?
108
+
109
+ 1 . Services
110
+ 2 . Pods
111
+
112
+ <!--
113
+ The following sections detail the supported DNS record types and layout that is
40
114
supported. Any other layout or names or queries that happen to work are
41
115
considered implementation details and are subject to change without warning.
42
116
For more up-to-date specification, see
43
117
[Kubernetes DNS-Based Service Discovery](https://github.com/kubernetes/dns/blob/master/docs/specification.md).
44
118
-->
45
- ## 哪些对象会有 DNS 名字? {#what-things-get-dns-names}
46
-
47
- 在集群中定义的每个 Service(包括 DNS 服务器自身)都会被指派一个 DNS 名称。
48
- 默认,一个客户端 Pod 的 DNS 搜索列表将包含该 Pod 自己的名字空间和集群默认域。
49
- 如下示例是一个很好的说明:
50
-
51
- 假设在 Kubernetes 集群的名字空间 ` bar ` 中,定义了一个服务 ` foo ` 。
52
- 运行在名字空间 ` bar ` 中的 Pod 可以简单地通过 DNS 查询 ` foo ` 来找到该服务。
53
- 运行在名字空间 ` quux ` 中的 Pod 可以通过 DNS 查询 ` foo.bar ` 找到该服务。
54
-
55
- 以下各节详细介绍了受支持的记录类型和支持的布局。
119
+ 以下各节详细介绍了被支持的 DNS 记录类型和被支持的布局。
56
120
其它布局、名称或者查询即使碰巧可以工作,也应视为实现细节,
57
- 将来很可能被更改而且不会因此出现警告 。
121
+ 将来很可能被更改而且不会因此发出警告 。
58
122
有关最新规范请查看
59
123
[ Kubernetes 基于 DNS 的服务发现] ( https://github.com/kubernetes/dns/blob/master/docs/specification.md ) 。
60
124
@@ -301,7 +365,7 @@ If a Pod enables this feature and its FQDN is longer than 64 character, it will
301
365
302
366
如果 Pod 启用这一特性,而其 FQDN 超出 64 字符,Pod 的启动会失败。
303
367
Pod 会一直出于 `Pending` 状态(通过 `kubectl` 所看到的 `ContainerCreating`),
304
- 并产生错误事件,例如
368
+ 并产生错误事件,例如
305
369
" Failed to construct FQDN from pod hostname and cluster domain, FQDN
306
370
`long-FQDN` is too long (64 characters is the max, 70 characters requested)."
307
371
(无法基于 Pod 主机名和集群域名构造 FQDN,FQDN `long-FQDN` 过长,至多 64
0 commit comments