Skip to content

Commit ba6cfa2

Browse files
committed
[zh]translate indexed-parallel-processing-static.md
1 parent e3544b2 commit ba6cfa2

File tree

1 file changed

+277
-0
lines changed

1 file changed

+277
-0
lines changed
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
---
2+
title: 使用索引作业完成静态工作分配下的并行处理
3+
content_type: task
4+
min-kubernetes-server-version: v1.21
5+
weight: 30
6+
---
7+
<!--
8+
title: Indexed Job for Parallel Processing with Static Work Assignment
9+
content_type: task
10+
min-kubernetes-server-version: v1.21
11+
weight: 30
12+
-->
13+
14+
{{< feature-state for_k8s_version="v1.22" state="beta" >}}
15+
16+
<!-- overview -->
17+
18+
19+
<!--
20+
In this example, you will run a Kubernetes Job that uses multiple parallel
21+
worker processes.
22+
Each worker is a different container running in its own Pod. The Pods have an
23+
_index number_ that the control plane sets automatically, which allows each Pod
24+
to identify which part of the overall task to work on.
25+
-->
26+
在此示例中,你将运行一个使用多个并行工作进程的 Kubernetes Job。
27+
每个 worker 都是在自己的 Pod 中运行的不同容器。
28+
Pod 具有控制平面自动设置的 _索引编号(index number)_
29+
这些编号使得每个 Pod 能识别出要处理整个任务的哪个部分。
30+
31+
<!--
32+
The pod index is available in the {{< glossary_tooltip text="annotation" term_id="annotation" >}}
33+
`batch.kubernetes.io/job-completion-index` as a string representing its
34+
decimal value. In order for the containerized task process to obtain this index,
35+
you can publish the value of the annotation using the [downward API](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api)
36+
mechanism.
37+
For convenience, the control plane automatically sets the downward API to
38+
expose the index in the `JOB_COMPLETION_INDEX` environment variable.
39+
-->
40+
Pod 索引在{{<glossary_tooltip text="注解" term_id="annotation" >}}
41+
`batch.kubernetes.io/job-completion-index` 中呈现,具体表示为一个十进制值字符串。
42+
为了让容器化的任务进程获得此索引,你可以使用
43+
[downward API](/zh/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api)
44+
机制发布注解的值。为方便起见,
45+
控制平面自动设置 downward API 以在 `JOB_COMPLETION_INDEX` 环境变量中公开索引。
46+
47+
<!--
48+
Here is an overview of the steps in this example:
49+
50+
1. **Define a Job manifest using indexed completion**.
51+
The downward API allows you to pass the pod index annotation as an
52+
environment variable or file to the container.
53+
2. **Start an `Indexed` Job based on that manifest**.
54+
-->
55+
以下是此示例中步骤的概述:
56+
57+
1. **定义使用带索引完成信息的 Job 清单**
58+
Downward API 使你可以将 Pod 索引注释作为环境变量或文件传递给容器。
59+
2. **根据该清单启动一个带索引(`Indexed`)的 Job**
60+
61+
## {{% heading "prerequisites" %}}
62+
63+
<!--
64+
You should already be familiar with the basic,
65+
non-parallel, use of [Job](/docs/concepts/workloads/controllers/job/).
66+
-->
67+
你应该已经熟悉 [Job](/zh/docs/concepts/workloads/controllers/job/) 的基本的、非并行的用法。
68+
69+
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
70+
71+
<!-- steps -->
72+
73+
<!-- ## Choose an approach -->
74+
## 选择一种方法
75+
76+
<!--
77+
To access the work item from the worker program, you have a few options:
78+
79+
1. Read the `JOB_COMPLETION_INDEX` environment variable. The Job
80+
{{< glossary_tooltip text="controller" term_id="controller" >}}
81+
automatically links this variable to the annotation containing the completion
82+
index.
83+
1. Read a file that contains the completion index.
84+
1. Assuming that you can't modify the program, you can wrap it with a script
85+
that reads the index using any of the methods above and converts it into
86+
something that the program can use as input.
87+
-->
88+
要从工作程序访问工作项,你有几个选择:
89+
90+
1. 读取 `JOB_COMPLETION_INDEX` 环境变量。Job
91+
{{< glossary_tooltip text="控制器" term_id="controller" >}}
92+
自动将此变量链接到包含完成索引的注解。
93+
1. 读取包含完整索引的文件。
94+
1. 假设你无法修改程序,你可以使用脚本包装它,
95+
该脚本使用上述任意方法读取索引并将其转换为程序可以用作输入的内容。
96+
97+
<!--
98+
For this example, imagine that you chose option 3 and you want to run the
99+
[rev](https://man7.org/linux/man-pages/man1/rev.1.html) utility. This
100+
program accepts a file as an argument and prints its content reversed.
101+
-->
102+
对于此示例,假设你选择了方法 3 并且想要运行
103+
[rev](https://man7.org/linux/man-pages/man1/rev.1.html) 实用程序。
104+
这个程序接受一个文件作为参数并按逆序打印其内容。
105+
106+
```shell
107+
rev data.txt
108+
```
109+
110+
<!--
111+
You'll use the `rev` tool from the
112+
[`busybox`](https://hub.docker.com/_/busybox) container image.
113+
-->
114+
你将使用 [`busybox`](https://hub.docker.com/_/busybox) 容器映像中的 `rev` 工具。
115+
116+
<!--
117+
As this is only an example, each Pod only does a tiny piece of work (reversing a short
118+
string). In a real workload you might, for example, create a Job that represents
119+
the
120+
task of producing 60 seconds of video based on scene data.
121+
Each work item in the video rendering Job would be to render a particular
122+
frame of that video clip. Indexed completion would mean that each Pod in
123+
the Job knows which frame to render and publish, by counting frames from
124+
the start of the clip.
125+
-->
126+
由于这只是一个例子,每个 Pod 只做一小部分工作(反转一个短字符串)。
127+
例如,在实际工作负载中,你可能会创建一个表示基于场景数据制作 60 秒视频的任务的 Job 。
128+
视频渲染 Job 中的每个工作项都将渲染该视频剪辑的特定帧。
129+
索引完成意味着 Job 中的每个 Pod 都知道通过从剪辑开始计算帧数,来确定渲染和发布哪一帧,。
130+
131+
<!-- ## Define an Indexed Job -->
132+
## 定义索引作业
133+
134+
<!--
135+
Here is a sample Job manifest that uses `Indexed` completion mode:
136+
-->
137+
这是一个使用 `Indexed` 完成模式的示例 Job 清单:
138+
139+
{{< codenew language="yaml" file="application/job/indexed-job.yaml" >}}
140+
141+
<!--
142+
In the example above, you use the builtin `JOB_COMPLETION_INDEX` environment
143+
variable set by the Job controller for all containers. An [init container](/docs/concepts/workloads/pods/init-containers/)
144+
maps the index to a static value and writes it to a file that is shared with the
145+
container running the worker through an [emptyDir volume](/docs/concepts/storage/volumes/#emptydir).
146+
Optionally, you can [define your own environment variable through the downward
147+
API](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/)
148+
to publish the index to containers. You can also choose to load a list of values
149+
from a [ConfigMap as an environment variable or file](/docs/tasks/configure-pod-container/configure-pod-configmap/).
150+
-->
151+
在上面的示例中,你使用 Job 控制器为所有容器设置的内置 `JOB_COMPLETION_INDEX` 环境变量。
152+
[Init 容器](/zh/docs/concepts/workloads/pods/init-containers/)
153+
将索引映射到一个静态值,并将其写入一个文件,该文件通过
154+
[emptyDir 卷](/zh/docs/concepts/storage/volumes/#emptydir)
155+
与运行 worker 的容器共享。或者,你可以
156+
[通过 Downward API 定义自己的环境变量](/zh/docs/tasks/inject-data-application/environment-variable-expose-pod-information/)
157+
将索引发布到容器。你还可以选择从
158+
[包含 ConfigMap 的环境变量或文件](/zh/docs/tasks/configure-pod-container/configure-pod-configmap/)
159+
加载值列表。
160+
161+
<!--
162+
Alternatively, you can directly [use the downward API to pass the annotation
163+
value as a volume file](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#store-pod-fields),
164+
like shown in the following example:
165+
-->
166+
或者也可以直接
167+
[使用 Downward API 将注解值作为卷文件传递](/zh/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#store-pod-fields)
168+
如下例所示:
169+
170+
{{< codenew language="yaml" file="application/job/indexed-job-vol.yaml" >}}
171+
172+
<!-- ## Running the Job -->
173+
## 执行 Job
174+
175+
<!-- Now run the Job: -->
176+
现在执行 Job:
177+
178+
```shell
179+
# 使用第一种方法(依赖于 $JOB_COMPLETION_INDEX)
180+
kubectl apply -f https://kubernetes.io/examples/application/job/indexed-job.yaml
181+
```
182+
183+
<!--
184+
When you create this Job, the control plane creates a series of Pods, one for each index you specified. The value of `.spec.parallelism` determines how many can run at once whereas `.spec.completions` determines how many Pods the Job creates in total.
185+
186+
Because `.spec.parallelism` is less than `.spec.completions`, the control plane waits for some of the first Pods to complete before starting more of them.
187+
188+
Once you have created the Job, wait a moment then check on progress:
189+
-->
190+
当你创建此 Job 时,控制平面会创建一系列 Pod,每个索引都由你指定。
191+
`.spec.parallelism` 的值决定了一次可以运行多少个,
192+
`.spec.completions` 决定了 Job 总共创建了多少个 Pod。
193+
194+
因为 `.spec.parallelism` 小于 `.spec.completions`
195+
控制平面在启动更多 Pod 之前,等待部分第一批 Pod 完成。
196+
197+
创建 Job 后,稍等片刻,然后检查进度:
198+
199+
```shell
200+
kubectl describe jobs/indexed-job
201+
```
202+
203+
<!-- The output is similar to: -->
204+
输出类似于:
205+
206+
```
207+
Name: indexed-job
208+
Namespace: default
209+
Selector: controller-uid=bf865e04-0b67-483b-9a90-74cfc4c3e756
210+
Labels: controller-uid=bf865e04-0b67-483b-9a90-74cfc4c3e756
211+
job-name=indexed-job
212+
Annotations: <none>
213+
Parallelism: 3
214+
Completions: 5
215+
Start Time: Thu, 11 Mar 2021 15:47:34 +0000
216+
Pods Statuses: 2 Running / 3 Succeeded / 0 Failed
217+
Completed Indexes: 0-2
218+
Pod Template:
219+
Labels: controller-uid=bf865e04-0b67-483b-9a90-74cfc4c3e756
220+
job-name=indexed-job
221+
Init Containers:
222+
input:
223+
Image: docker.io/library/bash
224+
Port: <none>
225+
Host Port: <none>
226+
Command:
227+
bash
228+
-c
229+
items=(foo bar baz qux xyz)
230+
echo ${items[$JOB_COMPLETION_INDEX]} > /input/data.txt
231+
232+
Environment: <none>
233+
Mounts:
234+
/input from input (rw)
235+
Containers:
236+
worker:
237+
Image: docker.io/library/busybox
238+
Port: <none>
239+
Host Port: <none>
240+
Command:
241+
rev
242+
/input/data.txt
243+
Environment: <none>
244+
Mounts:
245+
/input from input (rw)
246+
Volumes:
247+
input:
248+
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
249+
Medium:
250+
SizeLimit: <unset>
251+
Events:
252+
Type Reason Age From Message
253+
---- ------ ---- ---- -------
254+
Normal SuccessfulCreate 4s job-controller Created pod: indexed-job-njkjj
255+
Normal SuccessfulCreate 4s job-controller Created pod: indexed-job-9kd4h
256+
Normal SuccessfulCreate 4s job-controller Created pod: indexed-job-qjwsz
257+
Normal SuccessfulCreate 1s job-controller Created pod: indexed-job-fdhq5
258+
Normal SuccessfulCreate 1s job-controller Created pod: indexed-job-ncslj
259+
```
260+
261+
<!--
262+
In this example, you run the Job with custom values for each index. You can
263+
inspect the output of one of the pods:
264+
-->
265+
在此示例中,你使用每个索引的自定义值运行 Job。
266+
你可以检查其中一个 Pod 的输出:
267+
268+
```shell
269+
kubectl logs indexed-job-fdhq5 # 更改它以匹配来自该 Job 的 Pod 的名称
270+
```
271+
272+
<!-- The output is similar to: -->
273+
输出类似于:
274+
275+
```
276+
xuq
277+
```

0 commit comments

Comments
 (0)