@@ -7,12 +7,15 @@ weight: 40
7
7
<!-- overview -->
8
8
9
9
<!--
10
- This page shows how a Pod can use a DownwardAPIVolumeFile to expose information
11
- about itself to Containers running in the Pod. A DownwardAPIVolumeFile can expose
12
- Pod fields and Container fields.
10
+ This page shows how a Pod can use a
11
+ [`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
12
+ to expose information about itself to Containers running in the Pod.
13
+ A `DownwardAPIVolumeFile` can expose Pod fields and Container fields.
13
14
-->
14
- 此页面描述 Pod 如何使用 DownwardAPIVolumeFile 把自己的信息呈现给 Pod 中运行的容器。
15
- DownwardAPIVolumeFile 可以呈现 Pod 的字段和容器字段。
15
+ 此页面描述 Pod 如何使用
16
+ [ ` DownwardAPIVolumeFile ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
17
+ 把自己的信息呈现给 Pod 中运行的容器。
18
+ ` DownwardAPIVolumeFile ` 可以呈现 Pod 和容器的字段。
16
19
17
20
## {{% heading "prerequisites" %}}
18
21
@@ -26,24 +29,27 @@ DownwardAPIVolumeFile 可以呈现 Pod 的字段和容器字段。
26
29
There are two ways to expose Pod and Container fields to a running Container:
27
30
28
31
* [Environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api)
29
- * Volume Files
32
+ * Volume files
33
+
34
+ Together, these two ways of exposing Pod and Container fields are called the
35
+ "Downward API".
30
36
-->
31
- ## Downward API
37
+ ## Downward API {#the-downward-api}
32
38
33
39
有两种方式可以将 Pod 和 Container 字段呈现给运行中的容器:
34
40
35
41
* [ 环境变量] ( /zh/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api )
36
42
* 卷文件
37
43
38
- 这两种呈现 Pod 和 Container 字段的方式都称为 * Downward API* 。
44
+ 这两种呈现 Pod 和 Container 字段的方式都称为 " Downward API" 。
39
45
40
46
<!--
41
47
## Store Pod fields
42
48
43
49
In this exercise, you create a Pod that has one Container.
44
50
Here is the configuration file for the Pod:
45
51
-->
46
- ## 存储 Pod 字段
52
+ ## 存储 Pod 字段 {#store-pod-fields}
47
53
48
54
在这个练习中,你将创建一个包含一个容器的 Pod。Pod 的配置文件如下:
49
55
@@ -88,7 +94,7 @@ kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume.yaml
88
94
```
89
95
90
96
<!--
91
- Verify that the Container in the Pod is running:
97
+ Verify that the container in the Pod is running:
92
98
-->
93
99
验证Pod中的容器运行正常:
94
100
@@ -97,7 +103,7 @@ kubectl get pods
97
103
```
98
104
99
105
<!--
100
- View the Container 's logs:
106
+ View the container 's logs:
101
107
-->
102
108
查看容器的日志:
103
109
@@ -120,7 +126,7 @@ builder="john-doe"
120
126
```
121
127
122
128
<!--
123
- Get a shell into the Container that is running in your Pod:
129
+ Get a shell into the container that is running in your Pod:
124
130
-->
125
131
进入 Pod 中运行的容器,打开一个 Shell:
126
132
@@ -194,11 +200,10 @@ total 8
194
200
<!--
195
201
Using symbolic links enables dynamic atomic refresh of the metadata; updates are
196
202
written to a new temporary directory, and the `..data` symlink is updated
197
- atomically using
198
- [rename(2)](http://man7.org/linux/man-pages/man2/rename.2.html).
203
+ atomically using [rename(2)](http://man7.org/linux/man-pages/man2/rename.2.html).
199
204
-->
200
205
用符号链接可实现元数据的动态原子性刷新;更新将写入一个新的临时目录,
201
- 然后通过使用[ rename(2)] ( http://man7.org/linux/man-pages/man2/rename.2.html )
206
+ 然后通过使用 [ rename(2)] ( http://man7.org/linux/man-pages/man2/rename.2.html )
202
207
完成 ` ..data ` 符号链接的原子性更新。
203
208
204
209
<!--
@@ -218,24 +223,30 @@ Downward API,则该容器无法收到更新事件。
218
223
/# exit
219
224
```
220
225
221
- ## 存储容器字段
222
-
223
226
<!--
224
- The preceding exercise, you stored Pod fields in a DownwardAPIVolumeFile.
227
+ ## Store Container fields
228
+
229
+ The preceding exercise, you stored Pod fields in a
230
+ [`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)..
225
231
In this next exercise, you store Container fields. Here is the configuration
226
232
file for a Pod that has one Container:
227
233
-->
228
- 前面的练习中,你将 Pod 字段保存到 DownwardAPIVolumeFile 中。
234
+ ## 存储容器字段 {#store-container-fields}
235
+
236
+ 前面的练习中,你将 Pod 字段保存到
237
+ [ ` DownwardAPIVolumeFile ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
238
+ 中。
229
239
接下来这个练习,你将存储 Container 字段。这里是包含一个容器的 Pod 的配置文件:
230
240
231
241
{{< codenew file="pods/inject/dapi-volume-resources.yaml" >}}
232
242
233
243
<!--
234
- In the configuration file, you can see that the Pod has a `downwardAPI` Volume,
235
- and the Container mounts the Volume at `/etc/podinfo`.
244
+ In the configuration file, you can see that the Pod has a
245
+ [`downwardAPI` volume](/concepts/storage/volumes/#downwardapi),
246
+ and the Container mounts the volume at `/etc/podinfo`.
236
247
237
248
Look at the `items` array under `downwardAPI`. Each element of the array is a
238
- DownwardAPIVolumeFile.
249
+ [` DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core) .
239
250
240
251
The first element specifies that in the Container named `client-container`,
241
252
the value of the `limits.cpu` field in the format specified by `1m` should be
@@ -244,13 +255,16 @@ default value of `1` which means cores for cpu and bytes for memory.
244
255
245
256
Create the Pod:
246
257
-->
247
- 在这个配置文件中,你可以看到 Pod 有一个 ` downwardAPI ` 类型的卷,并且挂载到容器的
248
- ` /etc/podinfo ` 目录。
258
+ 在这个配置文件中,你可以看到 Pod 有一个
259
+ [ ` downwardAPI ` 卷] ( /zh/docs/concepts/storage/volumes/#downwardapi ) ,
260
+ 并且挂载到容器的 ` /etc/podinfo ` 目录。
249
261
250
- 查看 ` downwardAPI ` 下面的 ` items ` 数组。每个数组元素都是一个 DownwardAPIVolumeFile。
262
+ 查看 ` downwardAPI ` 下面的 ` items ` 数组。每个数组元素都是一个
263
+ [ ` DownwardAPIVolumeFile ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)。
251
264
252
- 第一个元素指定名为 ` client-container ` 的容器中 ` limits.cpu ` 字段的值应保存在名为
253
- ` cpu_limit ` 的文件中。
265
+ 第一个元素指定在名为 ` client-container ` 的容器中,
266
+ 以 ` 1m ` 所指定格式的 ` limits.cpu ` 字段的值应保存在名为 ` cpu_limit ` 的文件中。
267
+ ` divisor ` 字段是可选的,默认值为 ` 1 ` ,表示 CPU 的核心和内存的字节。
254
268
255
269
创建Pod:
256
270
@@ -259,7 +273,7 @@ kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume-resources.yaml
259
273
```
260
274
261
275
<!--
262
- Get a shell into the Container that is running in your Pod:
276
+ Get a shell into the container that is running in your Pod:
263
277
-->
264
278
打开一个 Shell,进入 Pod 中运行的容器:
265
279
@@ -283,39 +297,50 @@ You can use similar commands to view the `cpu_request`, `mem_limit` and
283
297
你可以使用同样的命令查看 ` cpu_request ` 、` mem_limit ` 和 ` mem_request ` 文件.
284
298
285
299
<!-- discussion -->
300
+
301
+ <!-- TODO: This section should be extracted out of the task page. -->
286
302
<!--
287
303
## Capabilities of the Downward API
288
304
-->
289
- ## Downward API 的能力
305
+ ## Downward API 的能力 {#capabilities-of-the-downward-api}
290
306
291
307
<!--
292
308
The following information is available to containers through environment
293
309
variables and `downwardAPI` volumes:
294
310
295
311
* Information available via `fieldRef`:
312
+
296
313
* `metadata.name` - the pod's name
297
314
* `metadata.namespace` - the pod's namespace
298
315
* `metadata.uid` - the pod's UID
299
- * `metadata.labels['<KEY>']` - the value of the pod's label `<KEY>` (for example, `metadata.labels['mylabel']`)
300
- * `metadata.annotations['<KEY>']` - the value of the pod's annotation `<KEY>` (for example, `metadata.annotations['myannotation']`)
316
+ * `metadata.labels['<KEY>']` - the value of the pod's label `<KEY>`
317
+ (for example, `metadata.labels['mylabel']`)
318
+ * `metadata.annotations['<KEY>']` - the value of the pod's annotation `<KEY>`
319
+ (for example, `metadata.annotations['myannotation']`)
301
320
-->
302
321
下面这些信息可以通过环境变量和 ` downwardAPI ` 卷提供给容器:
303
322
304
323
* 能通过 ` fieldRef ` 获得的:
324
+
305
325
* ` metadata.name ` - Pod 名称
306
326
* ` metadata.namespace ` - Pod 名字空间
307
327
* ` metadata.uid ` - Pod 的 UID
308
- * ` metadata.labels['<KEY>'] ` - Pod 标签 ` <KEY> ` 的值 (例如, ` metadata.labels['mylabel'] ` )
309
- * ` metadata.annotations['<KEY>'] ` - Pod 的注解 ` <KEY> ` 的值(例如, ` metadata.annotations['myannotation'] ` )
328
+ * ` metadata.labels['<KEY>'] ` - Pod 标签 ` <KEY> ` 的值
329
+ (例如:` metadata.labels['mylabel'] ` )
330
+ * ` metadata.annotations['<KEY>'] ` - Pod 的注解 ` <KEY> ` 的值
331
+ (例如:` metadata.annotations['myannotation'] ` )
310
332
311
333
<!--
312
334
* Information available via `resourceFieldRef`:
335
+
313
336
* A Container's CPU limit
314
337
* A Container's CPU request
315
338
* A Container's memory limit
316
339
* A Container's memory request
317
- * A Container's hugepages limit (providing that the `DownwardAPIHugePages` [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is enabled)
318
- * A Container's hugepages request (providing that the `DownwardAPIHugePages` [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is enabled)
340
+ * A Container's hugepages limit (provided that the `DownwardAPIHugePages`
341
+ [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is enabled)
342
+ * A Container's hugepages request (provided that the `DownwardAPIHugePages`
343
+ [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is enabled)
319
344
* A Container's ephemeral-storage limit
320
345
* A Container's ephemeral-storage request
321
346
-->
@@ -324,8 +349,10 @@ variables and `downwardAPI` volumes:
324
349
* 容器的 CPU 请求值
325
350
* 容器的内存约束值
326
351
* 容器的内存请求值
327
- * 容器的巨页限制值(前提是启用了 ` DownwardAPIHugePages ` [ 特性门控] ( /zh/docs/reference/command-line-tools-reference/feature-gates/ ) )
328
- * 容器的巨页请求值(前提是启用了 ` DownwardAPIHugePages ` [ 特性门控] ( /zh/docs/reference/command-line-tools-reference/feature-gates/ ) )
352
+ * 容器的巨页限制值(前提是启用了 ` DownwardAPIHugePages `
353
+ [ 特性门控] ( /zh/docs/reference/command-line-tools-reference/feature-gates/ ) )
354
+ * 容器的巨页请求值(前提是启用了 ` DownwardAPIHugePages `
355
+ [ 特性门控] ( /zh/docs/reference/command-line-tools-reference/feature-gates/ ) )
329
356
* 容器的临时存储约束值
330
357
* 容器的临时存储请求值
331
358
@@ -334,23 +361,33 @@ In addition, the following information is available through
334
361
`downwardAPI` volume `fieldRef`:
335
362
-->
336
363
此外,以下信息可通过 ` downwardAPI ` 卷从 ` fieldRef ` 获得:
364
+
337
365
<!--
338
- * `metadata.labels` - all of the pod’s labels, formatted as `label-key="escaped-label-value"` with one label per line
339
- * `metadata.annotations` - all of the pod’s annotations, formatted as `annotation-key="escaped-annotation-value"` with one annotation per line
366
+ * `metadata.labels` - all of the pod's labels, formatted as `label-key="escaped-label-value"`
367
+ with one label per line
368
+ * `metadata.annotations` - all of the pod's annotations, formatted as
369
+ `annotation-key="escaped-annotation-value"` with one annotation per line
340
370
-->
341
- * ` metadata.labels ` - Pod 的所有标签,以 ` label-key="escaped-label-value" ` 格式显示,每行显示一个标签
342
- * ` metadata.annotations ` - Pod 的所有注解,以 ` annotation-key="escaped-annotation-value" `
343
- 格式显示,每行显示一个标签
371
+ * ` metadata.labels ` - Pod 的所有标签,以
372
+ ` label-key="escaped-label-value" ` 格式显示,每行显示一个标签
373
+ * ` metadata.annotations ` - Pod 的所有注解,以
374
+ ` annotation-key="escaped-annotation-value" ` 格式显示,每行显示一个标签
344
375
345
376
<!--
346
377
The following information is available through environment variables:
378
+
379
+ * `status.podIP` - the pod's IP address
380
+ * `spec.serviceAccountName` - the pod's service account name
381
+ * `spec.nodeName` - the name of the node to which the scheduler always attempts to
382
+ schedule the pod
383
+ * `status.hostIP` - the IP of the node to which the Pod is assigned
347
384
-->
348
385
以下信息可通过环境变量获得:
349
386
350
- * ` status.podIP ` - 节点 IP
351
- * ` spec.serviceAccountName ` - Pod 服务帐号名称, 版本要求 v1.4.0-alpha.3
352
- * ` spec.nodeName ` - 节点名称, 版本要求 v1.4.0-alpha.3
353
- * ` status.hostIP ` - 节点 IP, 版本要求 v1.7.0-alpha.1
387
+ * ` status.podIP ` - Pod IP 地址
388
+ * ` spec.serviceAccountName ` - Pod 服务帐号名称
389
+ * ` spec.nodeName ` - 调度器总是尝试将 Pod 调度到的节点的名称
390
+ * ` status.hostIP ` - Pod 分配到的节点的 IP
354
391
355
392
<!--
356
393
If CPU and memory limits are not specified for a Container, the
@@ -368,15 +405,15 @@ You can project keys to specific paths and specific permissions on a per-file
368
405
basis. For more information, see
369
406
[Secrets](/docs/concepts/configuration/secret/).
370
407
-->
371
- ## 投射键名到指定路径并且指定文件权限
408
+ ## 投射键名到指定路径并且指定文件权限 {#project-keys-to-specific-paths-and-file-permissions}
372
409
373
410
你可以将键名投射到指定路径并且指定每个文件的访问权限。
374
411
更多信息,请参阅[ Secrets] ( /zh/docs/concepts/configuration/secret/ ) .
375
412
376
413
<!--
377
414
## Motivation for the Downward API
378
415
379
- It is sometimes useful for a Container to have information about itself, without
416
+ It is sometimes useful for a container to have information about itself, without
380
417
being overly coupled to Kubernetes. The Downward API allows containers to consume
381
418
information about themselves or the cluster without using the Kubernetes client
382
419
or API server.
@@ -387,7 +424,7 @@ application, but that is tedious and error prone, and it violates the goal of lo
387
424
coupling. A better option would be to use the Pod's name as an identifier, and
388
425
inject the Pod's name into the well-known environment variable.
389
426
-->
390
- ## Downward API的动机
427
+ ## Downward API 的动机 {#motivation-for-the-downward-api}
391
428
392
429
对于容器来说,有时候拥有自己的信息是很有用的,可避免与 Kubernetes 过度耦合。
393
430
Downward API 使得容器使用自己或者集群的信息,而不必通过 Kubernetes 客户端或
@@ -399,9 +436,32 @@ API 服务器来获得。
399
436
400
437
## {{% heading "whatsnext" %}}
401
438
402
- * [ PodSpec] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
403
- * [ Volume] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
404
- * [ DownwardAPIVolumeSource] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumesource-v1-core)
405
- * [ DownwardAPIVolumeFile] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
406
- * [ ResourceFieldSelector] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
439
+ <!--
440
+ * Check the [`PodSpec`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
441
+ API definition which defines the desired state of a Pod.
442
+ * Check the [`Volume`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
443
+ API definition which defines a generic volume in a Pod for containers to access.
444
+ * Check the [`DownwardAPIVolumeSource`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumesource-v1-core)
445
+ API definition which defines a volume that contains Downward API information.
446
+ * Check the [`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
447
+ API definition which contains references to object or resource fields for
448
+ populating a file in the Downward API volume.
449
+ * Check the [`ResourceFieldSelector`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
450
+ API definition which specifies the container resources and their output format.
451
+ -->
452
+ * 参阅
453
+ [ ` PodSpec ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
454
+ API 定义,该 API 定义 Pod 所需状态。
455
+ * 参阅
456
+ [ ` Volume ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
457
+ API 定义,该 API 在 Pod 中定义通用卷以供容器访问。
458
+ * 参阅
459
+ [ ` DownwardAPIVolumeSource ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumesource-v1-core)
460
+ API 定义,该 API 定义包含 Downward API 信息的卷。
461
+ * 参阅
462
+ [ ` DownwardAPIVolumeFile ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
463
+ API 定义,该 API 包含对对象或资源字段的引用,用于在 Downward API 卷中填充文件。
464
+ * 参阅
465
+ [ ` ResourceFieldSelector ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
466
+ API 定义,该 API 指定容器资源及其输出格式。
407
467
0 commit comments