Switch language name 'zh' to 'zh-cn'
This is the first step to rename 'zh' to 'zh-cn'. There are several reasons why we rename the language name.
- The upstream docsy theme changed the language name, leading to many warnings during site build;
The side-effect is that the i18n strings are no longer working.
- We believe renaming the language is the right thing to do, because this move can make room for other variants of Chinese language, such as 'zh-tw', 'zh-sg' etc.
There would be several follow-ups to this PR, such as fixing the intra-site links, adding redirects etc.
We will lock up changes to zh/zh-cn pages for the moment, until this one gets in.
This PR is based on commit cdad0a7342.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
title: "给应用注入数据"
|
||||
weight: 30
|
||||
description: 给你的工作负载 Pod 指定配置和其他数据。
|
||||
---
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
---
|
||||
title: 为容器设置启动时要执行的命令和参数
|
||||
content_type: task
|
||||
weight: 10
|
||||
---
|
||||
<!--
|
||||
title: Define a Command and Arguments for a Container
|
||||
content_type: task
|
||||
weight: 10
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how to define commands and arguments when you run a container
|
||||
in a {{< glossary_tooltip term_id="pod" >}}.
|
||||
-->
|
||||
本页将展示如何为 {{< glossary_tooltip text="Pod" term_id="pod" >}}
|
||||
中容器设置启动时要执行的命令及其参数。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## Define a command and arguments when you create a Pod
|
||||
|
||||
When you create a Pod, you can define a command and arguments for the
|
||||
containers that run in the Pod. To define a command, include the `command`
|
||||
field in the configuration file. To define arguments for the command, include
|
||||
the `args` field in the configuration file. The command and arguments that
|
||||
you define cannot be changed after the Pod is created.
|
||||
-->
|
||||
## 创建 Pod 时设置命令及参数
|
||||
|
||||
创建 Pod 时,可以为其下的容器设置启动时要执行的命令及其参数。如果要设置命令,就填写在配置文件的 `command` 字段下,如果要设置命令的参数,就填写在配置文件的 `args` 字段下。一旦 Pod 创建完成,该命令及其参数就无法再进行更改了。
|
||||
|
||||
<!--
|
||||
The command and arguments that you define in the configuration file
|
||||
override the default command and arguments provided by the container image.
|
||||
If you define args, but do not define a command, the default command is used
|
||||
with your new arguments.
|
||||
-->
|
||||
如果在配置文件中设置了容器启动时要执行的命令及其参数,那么容器镜像中自带的命令与参数将会被覆盖而不再执行。如果配置文件中只是设置了参数,却没有设置其对应的命令,那么容器镜像中自带的命令会使用该新参数作为其执行时的参数。
|
||||
|
||||
<!--
|
||||
The `command` field corresponds to `entrypoint` in some container runtimes.
|
||||
-->
|
||||
{{< note >}}
|
||||
在有些容器运行时中,`command` 字段对应 `entrypoint`,请参阅下面的
|
||||
[说明事项](#notes)。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
In this exercise, you create a Pod that runs one container. The configuration
|
||||
file for the Pod defines a command and two arguments:
|
||||
-->
|
||||
本示例中,将创建一个只包含单个容器的 Pod。在 Pod 配置文件中设置了一个命令与两个参数:
|
||||
|
||||
{{< codenew file="pods/commands.yaml" >}}
|
||||
|
||||
<!--
|
||||
1. Create a Pod based on the YAML configuration file:
|
||||
-->
|
||||
1. 基于 YAML 文件创建一个 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/pods/commands.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
1. List the running Pods:
|
||||
-->
|
||||
2. 获取正在运行的 Pods:
|
||||
|
||||
```shell
|
||||
kubectl get pods
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the container that ran in the command-demo Pod has completed.
|
||||
-->
|
||||
查询结果显示在 command-demo 这个 Pod 下运行的容器已经启动完成。
|
||||
|
||||
<!--
|
||||
1. To see the output of the command that ran in the container, view the logs
|
||||
from the Pod:
|
||||
-->
|
||||
3. 如果要获取容器启动时执行命令的输出结果,可以通过 Pod 的日志进行查看:
|
||||
|
||||
```shell
|
||||
kubectl logs command-demo
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows the values of the HOSTNAME and KUBERNETES_PORT environment variables:
|
||||
-->
|
||||
日志中显示了 HOSTNAME 与 KUBERNETES_PORT 这两个环境变量的值:
|
||||
|
||||
```
|
||||
command-demo
|
||||
tcp://10.3.240.1:443
|
||||
```
|
||||
|
||||
<!--
|
||||
## Use environment variables to define arguments
|
||||
|
||||
In the preceding example, you defined the arguments directly by
|
||||
providing strings. As an alternative to providing strings directly,
|
||||
you can define arguments by using environment variables:
|
||||
-->
|
||||
## 使用环境变量来设置参数
|
||||
|
||||
在上面的示例中,我们直接将一串字符作为命令的参数。除此之外,我们还可以将环境变量作为命令的参数。
|
||||
|
||||
```yaml
|
||||
env:
|
||||
- name: MESSAGE
|
||||
value: "hello world"
|
||||
command: ["/bin/echo"]
|
||||
args: ["$(MESSAGE)"]
|
||||
```
|
||||
|
||||
<!--
|
||||
This means you can define an argument for a Pod using any of
|
||||
the techniques available for defining environment variables, including
|
||||
[ConfigMaps](/docs/tasks/configure-pod-container/configure-pod-configmap/)
|
||||
and
|
||||
[Secrets](/docs/concepts/configuration/secret/).
|
||||
-->
|
||||
这意味着你可以将那些用来设置环境变量的方法应用于设置命令的参数,其中包括了
|
||||
[ConfigMaps](/zh/docs/tasks/configure-pod-container/configure-pod-configmap/) 与
|
||||
[Secrets](/zh/docs/concepts/configuration/secret/)。
|
||||
|
||||
<!--
|
||||
The environment variable appears in parentheses, `"$(VAR)"`. This is
|
||||
required for the variable to be expanded in the `command` or `args` field.
|
||||
-->
|
||||
{{< note >}}
|
||||
环境变量需要加上括号,类似于 `"$(VAR)"`。这是在 `command` 或 `args` 字段使用变量的格式要求。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
## Run a command in a shell
|
||||
|
||||
In some cases, you need your command to run in a shell. For example, your
|
||||
command might consist of several commands piped together, or it might be a shell
|
||||
script. To run your command in a shell, wrap it like this:
|
||||
-->
|
||||
## 在 Shell 来执行命令
|
||||
|
||||
有时候,你需要在 Shell 脚本中运行命令。
|
||||
例如,你要执行的命令可能由多个命令组合而成,或者它就是一个 Shell 脚本。
|
||||
这时,就可以通过如下方式在 Shell 中执行命令:
|
||||
|
||||
```shell
|
||||
command: ["/bin/sh"]
|
||||
args: ["-c", "while true; do echo hello; sleep 10;done"]
|
||||
```
|
||||
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
* Learn more about [configuring pods and containers](/docs/tasks/).
|
||||
* Learn more about [running commands in a container](/docs/tasks/debug/debug-application/get-shell-running-container/).
|
||||
* See [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core).
|
||||
-->
|
||||
* 进一步了解[配置 Pod 和容器](/zh/docs/tasks/)
|
||||
* 进一步了解[在容器中运行命令](/zh/docs/tasks/debug/debug-application/get-shell-running-container/)
|
||||
* 参阅 [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core)
|
||||
API 资源
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
---
|
||||
title: 为容器设置环境变量
|
||||
content_type: task
|
||||
weight: 20
|
||||
---
|
||||
|
||||
<!--
|
||||
title: Define Environment Variables for a Container
|
||||
content_type: task
|
||||
weight: 20
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how to define environment variables for a container
|
||||
in a Kubernetes Pod.
|
||||
-->
|
||||
本页将展示如何为 kubernetes Pod 下的容器设置环境变量。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
<!-- 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 manifest for the
|
||||
Pod:
|
||||
-->
|
||||
本示例中,将创建一个只包含单个容器的 Pod。Pod 的配置文件中设置环境变量的名称为 `DEMO_GREETING`,
|
||||
其值为 `"Hello from the environment"`。下面是 Pod 的配置清单:
|
||||
|
||||
{{< codenew file="pods/inject/envars.yaml" >}}
|
||||
|
||||
<!--
|
||||
1. Create a Pod based on that manifest:
|
||||
-->
|
||||
1. 基于配置清单创建一个 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/pods/inject/envars.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
1. List the running Pods:
|
||||
-->
|
||||
2. 获取一下当前正在运行的 Pods 信息:
|
||||
|
||||
```shell
|
||||
kubectl get pods -l purpose=demonstrate-envars
|
||||
```
|
||||
|
||||
<!--
|
||||
The output is similar to this:
|
||||
-->
|
||||
查询结果应为:
|
||||
|
||||
```
|
||||
NAME READY STATUS RESTARTS AGE
|
||||
envar-demo 1/1 Running 0 9s
|
||||
```
|
||||
|
||||
<!--
|
||||
1. List the Pod's container environment variables:
|
||||
-->
|
||||
3. 列出 Pod 容器的环境变量:
|
||||
|
||||
```shell
|
||||
kubectl exec envar-demo -- printenv
|
||||
```
|
||||
|
||||
<!--
|
||||
The output is similar to this:
|
||||
-->
|
||||
打印结果应为:
|
||||
|
||||
```
|
||||
NODE_VERSION=4.4.2
|
||||
EXAMPLE_SERVICE_PORT_8080_TCP_ADDR=10.3.245.237
|
||||
HOSTNAME=envar-demo
|
||||
...
|
||||
DEMO_GREETING=Hello from the environment
|
||||
DEMO_FAREWELL=Such a sweet sorrow
|
||||
```
|
||||
|
||||
<!--
|
||||
{{< note >}}
|
||||
The environment variables set using the `env` or `envFrom` field
|
||||
override any environment variables specified in the container image.
|
||||
{{< /note >}}
|
||||
-->
|
||||
{{< note >}}
|
||||
通过 `env` 或 `envFrom` 字段设置的环境变量将覆盖容器镜像中指定的所有环境变量。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
{{< note >}}
|
||||
Environment variables may reference each other, however ordering is important.
|
||||
Variables making use of others defined in the same context must come later in
|
||||
the list. Similarly, avoid circular references.
|
||||
{{< /note >}}
|
||||
-->
|
||||
{{< note >}}
|
||||
环境变量可以互相引用,但是顺序很重要。
|
||||
使用在相同上下文中定义的其他变量的变量必须在列表的后面。
|
||||
同样,请避免使用循环引用。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
## Using environment variables inside of your config
|
||||
|
||||
Environment variables that you define in a Pod's configuration can be used
|
||||
elsewhere in the configuration, for example in commands and arguments that
|
||||
you set for the Pod's containers.
|
||||
In the example configuration below, the `GREETING`, `HONORIFIC`, and
|
||||
`NAME` environment variables are set to `Warm greetings to`, `The Most
|
||||
Honorable`, and `Kubernetes`, respectively. Those environment variables
|
||||
are then used in the CLI arguments passed to the `env-print-demo`
|
||||
container.
|
||||
-->
|
||||
## 在配置中使用环境变量
|
||||
|
||||
你在 Pod 的配置中定义的环境变量可以在配置的其他地方使用,
|
||||
例如可用在为 Pod 的容器设置的命令和参数中。
|
||||
在下面的示例配置中,环境变量 `GREETING` ,`HONORIFIC` 和 `NAME` 分别设置为 `Warm greetings to` ,
|
||||
`The Most Honorable` 和 `Kubernetes`。然后这些环境变量在传递给容器 `env-print-demo` 的 CLI 参数中使用。
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: print-greeting
|
||||
spec:
|
||||
containers:
|
||||
- name: env-print-demo
|
||||
image: bash
|
||||
env:
|
||||
- name: GREETING
|
||||
value: "Warm greetings to"
|
||||
- name: HONORIFIC
|
||||
value: "The Most Honorable"
|
||||
- name: NAME
|
||||
value: "Kubernetes"
|
||||
command: ["echo"]
|
||||
args: ["$(GREETING) $(HONORIFIC) $(NAME)"]
|
||||
```
|
||||
|
||||
<!--
|
||||
Upon creation, the command `echo Warm greetings to The Most Honorable Kubernetes` is run on the container.
|
||||
-->
|
||||
创建后,命令 `echo Warm greetings to The Most Honorable Kubernetes` 将在容器中运行。
|
||||
|
||||
## {{% heading "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).
|
||||
-->
|
||||
|
||||
* 进一步了解[环境变量](/zh/docs/tasks/inject-data-application/environment-variable-expose-pod-information/)
|
||||
* 进一步了解[通过环境变量来使用 Secret](/zh/docs/concepts/configuration/secret/#using-secrets-as-environment-variables)
|
||||
* 关于 [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core) 资源的信息。
|
||||
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
---
|
||||
title: 定义相互依赖的环境变量
|
||||
content_type: task
|
||||
weight: 20
|
||||
---
|
||||
<!--
|
||||
title: Define Dependent Environment Variables
|
||||
-->
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how to define dependent environment variables for a container
|
||||
in a Kubernetes Pod.
|
||||
-->
|
||||
本页展示了如何为 Kubernetes Pod 中的容器定义相互依赖的环境变量。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## Define an environment dependent variable for a container
|
||||
|
||||
When you create a Pod, you can set dependent environment variables for the containers that run in the Pod. To set dependent environment variables, you can use $(VAR_NAME) in the `value` of `env` in the configuration file.
|
||||
|
||||
In this exercise, you create a Pod that runs one container. The configuration
|
||||
file for the Pod defines an dependent environment variable with common usage defined. Here is the configuration manifest for the
|
||||
Pod:
|
||||
-->
|
||||
## 为容器定义相互依赖的环境变量 {#define-an-environment-dependent-variable-for-a-container}
|
||||
|
||||
当创建一个 Pod 时,你可以为运行在 Pod 中的容器设置相互依赖的环境变量。
|
||||
设置相互依赖的环境变量,你就可以在配置清单文件的 `env` 的 `value` 中使用 $(VAR_NAME)。
|
||||
|
||||
在本练习中,你会创建一个单容器的 Pod。
|
||||
此 Pod 的配置文件定义了一个已定义常用用法的相互依赖的环境变量。
|
||||
下面是 Pod 的配置清单:
|
||||
|
||||
{{< codenew file="pods/inject/dependent-envars.yaml" >}}
|
||||
|
||||
<!-- 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: -->
|
||||
2. 列出运行的 Pod:
|
||||
|
||||
```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. 检查 Pod 中运行容器的日志:
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
<!--
|
||||
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`.
|
||||
|
||||
When an environment variable is already defined when being referenced,
|
||||
the reference can be correctly resolved, such as in the `SERVICE_ADDRESS` case.
|
||||
-->
|
||||
如上所示,你已经定义了 `SERVICE_ADDRESS` 的正确依赖引用,
|
||||
`UNCHANGED_REFERENCE` 的错误依赖引用,
|
||||
并跳过了 `ESCAPED_REFERENCE` 的依赖引用。
|
||||
|
||||
如果环境变量被引用时已事先定义,则引用可以正确解析,
|
||||
比如 `SERVICE_ADDRESS` 的例子。
|
||||
|
||||
<!--
|
||||
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.
|
||||
|
||||
The `$(VAR_NAME)` syntax can be escaped with a double `$`, ie: `$$(VAR_NAME)`.
|
||||
Escaped references are never expanded, regardless of whether the referenced variable
|
||||
is defined or not. This can be seen from the `ESCAPED_REFERENCE` case above.
|
||||
-->
|
||||
当环境变量未定义或仅包含部分变量时,未定义的变量会被当做普通字符串对待,
|
||||
比如 `UNCHANGED_REFERENCE` 的例子。
|
||||
注意,解析不正确的环境变量通常不会阻止容器启动。
|
||||
|
||||
`$(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/docs/tasks/inject-data-application/environment-variable-expose-pod-information/).
|
||||
* 参阅 [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core).
|
||||
|
||||
@@ -0,0 +1,381 @@
|
||||
---
|
||||
title: 使用 Secret 安全地分发凭证
|
||||
content_type: task
|
||||
weight: 50
|
||||
min-kubernetes-server-version: v1.6
|
||||
---
|
||||
|
||||
<!-- overview -->
|
||||
<!--
|
||||
This page shows how to securely inject sensitive data, such as passwords and
|
||||
encryption keys, into Pods.
|
||||
-->
|
||||
本文展示如何安全地将敏感数据(如密码和加密密钥)注入到 Pods 中。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}}
|
||||
|
||||
|
||||
<!--
|
||||
### Convert your secret data to a base-64 representation
|
||||
|
||||
Suppose you want to have two pieces of secret data: a username `my-app` and a password
|
||||
`39528$vdg7Jb`. First, use a base64 encoding tool to convert your username and password to a base64 representation. Here's an example using the commonly available base64 program:
|
||||
-->
|
||||
### 将 secret 数据转换为 base-64 形式
|
||||
|
||||
假设用户想要有两条 Secret 数据:用户名 `my-app` 和密码 `39528$vdg7Jb`。
|
||||
首先使用 [Base64 编码](https://www.base64encode.org/) 将用户名和密码转化为 base-64 形式。
|
||||
下面是一个使用常用的 base64 程序的示例:
|
||||
|
||||
```shell
|
||||
echo -n 'my-app' | base64
|
||||
echo -n '39528$vdg7Jb' | base64
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that the base-64 representation of your username is `bXktYXBw`,
|
||||
and the base-64 representation of your password is `Mzk1MjgkdmRnN0pi`.
|
||||
-->
|
||||
结果显示 base-64 形式的用户名为 `bXktYXBw`,
|
||||
base-64 形式的密码为 `Mzk1MjgkdmRnN0pi`。
|
||||
|
||||
<!--
|
||||
Use a local tool trusted by your OS to decrease the security risks of external tools.
|
||||
-->
|
||||
{{< caution >}}
|
||||
使用你的操作系统所能信任的本地工具以降低使用外部工具的风险。
|
||||
{{< /caution >}}
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## Create a Secret
|
||||
|
||||
Here is a configuration file you can use to create a Secret that holds your
|
||||
username and password:
|
||||
-->
|
||||
## 创建 Secret
|
||||
|
||||
这里是一个配置文件,可以用来创建存有用户名和密码的 Secret:
|
||||
|
||||
{{< codenew file="pods/inject/secret.yaml" >}}
|
||||
|
||||
1. <!--Create the Secret -->
|
||||
创建 Secret:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/pods/inject/secret.yaml
|
||||
```
|
||||
|
||||
1. <!-- View information about the Secret -->
|
||||
查看 Secret 相关信息:
|
||||
|
||||
```shell
|
||||
kubectl get secret test-secret
|
||||
```
|
||||
|
||||
<!-- Output: -->
|
||||
输出:
|
||||
|
||||
```shell
|
||||
NAME TYPE DATA AGE
|
||||
test-secret Opaque 2 1m
|
||||
```
|
||||
|
||||
1. <!-- View more detailed information about the Secret -->
|
||||
查看 Secret 相关的更多详细信息:
|
||||
|
||||
```shell
|
||||
kubectl describe secret test-secret
|
||||
```
|
||||
|
||||
<!-- Output: -->
|
||||
输出:
|
||||
|
||||
```shell
|
||||
Name: test-secret
|
||||
Namespace: default
|
||||
Labels: <none>
|
||||
Annotations: <none>
|
||||
|
||||
Type: Opaque
|
||||
|
||||
Data
|
||||
====
|
||||
password: 13 bytes
|
||||
username: 7 bytes
|
||||
```
|
||||
|
||||
<!--
|
||||
### Create a Secret directly with kubectl
|
||||
|
||||
If you want to skip the Base64 encoding step, you can create the
|
||||
same Secret using the `kubectl create secret` command. For example:
|
||||
-->
|
||||
### 直接用 kubectl 创建 Secret
|
||||
|
||||
如果你希望略过 Base64 编码的步骤,你也可以使用 `kubectl create secret`
|
||||
命令直接创建 Secret。例如:
|
||||
|
||||
```shell
|
||||
kubectl create secret generic test-secret --from-literal='username=my-app' --from-literal='password=39528$vdg7Jb'
|
||||
```
|
||||
|
||||
<!--
|
||||
This is more convenient. The detailed approach shown earlier runs
|
||||
through each step explicitly to demonstrate what is happening.
|
||||
-->
|
||||
这是一种更为方便的方法。
|
||||
前面展示的详细分解步骤有助于了解究竟发生了什么事情。
|
||||
|
||||
<!--
|
||||
## Create a Pod that has access to the secret data through a Volume
|
||||
|
||||
Here is a configuration file you can use to create a Pod:
|
||||
-->
|
||||
## 创建一个可以通过卷访问 secret 数据的 Pod
|
||||
|
||||
这里是一个可以用来创建 pod 的配置文件:
|
||||
|
||||
{{< codenew file="pods/inject/secret-pod.yaml" >}}
|
||||
|
||||
1. <!-- Create the Pod:-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl create -f secret-pod.yaml
|
||||
```
|
||||
|
||||
1. <!-- Verify that your Pod is running: -->
|
||||
确认 Pod 正在运行:
|
||||
|
||||
```shell
|
||||
kubectl get pod secret-test-pod
|
||||
```
|
||||
|
||||
输出:
|
||||
|
||||
```shell
|
||||
NAME READY STATUS RESTARTS AGE
|
||||
secret-test-pod 1/1 Running 0 42m
|
||||
```
|
||||
|
||||
1. <!-- Get a shell into the Container that is running in your Pod:-->
|
||||
获取一个 shell 进入 Pod 中运行的容器:
|
||||
|
||||
```shell
|
||||
kubectl exec -it secret-test-pod -- /bin/bash
|
||||
```
|
||||
|
||||
1. <!-- The secret data is exposed to the Container through a Volume mounted under
|
||||
`/etc/secret-volume`.
|
||||
|
||||
In your shell, list the files in the `/etc/secret-volume` directory:
|
||||
-->
|
||||
Secret 数据通过挂载在 `/etc/secret-volume` 目录下的卷暴露在容器中。
|
||||
|
||||
在 shell 中,列举 `/etc/secret-volume` 目录下的文件:
|
||||
|
||||
```shell
|
||||
ls /etc/secret-volume
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows two files, one for each piece of secret data:
|
||||
-->
|
||||
输出包含两个文件,每个对应一个 Secret 数据条目:
|
||||
|
||||
```
|
||||
password username
|
||||
```
|
||||
|
||||
1. <!--
|
||||
In your shell, display the contents of the `username` and `password` files:
|
||||
-->
|
||||
在 Shell 中,显示 `username` 和 `password` 文件的内容:
|
||||
|
||||
```shell
|
||||
# 在容器中 Shell 运行下面命令
|
||||
echo "$(cat /etc/secret-volume/username)"
|
||||
echo "$(cat /etc/secret-volume/password)"
|
||||
```
|
||||
|
||||
<!--
|
||||
The output is your username and password:
|
||||
-->
|
||||
输出为用户名和密码:
|
||||
|
||||
```shell
|
||||
my-app
|
||||
39528$vdg7Jb
|
||||
```
|
||||
|
||||
<!--
|
||||
## Define container environment variables using Secret data
|
||||
|
||||
### Define a container environment variable with data from a single Secret
|
||||
|
||||
-->
|
||||
## 使用 Secret 数据定义容器变量
|
||||
|
||||
### 使用来自 Secret 中的数据定义容器变量
|
||||
|
||||
<!--
|
||||
* Define an environment variable as a key-value pair in a Secret:
|
||||
-->
|
||||
* 定义环境变量为 Secret 中的键值偶对:
|
||||
|
||||
```shell
|
||||
kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
|
||||
```
|
||||
|
||||
<!--
|
||||
* Assign the `backend-username` value defined in the Secret to the `SECRET_USERNAME` environment variable in the Pod specification.
|
||||
-->
|
||||
* 在 Pod 规约中,将 Secret 中定义的值 `backend-username` 赋给 `SECRET_USERNAME` 环境变量
|
||||
|
||||
{{< codenew file="pods/inject/pod-single-secret-env-variable.yaml" >}}
|
||||
|
||||
<!--
|
||||
* Create the Pod:
|
||||
-->
|
||||
* 创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl create -f https://k8s.io/examples/pods/inject/pod-single-secret-env-variable.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
* In your shell, display the content of `SECRET_USERNAME` container environment variable
|
||||
-->
|
||||
* 在 Shell 中,显示容器环境变量 `SECRET_USERNAME` 的内容:
|
||||
|
||||
```shell
|
||||
kubectl exec -i -t env-single-secret -- /bin/sh -c 'echo $SECRET_USERNAME'
|
||||
```
|
||||
|
||||
输出为:
|
||||
|
||||
```
|
||||
backend-admin
|
||||
```
|
||||
<!--
|
||||
### Define container environment variables with data from multiple Secrets
|
||||
-->
|
||||
### 使用来自多个 Secret 的数据定义环境变量
|
||||
|
||||
<!--
|
||||
* As with the previous example, create the Secrets first.
|
||||
-->
|
||||
* 和前面的例子一样,先创建 Secret:
|
||||
|
||||
```shell
|
||||
kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'
|
||||
kubectl create secret generic db-user --from-literal=db-username='db-admin'
|
||||
```
|
||||
|
||||
<!--
|
||||
* Define the environment variables in the Pod specification.
|
||||
-->
|
||||
* 在 Pod 规约中定义环境变量:
|
||||
|
||||
{{< codenew file="pods/inject/pod-multiple-secret-env-variable.yaml" >}}
|
||||
|
||||
<!--
|
||||
* Create the Pod:
|
||||
-->
|
||||
* 创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl create -f https://k8s.io/examples/pods/inject/pod-multiple-secret-env-variable.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
* In your shell, display the container environment variables
|
||||
-->
|
||||
* 在你的 Shell 中,显示容器环境变量的内容:
|
||||
|
||||
```shell
|
||||
kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c 'env | grep _USERNAME'
|
||||
```
|
||||
|
||||
输出:
|
||||
```
|
||||
DB_USERNAME=db-admin
|
||||
BACKEND_USERNAME=backend-admin
|
||||
```
|
||||
|
||||
<!--
|
||||
## Configure all key-value pairs in a Secret as container environment variables
|
||||
-->
|
||||
## 将 Secret 中的所有键值偶对定义为环境变量
|
||||
|
||||
<!--
|
||||
This functionality is available in Kubernetes v1.6 and later.
|
||||
-->
|
||||
{{< note >}}
|
||||
此功能在 Kubernetes 1.6 版本之后可用。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
* Create a Secret containing multiple key-value pairs
|
||||
-->
|
||||
* 创建包含多个键值偶对的 Secret:
|
||||
|
||||
```shell
|
||||
kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb'
|
||||
```
|
||||
|
||||
<!--
|
||||
* Use envFrom to define all of the Secret's data as container environment variables. The key from the Secret becomes the environment variable name in the Pod.
|
||||
-->
|
||||
* 使用 `envFrom` 来将 Secret 中的所有数据定义为环境变量。
|
||||
Secret 中的键名成为容器中的环境变量名:
|
||||
|
||||
{{< codenew file="pods/inject/pod-secret-envFrom.yaml" >}}
|
||||
|
||||
<!--
|
||||
* Create the Pod:
|
||||
-->
|
||||
* 创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl create -f https://k8s.io/examples/pods/inject/pod-secret-envFrom.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
* In your shell, display `username` and `password` container environment variables
|
||||
-->
|
||||
* 在 Shell 中,显示环境变量 `username` 和 `password` 的内容:
|
||||
|
||||
```shell
|
||||
kubectl exec -i -t envfrom-secret -- /bin/sh -c 'echo "username: $username\npassword: $password\n"'
|
||||
```
|
||||
|
||||
输出为:
|
||||
|
||||
```
|
||||
username: my-app
|
||||
password: 39528$vdg7Jb
|
||||
```
|
||||
|
||||
<!-- ### References -->
|
||||
### 参考
|
||||
|
||||
* [Secret](/docs/api-reference/{{< param "version" >}}/#secret-v1-core)
|
||||
* [Volume](/docs/api-reference/{{< param "version" >}}/#volume-v1-core)
|
||||
* [Pod](/docs/api-reference/{{< param "version" >}}/#pod-v1-core)
|
||||
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
* Learn more about [Secrets](/docs/concepts/configuration/secret/).
|
||||
* Learn about [Volumes](/docs/concepts/storage/volumes/).
|
||||
-->
|
||||
* 进一步了解 [Secret](/zh/docs/concepts/configuration/secret/)。
|
||||
* 了解 [Volumes](/zh/docs/concepts/storage/volumes/)。
|
||||
|
||||
|
||||
+470
@@ -0,0 +1,470 @@
|
||||
---
|
||||
title: 通过文件将 Pod 信息呈现给容器
|
||||
content_type: task
|
||||
weight: 40
|
||||
---
|
||||
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
This page shows how a Pod can use a
|
||||
[`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
|
||||
to expose information about itself to Containers running in the Pod.
|
||||
A `DownwardAPIVolumeFile` can expose Pod fields and Container fields.
|
||||
-->
|
||||
此页面描述 Pod 如何使用
|
||||
[`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
|
||||
把自己的信息呈现给 Pod 中运行的容器。
|
||||
`DownwardAPIVolumeFile` 可以呈现 Pod 和容器的字段。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
||||
|
||||
<!-- steps -->
|
||||
|
||||
<!--
|
||||
## The Downward API
|
||||
|
||||
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)
|
||||
* Volume files
|
||||
|
||||
Together, these two ways of exposing Pod and Container fields are called the
|
||||
"Downward API".
|
||||
-->
|
||||
## Downward API {#the-downward-api}
|
||||
|
||||
有两种方式可以将 Pod 和 Container 字段呈现给运行中的容器:
|
||||
|
||||
* [环境变量](/zh/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api)
|
||||
* 卷文件
|
||||
|
||||
这两种呈现 Pod 和 Container 字段的方式都称为 "Downward API"。
|
||||
|
||||
<!--
|
||||
## Store Pod fields
|
||||
|
||||
In this exercise, you create a Pod that has one Container.
|
||||
Here is the configuration file for the Pod:
|
||||
-->
|
||||
## 存储 Pod 字段 {#store-pod-fields}
|
||||
|
||||
在这个练习中,你将创建一个包含一个容器的 Pod。Pod 的配置文件如下:
|
||||
|
||||
{{< codenew file="pods/inject/dapi-volume.yaml" >}}
|
||||
|
||||
<!--
|
||||
In the configuration file, you can see that the Pod has a `downwardAPI` Volume,
|
||||
and the Container mounts the Volume at `/etc/podinfo`.
|
||||
|
||||
Look at the `items` array under `downwardAPI`. Each element of the array is a
|
||||
[DownwardAPIVolumeFile](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core).
|
||||
The first element specifies that the value of the Pod's
|
||||
`metadata.labels` field should be stored in a file named `labels`.
|
||||
The second element specifies that the value of the Pod's `annotations`
|
||||
field should be stored in a file named `annotations`.
|
||||
-->
|
||||
在配置文件中,你可以看到 Pod 有一个 `downwardAPI` 类型的卷,并且挂载到容器中的
|
||||
`/etc/podinfo` 目录。
|
||||
|
||||
查看 `downwardAPI` 下面的 `items` 数组。
|
||||
每个数组元素都是一个
|
||||
[DownwardAPIVolumeFile](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
|
||||
对象。
|
||||
第一个元素指示 Pod 的 `metadata.labels` 字段的值保存在名为 `labels` 的文件中。
|
||||
第二个元素指示 Pod 的 `annotations` 字段的值保存在名为 `annotations` 的文件中。
|
||||
|
||||
<!--
|
||||
The fields in this example are Pod fields. They are not
|
||||
fields of the Container in the Pod.
|
||||
-->
|
||||
{{< note >}}
|
||||
本示例中的字段是Pod字段,不是Pod中容器的字段。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建 Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
Verify that the container in the Pod is running:
|
||||
-->
|
||||
验证Pod中的容器运行正常:
|
||||
|
||||
```shell
|
||||
kubectl get pods
|
||||
```
|
||||
|
||||
<!--
|
||||
View the container's logs:
|
||||
-->
|
||||
查看容器的日志:
|
||||
|
||||
```shell
|
||||
kubectl logs kubernetes-downwardapi-volume-example
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows the contents of the `labels` file and the `annotations` file:
|
||||
-->
|
||||
输出显示 `labels` 和 `annotations` 文件的内容:
|
||||
|
||||
```
|
||||
cluster="test-cluster1"
|
||||
rack="rack-22"
|
||||
zone="us-est-coast"
|
||||
|
||||
build="two"
|
||||
builder="john-doe"
|
||||
```
|
||||
|
||||
<!--
|
||||
Get a shell into the container that is running in your Pod:
|
||||
-->
|
||||
进入 Pod 中运行的容器,打开一个 Shell:
|
||||
|
||||
```shell
|
||||
kubectl exec -it kubernetes-downwardapi-volume-example -- sh
|
||||
```
|
||||
|
||||
<!--
|
||||
In your shell, view the `labels` file:
|
||||
-->
|
||||
在该 Shell中,查看 `labels` 文件:
|
||||
|
||||
```shell
|
||||
/# cat /etc/podinfo/labels
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that all of the Pod's labels have been written
|
||||
to the `labels` file:
|
||||
-->
|
||||
输出显示 Pod 的所有标签都已写入 `labels` 文件。
|
||||
|
||||
```
|
||||
cluster="test-cluster1"
|
||||
rack="rack-22"
|
||||
zone="us-est-coast"
|
||||
```
|
||||
|
||||
<!--
|
||||
Similarly, view the `annotations` file:
|
||||
-->
|
||||
同样,查看 `annotations` 文件:
|
||||
|
||||
```shell
|
||||
/# cat /etc/podinfo/annotations
|
||||
```
|
||||
|
||||
<!--
|
||||
View the files in the `/etc/podinfo` directory:
|
||||
-->
|
||||
查看 `/etc/podinfo` 目录下的文件:
|
||||
|
||||
```shell
|
||||
/# ls -laR /etc/podinfo
|
||||
```
|
||||
|
||||
<!--
|
||||
In the output, you can see that the `labels` and `annotations` files
|
||||
are in a temporary subdirectory: in this example,
|
||||
`..2982_06_02_21_47_53.299460680`. In the `/etc/podinfo` directory, `..data` is
|
||||
a symbolic link to the temporary subdirectory. Also in the `/etc/podinfo` directory,
|
||||
`labels` and `annotations` are symbolic links.
|
||||
-->
|
||||
在输出中可以看到,`labels` 和 `annotations` 文件都在一个临时子目录中。
|
||||
在这个例子,`..2982_06_02_21_47_53.299460680`。
|
||||
在 `/etc/podinfo` 目录中,`..data` 是一个指向临时子目录
|
||||
的符号链接。`/etc/podinfo` 目录中,`labels` 和 `annotations` 也是符号链接。
|
||||
|
||||
```
|
||||
drwxr-xr-x ... Feb 6 21:47 ..2982_06_02_21_47_53.299460680
|
||||
lrwxrwxrwx ... Feb 6 21:47 ..data -> ..2982_06_02_21_47_53.299460680
|
||||
lrwxrwxrwx ... Feb 6 21:47 annotations -> ..data/annotations
|
||||
lrwxrwxrwx ... Feb 6 21:47 labels -> ..data/labels
|
||||
|
||||
/etc/..2982_06_02_21_47_53.299460680:
|
||||
total 8
|
||||
-rw-r--r-- ... Feb 6 21:47 annotations
|
||||
-rw-r--r-- ... Feb 6 21:47 labels
|
||||
```
|
||||
|
||||
<!--
|
||||
Using symbolic links enables dynamic atomic refresh of the metadata; updates are
|
||||
written to a new temporary directory, and the `..data` symlink is updated
|
||||
atomically using [rename(2)](http://man7.org/linux/man-pages/man2/rename.2.html).
|
||||
-->
|
||||
用符号链接可实现元数据的动态原子性刷新;更新将写入一个新的临时目录,
|
||||
然后通过使用 [rename(2)](http://man7.org/linux/man-pages/man2/rename.2.html)
|
||||
完成 `..data` 符号链接的原子性更新。
|
||||
|
||||
<!--
|
||||
A container using Downward API as a
|
||||
[subPath](/docs/concepts/storage/volumes/#using-subpath) volume mount will not
|
||||
receive Downward API updates.
|
||||
-->
|
||||
{{< note >}}
|
||||
如果容器以
|
||||
[subPath](/zh/docs/concepts/storage/volumes/#using-subpath)卷挂载方式来使用
|
||||
Downward API,则该容器无法收到更新事件。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
Exit the shell:
|
||||
-->
|
||||
退出 Shell:
|
||||
|
||||
```shell
|
||||
/# exit
|
||||
```
|
||||
|
||||
<!--
|
||||
## Store Container fields
|
||||
|
||||
The preceding exercise, you stored Pod fields in a
|
||||
[`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)..
|
||||
In this next exercise, you store Container fields. Here is the configuration
|
||||
file for a Pod that has one Container:
|
||||
-->
|
||||
## 存储容器字段 {#store-container-fields}
|
||||
|
||||
前面的练习中,你将 Pod 字段保存到
|
||||
[`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
|
||||
中。
|
||||
接下来这个练习,你将存储 Container 字段。这里是包含一个容器的 Pod 的配置文件:
|
||||
|
||||
{{< codenew file="pods/inject/dapi-volume-resources.yaml" >}}
|
||||
|
||||
<!--
|
||||
In the configuration file, you can see that the Pod has a
|
||||
[`downwardAPI` volume](/docs/concepts/storage/volumes/#downwardapi),
|
||||
and the Container mounts the volume at `/etc/podinfo`.
|
||||
|
||||
Look at the `items` array under `downwardAPI`. Each element of the array is a
|
||||
[`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core).
|
||||
|
||||
The first element specifies that in the Container named `client-container`,
|
||||
the value of the `limits.cpu` field in the format specified by `1m` should be
|
||||
stored in a file named `cpu_limit`. The `divisor` field is optional and has the
|
||||
default value of `1` which means cores for cpu and bytes for memory.
|
||||
|
||||
Create the Pod:
|
||||
-->
|
||||
在这个配置文件中,你可以看到 Pod 有一个
|
||||
[`downwardAPI` 卷](/zh/docs/concepts/storage/volumes/#downwardapi),
|
||||
并且挂载到容器的 `/etc/podinfo` 目录。
|
||||
|
||||
查看 `downwardAPI` 下面的 `items` 数组。每个数组元素都是一个
|
||||
[`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)。
|
||||
|
||||
第一个元素指定在名为 `client-container` 的容器中,
|
||||
以 `1m` 所指定格式的 `limits.cpu` 字段的值应保存在名为 `cpu_limit` 的文件中。
|
||||
`divisor` 字段是可选的,默认值为 `1`,表示 CPU 的核心和内存的字节。
|
||||
|
||||
创建Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume-resources.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
Get a shell into the container that is running in your Pod:
|
||||
-->
|
||||
打开一个 Shell,进入 Pod 中运行的容器:
|
||||
|
||||
```shell
|
||||
kubectl exec -it kubernetes-downwardapi-volume-example-2 -- sh
|
||||
```
|
||||
|
||||
<!--
|
||||
In your shell, view the `cpu_limit` file:
|
||||
-->
|
||||
在 Shell 中,查看 `cpu_limit` 文件:
|
||||
|
||||
```shell
|
||||
/# cat /etc/podinfo/cpu_limit
|
||||
```
|
||||
|
||||
<!--
|
||||
You can use similar commands to view the `cpu_request`, `mem_limit` and
|
||||
`mem_request` files.
|
||||
-->
|
||||
你可以使用同样的命令查看 `cpu_request`、`mem_limit` 和 `mem_request` 文件.
|
||||
|
||||
<!-- discussion -->
|
||||
|
||||
<!-- TODO: This section should be extracted out of the task page. -->
|
||||
<!--
|
||||
## Capabilities of the Downward API
|
||||
-->
|
||||
## Downward API 的能力 {#capabilities-of-the-downward-api}
|
||||
|
||||
<!--
|
||||
The following information is available to containers through environment
|
||||
variables and `downwardAPI` volumes:
|
||||
|
||||
* Information available via `fieldRef`:
|
||||
|
||||
* `metadata.name` - the pod's name
|
||||
* `metadata.namespace` - the pod's namespace
|
||||
* `metadata.uid` - the pod's UID
|
||||
* `metadata.labels['<KEY>']` - the value of the pod's label `<KEY>`
|
||||
(for example, `metadata.labels['mylabel']`)
|
||||
* `metadata.annotations['<KEY>']` - the value of the pod's annotation `<KEY>`
|
||||
(for example, `metadata.annotations['myannotation']`)
|
||||
-->
|
||||
下面这些信息可以通过环境变量和 `downwardAPI` 卷提供给容器:
|
||||
|
||||
* 能通过 `fieldRef` 获得的:
|
||||
|
||||
* `metadata.name` - Pod 名称
|
||||
* `metadata.namespace` - Pod 名字空间
|
||||
* `metadata.uid` - Pod 的 UID
|
||||
* `metadata.labels['<KEY>']` - Pod 标签 `<KEY>` 的值
|
||||
(例如:`metadata.labels['mylabel']`)
|
||||
* `metadata.annotations['<KEY>']` - Pod 的注解 `<KEY>` 的值
|
||||
(例如:`metadata.annotations['myannotation']`)
|
||||
|
||||
<!--
|
||||
* Information available via `resourceFieldRef`:
|
||||
|
||||
* A Container's CPU limit
|
||||
* A Container's CPU request
|
||||
* A Container's memory limit
|
||||
* A Container's memory request
|
||||
* A Container's hugepages limit (provided that the `DownwardAPIHugePages`
|
||||
[feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is enabled)
|
||||
* A Container's hugepages request (provided that the `DownwardAPIHugePages`
|
||||
[feature gate](/docs/reference/command-line-tools-reference/feature-gates/) is enabled)
|
||||
* A Container's ephemeral-storage limit
|
||||
* A Container's ephemeral-storage request
|
||||
-->
|
||||
* 能通过 `resourceFieldRef` 获得的:
|
||||
* 容器的 CPU 约束值
|
||||
* 容器的 CPU 请求值
|
||||
* 容器的内存约束值
|
||||
* 容器的内存请求值
|
||||
* 容器的巨页限制值(前提是启用了 `DownwardAPIHugePages`
|
||||
[特性门控](/zh/docs/reference/command-line-tools-reference/feature-gates/))
|
||||
* 容器的巨页请求值(前提是启用了 `DownwardAPIHugePages`
|
||||
[特性门控](/zh/docs/reference/command-line-tools-reference/feature-gates/))
|
||||
* 容器的临时存储约束值
|
||||
* 容器的临时存储请求值
|
||||
|
||||
<!--
|
||||
In addition, the following information is available through
|
||||
`downwardAPI` volume `fieldRef`:
|
||||
-->
|
||||
此外,以下信息可通过 `downwardAPI` 卷从 `fieldRef` 获得:
|
||||
|
||||
<!--
|
||||
* `metadata.labels` - all of the pod's labels, formatted as `label-key="escaped-label-value"`
|
||||
with one label per line
|
||||
* `metadata.annotations` - all of the pod's annotations, formatted as
|
||||
`annotation-key="escaped-annotation-value"` with one annotation per line
|
||||
-->
|
||||
* `metadata.labels` - Pod 的所有标签,以
|
||||
`label-key="escaped-label-value"` 格式显示,每行显示一个标签
|
||||
* `metadata.annotations` - Pod 的所有注解,以
|
||||
`annotation-key="escaped-annotation-value"` 格式显示,每行显示一个标签
|
||||
|
||||
<!--
|
||||
The following information is available through environment variables:
|
||||
|
||||
* `status.podIP` - the pod's IP address
|
||||
* `spec.serviceAccountName` - the pod's service account name
|
||||
* `spec.nodeName` - the name of the node to which the scheduler always attempts to
|
||||
schedule the pod
|
||||
* `status.hostIP` - the IP of the node to which the Pod is assigned
|
||||
-->
|
||||
以下信息可通过环境变量获得:
|
||||
|
||||
* `status.podIP` - Pod IP 地址
|
||||
* `spec.serviceAccountName` - Pod 服务帐号名称
|
||||
* `spec.nodeName` - 调度器总是尝试将 Pod 调度到的节点的名称
|
||||
* `status.hostIP` - Pod 分配到的节点的 IP
|
||||
|
||||
<!--
|
||||
If CPU and memory limits are not specified for a Container, the
|
||||
Downward API defaults to the node allocatable value for CPU and memory.
|
||||
-->
|
||||
{{< note >}}
|
||||
如果容器未指定 CPU 和内存限制,则 Downward API 默认将节点可分配值
|
||||
视为容器的 CPU 和内存限制。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
## Project keys to specific paths and file permissions
|
||||
|
||||
You can project keys to specific paths and specific permissions on a per-file
|
||||
basis. For more information, see
|
||||
[Secrets](/docs/concepts/configuration/secret/).
|
||||
-->
|
||||
## 投射键名到指定路径并且指定文件权限 {#project-keys-to-specific-paths-and-file-permissions}
|
||||
|
||||
你可以将键名投射到指定路径并且指定每个文件的访问权限。
|
||||
更多信息,请参阅 [Secret](/zh/docs/concepts/configuration/secret/)。
|
||||
|
||||
<!--
|
||||
## Motivation for the Downward API
|
||||
|
||||
It is sometimes useful for a container to have information about itself, without
|
||||
being overly coupled to Kubernetes. The Downward API allows containers to consume
|
||||
information about themselves or the cluster without using the Kubernetes client
|
||||
or API server.
|
||||
|
||||
An example is an existing application that assumes a particular well-known
|
||||
environment variable holds a unique identifier. One possibility is to wrap the
|
||||
application, but that is tedious and error prone, and it violates the goal of low
|
||||
coupling. A better option would be to use the Pod's name as an identifier, and
|
||||
inject the Pod's name into the well-known environment variable.
|
||||
-->
|
||||
## Downward API 的动机 {#motivation-for-the-downward-api}
|
||||
|
||||
对于容器来说,有时候拥有自己的信息是很有用的,可避免与 Kubernetes 过度耦合。
|
||||
Downward API 使得容器使用自己或者集群的信息,而不必通过 Kubernetes 客户端或
|
||||
API 服务器来获得。
|
||||
|
||||
一个例子是有一个现有的应用假定要用一个非常熟悉的环境变量来保存一个唯一标识。
|
||||
一种可能是给应用增加处理层,但这样是冗余和易出错的,而且它违反了低耦合的目标。
|
||||
更好的选择是使用 Pod 名称作为标识,把 Pod 名称注入这个环境变量中。
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
* Check the [`PodSpec`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
|
||||
API definition which defines the desired state of a Pod.
|
||||
* Check the [`Volume`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
|
||||
API definition which defines a generic volume in a Pod for containers to access.
|
||||
* Check the [`DownwardAPIVolumeSource`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumesource-v1-core)
|
||||
API definition which defines a volume that contains Downward API information.
|
||||
* Check the [`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
|
||||
API definition which contains references to object or resource fields for
|
||||
populating a file in the Downward API volume.
|
||||
* Check the [`ResourceFieldSelector`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
|
||||
API definition which specifies the container resources and their output format.
|
||||
-->
|
||||
* 参阅
|
||||
[`PodSpec`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
|
||||
API 定义,该 API 定义 Pod 所需状态。
|
||||
* 参阅
|
||||
[`Volume`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
|
||||
API 定义,该 API 在 Pod 中定义通用卷以供容器访问。
|
||||
* 参阅
|
||||
[`DownwardAPIVolumeSource`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumesource-v1-core)
|
||||
API 定义,该 API 定义包含 Downward API 信息的卷。
|
||||
* 参阅
|
||||
[`DownwardAPIVolumeFile`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#downwardapivolumefile-v1-core)
|
||||
API 定义,该 API 包含对对象或资源字段的引用,用于在 Downward API 卷中填充文件。
|
||||
* 参阅
|
||||
[`ResourceFieldSelector`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
|
||||
API 定义,该 API 指定容器资源及其输出格式。
|
||||
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
---
|
||||
title: 通过环境变量将 Pod 信息呈现给容器
|
||||
content_type: task
|
||||
weight: 30
|
||||
---
|
||||
<!--
|
||||
title: Expose Pod Information to Containers Through Environment Variables
|
||||
content_type: task
|
||||
weight: 30
|
||||
-->
|
||||
<!-- overview -->
|
||||
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
此页面展示 Pod 如何使用环境变量把自己的信息呈现给 Pod 中运行的容器。
|
||||
环境变量可以呈现 Pod 的字段和容器字段。
|
||||
|
||||
## {{% heading "prerequisites" %}}
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
||||
|
||||
<!-- 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/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:
|
||||
-->
|
||||
## 用 Pod 字段作为环境变量的值
|
||||
|
||||
在这个练习中,你将创建一个包含一个容器的 Pod。这是该 Pod 的配置文件:
|
||||
|
||||
{{< codenew file="pods/inject/dapi-envars-pod.yaml" >}}
|
||||
|
||||
<!--
|
||||
In the configuration file, you can see five environment variables. The `env`
|
||||
field is an array of
|
||||
[EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
|
||||
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).
|
||||
对象的数组。
|
||||
数组中第一个元素指定 `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.
|
||||
-->
|
||||
{{< note >}}
|
||||
本示例中的字段是 Pod 字段,不是 Pod 中 Container 的字段。
|
||||
{{< /note >}}
|
||||
|
||||
<!--
|
||||
Create the Pod:
|
||||
-->
|
||||
创建Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-pod.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
Verify that the Container in the Pod is running:
|
||||
-->
|
||||
验证 Pod 中的容器运行正常:
|
||||
|
||||
```
|
||||
kubectl get pods
|
||||
```
|
||||
|
||||
<!--
|
||||
View the Container's logs:
|
||||
-->
|
||||
查看容器日志:
|
||||
|
||||
```
|
||||
kubectl logs dapi-envars-fieldref
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows the values of selected environment variables:
|
||||
-->
|
||||
输出信息显示了所选择的环境变量的值:
|
||||
|
||||
```
|
||||
minikube
|
||||
dapi-envars-fieldref
|
||||
default
|
||||
172.17.0.4
|
||||
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
|
||||
five environment variables to stdout. It repeats this every ten seconds.
|
||||
|
||||
Next, get a shell into the Container that is running in your Pod:
|
||||
-->
|
||||
要了解为什么这些值在日志中,请查看配置文件中的`command` 和 `args`字段。
|
||||
当容器启动时,它将五个环境变量的值写入 stdout。每十秒重复执行一次。
|
||||
|
||||
接下来,通过打开一个 Shell 进入 Pod 中运行的容器:
|
||||
|
||||
```
|
||||
kubectl exec -it dapi-envars-fieldref -- sh
|
||||
```
|
||||
|
||||
<!--
|
||||
In your shell, view the environment variables:
|
||||
-->
|
||||
在 Shell 中,查看环境变量:
|
||||
|
||||
```
|
||||
/# printenv
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows that certain environment variables have been assigned the
|
||||
values of Pod fields:
|
||||
-->
|
||||
输出信息显示环境变量已经设置为 Pod 字段的值。
|
||||
|
||||
```
|
||||
MY_POD_SERVICE_ACCOUNT=default
|
||||
...
|
||||
MY_POD_NAMESPACE=default
|
||||
MY_POD_IP=172.17.0.4
|
||||
...
|
||||
MY_NODE_NAME=minikube
|
||||
...
|
||||
MY_POD_NAME=dapi-envars-fieldref
|
||||
```
|
||||
|
||||
<!--
|
||||
## Use Container fields as values for environment variables
|
||||
|
||||
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 的配置文件:
|
||||
|
||||
{{< codenew file="pods/inject/dapi-envars-container.yaml" >}}
|
||||
|
||||
<!--
|
||||
In the configuration file, you can see four environment variables. The `env`
|
||||
field is an array of
|
||||
[EnvVars](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvar-v1-core).
|
||||
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
|
||||
`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.
|
||||
|
||||
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 >}}
|
||||
|
||||
创建Pod:
|
||||
|
||||
```shell
|
||||
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-container.yaml
|
||||
```
|
||||
|
||||
<!--
|
||||
Verify that the Container in the Pod is running:
|
||||
-->
|
||||
验证 Pod 中的容器运行正常:
|
||||
|
||||
```
|
||||
kubectl get pods
|
||||
```
|
||||
|
||||
<!--
|
||||
View the Container's logs:
|
||||
-->
|
||||
查看容器日志:
|
||||
|
||||
```
|
||||
kubectl logs dapi-envars-resourcefieldref
|
||||
```
|
||||
|
||||
<!--
|
||||
The output shows the values of selected environment variables:
|
||||
-->
|
||||
输出信息显示了所选择的环境变量的值:
|
||||
|
||||
```
|
||||
1
|
||||
1
|
||||
33554432
|
||||
67108864
|
||||
```
|
||||
|
||||
## {{% heading "whatsnext" %}}
|
||||
|
||||
<!--
|
||||
* [Defining Environment Variables for a Container](/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)
|
||||
* [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core)
|
||||
* [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core)
|
||||
* [ResourceFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
|
||||
-->
|
||||
|
||||
* [给容器定义环境变量](/zh/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)
|
||||
* [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core)
|
||||
* [ObjectFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#objectfieldselector-v1-core)
|
||||
* [ResourceFieldSelector](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcefieldselector-v1-core)
|
||||
|
||||
Reference in New Issue
Block a user