website/content/zh/docs/tasks/inject-data-application/downward-api-volume-expose-...

216 lines
6.2 KiB
Markdown
Raw Normal View History

---
title: 通过文件将Pod信息呈现给容器
content_template: templates/task
---
{{% capture overview %}}
此页面描述Pod如何使用DownwardAPIVolumeFile把自己的信息呈现给pod中运行的容器。DownwardAPIVolumeFile可以呈现pod的字段和容器字段。
{{% /capture %}}
{{% capture prerequisites %}}
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
{{% /capture %}}
{{% capture steps %}}
## Downward API
有两种方式可以将Pod和Container字段呈现给运行中的容器
Update localization guidelines (#10485) * Update localization guidelines for language labels Continuing work Continuing work Continuing work More work in progress Add local OWNERS folders Add an OWNERS file to Chinese Remove shortcode for repos Add Japanese Alphabetize languages, change weights accordingly More updates Add Korean in Korean Add English to languageName Feedback from gochist Move Chinese content from cn/ to zh/ Move OWNERS from cn/ to zh/ Resolve merge conflicts by updating from master Add files back in to prep for resolution After rebase on upstream/master, remove files Review and update localization guidelines Feedback from gochist, tnir, cstoku Add a trailing newline to content/ja/OWNERS Add a trailing newline to content/zh/OWNERS Drop requirement for GH repo project Clarify language about forks/branches Edits and typos Remove a shortcode specific to a multi-repo language setup Update aliases and owners Add explicit OWNERS for content/en Migrate content from Chinese repo, update regex in config.toml Remove untranslated strings Add trailing newline to content/en/OWNERS Add trailing newlines to OWNERS files add Jaguar project description (#10433) * add Jaguar project description [Jaguar](https://gitlab.com/sdnlab/jaguar) is an open source solution for Kubernetes's network based on OpenDaylight. Jaguar provides overlay network using vxlan and Jaguar CNIPlugin provides one IP address per pod. * Minor newline tweak blog post for azure vmss (#10538) Add microk8s to pick-right-solution.md (#10542) * Add microk8s to pick-right-solution.md Microk8s is a single-command installation of upstream Kubernetes on any Linux and should be included in the list of local-machine solutions. * capitalized Istio Add microk8s to foundational.md (#10543) * Add microk8s to foundational.md Adding microk8s as credible and stable alternative to get started with Kubernetes on a local machine. This is especially attractive for those not wanting to incur the overhead of running a VM for a local cluster. * Update foundational.md Thank you for your suggestions! LMK if this works now? * Rewrote first paragraph And included a bullet list of features of microk8s * Copyedit fix typo (#10545) Fix the kubectl subcommands links. (#10550) Signed-off-by: William Zhang <warmchang@outlook.com> Fix command issue (#10515) Signed-off-by: mooncake <xcoder@tenxcloud.com> remove imported community files per issue 10184 (#10501) networking.md: Markdown fix (#10498) Fix front matter, federation command-line tools (#10500) Clean up glossary entry (#10399) update slack link (#10536) typo in StatefulSet docs (#10558) fix discription about horizontal pod autoscale (#10557) Remove redundant symbols (#10556) Fix issue #10520 (#10554) Signed-off-by: William Zhang <warmchang@outlook.com> Update api-concepts.md (#10534) Revert "Fix command issue (#10515)" This reverts commit c02a7fb9f9d19872d9227814b3e9ffaaa28d85f0. Update memory-constraint-namespace.md (#10530) update memory request to 100MiB corresponding the yaml content Blog: Introducing Volume Snapshot Alpha for Kubernetes (#10562) * blog post for azure vmss * snapshot blog post Resolve merge conflicts in OWNERS* Minor typo fix (#10567) Not sure what's supposed to be here, proposing removing it. * Feedback from gochist Tweaks to feedback * Feedback from ClaudiaJKang
2018-10-12 21:25:01 +00:00
* [环境变量](/docs/tasks/configure-pod-container/environment-variable-expose-pod-information/)
* DownwardAPIVolumeFile
这两种呈现Pod和Container字段的方式都称为*Downward API*。
## 存储Pod字段
在这个练习中你将创建一个包含一个容器的pod。这是该pod的配置文件
{{< code file="dapi-volume.yaml" >}}
在配置文件中你可以看到Pod有一个`downwardAPI`类型的Volume并且挂载到容器中的`/etc`。
查看`downwardAPI`下面的`items`数组。每个数组元素都是一个[DownwardAPIVolumeFile](/docs/resources-reference/{{< param "version" >}}/#downwardapivolumefile-v1-core)。
第一个元素指示Pod的`metadata.labels`字段的值保存在名为`labels`的文件中。
第二个元素指示Pod的`annotations`字段的值保存在名为`annotations`的文件中。
{{< note >}}
**注意:** 本示例中的字段是Pod字段不是Pod中容器的字段。
{{< /note >}}
创建 Pod
```shell
kubectl create -f https://k8s.io/cn/docs/tasks/inject-data-application/dapi-volume.yaml
```
验证Pod中的容器运行正常
```shell
kubectl get pods
```
查看容器的日志:
```shell
kubectl logs kubernetes-downwardapi-volume-example
```
<!--
The output shows the contents of the labels file and the annotations file:
-->
输出显示 `labels``annotations` 文件的内容:
```shell
cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"
build="two"
builder="john-doe"
```
进入Pod中运行的容器打开一个shell
```
kubectl exec -it kubernetes-downwardapi-volume-example -- sh
```
在该shell中查看`labels`文件:
```shell
/# cat /etc/labels
```
输出显示Pod的所有labels都已写入`labels`文件。
```shell
cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"
```
同样,查看`annotations`文件:
```shell
/# cat /etc/annotations
```
查看`/etc`目录下的文件:
```shell
/# ls -laR /etc
```
在输出中可以看到,`labels` 和 `annotations`文件都在一个临时子目录中:这个例子,`..2982_06_02_21_47_53.299460680`。在`/etc`目录中,`..data`是一个指向临时子目录
的符号链接。`/etc`目录中,`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
```
用符号链接可实现元数据的动态原子刷新;更新将写入一个新的临时目录,然后`..data`符号链接完成原子更新,通过使用[rename(2)](http://man7.org/linux/man-pages/man2/rename.2.html)。
退出shell
```shell
/# exit
```
## 存储容器字段
前面的练习中你将Pod字段保存到DownwardAPIVolumeFile中。接下来这个练习你将存储容器字段。这里是包含一个容器的pod的配置文件
{{< code file="dapi-volume-resources.yaml" >}}
在这个配置文件中你可以看到Pod有一个`downwardAPI`类型的Volume,并且挂载到容器的`/etc`目录。
查看`downwardAPI`下面的`items`数组。每个数组元素都是一个DownwardAPIVolumeFile。
第一个元素指定名为`client-container`的容器中`limits.cpu`字段的值应保存在名为`cpu_limit`的文件中。
创建Pod
```shell
kubectl create -f https://k8s.io/cn/docs/tasks/inject-data-application/dapi-volume-resources.yaml
```
进入Pod中运行的容器打开一个shell
```
kubectl exec -it kubernetes-downwardapi-volume-example-2 -- sh
```
在shell中查看`cpu_limit`文件:
```shell
/# cat /etc/cpu_limit
```
你可以使用同样的命令查看`cpu_request`, `mem_limit` 和`mem_request` 文件.
{{% /capture %}}
{{% capture discussion %}}
## Capabilities of the Downward API
下面这些信息可以通过环境变量和DownwardAPIVolumeFiles提供给容器
* 节点名称
* 节点IP
* Pod名称
* Pod名字空间
* Pod IP地址
* Pod服务帐号名称
* Pod的UID
* 容器的CPU约束
* 容器的CPU请求值
* 容器的内存约束
* 容器的内存请求值
此外以下信息可通过DownwardAPIVolumeFiles获得
* Pod的标签
* Pod的注释
{{< note >}}
如果容器未指定CPU和memory limits则Downward API默认为节点可分配值。
{{< /note >}}
## 投射密钥到指定路径并且指定文件权限
你可以将密钥投射到指定路径并且指定每个文件的访问权限。更多信息,请参阅[Secrets](/docs/concepts/configuration/secret/).
## Downward API的动机
对于容器来说有时候拥有自己的信息是很有用的可避免与Kubernetes过度耦合。Downward API使得容器使用自己或者集群的信息而不必通过Kubernetes客户端或API服务器。
一个例子是有一个现有的应用假定要用一个非常熟悉的环境变量来保存一个唯一标识。一种可能是给应用增加处理层但这样是冗余和易出错的而且它违反了低耦合的目标。更好的选择是使用Pod名称作为标识把Pod名称注入这个环境变量中。
{{% /capture %}}
{{% capture whatsnext %}}
* [PodSpec](/docs/resources-reference/{{< param "version" >}}/#podspec-v1-core)
* [Volume](/docs/resources-reference/{{< param "version" >}}/#volume-v1-core)
* [DownwardAPIVolumeSource](/docs/resources-reference/{{< param "version" >}}/#downwardapivolumesource-v1-core)
* [DownwardAPIVolumeFile](/docs/resources-reference/{{< param "version" >}}/#downwardapivolumefile-v1-core)
* [ResourceFieldSelector](/docs/resources-reference/{{< param "version" >}}/#resourcefieldselector-v1-core)
{{% /capture %}}