add en content/zh/docs/tasks/inject-data-application/define-environment-variable-container.md (#19754)

pull/19764/head
chentanjun 2020-03-21 10:40:44 +08:00 committed by GitHub
parent f4ab262cba
commit ef4642ae22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 61 additions and 7 deletions

View File

@ -3,8 +3,21 @@ title: 为容器设置环境变量
content_template: templates/task
---
<!--
---
title: Define Environment Variables for a Container
content_template: templates/task
weight: 20
---
-->
{{% capture overview %}}
<!--
This page shows how to define environment variables for a container
in a Kubernetes Pod.
-->
本页将展示如何为 kubernetes Pod 下的容器设置环境变量。
{{% /capture %}}
@ -19,46 +32,81 @@ content_template: templates/task
{{% capture steps %}}
<!--
## Define an environment variable for a container
-->
## 为容器设置一个环境变量
<!--
When you create a Pod, you can set environment variables for the containers
that run in the Pod. To set environment variables, include the `env` or
`envFrom` field in the configuration file.
-->
创建 Pod 时,可以为其下的容器设置环境变量。通过配置文件的 `env` 或者 `envFrom` 字段来设置环境变量。
<!--
In this exercise, you create a Pod that runs one container. The configuration
file for the Pod defines an environment variable with name `DEMO_GREETING` and
value `"Hello from the environment"`. Here is the configuration file for the
Pod:
-->
本示例中,将创建一个只包含单个容器的 Pod。Pod 的配置文件中设置环境变量的名称为 `DEMO_GREETING`
其值为 `"Hello from the environment"`。下面是 Pod 的配置文件内容:
{{< codenew file="pods/inject/envars.yaml" >}}
<!--
1. Create a Pod based on the YAML configuration file:
-->
1. 基于 YAML 文件创建一个 Pod
```shell
kubectl apply -f https://k8s.io/examples/pods/inject/envars.yaml
```
<!--
1. List the running Pods:
-->
1. 获取一下当前正在运行的 Pods 信息:
```shell
kubectl get pods -l purpose=demonstrate-envars
```
<!--
The output is similar to this:
-->
查询结果应为:
```shell
NAME READY STATUS RESTARTS AGE
envar-demo 1/1 Running 0 9s
```
<!--
1. Get a shell to the container running in your Pod:
-->
1. 进入该 Pod 下的容器并打开一个命令终端:
```shell
kubectl exec -it envar-demo -- /bin/bash
```
<!--
1. In your shell, run the `printenv` command to list the environment variables.
-->
1. 在命令终端中通过执行 `printenv` 打印出环境变量。
```shell
root@envar-demo:/# printenv
```
<!--
The output is similar to this:
-->
打印结果应为:
```shell
@ -69,7 +117,10 @@ content_template: templates/task
DEMO_GREETING=Hello from the environment
DEMO_FAREWELL=Such a sweet sorrow
```
<!--
1. To exit the shell, enter `exit`.
-->
1. 通过键入 `exit` 退出命令终端。
<!--
@ -120,11 +171,14 @@ Upon creation, the command `echo Warm greetings to The Most Honorable Kubernetes
{{% capture whatsnext %}}
<!--
* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/).
* Learn about [using secrets as environment variables](/docs/user-guide/secrets/#using-secrets-as-environment-variables).
* See [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core).
-->
* 有关环境变量的更多信息,请参阅[这里](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/)。
* 有关如何通过环境变量来使用 Secret请参阅[这里](/docs/user-guide/secrets/#using-secrets-as-environment-variables)。
* 关于 [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core) 资源的信息。
{{% /capture %}}