Merge pull request #36755 from windsonsea/defenv

[zh]Resync define-interdependent-environment-variables.md
pull/33515/merge
Kubernetes Prow Robot 2022-09-13 06:35:05 -07:00 committed by GitHub
commit 0b99c6abcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 33 deletions

View File

@ -17,10 +17,8 @@ in a Kubernetes Pod.
## {{% heading "prerequisites" %}}
{{< include "task-tutorial-prereqs.md" >}}
<!-- steps -->
<!--
@ -34,46 +32,54 @@ Pod:
-->
## 为容器定义相互依赖的环境变量 {#define-an-environment-dependent-variable-for-a-container}
当创建一个 Pod 时,你可以为运行在 Pod 中的容器设置相互依赖的环境变量。
设置相互依赖的环境变量,你可以在配置清单文件的 `env``value` 中使用 $(VAR_NAME)。
当创建一个 Pod 时,你可以为运行在 Pod 中的容器设置相互依赖的环境变量。
若要设置相互依赖的环境变量,你可以在配置清单文件的 `env``value` 中使用 $(VAR_NAME)。
在本练习中,你会创建一个单容器的 Pod。
此 Pod 的配置文件定义了一个已定义常用用法的相互依赖的环境变量。
下面是 Pod 的配置清单:
下面是 Pod 的配置清单:
{{< codenew file="pods/inject/dependent-envars.yaml" >}}
<!-- 1. Create a Pod based on that manifest: -->
<!--
1. Create a Pod based on that manifest:
-->
1. 依据清单创建 Pod
```shell
kubectl apply -f https://k8s.io/examples/pods/inject/dependent-envars.yaml
```
```
pod/dependent-envars-demo created
```
<!-- 2. List the running Pods: -->
```shell
kubectl apply -f https://k8s.io/examples/pods/inject/dependent-envars.yaml
```
```
pod/dependent-envars-demo created
```
<!--
2. List the running Pods:
-->
2. 列出运行的 Pod
```shell
kubectl get pods dependent-envars-demo
```
```
NAME READY STATUS RESTARTS AGE
dependent-envars-demo 1/1 Running 0 9s
```
```shell
kubectl get pods dependent-envars-demo
```
```
NAME READY STATUS RESTARTS AGE
dependent-envars-demo 1/1 Running 0 9s
```
<!-- 3. Check the logs for the container running in your Pod: -->
<!--
3. Check the logs for the container running in your Pod:
-->
3. 检查 Pod 中运行容器的日志:
```shell
kubectl logs pod/dependent-envars-demo
```
```
```shell
kubectl logs pod/dependent-envars-demo
```
```
UNCHANGED_REFERENCE=$(PROTOCOL)://172.17.0.1:80
SERVICE_ADDRESS=https://172.17.0.1:80
ESCAPED_REFERENCE=$(PROTOCOL)://172.17.0.1:80
```
UNCHANGED_REFERENCE=$(PROTOCOL)://172.17.0.1:80
SERVICE_ADDRESS=https://172.17.0.1:80
ESCAPED_REFERENCE=$(PROTOCOL)://172.17.0.1:80
```
<!--
As shown above, you have defined the correct dependency reference of `SERVICE_ADDRESS`, bad dependency reference of `UNCHANGED_REFERENCE` and skip dependent references of `ESCAPED_REFERENCE`.
@ -88,6 +94,15 @@ the reference can be correctly resolved, such as in the `SERVICE_ADDRESS` case.
如果环境变量被引用时已事先定义,则引用可以正确解析,
比如 `SERVICE_ADDRESS` 的例子。
<!--
Note that order matters in the `env` list. An environment variable is not considered
"defined" if it is specified further down the list. That is why `UNCHANGED_REFERENCE`
fails to resolve `$(PROTOCOL)` in the example above.
-->
请注意,`env` 列表中的顺序很重要。如果某环境变量定义出现在列表的尾部,
则在解析列表前部环境变量时不会视其为“已被定义”。
这就是为什么 `UNCHANGED_REFERENCE` 在上面的示例中解析 `$(PROTOCOL)` 失败的原因。
<!--
When the environment variable is undefined or only includes some variables, the undefined environment variable is treated as a normal string, such as `UNCHANGED_REFERENCE`. Note that incorrectly parsed environment variables, in general, will not block the container from starting.
@ -99,17 +114,16 @@ is defined or not. This can be seen from the `ESCAPED_REFERENCE` case above.
比如 `UNCHANGED_REFERENCE` 的例子。
注意,解析不正确的环境变量通常不会阻止容器启动。
`$(VAR_NAME)` 这样的语法可以用两个 `$` 转义,`$$(VAR_NAME)`。
`$(VAR_NAME)` 这样的语法可以用两个 `$` 转义,`$$(VAR_NAME)`。
无论引用的变量是否定义,转义的引用永远不会展开。
这一点可以从上面 `ESCAPED_REFERENCE` 的例子得到印证。
## {{% heading "whatsnext" %}}
<!--
* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/).
* See [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core).
-->
* 进一步了解[环境变量](/zh-cn/docs/tasks/inject-data-application/environment-variable-expose-pod-information/).
* 参阅 [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core).
* 进一步了解[环境变量](/zh-cn/docs/tasks/inject-data-application/environment-variable-expose-pod-information/)
* 参阅 [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core)