Merge pull request #36615 from yanrongshi/zh-cn]Sync-docs/tasks/inject-data-application/downward-api-volume-expose-pod-information.md
[zh-cn] Sync 2 files in tasks/inject-data-application/
This commit is contained in:
commit
5dc911a0a5
|
|
@ -20,7 +20,7 @@ A `downwardAPI` volume can expose Pod fields and container fields.
|
|||
<!--
|
||||
In Kubernetes, there are two ways to expose Pod and container fields to a running container:
|
||||
|
||||
* [Environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api)
|
||||
* [Environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/)
|
||||
* Volume files, as explained in this task
|
||||
|
||||
Together, these two ways of exposing Pod and container fields are called the
|
||||
|
|
@ -75,7 +75,7 @@ field should be stored in a file named `annotations`.
|
|||
|
||||
<!--
|
||||
The fields in this example are Pod fields. They are not
|
||||
fields of the Container in the Pod.
|
||||
fields of the container in the Pod.
|
||||
-->
|
||||
{{< note >}}
|
||||
本示例中的字段是 Pod 字段,不是 Pod 中容器的字段。
|
||||
|
|
|
|||
|
|
@ -12,66 +12,63 @@ weight: 30
|
|||
|
||||
<!--
|
||||
This page shows how a Pod can use environment variables to expose information
|
||||
about itself to Containers running in the Pod. Environment variables can expose
|
||||
Pod fields and Container fields.
|
||||
about itself to containers running in the Pod, using the _downward API_.
|
||||
You can use environment variables to expose Pod fields, container fields, or both.
|
||||
-->
|
||||
此页面展示 Pod 如何使用环境变量把自己的信息呈现给 Pod 中运行的容器。
|
||||
环境变量可以呈现 Pod 的字段和容器字段。
|
||||
此页面展示 Pod 如何使用环境变量把自身的信息呈现给 Pod 中运行的容器。
|
||||
使用 **downward API** 你可以使用环境变量来呈现 Pod 的字段、容器字段或两者。
|
||||
|
||||
<!--
|
||||
In Kubernetes, there are two ways to expose Pod and container fields to a running container:
|
||||
|
||||
* _Environment variables_, as explained in this task
|
||||
* [Volume files](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/)
|
||||
|
||||
Together, these two ways of exposing Pod and container fields are called the
|
||||
downward API.
|
||||
-->
|
||||
在 Kubernetes 中有两种方式可以将 Pod 和 容器字段呈现给运行中的容器:
|
||||
|
||||
* 如本任务所述的**环境变量**。
|
||||
* [卷文件](/zh-cn/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/)
|
||||
|
||||
这两种呈现 Pod 和 容器字段的方式统称为 downward API。
|
||||
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
## Downward API
|
||||
|
||||
<!--
|
||||
There are two ways to expose Pod and Container fields to a running Container:
|
||||
|
||||
* Environment variables
|
||||
* [Volume Files](/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api)
|
||||
|
||||
Together, these two ways of exposing Pod and Container fields are called the
|
||||
*Downward API*.
|
||||
-->
|
||||
|
||||
有两种方式可以将 Pod 和 Container 字段呈现给运行中的容器:
|
||||
|
||||
* 环境变量
|
||||
* [卷文件](/zh-cn/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api)
|
||||
|
||||
这两种呈现 Pod 和 Container 字段的方式统称为 *Downward API*。
|
||||
|
||||
<!--
|
||||
## Use Pod fields as values for environment variables
|
||||
|
||||
In this exercise, you create a Pod that has one Container. Here is the
|
||||
configuration file for the Pod:
|
||||
In this part of exercise, you create a Pod that has one container, and you
|
||||
project Pod-level fields into the running container as environment variables.
|
||||
-->
|
||||
## 用 Pod 字段作为环境变量的值
|
||||
|
||||
在这个练习中,你将创建一个包含一个容器的 Pod。这是该 Pod 的配置文件:
|
||||
在这部分练习中,你将创建一个包含一个容器的 Pod。并将 Pod 级别的字段作为环境变量映射到正在运行的容器中。
|
||||
|
||||
{{< codenew file="pods/inject/dapi-envars-pod.yaml" >}}
|
||||
|
||||
<!--
|
||||
In the configuration file, you can see five environment variables. The `env`
|
||||
In that manifest, you can see five environment variables. The `env`
|
||||
field is an array of
|
||||
[EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
|
||||
environment variable definitions.
|
||||
The first element in the array specifies that the `MY_NODE_NAME` environment
|
||||
variable gets its value from the Pod's `spec.nodeName` field. Similarly, the
|
||||
other environment variables get their names from Pod fields.
|
||||
-->
|
||||
这个配置文件中,你可以看到五个环境变量。`env` 字段是一个
|
||||
[EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
|
||||
对象的数组。
|
||||
这个清单中,你可以看到五个环境变量。`env` 字段定义了一组环境变量。
|
||||
|
||||
数组中第一个元素指定 `MY_NODE_NAME` 这个环境变量从 Pod 的 `spec.nodeName` 字段获取变量值。
|
||||
同样,其它环境变量也是从 Pod 的字段获取它们的变量值。
|
||||
|
||||
<!--
|
||||
The fields in this example are Pod fields. They are not fields of the
|
||||
Container in the Pod.
|
||||
container in the Pod.
|
||||
-->
|
||||
{{< note >}}
|
||||
本示例中的字段是 Pod 字段,不是 Pod 中 Container 的字段。
|
||||
|
|
@ -91,16 +88,17 @@ Verify that the Container in the Pod is running:
|
|||
-->
|
||||
验证 Pod 中的容器运行正常:
|
||||
|
||||
```
|
||||
```shell
|
||||
# 如果新创建的 Pod 还是处于不健康状态,请重新运行此命令几次。
|
||||
kubectl get pods
|
||||
```
|
||||
|
||||
<!--
|
||||
View the Container's logs:
|
||||
View the container's logs:
|
||||
-->
|
||||
查看容器日志:
|
||||
|
||||
```
|
||||
```shell
|
||||
kubectl logs dapi-envars-fieldref
|
||||
```
|
||||
|
||||
|
|
@ -119,17 +117,17 @@ default
|
|||
|
||||
<!--
|
||||
To see why these values are in the log, look at the `command` and `args` fields
|
||||
in the configuration file. When the Container starts, it writes the values of
|
||||
in the configuration file. When the container starts, it writes the values of
|
||||
five environment variables to stdout. It repeats this every ten seconds.
|
||||
|
||||
Next, get a shell into the Container that is running in your Pod:
|
||||
Next, get a shell into the container that is running in your Pod:
|
||||
-->
|
||||
要了解为什么这些值在日志中,请查看配置文件中的`command` 和 `args`字段。
|
||||
当容器启动时,它将五个环境变量的值写入 stdout。每十秒重复执行一次。
|
||||
要了解为什么这些值出现在日志中,请查看配置文件中的 `command` 和 `args` 字段。
|
||||
当容器启动时,它将五个环境变量的值写入标准输出。每十秒重复执行一次。
|
||||
|
||||
接下来,通过打开一个 Shell 进入 Pod 中运行的容器:
|
||||
|
||||
```
|
||||
```shell
|
||||
kubectl exec -it dapi-envars-fieldref -- sh
|
||||
```
|
||||
|
||||
|
|
@ -138,8 +136,9 @@ In your shell, view the environment variables:
|
|||
-->
|
||||
在 Shell 中,查看环境变量:
|
||||
|
||||
```
|
||||
/# printenv
|
||||
```shell
|
||||
# 在容器内的 `shell` 中运行
|
||||
printenv
|
||||
```
|
||||
|
||||
<!--
|
||||
|
|
@ -160,42 +159,41 @@ MY_POD_NAME=dapi-envars-fieldref
|
|||
```
|
||||
|
||||
<!--
|
||||
## Use Container fields as values for environment variables
|
||||
## Use container fields as values for environment variables
|
||||
|
||||
In the preceding exercise, you used information from Pod-level fields as the values
|
||||
for environment variables.
|
||||
In this next exercise, you are going to pass fields that are part of the Pod
|
||||
definition, but taken from the specific
|
||||
[container](/docs/reference/kubernetes-api/workload-resources/pod-v1/#Container)
|
||||
rather than from the Pod overall.
|
||||
|
||||
Here is a manifest for another Pod that again has just one container:
|
||||
|
||||
In the preceding exercise, you used Pod fields as the values for environment
|
||||
variables. In this next exercise, you use Container fields as the values for
|
||||
environment variables. Here is the configuration file for a Pod that has one
|
||||
container:
|
||||
-->
|
||||
## 用 Container 字段作为环境变量的值
|
||||
## 使用容器字段作为环境变量的值
|
||||
|
||||
前面的练习中,你将 Pod 字段作为环境变量的值。
|
||||
接下来这个练习中,你将用 Container 字段作为环境变量的值。这里是包含一个容器的 Pod 的配置文件:
|
||||
前面的练习中,你将 Pod 级别的字段作为环境变量的值。
|
||||
接下来这个练习中,你将传递属于 Pod 定义的字段,但这些字段取自特定容器而不是整个 Pod。
|
||||
|
||||
这里是只包含一个容器的 Pod 的清单:
|
||||
|
||||
{{< codenew file="pods/inject/dapi-envars-container.yaml" >}}
|
||||
|
||||
<!--
|
||||
In the configuration file, you can see four environment variables. The `env`
|
||||
In this manifest, you can see four environment variables. The `env`
|
||||
field is an array of
|
||||
[EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
|
||||
environment variable definitions.
|
||||
The first element in the array specifies that the `MY_CPU_REQUEST` environment
|
||||
variable gets its value from the `requests.cpu` field of a Container named
|
||||
variable gets its value from the `requests.cpu` field of a container named
|
||||
`test-container`. Similarly, the other environment variables get their values
|
||||
from Container fields.
|
||||
|
||||
The fields in this example are Pod fields. They are not fields of the
|
||||
Container in the Pod.
|
||||
from fields that are specific to this container.
|
||||
|
||||
Create the Pod:
|
||||
-->
|
||||
这个配置文件中,你可以看到四个环境变量。`env` 字段是一个
|
||||
[EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
|
||||
对象的数组。数组中第一个元素指定 `MY_CPU_REQUEST` 这个环境变量从 Container 的 `requests.cpu`
|
||||
字段获取变量值。同样,其它环境变量也是从 Container 的字段获取它们的变量值。
|
||||
|
||||
{{< note >}}
|
||||
本例中使用的是 Container 的字段而不是 Pod 的字段。
|
||||
{{< /note >}}
|
||||
这个清单中,你可以看到四个环境变量。`env` 字段定义了一组环境变量。
|
||||
数组中第一个元素指定 `MY_CPU_REQUEST` 这个环境变量从容器的 `requests.cpu`
|
||||
字段获取变量值。同样,其它的环境变量也是从特定于这个容器的字段中获取它们的变量值。
|
||||
|
||||
创建Pod:
|
||||
|
||||
|
|
@ -204,20 +202,21 @@ kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-container.yaml
|
|||
```
|
||||
|
||||
<!--
|
||||
Verify that the Container in the Pod is running:
|
||||
Verify that the container in the Pod is running:
|
||||
-->
|
||||
验证 Pod 中的容器运行正常:
|
||||
|
||||
```
|
||||
```shell
|
||||
# 如果新创建的 Pod 还是处于不健康状态,请重新运行此命令几次。
|
||||
kubectl get pods
|
||||
```
|
||||
|
||||
<!--
|
||||
View the Container's logs:
|
||||
View the container's logs:
|
||||
-->
|
||||
查看容器日志:
|
||||
|
||||
```
|
||||
```shell
|
||||
kubectl logs dapi-envars-resourcefieldref
|
||||
```
|
||||
|
||||
|
|
@ -236,7 +235,20 @@ The output shows the values of selected environment variables:
|
|||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
* [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
|
||||
* Read [Defining Environment Variables for a Container](/docs/tasks/inject-data-application/define-environment-variable-container/)
|
||||
* Read the [`spec`](/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec)
|
||||
API definition for Pod. This includes the definition of Container (part of Pod).
|
||||
* Read the list of [available fields](/docs/concepts/workloads/pods/downward-api/#available-fields) that you
|
||||
can expose using the downward API.
|
||||
-->
|
||||
* 阅读[给容器定义环境变量](/zh-cn/docs/tasks/inject-data-application/define-environment-variable-container/)
|
||||
* 阅读 Pod 的 [`spec`](/zh-cn/docs/reference/kubernetes-api/workload-resources/pod-v1/#PodSpec)
|
||||
API 包括容器(Pod 的一部分)的定义。
|
||||
* 阅读可以使用 downward API 公开的[可用字段](/zh-cn/docs/concepts/workloads/pods/downward-api/#available-fields)列表。
|
||||
|
||||
<!--
|
||||
Read about Pods, containers and environment variables in the legacy API reference:
|
||||
|
||||
* [PodSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
|
||||
* [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
|
||||
* [EnvVar](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core)
|
||||
|
|
@ -244,8 +256,8 @@ The output shows the values of selected environment variables:
|
|||
* [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core)
|
||||
* [ResourceFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
|
||||
-->
|
||||
在旧版 API 参考中阅读有关 Pod、容器和环境变量的信息:
|
||||
|
||||
* [给容器定义环境变量](/zh-cn/docs/tasks/inject-data-application/define-environment-variable-container/)
|
||||
* [PodSpec](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
|
||||
* [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
|
||||
* [EnvVar](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core)
|
||||
|
|
|
|||
Loading…
Reference in New Issue