Skip to content

Commit dca426c

Browse files
authored
Merge pull request #44490 from my-git9/fine-parallel-processing-work-queue-3i5
[zh-cn] sync fine-parallel-processing-work-queue.md
2 parents 7d1f4aa + 60b70a1 commit dca426c

File tree

1 file changed

+72
-61
lines changed

1 file changed

+72
-61
lines changed

content/zh-cn/docs/tasks/job/fine-parallel-processing-work-queue.md

Lines changed: 72 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
---
22
title: 使用工作队列进行精细的并行处理
33
content_type: task
4-
min-kubernetes-server-version: v1.8
54
weight: 30
65
---
76
<!--
87
title: Fine Parallel Processing Using a Work Queue
98
content_type: task
109
weight: 30
11-
min-kubernetes-server-version: v1.8
1210
-->
1311

1412
<!-- overview -->
1513

1614
<!--
17-
In this example, we will run a Kubernetes Job with multiple parallel
18-
worker processes in a given pod.
15+
In this example, you will run a Kubernetes Job that runs multiple parallel
16+
tasks as worker processes, each running as a separate Pod.
1917
-->
20-
在这个例子中,我们会运行一个 Kubernetes Job,其中的 Pod 会运行多个并行工作进程。
18+
在此示例中,你将运行一个 Kubernetes Job,该 Job 将多个并行任务作为工作进程运行,
19+
每个任务在单独的 Pod 中运行。
2120

2221
<!--
2322
In this example, as each pod is created, it picks up one unit of work
@@ -30,17 +29,19 @@ Here is an overview of the steps in this example:
3029
下面是这个示例的步骤概述:
3130

3231
<!--
33-
1. **Start a storage service to hold the work queue.** In this example, we use Redis to store
34-
our work items. In the previous example, we used RabbitMQ. In this example, we use Redis and
35-
a custom work-queue client library because AMQP does not provide a good way for clients to
32+
1. **Start a storage service to hold the work queue.** In this example, you will use Redis to store
33+
work items. In the [previous example](/docs/tasks/job/coarse-parallel-processing-work-queue),
34+
you used RabbitMQ. In this example, you will use Redis and a custom work-queue client library;
35+
this is because AMQP does not provide a good way for clients to
3636
detect when a finite-length work queue is empty. In practice you would set up a store such
3737
as Redis once and reuse it for the work queues of many jobs, and other things.
3838
-->
3939

40-
1. **启动存储服务用于保存工作队列。** 在这个例子中,我们使用 Redis 来存储工作项。
41-
在上一个例子中,我们使用了 RabbitMQ。
40+
1. **启动存储服务用于保存工作队列。** 在这个例子中,你将使用 Redis 来存储工作项。
41+
[上一个例子中](/zh-cn/docs/tasks/job/coarse-parallel-processing-work-queue)
42+
你使用了 RabbitMQ。
4243
在这个例子中,由于 AMQP 不能为客户端提供一个良好的方法来检测一个有限长度的工作队列是否为空,
43-
我们使用了 Redis 和一个自定义的工作队列客户端库。
44+
你将使用 Redis 和一个自定义的工作队列客户端库。
4445
在实践中,你可能会设置一个类似于 Redis 的存储库,并将其同时用于多项任务或其他事务的工作队列。
4546

4647
<!--
@@ -61,6 +62,20 @@ Here is an overview of the steps in this example:
6162

6263
{{< include "task-tutorial-prereqs.md" >}}
6364

65+
<!--
66+
You will need a container image registry where you can upload images to run in your cluster.
67+
The example uses [Docker Hub](https://hub.docker.com/), but you could adapt it to a different
68+
container image registry.
69+
70+
This task example also assumes that you have Docker installed locally. You use Docker to
71+
build container images.
72+
-->
73+
你将需要一个容器镜像仓库,可以向其中上传镜像以在集群中运行。
74+
此示例使用的是 [Docker Hub](https://hub.docker.com/)
75+
当然你可以将其调整为别的容器镜像仓库。
76+
77+
此任务示例还假设你已在本地安装了 Docker,并使用 Docker 来构建容器镜像。
78+
6479
<!-- steps -->
6580

6681
<!--
@@ -74,13 +89,13 @@ non-parallel, use of [Job](/docs/concepts/workloads/controllers/job/).
7489
<!--
7590
## Starting Redis
7691
77-
For this example, for simplicity, we will start a single instance of Redis.
92+
For this example, for simplicity, you will start a single instance of Redis.
7893
See the [Redis Example](https://github.com/kubernetes/examples/tree/master/guestbook) for an example
7994
of deploying Redis scalably and redundantly.
8095
-->
8196
## 启动 Redis
8297

83-
对于这个例子,为了简单起见,我们将启动一个单实例的 Redis。
98+
对于这个例子,为了简单起见,你将启动一个单实例的 Redis。
8499
了解如何部署一个可伸缩、高可用的 Redis 例子,请查看
85100
[Redis 示例](https://github.com/kubernetes/examples/tree/master/guestbook)
86101

@@ -97,9 +112,9 @@ You could also download the following files directly:
97112
- [`worker.py`](/examples/application/job/redis/worker.py)
98113

99114
<!--
100-
## Filling the Queue with tasks
115+
## Filling the queue with tasks
101116
102-
Now let's fill the queue with some "tasks". In our example, our tasks are strings to be
117+
Now let's fill the queue with some "tasks". In this example, our tasks are strings to be
103118
printed.
104119
105120
Start a temporary interactive pod for running the Redis CLI.
@@ -121,12 +136,14 @@ Hit enter for command prompt
121136
```
122137

123138
<!--
124-
Now hit enter, start the redis CLI, and create a list with some work items in it.
139+
Now hit enter, start the Redis CLI, and create a list with some work items in it.
125140
-->
126141
现在按回车键,启动 Redis 命令行界面,然后创建一个存在若干个工作项的列表。
127142

128143
```shell
129-
# redis-cli -h redis
144+
redis-cli -h redis
145+
```
146+
```console
130147
redis:6379> rpush job2 "apple"
131148
(integer) 1
132149
redis:6379> rpush job2 "banana"
@@ -158,9 +175,9 @@ redis:6379> lrange job2 0 -1
158175
```
159176

160177
<!--
161-
So, the list with key `job2` will be our work queue.
178+
So, the list with key `job2` will be the work queue.
162179
-->
163-
因此,这个键为 `job2` 的列表就是我们的工作队列
180+
因此,这个键为 `job2` 的列表就是工作队列
164181

165182
<!--
166183
Note: if you do not have Kube DNS setup correctly, you may need to change
@@ -170,23 +187,24 @@ the first step of the above block to `redis-cli -h $REDIS_SERVICE_HOST`.
170187
`redis-cli -h $REDIS_SERVICE_HOST`
171188

172189
<!--
173-
## Create an Image
190+
## Create a container image {#create-an-image}
174191
175-
Now we are ready to create an image that we will run.
192+
Now you are ready to create an image that will process the work in that queue.
176193
177-
We will use a python worker program with a redis client to read
194+
You're going to use a Python worker program with a Redis client to read
178195
the messages from the message queue.
179196
180197
A simple Redis work queue client library is provided,
181-
called rediswq.py ([Download](/examples/application/job/redis/rediswq.py)).
198+
called `rediswq.py` ([Download](/examples/application/job/redis/rediswq.py)).
182199
-->
183-
## 创建镜像
200+
## 创建容器镜像 {#create-an-image}
184201

185-
现在我们已经准备好创建一个我们要运行的镜像
202+
现在你已准备好创建一个镜像来处理该队列中的工作
186203

187-
我们会使用一个带有 Redis 客户端的 Python 工作程序从消息队列中读出消息。
204+
你将使用一个带有 Redis 客户端的 Python 工作程序从消息队列中读出消息。
188205

189-
这里提供了一个简单的 Redis 工作队列客户端库,名为 rediswq.py ([下载](/examples/application/job/redis/rediswq.py))。
206+
这里提供了一个简单的 Redis 工作队列客户端库,名为 `rediswq.py`
207+
[下载](/zh-cn/examples/application/job/redis/rediswq.py))。
190208

191209
<!--
192210
The "worker" program in each Pod of the Job uses the work queue
@@ -200,11 +218,12 @@ Job 中每个 Pod 内的“工作程序” 使用工作队列客户端库获取
200218
You could also download [`worker.py`](/examples/application/job/redis/worker.py),
201219
[`rediswq.py`](/examples/application/job/redis/rediswq.py), and
202220
[`Dockerfile`](/examples/application/job/redis/Dockerfile) files, then build
203-
the image:
221+
the container image. Here's an example using Docker to do the image build:
204222
-->
205223
你也可以下载 [`worker.py`](/examples/application/job/redis/worker.py)
206224
[`rediswq.py`](/examples/application/job/redis/rediswq.py)
207-
[`Dockerfile`](/examples/application/job/redis/Dockerfile) 文件。然后构建镜像:
225+
[`Dockerfile`](/examples/application/job/redis/Dockerfile) 文件。然后构建容器镜像。
226+
以下是使用 Docker 进行镜像构建的示例:
208227

209228
```shell
210229
docker build -t job-wq-2 .
@@ -234,52 +253,40 @@ your private repository](/docs/concepts/containers/images/).
234253
你需要将镜像 push 到一个公共仓库或者
235254
[配置集群访问你的私有仓库](/zh-cn/docs/concepts/containers/images/)
236255

237-
<!--
238-
If you are using [Google Container
239-
Registry](https://cloud.google.com/tools/container-registry/), tag
240-
your app image with your project ID, and push to GCR. Replace
241-
`<project>` with your project ID.
242-
-->
243-
如果你使用的是 [Google Container Registry](https://cloud.google.com/tools/container-registry/)
244-
请先用你的 project ID 给你的镜像打上标签,然后 push 到 GCR。请将 `<project>` 替换为你自己的 project ID。
245-
246-
```shell
247-
docker tag job-wq-2 gcr.io/<project>/job-wq-2
248-
gcloud docker -- push gcr.io/<project>/job-wq-2
249-
```
250-
251256
<!--
252257
## Defining a Job
253258
254-
Here is the job definition:
259+
Here is a manifest for the Job you will create:
255260
-->
256261
## 定义一个 Job
257262

258-
这是 Job 定义
263+
以下是你将创建的 Job 的清单
259264

260265
{{% code_sample file="application/job/redis/job.yaml" %}}
261266

267+
{{< note >}}
262268
<!--
263-
Be sure to edit the job template to
269+
Be sure to edit the manifest to
264270
change `gcr.io/myproject` to your own path.
265271
-->
266-
请确保将 Job 模板中的 `gcr.io/myproject` 更改为你自己的路径。
272+
请确保将 Job 清单中的 `gcr.io/myproject` 更改为你自己的路径。
273+
{{< /note >}}
267274

268275
<!--
269276
In this example, each pod works on several items from the queue and then exits when there are no more items.
270277
Since the workers themselves detect when the workqueue is empty, and the Job controller does not
271278
know about the workqueue, it relies on the workers to signal when they are done working.
272-
The workers signal that the queue is empty by exiting with success. So, as soon as any worker
273-
exits with success, the controller knows the work is done, and the Pods will exit soon.
274-
So, we set the completion count of the Job to 1. The job controller will wait for the other pods to complete
275-
too.
279+
The workers signal that the queue is empty by exiting with success. So, as soon as **any** worker
280+
exits with success, the controller knows the work is done, and that the Pods will exit soon.
281+
So, we need to set the completion count of the Job to 1. The job controller will wait for
282+
the other pods to complete too.
276283
-->
277284
在这个例子中,每个 Pod 处理了队列中的多个项目,直到队列中没有项目时便退出。
278285
因为是由工作程序自行检测工作队列是否为空,并且 Job 控制器不知道工作队列的存在,
279286
这依赖于工作程序在完成工作时发出信号。
280287
工作程序以成功退出的形式发出信号表示工作队列已经为空。
281-
所以,只要有任意一个工作程序成功退出,控制器就知道工作已经完成了,所有的 Pod 将很快会退出。
282-
因此,我们将 Job 的完成计数(Completion Count)设置为 1。
288+
所以,只要有**任意**一个工作程序成功退出,控制器就知道工作已经完成了,所有的 Pod 将很快会退出。
289+
因此,我们需要将 Job 的完成计数(Completion Count)设置为 1。
283290
尽管如此,Job 控制器还是会等待其它 Pod 完成。
284291

285292
<!--
@@ -291,14 +298,18 @@ So, now run the Job:
291298

292299
现在运行这个 Job:
293300

301+
<!--
302+
# this assumes you downloaded and then edited the manifest already
303+
-->
294304
```shell
305+
# 这假设你已经下载并编辑了清单
295306
kubectl apply -f ./job.yaml
296307
```
297308

298309
<!--
299-
Now wait a bit, then check on the job.
310+
Now wait a bit, then check on the Job:
300311
-->
301-
稍等片刻,然后检查这个 Job
312+
稍等片刻,然后检查这个 Job
302313

303314
```shell
304315
kubectl describe jobs/job-wq-2
@@ -313,14 +324,14 @@ Labels: controller-uid=b1c7e4e3-92e1-11e7-b85e-fa163ee3c11f
313324
Annotations: <none>
314325
Parallelism: 2
315326
Completions: <unset>
316-
Start Time: Mon, 11 Jan 2016 17:07:59 -0800
327+
Start Time: Mon, 11 Jan 2022 17:07:59 +0000
317328
Pods Statuses: 1 Running / 0 Succeeded / 0 Failed
318329
Pod Template:
319330
Labels: controller-uid=b1c7e4e3-92e1-11e7-b85e-fa163ee3c11f
320331
job-name=job-wq-2
321332
Containers:
322333
c:
323-
Image: gcr.io/exampleproject/job-wq-2
334+
Image: container-registry.example/exampleproject/job-wq-2
324335
Port:
325336
Environment: <none>
326337
Mounts: <none>
@@ -353,9 +364,9 @@ Working on lemon
353364
```
354365

355366
<!--
356-
As you can see, one of our pods worked on several work units.
367+
As you can see, one of the pods for this Job worked on several work units.
357368
-->
358-
你可以看到,其中的一个 Pod 处理了若干个工作单元。
369+
你可以看到,此 Job 中的一个 Pod 处理了若干个工作单元。
359370

360371
<!-- discussion -->
361372

@@ -374,10 +385,10 @@ want to consider one of the other
374385

375386
<!--
376387
If you have a continuous stream of background processing work to run, then
377-
consider running your background workers with a `ReplicaSet` instead,
388+
consider running your background workers with a ReplicaSet instead,
378389
and consider running a background processing library such as
379390
[https://github.com/resque/resque](https://github.com/resque/resque).
380391
-->
381-
如果你有持续的后台处理业务,那么可以考虑使用 `ReplicaSet` 来运行你的后台业务,
392+
如果你有持续的后台处理业务,那么可以考虑使用 ReplicaSet 来运行你的后台业务,
382393
和运行一个类似 [https://github.com/resque/resque](https://github.com/resque/resque)
383394
的后台处理库。

0 commit comments

Comments
 (0)