@@ -14,11 +14,13 @@ weight: 30
14
14
<!-- overview -->
15
15
16
16
<!--
17
- In this example, you will run a Job in [Indexed completion mode](/blog/2021/04/19/introducing-indexed-jobs/) configured such that
18
- the pods created by the Job can communicate with each other using pod hostnames rather than pod IP addresses.
17
+ In this example, you will run a Job in [Indexed completion mode](/blog/2021/04/19/introducing-indexed-jobs/)
18
+ configured such that the pods created by the Job can communicate with each other using pod hostnames rather
19
+ than pod IP addresses.
19
20
20
- Pods within a Job might need to communicate among themselves. The user workload running in each pod could query the Kubernetes API server
21
- to learn the IPs of the other Pods, but it's much simpler to rely on Kubernetes' built-in DNS resolution.
21
+ Pods within a Job might need to communicate among themselves. The user workload running in each pod
22
+ could query the Kubernetes API server to learn the IPs of the other Pods, but it's much simpler to
23
+ rely on Kubernetes' built-in DNS resolution.
22
24
-->
23
25
在此例中,你将以[ 索引完成模式] ( /blog/2021/04/19/introducing-indexed-jobs/ ) 运行一个 Job,
24
26
并通过配置使得该 Job 所创建的各 Pod 之间可以使用 Pod 主机名而不是 Pod IP 地址进行通信。
@@ -30,11 +32,10 @@ to learn the IPs of the other Pods, but it's much simpler to rely on Kubernetes'
30
32
Jobs in Indexed completion mode automatically set the pods' hostname to be in the format of
31
33
`${jobName}-${completionIndex}`. You can use this format to deterministically build
32
34
pod hostnames and enable pod communication *without* needing to create a client connection to
33
- the Kubernetes control plane to obtain pod hostnames/IPs via API requests.
35
+ the Kubernetes control plane to obtain pod hostnames/IPs via API requests.
34
36
35
- This configuration is useful
36
- for use cases where pod networking is required but you don't want to depend on a network
37
- connection with the Kubernetes API server.
37
+ This configuration is useful for use cases where pod networking is required but you don't want
38
+ to depend on a network connection with the Kubernetes API server.
38
39
-->
39
40
索引完成模式下的 Job 自动将 Pod 的主机名设置为 ` ${jobName}-${completionIndex} ` 的格式。
40
41
你可以使用此格式确定性地构建 Pod 主机名并启用 Pod 通信,无需创建到 Kubernetes
@@ -51,20 +52,20 @@ You should already be familiar with the basic use of [Job](/docs/concepts/worklo
51
52
52
53
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
53
54
54
- {{<note >}}
55
+ {{< note >}}
55
56
<!--
56
- If you are using MiniKube or a similar tool, you may need to take
57
+ If you are using minikube or a similar tool, you may need to take
57
58
[extra steps](https://minikube.sigs.k8s.io/docs/handbook/addons/ingress-dns/)
58
59
to ensure you have DNS.
59
60
-->
60
- 如果你正在使用 MiniKube 或类似的工具,
61
+ 如果你正在使用 minikube 或类似的工具,
61
62
你可能需要采取[ 额外的步骤] ( https://minikube.sigs.k8s.io/docs/handbook/addons/ingress-dns/ ) 来确保你拥有 DNS。
62
- {{</note >}}
63
+ {{< /note >}}
63
64
64
65
<!-- steps -->
65
66
66
67
<!--
67
- ## Starting a Job with Pod-to-Pod Communication
68
+ ## Starting a Job with Pod-to-Pod communication
68
69
69
70
To enable pod-to-pod communication using pod hostnames in a Job, you must do the following:
70
71
-->
@@ -74,11 +75,14 @@ To enable pod-to-pod communication using pod hostnames in a Job, you must do the
74
75
75
76
<!--
76
77
1. Set up a [headless Service](/docs/concepts/services-networking/service/#headless-services)
77
- with a valid label selector for the pods created by your Job. The headless service must be in the same namespace as
78
- the Job. One easy way to do this is to use the `job-name: <your-job-name>` selector, since the `job-name` label will be automatically added by Kubernetes. This configuration will trigger the DNS system to create records of the hostnames of
79
- the pods running your Job.
80
-
81
- 2. Configure the headless service as subdomain service for the Job pods by including the following value in your Job template spec:
78
+ with a valid label selector for the pods created by your Job. The headless service must be
79
+ in the same namespace as the Job. One easy way to do this is to use the
80
+ `job-name: <your-job-name>` selector, since the `job-name` label will be automatically added
81
+ by Kubernetes. This configuration will trigger the DNS system to create records of the hostnames
82
+ of the pods running your Job.
83
+
84
+ 2. Configure the headless service as subdomain service for the Job pods by including the following
85
+ value in your Job template spec:
82
86
-->
83
87
1 . 对于 Job 所创建的那些 Pod,
84
88
使用一个有效的标签选择算符创建[ 无头服务] ( /zh-cn/docs/concepts/services-networking/service/#headless-services ) 。
@@ -89,12 +93,18 @@ the pods running your Job.
89
93
90
94
2 . 通过将以下值包括到你的 Job 模板规约中,针对该 Job 的 Pod,将无头服务配置为其子域服务:
91
95
96
+ <!--
97
+ ```yaml
98
+ subdomain: <headless-svc-name>
99
+ ```
100
+ -->
92
101
``` yaml
93
102
subdomain : <无头服务的名称>
94
103
` ` `
95
104
96
105
<!--
97
- ### Example
106
+ ### Example
107
+
98
108
Below is a working example of a Job with pod-to-pod communication via pod hostnames enabled.
99
109
The Job is completed only after all pods successfully ping each other using hostnames.
100
110
-->
@@ -103,15 +113,62 @@ The Job is completed only after all pods successfully ping each other using host
103
113
以下是启用通过 Pod 主机名来完成 Pod 间通信的 Job 示例。
104
114
只有在使用主机名成功 ping 通所有 Pod 之后,此 Job 才会结束。
105
115
106
- {{<note>}}
116
+ {{< note >}}
107
117
<!--
108
118
In the Bash script executed on each pod in the example below, the pod hostnames can be prefixed
109
- by the namespace as well if the pod needs to be reached from outside the namespace.
119
+ by the namespace as well if the pod needs to be reached from outside the namespace.
110
120
-->
111
121
在以下示例中的每个 Pod 中执行的 Bash 脚本中,如果需要从名字空间外到达 Pod,
112
122
Pod 主机名也可以带有该名字空间作为前缀。
113
- {{</note>}}
123
+ {{< /note >}}
114
124
125
+ <!--
126
+ ` ` ` yaml
127
+ apiVersion : v1
128
+ kind : Service
129
+ metadata :
130
+ name : headless-svc
131
+ spec :
132
+ clusterIP : None # clusterIP must be None to create a headless service
133
+ selector :
134
+ job-name : example-job # must match Job name
135
+ ---
136
+ apiVersion : batch/v1
137
+ kind : Job
138
+ metadata :
139
+ name : example-job
140
+ spec :
141
+ completions : 3
142
+ parallelism : 3
143
+ completionMode : Indexed
144
+ template :
145
+ spec :
146
+ subdomain : headless-svc # has to match Service name
147
+ restartPolicy : Never
148
+ containers :
149
+ - name : example-workload
150
+ image : bash:latest
151
+ command :
152
+ - bash
153
+ - -c
154
+ - |
155
+ for i in 0 1 2
156
+ do
157
+ gotStatus="-1"
158
+ wantStatus="0"
159
+ while [ $gotStatus -ne $wantStatus ]
160
+ do
161
+ ping -c 1 example-job-${i}.headless-svc > /dev/null 2>&1
162
+ gotStatus=$?
163
+ if [ $gotStatus -ne $wantStatus ]; then
164
+ echo "Failed to ping pod example-job-${i}.headless-svc, retrying in 1 second..."
165
+ sleep 1
166
+ fi
167
+ done
168
+ echo "Successfully pinged pod: example-job-${i}.headless-svc"
169
+ done
170
+ ` ` `
171
+ -->
115
172
` ` ` yaml
116
173
apiVersion : v1
117
174
kind : Service
@@ -176,13 +233,12 @@ Successfully pinged pod: example-job-1.headless-svc
176
233
Successfully pinged pod: example-job-2.headless-svc
177
234
```
178
235
179
- {{<note>}}
236
+ {{< note >}}
180
237
<!--
181
238
Keep in mind that the `<pod-hostname>.<headless-service-name>` name format used
182
239
in this example would not work with DNS policy set to `None` or `Default`.
183
- You can learn more about pod DNS policies [here ](/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy).
240
+ Refer to [Pod's DNS Policy ](/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy).
184
241
-->
185
242
谨记此例中使用的 `<Pod 主机名>.<无头服务名称>` 名称格式不适用于设置为 `None` 或 `Default` 的 DNS 策略。
186
- 你可以在[此处](/zh-cn/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy)了解有关
187
- Pod DNS 策略的更多信息。
188
- {{</note>}}
243
+ 请参阅 [Pod 的 DNS 策略](/zh-cn/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy)。
244
+ {{< /note >}}
0 commit comments