@@ -8,71 +8,68 @@ weight: 40
8
8
9
9
<!--
10
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.
11
+ [`downwardAPI` volume ](/docs/concepts/storage/volumes/#downwardapi),
12
+ to expose information about itself to containers running in the Pod.
13
+ A `downwardAPI` volume can expose Pod fields and container fields.
14
14
-->
15
15
此页面描述 Pod 如何使用
16
- [ ` DownwardAPIVolumeFile ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core )
16
+ [ ` downwardAPI ` 卷 ] ( /zh-cn/ docs/concepts/storage/volumes/#downwardapi )
17
17
把自己的信息呈现给 Pod 中运行的容器。
18
- ` DownwardAPIVolumeFile ` 可以呈现 Pod 和容器的字段。
19
-
20
- ## {{% heading "prerequisites" %}}
21
-
22
- {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
23
-
24
- <!-- steps -->
18
+ ` downwardAPI ` 可以呈现 Pod 和容器的字段。
25
19
26
20
<!--
27
- ## The Downward API
28
-
29
- There are two ways to expose Pod and Container fields to a running Container:
21
+ In Kubernetes, there are two ways to expose Pod and container fields to a running container:
30
22
31
23
* [Environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api)
32
- * Volume files
24
+ * Volume files, as explained in this task
33
25
34
- Together, these two ways of exposing Pod and Container fields are called the
35
- "Downward API" .
26
+ Together, these two ways of exposing Pod and container fields are called the
27
+ _downward API_ .
36
28
-->
37
- ## Downward API {#the-downward-api}
38
-
39
- 有两种方式可以将 Pod 和 Container 字段呈现给运行中的容器:
29
+ 在 Kubernetes 中,有两种方式可以将 Pod 和容器字段呈现给运行中的容器:
40
30
41
31
* [ 环境变量] ( /zh-cn/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api )
42
- * 卷文件
32
+ * 本页面所述的卷文件
33
+
34
+ 这两种呈现 Pod 和容器字段的方式都称为 ** downward API** 。
35
+
36
+ ## {{% heading "prerequisites" %}}
37
+
38
+ {{< include "task-tutorial-prereqs.md" >}}
43
39
44
- 这两种呈现 Pod 和 Container 字段的方式都称为 "Downward API"。
40
+
41
+ <!-- steps -->
45
42
46
43
<!--
47
44
## Store Pod fields
48
45
49
- In this exercise, you create a Pod that has one Container.
50
- Here is the configuration file for the Pod:
46
+ In this part of exercise, you create a Pod that has one container, and you
47
+ project Pod-level fields into the running container as files.
48
+ Here is the manifest for the Pod:
51
49
-->
52
50
## 存储 Pod 字段 {#store-pod-fields}
53
51
54
- 在这个练习中,你将创建一个包含一个容器的 Pod。Pod 的配置文件如下:
52
+ 在这部分的练习中,你将创建一个包含一个容器的 Pod,并将 Pod 级别的字段作为文件映射到正在运行的容器中。
53
+ Pod 的清单如下:
55
54
56
55
{{< codenew file="pods/inject/dapi-volume.yaml" >}}
57
56
58
57
<!--
59
- In the configuration file , you can see that the Pod has a `downwardAPI` Volume,
60
- and the Container mounts the Volume at `/etc/podinfo`.
58
+ In the manifest , you can see that the Pod has a `downwardAPI` Volume,
59
+ and the container mounts the volume at `/etc/podinfo`.
61
60
62
- Look at the `items` array under `downwardAPI`. Each element of the array is a
63
- [DownwardAPIVolumeFile](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core) .
61
+ Look at the `items` array under `downwardAPI`. Each element of the array
62
+ defines a `downwardAPI` volume .
64
63
The first element specifies that the value of the Pod's
65
64
`metadata.labels` field should be stored in a file named `labels`.
66
65
The second element specifies that the value of the Pod's `annotations`
67
66
field should be stored in a file named `annotations`.
68
67
-->
69
- 在配置文件中 ,你可以看到 Pod 有一个 ` downwardAPI ` 类型的卷,并且挂载到容器中的
68
+ 在 Pod 清单中 ,你可以看到 Pod 有一个 ` downwardAPI ` 类型的卷,并且挂载到容器中的
70
69
` /etc/podinfo ` 目录。
71
70
72
71
查看 ` downwardAPI ` 下面的 ` items ` 数组。
73
- 每个数组元素都是一个
74
- [ DownwardAPIVolumeFile] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
75
- 对象。
72
+ 数组的每个元素定义一个 ` downwardAPI ` 卷。
76
73
第一个元素指示 Pod 的 ` metadata.labels ` 字段的值保存在名为 ` labels ` 的文件中。
77
74
第二个元素指示 Pod 的 ` annotations ` 字段的值保存在名为 ` annotations ` 的文件中。
78
75
@@ -81,7 +78,7 @@ The fields in this example are Pod fields. They are not
81
78
fields of the Container in the Pod.
82
79
-->
83
80
{{< note >}}
84
- 本示例中的字段是Pod字段,不是Pod中容器的字段 。
81
+ 本示例中的字段是 Pod 字段,不是 Pod 中容器的字段 。
85
82
{{< /note >}}
86
83
87
84
<!--
@@ -227,47 +224,50 @@ Exit the shell:
227
224
```
228
225
229
226
<!--
230
- ## Store Container fields
227
+ ## Store container fields
231
228
232
- The preceding exercise, you stored Pod fields in a
233
- [`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)..
234
- In this next exercise, you store Container fields. Here is the configuration
235
- file for a Pod that has one Container:
229
+ The preceding exercise, you made Pod-level fields accessible using the
230
+ downward API.
231
+ In this next exercise, you are going to pass fields that are part of the Pod
232
+ definition, but taken from the specific
233
+ [container](/docs/reference/kubernetes-api/workload-resources/pod-v1/#Container)
234
+ rather than from the Pod overall. Here is a manifest for a Pod that again has
235
+ just one container:
236
236
-->
237
237
## 存储容器字段 {#store-container-fields}
238
238
239
- 前面的练习中,你将 Pod 字段保存到
240
- [ ` DownwardAPIVolumeFile ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
241
- 中。
242
- 接下来这个练习,你将存储 Container 字段。这里是包含一个容器的 Pod 的配置文件:
239
+ 前面的练习中,你使用 downward API 使 Pod 级别的字段可以被 Pod 内正在运行的容器访问。
240
+ 接下来这个练习,你将只传递由 Pod 定义的部分的字段到 Pod 内正在运行的容器中,但这些字段取自特定[ 容器] ( /zh-cn/docs/reference/kubernetes-api/workload-resources/pod-v1/#Container ) 而不是整个 Pod。
241
+ 下面是一个同样只有一个容器的 Pod 的清单:
243
242
244
243
{{< codenew file="pods/inject/dapi-volume-resources.yaml" >}}
245
244
246
245
<!--
247
- In the configuration file , you can see that the Pod has a
246
+ In the manifest , you can see that the Pod has a
248
247
[`downwardAPI` volume](/docs/concepts/storage/volumes/#downwardapi),
249
- and the Container mounts the volume at `/etc/podinfo`.
248
+ and that the single container in that Pod mounts the volume at `/etc/podinfo`.
250
249
251
- Look at the `items` array under `downwardAPI`. Each element of the array is a
252
- [`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core) .
250
+ Look at the `items` array under `downwardAPI`. Each element of the array
251
+ defines a file in the downward API volume .
253
252
254
- The first element specifies that in the Container named `client-container`,
253
+ The first element specifies that in the container named `client-container`,
255
254
the value of the `limits.cpu` field in the format specified by `1m` should be
256
- stored in a file named `cpu_limit`. The `divisor` field is optional and has the
257
- default value of `1` which means cores for cpu and bytes for memory.
255
+ published as a file named `cpu_limit`. The `divisor` field is optional and has the
256
+ default value of `1`. A divisor of 1 means cores for `cpu` resources, or
257
+ bytes for `memory` resources.
258
258
259
259
Create the Pod:
260
260
-->
261
- 在这个配置文件中 ,你可以看到 Pod 有一个
261
+ 在这个清单中 ,你可以看到 Pod 有一个
262
262
[ ` downwardAPI ` 卷] ( /zh-cn/docs/concepts/storage/volumes/#downwardapi ) ,
263
- 并且挂载到容器的 ` /etc/podinfo ` 目录。
263
+ 并且这个会挂载到 Pod 内的单个容器的 ` /etc/podinfo ` 目录。
264
264
265
- 查看 ` downwardAPI ` 下面的 ` items ` 数组。每个数组元素都是一个
266
- [ ` DownwardAPIVolumeFile ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core) 。
265
+ 查看 ` downwardAPI ` 下面的 ` items ` 数组。
266
+ 数组的每个元素定义一个 ` downwardAPI ` 卷 。
267
267
268
268
第一个元素指定在名为 ` client-container ` 的容器中,
269
- 以 ` 1m ` 所指定格式的 ` limits.cpu ` 字段的值应保存在名为 ` cpu_limit ` 的文件中。
270
- ` divisor ` 字段是可选的,默认值为 ` 1 ` ,表示 CPU 的核心和内存的字节 。
269
+ 以 ` 1m ` 所指定格式的 ` limits.cpu ` 字段的值应推送到名为 ` cpu_limit ` 的文件中。
270
+ ` divisor ` 字段是可选的,默认值为 ` 1 ` ,1 的除数表示 CPU 资源的核心或内存资源的字节 。
271
271
272
272
创建Pod:
273
273
@@ -290,7 +290,8 @@ In your shell, view the `cpu_limit` file:
290
290
在 Shell 中,查看 ` cpu_limit ` 文件:
291
291
292
292
``` shell
293
- /# cat /etc/podinfo/cpu_limit
293
+ # 在容器内的 Shell 中运行
294
+ cat /etc/podinfo/cpu_limit
294
295
```
295
296
296
297
<!--
@@ -301,106 +302,6 @@ You can use similar commands to view the `cpu_request`, `mem_limit` and
301
302
302
303
<!-- discussion -->
303
304
304
- <!-- TODO: This section should be extracted out of the task page. -->
305
- <!--
306
- ## Capabilities of the Downward API
307
- -->
308
- ## Downward API 的能力 {#capabilities-of-the-downward-api}
309
-
310
- <!--
311
- The following information is available to containers through environment
312
- variables and `downwardAPI` volumes:
313
-
314
- * Information available via `fieldRef`:
315
-
316
- * `metadata.name` - the pod's name
317
- * `metadata.namespace` - the pod's namespace
318
- * `metadata.uid` - the pod's UID
319
- * `metadata.labels['<KEY>']` - the value of the pod's label `<KEY>`
320
- (for example, `metadata.labels['mylabel']`)
321
- * `metadata.annotations['<KEY>']` - the value of the pod's annotation `<KEY>`
322
- (for example, `metadata.annotations['myannotation']`)
323
- -->
324
- 下面这些信息可以通过环境变量和 ` downwardAPI ` 卷提供给容器:
325
-
326
- * 能通过 ` fieldRef ` 获得的:
327
-
328
- * ` metadata.name ` - Pod 名称
329
- * ` metadata.namespace ` - Pod 名字空间
330
- * ` metadata.uid ` - Pod 的 UID
331
- * ` metadata.labels['<KEY>'] ` - Pod 标签 ` <KEY> ` 的值
332
- (例如:` metadata.labels['mylabel'] ` )
333
- * ` metadata.annotations['<KEY>'] ` - Pod 的注解 ` <KEY> ` 的值
334
- (例如:` metadata.annotations['myannotation'] ` )
335
-
336
- <!--
337
- * Information available via `resourceFieldRef`:
338
-
339
- * A Container's CPU limit
340
- * A Container's CPU request
341
- * A Container's memory limit
342
- * A Container's memory request
343
- * A Container's hugepages limit (provided that the `DownwardAPIHugePages`
344
- [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is enabled)
345
- * A Container's hugepages request (provided that the `DownwardAPIHugePages`
346
- [feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is enabled)
347
- * A Container's ephemeral-storage limit
348
- * A Container's ephemeral-storage request
349
- -->
350
- * 能通过 ` resourceFieldRef ` 获得的:
351
- * 容器的 CPU 约束值
352
- * 容器的 CPU 请求值
353
- * 容器的内存约束值
354
- * 容器的内存请求值
355
- * 容器的巨页限制值(前提是启用了 ` DownwardAPIHugePages `
356
- [ 特性门控] ( /zh-cn/docs/reference/command-line-tools-reference/feature-gates/ ) )
357
- * 容器的巨页请求值(前提是启用了 ` DownwardAPIHugePages `
358
- [ 特性门控] ( /zh-cn/docs/reference/command-line-tools-reference/feature-gates/ ) )
359
- * 容器的临时存储约束值
360
- * 容器的临时存储请求值
361
-
362
- <!--
363
- In addition, the following information is available through
364
- `downwardAPI` volume `fieldRef`:
365
- -->
366
- 此外,以下信息可通过 ` downwardAPI ` 卷从 ` fieldRef ` 获得:
367
-
368
- <!--
369
- * `metadata.labels` - all of the pod's labels, formatted as `label-key="escaped-label-value"`
370
- with one label per line
371
- * `metadata.annotations` - all of the pod's annotations, formatted as
372
- `annotation-key="escaped-annotation-value"` with one annotation per line
373
- -->
374
- * ` metadata.labels ` - Pod 的所有标签,以
375
- ` label-key="escaped-label-value" ` 格式显示,每行显示一个标签
376
- * ` metadata.annotations ` - Pod 的所有注解,以
377
- ` annotation-key="escaped-annotation-value" ` 格式显示,每行显示一个标签
378
-
379
- <!--
380
- The following information is available through environment variables:
381
-
382
- * `status.podIP` - the pod's IP address
383
- * `spec.serviceAccountName` - the pod's service account name
384
- * `spec.nodeName` - the name of the node to which the scheduler always attempts to
385
- schedule the pod
386
- * `status.hostIP` - the IP of the node to which the Pod is assigned
387
- -->
388
- 以下信息可通过环境变量获得:
389
-
390
- * ` status.podIP ` - Pod IP 地址
391
- * ` spec.serviceAccountName ` - Pod 服务帐号名称
392
- * ` spec.nodeName ` - 调度器总是尝试将 Pod 调度到的节点的名称
393
- * ` status.hostIP ` - Pod 分配到的节点的 IP
394
-
395
- <!--
396
- If CPU and memory limits are not specified for a Container, the
397
- Downward API defaults to the node allocatable value for CPU and memory.
398
- -->
399
- {{< note >}}
400
- 如果容器未指定 CPU 和内存限制,则 Downward API 默认将节点可分配值
401
- 视为容器的 CPU 和内存限制。
402
- {{< /note >}}
403
-
404
305
<!--
405
306
## Project keys to specific paths and file permissions
406
307
@@ -413,35 +314,15 @@ basis. For more information, see
413
314
你可以将键名投射到指定路径并且指定每个文件的访问权限。
414
315
更多信息,请参阅 [ Secret] ( /zh-cn/docs/concepts/configuration/secret/ ) 。
415
316
416
- <!--
417
- ## Motivation for the Downward API
418
-
419
- It is sometimes useful for a container to have information about itself, without
420
- being overly coupled to Kubernetes. The Downward API allows containers to consume
421
- information about themselves or the cluster without using the Kubernetes client
422
- or API server.
423
-
424
- An example is an existing application that assumes a particular well-known
425
- environment variable holds a unique identifier. One possibility is to wrap the
426
- application, but that is tedious and error prone, and it violates the goal of low
427
- coupling. A better option would be to use the Pod's name as an identifier, and
428
- inject the Pod's name into the well-known environment variable.
429
- -->
430
- ## Downward API 的动机 {#motivation-for-the-downward-api}
431
-
432
- 对于容器来说,有时候拥有自己的信息是很有用的,可避免与 Kubernetes 过度耦合。
433
- Downward API 使得容器使用自己或者集群的信息,而不必通过 Kubernetes 客户端或
434
- API 服务器来获得。
435
-
436
- 一个例子是有一个现有的应用假定要用一个非常熟悉的环境变量来保存一个唯一标识。
437
- 一种可能是给应用增加处理层,但这样是冗余和易出错的,而且它违反了低耦合的目标。
438
- 更好的选择是使用 Pod 名称作为标识,把 Pod 名称注入这个环境变量中。
439
-
440
317
## {{% heading "whatsnext" %}}
441
318
442
319
<!--
443
- * Check the [`PodSpec`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
444
- API definition which defines the desired state of a Pod.
320
+ * Read the [`spec`](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec)
321
+ API definition for Pod. This includes the definition of Container (part of Pod).
322
+ * Read the list of [available fields](/docs/concepts/workloads/pods/downward-api/#available-fields) that you
323
+ can expose using the downward API.
324
+
325
+ Read about volumes in the legacy API reference:
445
326
* Check the [`Volume`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
446
327
API definition which defines a generic volume in a Pod for containers to access.
447
328
* Check the [`DownwardAPIVolumeSource`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumesource-v1-core)
@@ -452,9 +333,11 @@ API 服务器来获得。
452
333
* Check the [`ResourceFieldSelector`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
453
334
API definition which specifies the container resources and their output format.
454
335
-->
455
- * 参阅
456
- [ ` PodSpec ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
457
- API 定义,该 API 定义 Pod 所需状态。
336
+ * 参阅 Pod [ ` spec ` ] ( /zh-cn/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec )
337
+ API 的定义,其中包括了容器(Pod 的一部分)的定义。
338
+ * 参阅你可以使用 downward API 公开的[ 可用字段] ( /zh-cn/docs/concepts/workloads/pods/downward-api/#available-fields ) 列表。
339
+
340
+ 阅读旧版的 API 参考中关于卷的内容:
458
341
* 参阅
459
342
[ ` Volume ` ] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
460
343
API 定义,该 API 在 Pod 中定义通用卷以供容器访问。
0 commit comments