通过文件将 Pod 信息暴露给容器

本页演示 Pod 如何使用 downwardAPI,向 Pod 中运行的容器暴露自身信息。downwardAPI 卷可以暴露 Pod 字段和容器字段。

在 Kubernetes 中,有两种方法可以向运行中的容器暴露 Pod 和容器字段

这两种暴露 Pod 和容器字段的方法统称为 downward API

开始之前

你需要有一个 Kubernetes 集群,并且 kubectl 命令行工具已经配置为能够与你的集群通信。建议在本教程中使用的集群至少包含两个非控制平面主机节点。如果你还没有集群,可以使用 minikube 创建一个,或者使用以下 Kubernetes 演练场之一

存储 Pod 字段

在本练习的这一部分,你将创建一个包含一个容器的 Pod,并将 Pod 级别的字段作为文件投射到运行中的容器内。以下是该 Pod 的清单

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-downwardapi-volume-example
  labels:
    zone: us-est-coast
    cluster: test-cluster1
    rack: rack-22
  annotations:
    build: two
    builder: john-doe
spec:
  containers:
    - name: client-container
      image: registry.k8s.io/busybox:1.27.2
      command: ["sh", "-c"]
      args:
      - while true; do
          if [[ -e /etc/podinfo/labels ]]; then
            echo -en '\n\n'; cat /etc/podinfo/labels; fi;
          if [[ -e /etc/podinfo/annotations ]]; then
            echo -en '\n\n'; cat /etc/podinfo/annotations; fi;
          sleep 5;
        done;
      volumeMounts:
        - name: podinfo
          mountPath: /etc/podinfo
  volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: "labels"
            fieldRef:
              fieldPath: metadata.labels
          - path: "annotations"
            fieldRef:
              fieldPath: metadata.annotations

在清单中,你可以看到该 Pod 有一个 downwardAPI 卷,并且容器将该卷挂载到 /etc/podinfo

查看 downwardAPI 下的 items 数组。数组中的每个元素定义一个 downwardAPI 卷。第一个元素指定 Pod 的 metadata.labels 字段的值应存储在一个名为 labels 的文件中。第二个元素指定 Pod 的 annotations 字段的值应存储在一个名为 annotations 的文件中。

创建 Pod

kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume.yaml

验证 Pod 中的容器正在运行

kubectl get pods

查看容器的日志

kubectl logs kubernetes-downwardapi-volume-example

输出显示了 labels 文件和 annotations 文件的内容

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 文件

/# cat /etc/podinfo/labels

输出显示 Pod 的所有标签都已写入 labels 文件

cluster="test-cluster1"
rack="rack-22"
zone="us-est-coast"

类似地,查看 annotations 文件

/# cat /etc/podinfo/annotations

查看 /etc/podinfo 目录中的文件

/# ls -laR /etc/podinfo

在输出中,你可以看到 labelsannotations 文件位于一个临时子目录中:在本例中是 ..2982_06_02_21_47_53.299460680。在 /etc/podinfo 目录中,..data 是指向该临时子目录的符号链接。同样在 /etc/podinfo 目录中,labelsannotations 也是符号链接。

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) 原子地进行更新。

退出 shell

/# exit

存储容器字段

在前面的练习中,你使用了 downward API 使 Pod 级别的字段可访问。在接下来的练习中,你将传递属于 Pod 定义一部分的字段,但这些字段是从特定的 容器 获取的,而不是从整个 Pod 获取的。以下是再次只包含一个容器的 Pod 的清单

apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-downwardapi-volume-example-2
spec:
  containers:
    - name: client-container
      image: registry.k8s.io/busybox:1.27.2
      command: ["sh", "-c"]
      args:
      - while true; do
          echo -en '\n';
          if [[ -e /etc/podinfo/cpu_limit ]]; then
            echo -en '\n'; cat /etc/podinfo/cpu_limit; fi;
          if [[ -e /etc/podinfo/cpu_request ]]; then
            echo -en '\n'; cat /etc/podinfo/cpu_request; fi;
          if [[ -e /etc/podinfo/mem_limit ]]; then
            echo -en '\n'; cat /etc/podinfo/mem_limit; fi;
          if [[ -e /etc/podinfo/mem_request ]]; then
            echo -en '\n'; cat /etc/podinfo/mem_request; fi;
          sleep 5;
        done;
      resources:
        requests:
          memory: "32Mi"
          cpu: "125m"
        limits:
          memory: "64Mi"
          cpu: "250m"
      volumeMounts:
        - name: podinfo
          mountPath: /etc/podinfo
  volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: "cpu_limit"
            resourceFieldRef:
              containerName: client-container
              resource: limits.cpu
              divisor: 1m
          - path: "cpu_request"
            resourceFieldRef:
              containerName: client-container
              resource: requests.cpu
              divisor: 1m
          - path: "mem_limit"
            resourceFieldRef:
              containerName: client-container
              resource: limits.memory
              divisor: 1Mi
          - path: "mem_request"
            resourceFieldRef:
              containerName: client-container
              resource: requests.memory
              divisor: 1Mi

在清单中,你可以看到该 Pod 有一个 downwardAPI,并且该 Pod 中的单个容器将该卷挂载到 /etc/podinfo

查看 downwardAPI 下的 items 数组。数组中的每个元素定义了 downward API 卷中的一个文件。

第一个元素指定,在名为 client-container 的容器中,limits.cpu 字段的值应以 1m 指定的格式发布为一个名为 cpu_limit 的文件。divisor 字段是可选的,其默认值为 1。除数为 1 表示 cpu 资源的单位是核,memory 资源的单位是字节。

创建 Pod

kubectl apply -f https://k8s.io/examples/pods/inject/dapi-volume-resources.yaml

进入运行在你 Pod 中的容器的 shell

kubectl exec -it kubernetes-downwardapi-volume-example-2 -- sh

在你的 shell 中,查看 cpu_limit 文件

# Run this in a shell inside the container
cat /etc/podinfo/cpu_limit

你可以使用类似的命令来查看 cpu_requestmem_limitmem_request 文件。

将键投射到特定路径并设置文件权限

你可以将键投射到特定路径并按文件设置特定权限。更多信息,请参阅Secret

下一步

  • 阅读 Pod 的 spec API 定义。这其中包括了容器(Pod 的一部分)的定义。
  • 阅读你可以使用 downward API 暴露的可用字段列表。

阅读旧版 API 参考中关于卷的信息

上次修改时间:2023 年 8 月 24 日下午 6:38 PST:使用 code_sample 短代码代替 code 短代码 (e8b136c3b3)