@@ -6,12 +6,10 @@ weight: 20
6
6
---
7
7
8
8
<!--
9
- ---
10
9
title: Coarse Parallel Processing Using a Work Queue
11
10
min-kubernetes-server-version: v1.8
12
11
content_type: task
13
12
weight: 20
14
- ---
15
13
-->
16
14
17
15
<!-- overview -->
@@ -26,24 +24,29 @@ from a task queue, completes it, deletes it from the queue, and exits.
26
24
Here is an overview of the steps in this example:
27
25
28
26
1. **Start a message queue service.** In this example, we use RabbitMQ, but you could use another
29
- one. In practice you would set up a message queue service once and reuse it for many jobs.
27
+ one. In practice you would set up a message queue service once and reuse it for many jobs.
30
28
1. **Create a queue, and fill it with messages.** Each message represents one task to be done. In
31
29
this example, a message is an integer that we will do a lengthy computation on.
32
30
1. **Start a Job that works on tasks from the queue**. The Job starts several pods. Each pod takes
33
- one task from the message queue, processes it, and repeats until the end of the queue is reached.
31
+ one task from the message queue, processes it, and repeats until the end of the queue is reached.
34
32
-->
35
33
本例中,我们会运行包含多个并行工作进程的 Kubernetes Job。
36
34
37
35
本例中,每个 Pod 一旦被创建,会立即从任务队列中取走一个工作单元并完成它,然后将工作单元从队列中删除后再退出。
38
36
39
37
下面是本次示例的主要步骤:
40
38
41
- 1 . ** 启动一个消息队列服务** 本例中,我们使用 RabbitMQ,你也可以用其他的消息队列服务。在实际工作环境中,你可以创建一次消息队列服务然后在多个任务中重复使用。
39
+ 1 . ** 启动一个消息队列服务** 。
40
+ 本例中,我们使用 RabbitMQ,你也可以用其他的消息队列服务。
41
+ 在实际工作环境中,你可以创建一次消息队列服务然后在多个任务中重复使用。
42
42
43
- 1 . ** 创建一个队列,放上消息数据** 每个消息表示一个要执行的任务。本例中,每个消息是一个整数值。我们将基于这个整数值执行很长的计算操作 。
44
-
45
- 1 . ** 启动一个在队列中执行这些任务的 Job ** 。该 Job 启动多个 Pod。每个 Pod 从消息队列中取走一个任务,处理它,然后重复执行,直到队列的队尾 。
43
+ 1 . ** 创建一个队列,放上消息数据** 。
44
+ 每个消息表示一个要执行的任务。本例中,每个消息是一个整数值。
45
+ 我们将基于这个整数值执行很长的计算操作 。
46
46
47
+ 1 . ** 启动一个在队列中执行这些任务的 Job** 。
48
+ 该 Job 启动多个 Pod。每个 Pod 从消息队列中取走一个任务,处理它,
49
+ 然后重复执行,直到队列的队尾。
47
50
48
51
## {{% heading "prerequisites" %}}
49
52
@@ -96,8 +99,8 @@ replicationcontroller "rabbitmq-controller" created
96
99
<!--
97
100
We will only use the rabbitmq part from the [celery-rabbitmq example](https://github.com/kubernetes/kubernetes/tree/release-1.3/examples/celery-rabbitmq).
98
101
-->
99
-
100
- 我们仅用到 [ celery-rabbitmq 示例] ( https://github.com/kubernetes/kubernetes/tree/release-1.3/examples/celery-rabbitmq ) 中描述的部分功能。
102
+ 我们仅用到
103
+ [ celery-rabbitmq 示例] ( https://github.com/kubernetes/kubernetes/tree/release-1.3/examples/celery-rabbitmq ) 中描述的部分功能。
101
104
102
105
<!--
103
106
## Testing the message queue service
@@ -110,13 +113,14 @@ First create a temporary interactive Pod.
110
113
-->
111
114
## 测试消息队列服务 {#testing-the-message-queue-service}
112
115
113
- 现在,我们可以试着访问消息队列。我们将会创建一个临时的可交互的 Pod,在它上面安装一些工具,然后用队列做实验。
116
+ 现在,我们可以试着访问消息队列。我们将会创建一个临时的可交互的 Pod,
117
+ 在它上面安装一些工具,然后用队列做实验。
114
118
115
119
首先创建一个临时的可交互的 Pod:
116
120
117
121
``` shell
118
122
# 创建一个临时的可交互的 Pod
119
- kubectl run -i --tty temp --image ubuntu:14 .04
123
+ kubectl run -i --tty temp --image ubuntu:18 .04
120
124
```
121
125
```
122
126
Waiting for pod default/temp-loe07 to be running, status is Pending, pod ready: false
@@ -130,7 +134,7 @@ Next install the `amqp-tools` so we can work with message queues.
130
134
-->
131
135
请注意你的 Pod 名称和命令提示符将会不同。
132
136
133
- 接下来安装 ` amqp-tools ` ,这样我们就能用消息队列了。
137
+ 接下来安装 ` amqp-tools ` ,这样我们就能用消息队列了。
134
138
135
139
``` shell
136
140
# 安装一些工具
@@ -145,10 +149,9 @@ Later, we will make a docker image that includes these packages.
145
149
146
150
Next, we will check that we can discover the rabbitmq service:
147
151
-->
148
-
149
152
后续,我们将制作一个包含这些包的 Docker 镜像。
150
153
151
- 接着,我们将要验证我们发现 RabbitMQ 服务:
154
+ 接着,我们将要验证可以发现 RabbitMQ 服务:
152
155
153
156
<!--
154
157
# Note the rabbitmq-service has a DNS name, provided by Kubernetes:
@@ -167,7 +170,7 @@ Address: 10.0.147.152
167
170
```
168
171
169
172
<!--
170
- If Kube-DNS is not setup correctly, the previous step may not work for you.
173
+ If Kube-DNS is not set up correctly, the previous step may not work for you.
171
174
You can also find the service IP in an env var:
172
175
-->
173
176
如果 Kube-DNS 没有正确安装,上一步可能会出错。
@@ -227,7 +230,7 @@ from the queue, and passes that message to the standard input of an arbitrary co
227
230
return so the example is readable.
228
231
-->
229
232
230
- 最后一个命令中, ` amqp-consume ` 工具从队列中取走了一个消息,并把该消息传递给了随机命令的标准输出。
233
+ 最后一个命令中,` amqp-consume ` 工具从队列中取走了一个消息,并把该消息传递给了随机命令的标准输出。
231
234
在这种情况下,` cat ` 会打印它从标准输入中读取的字符,echo 会添加回车符以便示例可读。
232
235
233
236
<!--
@@ -274,8 +277,7 @@ In practice, you might write a program to fill the queue using an amqp client li
274
277
275
278
``` shell
276
279
/usr/bin/amqp-declare-queue --url=$BROKER_URL -q job1 -d job1
277
-
278
- for f in apple banana cherry date fig grape lemon melon
280
+ for f in apple banana cherry date fig grape lemon melon
279
281
do
280
282
/usr/bin/amqp-publish --url=$BROKER_URL -r job1 -p -b $f
281
283
done
@@ -292,13 +294,14 @@ We will use the `amqp-consume` utility to read the message
292
294
from the queue and run our actual program. Here is a very simple
293
295
example program:
294
296
-->
295
- 这样,我们给队列中填充了8个消息 。
297
+ 这样,我们给队列中填充了 8 个消息 。
296
298
297
299
## 创建镜像 {#create-an-image}
298
300
299
301
现在我们可以创建一个做为 Job 来运行的镜像。
300
302
301
- 我们将用 ` amqp-consume ` 来从队列中读取消息并实际运行我们的程序。这里给出一个非常简单的示例程序:
303
+ 我们将用 ` amqp-consume ` 实用程序从队列中读取消息并运行实际的程序。
304
+ 这里给出一个非常简单的示例程序:
302
305
303
306
{{< codenew language="python" file="application/job/rabbitmq/worker.py" >}}
304
307
@@ -323,9 +326,9 @@ build the image with this command:
323
326
324
327
现在,编译镜像。如果你在用源代码树,那么切换到目录 ` examples/job/work-queue-1 ` 。
325
328
否则的话,创建一个临时目录,切换到这个目录。下载
326
- [ Dockerfile] ( /examples/application/job/rabbitmq/Dockerfile ) , 和
329
+ [ Dockerfile] ( /examples/application/job/rabbitmq/Dockerfile ) 和
327
330
[ worker.py] ( /examples/application/job/rabbitmq/worker.py ) 。
328
- 无论哪种情况,都可以用下面的命令编译镜像
331
+ 无论哪种情况,都可以用下面的命令编译镜像:
329
332
330
333
``` shell
331
334
docker build -t job-wq-1 .
@@ -367,7 +370,7 @@ image to match the name you used, and call it `./job.yaml`.
367
370
-->
368
371
## 定义 Job {#defining-a-job}
369
372
370
- 这里给出一个 Job 定义 yaml文件。你需要拷贝一份并编辑镜像以匹配你使用的名称 ,保存为 ` ./job.yaml ` 。
373
+ 这里给出一个 Job 定义 YAML 文件。你将需要拷贝一份 Job 并编辑该镜像以匹配你使用的名称 ,保存为 ` ./job.yaml ` 。
371
374
372
375
{{< codenew file="application/job/rabbitmq/job.yaml" >}}
373
376
@@ -380,7 +383,9 @@ done. So we set, `.spec.completions: 8` for the example, since we put 8 items i
380
383
381
384
So, now run the Job:
382
385
-->
383
- 本例中,每个 Pod 使用队列中的一个消息然后退出。这样,Job 的完成计数就代表了完成的工作项的数量。本例中我们设置 ` .spec.completions: 8 ` ,因为我们放了8项内容在队列中。
386
+ 本例中,每个 Pod 使用队列中的一个消息然后退出。
387
+ 这样,Job 的完成计数就代表了完成的工作项的数量。
388
+ 本例中我们设置 ` .spec.completions: 8 ` ,因为我们放了 8 项内容在队列中。
384
389
385
390
## 运行 Job {#running-the-job}
386
391
@@ -391,14 +396,23 @@ kubectl apply -f ./job.yaml
391
396
```
392
397
393
398
<!--
394
- Now wait a bit, then check on the job.
399
+ You can wait for the Job to succeed, with a timeout:
395
400
-->
396
- 稍等片刻,然后检查 Job。
401
+ 你可以等待 Job 在某个超时时间后成功:
397
402
398
403
``` shell
399
- kubectl describe jobs/job-wq-1
404
+ # 状况名称的检查不区分大小写
405
+ kubectl wait --for=condition=complete --timeout=300s job/job-wq-1
400
406
```
401
407
408
+ <!--
409
+ Next, check on the Job:
410
+ -->
411
+ 接下来查看 Job:
412
+
413
+ ``` shell
414
+ kubectl describe jobs/job-wq-1
415
+ ```
402
416
```
403
417
Name: job-wq-1
404
418
Namespace: default
@@ -436,9 +450,9 @@ Events:
436
450
```
437
451
438
452
<!--
439
- All our pods succeeded. Yay.
453
+ All the pods for that Job succeeded. Yay.
440
454
-->
441
- 我们所有的 Pod 都成功了 。耶!
455
+ 该 Job 的所有 Pod 都已成功 。耶!
442
456
443
457
<!-- discussion -->
444
458
@@ -456,8 +470,8 @@ want to consider one of the other [job patterns](/docs/concepts/workloads/contro
456
470
457
471
本文所讲述的处理方法的好处是你不需要修改你的 "worker" 程序使其知道工作队列的存在。
458
472
459
- 本文所描述的方法需要你运行一个消息队列服务。如果不方便运行消息队列服务,你也许会考虑另外一种
460
- [ 任务模式] ( /zh-cn/docs/concepts/workloads/controllers/job/#job-patterns ) 。
473
+ 本文所描述的方法需要你运行一个消息队列服务。如果不方便运行消息队列服务,
474
+ 你也许会考虑另外一种 [ 任务模式] ( /zh-cn/docs/concepts/workloads/controllers/job/#job-patterns ) 。
461
475
462
476
<!--
463
477
This approach creates a pod for every work item. If your work items only take a few seconds,
@@ -470,16 +484,15 @@ do not need to modify your program to be aware of the queue.
470
484
A [different example](/docs/tasks/job/fine-parallel-processing-work-queue/), shows how to
471
485
communicate with the work queue using a client library.
472
486
-->
473
-
474
487
本文所述的方法为每个工作项创建了一个 Pod。
475
- 如果你的工作项仅需数秒钟,为每个工作项创建 Pod会增加很多的常规消耗 。
488
+ 如果你的工作项仅需数秒钟,为每个工作项创建 Pod 会增加很多的常规消耗 。
476
489
可以考虑另外的方案请参考[ 示例] ( /zh-cn/docs/tasks/job/fine-parallel-processing-work-queue/ ) ,
477
490
这种方案可以实现每个 Pod 执行多个工作项。
478
491
479
492
示例中,我们使用 ` amqp-consume ` 从消息队列读取消息并执行我们真正的程序。
480
493
这样的好处是你不需要修改你的程序使其知道队列的存在。
481
- 要了解怎样使用客户端库和工作队列通信,请参考
482
- [ 不同的示例] ( /zh-cn/docs/tasks/job/fine-parallel-processing-work-queue/ ) 。
494
+ 要了解怎样使用客户端库和工作队列通信,
495
+ 请参考 [ 不同的示例] ( /zh-cn/docs/tasks/job/fine-parallel-processing-work-queue/ ) 。
483
496
484
497
<!--
485
498
## Caveats
0 commit comments