1
1
---
2
- title : 通过环境变量将Pod信息呈现给容器
2
+ title : 通过环境变量将 Pod 信息呈现给容器
3
3
content_type : task
4
+ weight : 30
4
5
---
5
-
6
+ <!--
7
+ title: Expose Pod Information to Containers Through Environment Variables
8
+ content_type: task
9
+ weight: 30
10
+ -->
6
11
<!-- overview -->
7
12
8
- 此页面显示了Pod如何使用环境变量把自己的信息呈现给pod中运行的容器。环境变量可以呈现pod的字段和容器字段。
9
-
10
- 有两种方式可以将Pod和Container字段呈现给运行中的容器:
11
- 环境变量 和[ DownwardAPIVolumeFiles] (/docs/resources-reference/{{< param "version" >}}/#downwardapivolumefile-v1-core).
12
- 这两种呈现Pod和Container字段的方式都称为* Downward API* 。
13
-
14
-
15
-
13
+ <!--
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.
17
+ -->
18
+ 此页面展示 Pod 如何使用环境变量把自己的信息呈现给 Pod 中运行的容器。
19
+ 环境变量可以呈现 Pod 的字段和容器字段。
16
20
17
21
## {{% heading "prerequisites" %}}
18
22
19
-
20
23
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
21
24
25
+ <!-- steps -->
22
26
27
+ ## Downward API
23
28
29
+ <!--
30
+ There are two ways to expose Pod and Container fields to a running Container:
24
31
25
- <!-- steps -->
32
+ * Environment variables
33
+ * [Volume Files](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api)
26
34
27
- ## Downward API
35
+ Together, these two ways of exposing Pod and Container fields are called the
36
+ *Downward API*.
37
+ -->
28
38
29
- 有两种方式可以将Pod和Container字段呈现给运行中的容器 :
39
+ 有两种方式可以将 Pod 和 Container 字段呈现给运行中的容器 :
30
40
31
41
* 环境变量
32
- * [ DownwardAPIVolumeFiles ] (/docs/resources-reference/{{< param "version" >}}/#downwardapivolumefile-v1-core)
42
+ * [ 卷文件 ] (/docs/resources-reference/{{< param "version" >}}/#downwardapivolumefile-v1-core)
33
43
34
- 这两种呈现Pod和Container字段的方式都称为 * Downward API* 。
44
+ 这两种呈现 Pod 和 Container 字段的方式统称为 * Downward API* 。
35
45
46
+ <!--
47
+ ## Use Pod fields as values for environment variables
36
48
37
- ## 用Pod字段作为环境变量的值
49
+ In this exercise, you create a Pod that has one Container. Here is the
50
+ configuration file for the Pod:
51
+ -->
52
+ ## 用 Pod 字段作为环境变量的值
38
53
39
- 在这个练习中,你将创建一个包含一个容器的pod。这是该pod的配置文件 :
54
+ 在这个练习中,你将创建一个包含一个容器的 Pod。这是该 Pod 的配置文件 :
40
55
41
56
{{< codenew file="pods/inject/dapi-envars-pod.yaml" >}}
42
57
43
- 这个配置文件中,你可以看到五个环境变量。` env ` 字段是一个[ EnvVars] (/docs/resources-reference/{{< param "version" >}}/#envvar-v1-core)类型的数组。
44
- 数组中第一个元素指定` MY_NODE_NAME ` 这个环境变量从Pod的` spec.nodeName ` 字段获取变量值。同样,其它环境变量也是从Pod的字段获取它们的变量值。
45
-
58
+ <!--
59
+ In the configuration file, you can see five environment variables. The `env`
60
+ field is an array of
61
+ [EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
62
+ The first element in the array specifies that the `MY_NODE_NAME` environment
63
+ variable gets its value from the Pod's `spec.nodeName` field. Similarly, the
64
+ other environment variables get their names from Pod fields.
65
+ -->
66
+ 这个配置文件中,你可以看到五个环境变量。` env ` 字段是一个
67
+ [ EnvVars] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
68
+ 对象的数组。
69
+ 数组中第一个元素指定 ` MY_NODE_NAME ` 这个环境变量从 Pod 的 ` spec.nodeName ` 字段获取变量值。
70
+ 同样,其它环境变量也是从 Pod 的字段获取它们的变量值。
71
+
72
+ <!--
73
+ The fields in this example are Pod fields. They are not fields of the
74
+ Container in the Pod.
75
+ -->
46
76
{{< note >}}
47
- 本示例中的字段是Pod字段,不是Pod中容器的字段 。
77
+ 本示例中的字段是 Pod 字段,不是 Pod 中 Container 的字段 。
48
78
{{< /note >}}
49
79
80
+ <!--
81
+ Create the Pod:
82
+ -->
50
83
创建Pod:
51
84
52
85
``` shell
53
86
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-pod.yaml
54
87
```
55
88
56
- 验证Pod中的容器运行正常:
89
+ <!--
90
+ Verify that the Container in the Pod is running:
91
+ -->
92
+ 验证 Pod 中的容器运行正常:
57
93
58
94
```
59
95
kubectl get pods
60
96
```
61
97
98
+ <!--
99
+ View the Container's logs:
100
+ -->
62
101
查看容器日志:
63
102
64
103
```
65
104
kubectl logs dapi-envars-fieldref
66
105
```
67
106
107
+ <!--
108
+ The output shows the values of selected environment variables:
109
+ -->
68
110
输出信息显示了所选择的环境变量的值:
69
111
70
112
```
@@ -75,21 +117,36 @@ default
75
117
default
76
118
```
77
119
78
- 要了解为什么这些值在日志中,请查看配置文件中的` command ` 和 ` args ` 字段。 当容器启动时,它将五个环境变量的值写入stdout。每十秒重复执行一次。
120
+ <!--
121
+ 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
123
+ five environment variables to stdout. It repeats this every ten seconds.
79
124
80
- 接下来,进入Pod中运行的容器,打开一个shell:
125
+ Next, get a shell into the Container that is running in your Pod:
126
+ -->
127
+ 要了解为什么这些值在日志中,请查看配置文件中的` command ` 和 ` args ` 字段。
128
+ 当容器启动时,它将五个环境变量的值写入 stdout。每十秒重复执行一次。
129
+
130
+ 接下来,通过打开一个 Shell 进入 Pod 中运行的容器:
81
131
82
132
```
83
133
kubectl exec -it dapi-envars-fieldref -- sh
84
134
```
85
135
86
- 在shell中,查看环境变量:
136
+ <!--
137
+ In your shell, view the environment variables:
138
+ -->
139
+ 在 Shell 中,查看环境变量:
87
140
88
141
```
89
142
/# printenv
90
143
```
91
144
92
- 输出信息显示环境变量已经指定为Pod的字段的值。
145
+ <!--
146
+ The output shows that certain environment variables have been assigned the
147
+ values of Pod fields:
148
+ -->
149
+ 输出信息显示环境变量已经设置为 Pod 字段的值。
93
150
94
151
```
95
152
MY_POD_SERVICE_ACCOUNT=default
@@ -102,33 +159,71 @@ MY_NODE_NAME=minikube
102
159
MY_POD_NAME=dapi-envars-fieldref
103
160
```
104
161
105
- ## 用容器字段作为环境变量的值
162
+ <!--
163
+ ## Use Container fields as values for environment variables
164
+
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
+ -->
170
+ ## 用 Container 字段作为环境变量的值
106
171
107
- 前面的练习中,你将Pod字段作为环境变量的值。接下来这个练习,你将用容器字段作为环境变量的值。这里是包含一个容器的pod的配置文件:
172
+ 前面的练习中,你将 Pod 字段作为环境变量的值。
173
+ 接下来这个练习中,你将用 Container 字段作为环境变量的值。这里是包含一个容器的 Pod 的配置文件:
108
174
109
175
{{< codenew file="pods/inject/dapi-envars-container.yaml" >}}
110
176
111
- 这个配置文件中,你可以看到四个环境变量。` env ` 字段是一个[ EnvVars] (/docs/resources-reference/{{< param "version" >}}/#envvar-v1-core)
112
- 类型的数组。数组中第一个元素指定` MY_CPU_REQUEST ` 这个环境变量从容器的` requests.cpu ` 字段获取变量值。同样,其它环境变量也是从容器的字段获取它们的变量值。
177
+ <!--
178
+ In the configuration file, you can see four environment variables. The `env`
179
+ field is an array of
180
+ [EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
181
+ 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
183
+ `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.
188
+
189
+ Create the Pod:
190
+ -->
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 >}}
113
199
114
200
创建Pod:
115
201
116
202
``` shell
117
203
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-container.yaml
118
204
```
119
205
120
- 验证Pod中的容器运行正常:
206
+ <!--
207
+ Verify that the Container in the Pod is running:
208
+ -->
209
+ 验证 Pod 中的容器运行正常:
121
210
122
211
```
123
212
kubectl get pods
124
213
```
125
214
215
+ <!--
216
+ View the Container's logs:
217
+ -->
126
218
查看容器日志:
127
219
128
220
```
129
221
kubectl logs dapi-envars-resourcefieldref
130
222
```
131
223
224
+ <!--
225
+ The output shows the values of selected environment variables:
226
+ -->
132
227
输出信息显示了所选择的环境变量的值:
133
228
134
229
```
@@ -138,20 +233,23 @@ kubectl logs dapi-envars-resourcefieldref
138
233
67108864
139
234
```
140
235
141
-
142
-
143
236
## {{% heading "whatsnext" %}}
144
237
145
-
146
- * [ 给容器定义环境变量] ( /docs/tasks/configure-pod-container/define-environment-variable-container/ )
147
- * [ PodSpec] (/docs/resources-reference/{{< param "version" >}}/#podspec-v1-core)
148
- * [ Container] (/docs/resources-reference/{{< param "version" >}}/#container-v1-core)
149
- * [ EnvVar] (/docs/resources-reference/{{< param "version" >}}/#envvar-v1-core)
150
- * [ EnvVarSource] (/docs/resources-reference/{{< param "version" >}}/#envvarsource-v1-core)
151
- * [ ObjectFieldSelector] (/docs/resources-reference/{{< param "version" >}}/#objectfieldselector-v1-core)
152
- * [ ResourceFieldSelector] (/docs/resources-reference/{{< param "version" >}}/#resourcefieldselector-v1-core)
153
-
154
-
155
-
156
-
238
+ <!--
239
+ * [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
240
+ * [PodSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
241
+ * [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
242
+ * [EnvVar](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core)
243
+ * [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core)
244
+ * [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core)
245
+ * [ResourceFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
246
+ -->
247
+
248
+ * [ 给容器定义环境变量] ( /zh/docs/tasks/inject-data-application/define-environment-variable-container/ )
249
+ * [ PodSpec] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
250
+ * [ Container] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
251
+ * [ EnvVar] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core)
252
+ * [ EnvVarSource] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core)
253
+ * [ ObjectFieldSelector] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core)
254
+ * [ ResourceFieldSelector] (/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
157
255
0 commit comments