@@ -12,66 +12,63 @@ weight: 30
12
12
13
13
<!--
14
14
This page shows how a Pod can use environment variables to expose information
15
- about itself to Containers running in the Pod. Environment variables can expose
16
- Pod fields and Container fields.
15
+ about itself to containers running in the Pod, using the _downward API_.
16
+ You can use environment variables to expose Pod fields, container fields, or both .
17
17
-->
18
- 此页面展示 Pod 如何使用环境变量把自己的信息呈现给 Pod 中运行的容器。
19
- 环境变量可以呈现 Pod 的字段和容器字段 。
18
+ 此页面展示 Pod 如何使用环境变量把自身的信息呈现给 Pod 中运行的容器。
19
+ 使用 ** downward API ** 你可以使用环境变量来呈现 Pod 的字段、容器字段或两者 。
20
20
21
- ## {{% heading "prerequisites" %}}
21
+ <!--
22
+ In Kubernetes, there are two ways to expose Pod and container fields to a running container:
22
23
23
- {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
24
+ * _Environment variables_, as explained in this task
25
+ * [Volume files](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/)
24
26
25
- <!-- steps -->
27
+ Together, these two ways of exposing Pod and container fields are called the
28
+ downward API.
29
+ -->
30
+ 在 Kubernetes 中有两种方式可以将 Pod 和 容器字段呈现给运行中的容器:
26
31
27
- ## Downward API
32
+ * 如本任务所述的** 环境变量** 。
33
+ * [ 卷文件] ( /zh-cn/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/ )
28
34
29
- <!--
30
- There are two ways to expose Pod and Container fields to a running Container:
35
+ 这两种呈现 Pod 和 容器字段的方式统称为 downward API。
31
36
32
- * Environment variables
33
- * [Volume Files](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api)
34
37
35
- Together, these two ways of exposing Pod and Container fields are called the
36
- *Downward API*.
37
- -->
38
-
39
- 有两种方式可以将 Pod 和 Container 字段呈现给运行中的容器:
38
+ ## {{% heading "prerequisites" %}}
40
39
41
- * 环境变量
42
- * [ 卷文件] ( /zh-cn/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api )
40
+ {{< include "task-tutorial-prereqs.md" >}}
43
41
44
- 这两种呈现 Pod 和 Container 字段的方式统称为 * Downward API * 。
42
+ <!-- steps -->
45
43
46
44
<!--
47
45
## Use Pod fields as values for environment variables
48
46
49
- In this exercise, you create a Pod that has one Container. Here is the
50
- configuration file for the Pod:
47
+ In this part of exercise, you create a Pod that has one container, and you
48
+ project Pod-level fields into the running container as environment variables.
51
49
-->
52
50
## 用 Pod 字段作为环境变量的值
53
51
54
- 在这个练习中 ,你将创建一个包含一个容器的 Pod。这是该 Pod 的配置文件:
52
+ 在这部分练习中 ,你将创建一个包含一个容器的 Pod。并将 Pod 级别的字段作为环境变量映射到正在运行的容器中。
55
53
56
54
{{< codenew file="pods/inject/dapi-envars-pod.yaml" >}}
57
55
58
56
<!--
59
- In the configuration file , you can see five environment variables. The `env`
57
+ In that manifest , you can see five environment variables. The `env`
60
58
field is an array of
61
- [EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core) .
59
+ environment variable definitions .
62
60
The first element in the array specifies that the `MY_NODE_NAME` environment
63
61
variable gets its value from the Pod's `spec.nodeName` field. Similarly, the
64
62
other environment variables get their names from Pod fields.
65
63
-->
66
- 这个配置文件中,你可以看到五个环境变量。` env ` 字段是一个
67
- [ EnvVars] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
68
- 对象的数组。
64
+ 这个清单中,你可以看到五个环境变量。` env ` 字段定义了一组环境变量。
65
+
69
66
数组中第一个元素指定 ` MY_NODE_NAME ` 这个环境变量从 Pod 的 ` spec.nodeName ` 字段获取变量值。
70
67
同样,其它环境变量也是从 Pod 的字段获取它们的变量值。
71
68
72
69
<!--
73
70
The fields in this example are Pod fields. They are not fields of the
74
- Container in the Pod.
71
+ container in the Pod.
75
72
-->
76
73
{{< note >}}
77
74
本示例中的字段是 Pod 字段,不是 Pod 中 Container 的字段。
@@ -91,16 +88,17 @@ Verify that the Container in the Pod is running:
91
88
-->
92
89
验证 Pod 中的容器运行正常:
93
90
94
- ```
91
+ ``` shell
92
+ # 如果新创建的 Pod 还是处于不健康状态,请重新运行此命令几次。
95
93
kubectl get pods
96
94
```
97
95
98
96
<!--
99
- View the Container 's logs:
97
+ View the container 's logs:
100
98
-->
101
99
查看容器日志:
102
100
103
- ```
101
+ ``` shell
104
102
kubectl logs dapi-envars-fieldref
105
103
```
106
104
@@ -119,17 +117,17 @@ default
119
117
120
118
<!--
121
119
To see why these values are in the log, look at the `command` and `args` fields
122
- in the configuration file. When the Container starts, it writes the values of
120
+ in the configuration file. When the container starts, it writes the values of
123
121
five environment variables to stdout. It repeats this every ten seconds.
124
122
125
- Next, get a shell into the Container that is running in your Pod:
123
+ Next, get a shell into the container that is running in your Pod:
126
124
-->
127
- 要了解为什么这些值在日志中 ,请查看配置文件中的` command ` 和 ` args ` 字段。
128
- 当容器启动时,它将五个环境变量的值写入 stdout 。每十秒重复执行一次。
125
+ 要了解为什么这些值出现在日志中 ,请查看配置文件中的 ` command ` 和 ` args ` 字段。
126
+ 当容器启动时,它将五个环境变量的值写入标准输出 。每十秒重复执行一次。
129
127
130
128
接下来,通过打开一个 Shell 进入 Pod 中运行的容器:
131
129
132
- ```
130
+ ``` shell
133
131
kubectl exec -it dapi-envars-fieldref -- sh
134
132
```
135
133
@@ -138,8 +136,9 @@ In your shell, view the environment variables:
138
136
-->
139
137
在 Shell 中,查看环境变量:
140
138
141
- ```
142
- /# printenv
139
+ ``` shell
140
+ # 在容器内的 `shell` 中运行
141
+ printenv
143
142
```
144
143
145
144
<!--
@@ -160,42 +159,41 @@ MY_POD_NAME=dapi-envars-fieldref
160
159
```
161
160
162
161
<!--
163
- ## Use Container fields as values for environment variables
162
+ ## Use container fields as values for environment variables
163
+
164
+ In the preceding exercise, you used information from Pod-level fields as the values
165
+ for environment variables.
166
+ In this next exercise, you are going to pass fields that are part of the Pod
167
+ definition, but taken from the specific
168
+ [container](/docs/reference/kubernetes-api/workload-resources/pod-v1/#Container)
169
+ rather than from the Pod overall.
170
+
171
+ Here is a manifest for another Pod that again has just one container:
164
172
165
- In the preceding exercise, you used Pod fields as the values for environment
166
- variables. In this next exercise, you use Container fields as the values for
167
- environment variables. Here is the configuration file for a Pod that has one
168
- container:
169
173
-->
170
- ## 用 Container 字段作为环境变量的值
174
+ ## 使用容器字段作为环境变量的值
175
+
176
+ 前面的练习中,你将 Pod 级别的字段作为环境变量的值。
177
+ 接下来这个练习中,你将传递属于 Pod 定义的字段,但这些字段取自特定容器而不是整个 Pod。
171
178
172
- 前面的练习中,你将 Pod 字段作为环境变量的值。
173
- 接下来这个练习中,你将用 Container 字段作为环境变量的值。这里是包含一个容器的 Pod 的配置文件:
179
+ 这里是只包含一个容器的 Pod 的清单:
174
180
175
181
{{< codenew file="pods/inject/dapi-envars-container.yaml" >}}
176
182
177
183
<!--
178
- In the configuration file , you can see four environment variables. The `env`
184
+ In this manifest , you can see four environment variables. The `env`
179
185
field is an array of
180
- [EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core) .
186
+ environment variable definitions .
181
187
The first element in the array specifies that the `MY_CPU_REQUEST` environment
182
- variable gets its value from the `requests.cpu` field of a Container named
188
+ variable gets its value from the `requests.cpu` field of a container named
183
189
`test-container`. Similarly, the other environment variables get their values
184
- from Container fields.
185
-
186
- The fields in this example are Pod fields. They are not fields of the
187
- Container in the Pod.
190
+ from fields that are specific to this container.
188
191
189
192
Create the Pod:
190
193
-->
191
- 这个配置文件中,你可以看到四个环境变量。` env ` 字段是一个
192
- [ EnvVars] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
193
- 对象的数组。数组中第一个元素指定 ` MY_CPU_REQUEST ` 这个环境变量从 Container 的 ` requests.cpu `
194
- 字段获取变量值。同样,其它环境变量也是从 Container 的字段获取它们的变量值。
195
-
196
- {{< note >}}
197
- 本例中使用的是 Container 的字段而不是 Pod 的字段。
198
- {{< /note >}}
194
+ 这个清单中,你可以看到四个环境变量。` env ` 字段定义了一组环境变量。
195
+ 数组中第一个元素指定 ` MY_CPU_REQUEST ` 这个环境变量从容器的 ` requests.cpu `
196
+ 字段获取变量值。同样,其它的环境变量也是从特定于这个容器的字段中获取它们的变量值。
199
197
200
198
创建Pod:
201
199
@@ -204,20 +202,21 @@ kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-container.yaml
204
202
```
205
203
206
204
<!--
207
- Verify that the Container in the Pod is running:
205
+ Verify that the container in the Pod is running:
208
206
-->
209
207
验证 Pod 中的容器运行正常:
210
208
211
- ```
209
+ ``` shell
210
+ # 如果新创建的 Pod 还是处于不健康状态,请重新运行此命令几次。
212
211
kubectl get pods
213
212
```
214
213
215
214
<!--
216
- View the Container 's logs:
215
+ View the container 's logs:
217
216
-->
218
217
查看容器日志:
219
218
220
- ```
219
+ ``` shell
221
220
kubectl logs dapi-envars-resourcefieldref
222
221
```
223
222
@@ -236,16 +235,29 @@ The output shows the values of selected environment variables:
236
235
## {{% heading "whatsnext" %}}
237
236
238
237
<!--
239
- * [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
238
+ * Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
239
+ * Read the [`spec`](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec)
240
+ API definition for Pod. This includes the definition of Container (part of Pod).
241
+ * Read the list of [available fields](/docs/concepts/workloads/pods/downward-api/#available-fields) that you
242
+ can expose using the downward API.
243
+ -->
244
+ * 阅读[ 给容器定义环境变量] ( /zh-cn/docs/tasks/inject-data-application/define-environment-variable-container/ )
245
+ * 阅读 Pod 的 [ ` spec ` ] ( /zh-cn/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec )
246
+ API 包括容器(Pod 的一部分)的定义。
247
+ * 阅读可以使用 downward API 公开的[ 可用字段] ( /zh-cn/docs/concepts/workloads/pods/downward-api/#available-fields ) 列表。
248
+
249
+ <!--
250
+ Read about Pods, containers and environment variables in the legacy API reference:
251
+
240
252
* [PodSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
241
253
* [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
242
254
* [EnvVar](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core)
243
255
* [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core)
244
256
* [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core)
245
257
* [ResourceFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
246
258
-->
259
+ 在旧版 API 参考中阅读有关 Pod、容器和环境变量的信息:
247
260
248
- * [ 给容器定义环境变量] ( /zh-cn/docs/tasks/inject-data-application/define-environment-variable-container/ )
249
261
* [ PodSpec] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
250
262
* [ Container] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
251
263
* [ EnvVar] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core)
0 commit comments