JobPlus知识库 IT 大数据 文章
kubernetes中通过文件将定义pod时的字段信息暴露给容器

本文描述如何将定义pod时的字段如标签、注释等某些字段以文件的形式暴露给容器,使容器在运行时能读到这些字段,注意这里是以文件的形式不是以环境变量的形式。这个特性的主要目的是将定义pod、容器时的信息很方便的传递向运行中的容器,比如说容器运行时对cpu、内存等的限定,运行中的容器可以不用通过与集群交互,直接可以很方便的获得这些信息。

Store Pod fields

假设有如下的pod定义:

  1. apiVersion: v1

  2. kind: Pod

  3. metadata:

  4. name: kubernetes-downwardapi-volume-example

  5. labels:

  6. zone: us-est-coast

  7. cluster: test-cluster1

  8. rack: rack-22

  9. annotations:

  10. build: two

  11. builder: john-doe

  12. spec:

  13. containers:

  14. - name: client-container

  15. image: k8s.gcr.io/busybox

  16. command: ["sh", "-c"]

  17. args:

  18. - while true; do

  19. if [[ -e /etc/podinfo/labels ]]; then

  20. echo -en '\n\n'; cat /etc/podinfo/labels; fi;

  21. if [[ -e /etc/podinfo/annotations ]]; then

  22. echo -en '\n\n'; cat /etc/podinfo/annotations; fi;

  23. sleep 5;

  24. done;

  25. volumeMounts:

  26. - name: podinfo

  27. mountPath: /etc/podinfo

  28. readOnly: false

  29. volumes:

  30. - name: podinfo

  31. downwardAPI:

  32. items:

  33. - path: "labels"

  34. fieldRef:

  35. fieldPath: metadata.labels

  36. - path: "annotations"

  37. fieldRef:

  38. fieldPath: metadata.annotations

你可以在配置文件中看到,pod有一个名为downwardAPI的volume,容器将这个volume挂载到了目录/etc/podinfo。现在关注volume中的内容,看它的items数组。第一项表示是一个名为“labels”的文件,它的内容就是配置文件中"metadata.labels"路径下的内容。同样第二项是一个名为"annotations”的文件,内容是配置文件中"metadata.annotations"路径下的内容。注意,在上述例子,无论是"labels"还是"annotations",它们都是pod的有关内容,不是container的内容。

创建pod:

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

确认容器在运行:

kubectl get pods

查看容器日志:

kubectl logs kubernetes-downwardapi-volume-example

输出会显示文件"labels"与"annotations"的内容:

  1. cluster="test-cluster1"

  2. rack="rack-22"

  3. zone="us-est-coast"

  4. build="two"

  5. builder="john-doe"

为容器运行交互式shell:

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

在shell内查看"labels"文件内容:

/# cat /etc/podinfo/labels

输出如下:

  1. cluster="test-cluster1"

  2. rack="rack-22"

  3. zone="us-est-coast"

“annotations"文件相同,省略。

运行如下命令查看目录内容:

/# ls -laR /etc/podinfo

  1. drwxr-xr-x  ... Feb 6 21:47 ..2982_06_02_21_47_53.299460680

  2. lrwxrwxrwx  ... Feb 6 21:47 ..data -> ..2982_06_02_21_47_53.299460680

  3. lrwxrwxrwx  ... Feb 6 21:47 annotations -> ..data/annotations

  4. lrwxrwxrwx  ... Feb 6 21:47 labels -> ..data/labels

  5. /etc/..2982_06_02_21_47_53.299460680:

  6. total 8

  7. -rw-r--r--  ... Feb  6 21:47 annotations

  8. -rw-r--r--  ... Feb  6 21:47 labels

labels与annotations都是符号链接文件,原始文件存放在宿主机的某个目录下,也就是如果pod的配置文件变更引,首先会更新宿主机上的原始文件,而pod无需重新重创,就可以自动更新labels、annotations文件。


Store Container fields

上节描述如何将pod定义中与pod有关的字段暴露给容器,本节描述如何将pod定义中与container有关的字段暴露给运行中的容器。以下是一个pod的示例配置文件:

  1. apiVersion: v1

  2. kind: Pod

  3. metadata:

  4. name: kubernetes-downwardapi-volume-example-2

  5. spec:

  6. containers:

  7. - name: client-container

  8. image: k8s.gcr.io/busybox:1.24

  9. command: ["sh", "-c"]

  10. args:

  11. - while true; do

  12. echo -en '\n';

  13. if [[ -e /etc/podinfo/cpu_limit ]]; then

  14. echo -en '\n'; cat /etc/podinfo/cpu_limit; fi;

  15. if [[ -e /etc/podinfo/cpu_request ]]; then

  16. echo -en '\n'; cat /etc/podinfo/cpu_request; fi;

  17. if [[ -e /etc/podinfo/mem_limit ]]; then

  18. echo -en '\n'; cat /etc/podinfo/mem_limit; fi;

  19. if [[ -e /etc/podinfo/mem_request ]]; then

  20. echo -en '\n'; cat /etc/podinfo/mem_request; fi;

  21. sleep 5;

  22. done;

  23. resources:

  24. requests:

  25. memory: "32Mi"

  26. cpu: "125m"

  27. limits:

  28. memory: "64Mi"

  29. cpu: "250m"

  30. volumeMounts:

  31. - name: podinfo

  32. mountPath: /etc/podinfo

  33. readOnly: false

  34. volumes:

  35. - name: podinfo

  36. downwardAPI:

  37. items:

  38. - path: "cpu_limit"

  39. resourceFieldRef:

  40. containerName: client-container

  41. resource: limits.cpu

  42. - path: "cpu_request"

  43. resourceFieldRef:

  44. containerName: client-container

  45. resource: requests.cpu

  46. - path: "mem_limit"

  47. resourceFieldRef:

  48. containerName: client-container

  49. resource: limits.memory

  50. - path: "mem_request"

  51. resourceFieldRef:

  52. containerName: client-container

  53. resource: requests.memory

从配置文件可以看出,在downwardAPI部分,关于容器字段的引用多了一个限定条件:containerName,因为一个pod中可以包含多个容器定义,所以要加上此字段以限定到底引用的是那个容器的内容,其它与上一节介绍的内容相同。

Capabilities of the Downward API

目前好像并不是任意字段都可以以这种方式暴露给运行中的容器。

feildRef,即可以暴露的pod定义中的项目:


  • spec.nodeName - the node’s name
  • status.hostIP - the node’s IP
  • metadata.name - the pod’s name
  • metadata.namespace - the pod’s namespace
  • status.podIP - the pod’s IP address
  • spec.serviceAccountName - the pod’s service account name
  • metadata.uid - the pod’s UID
  • metadata.labels['<KEY>'] - the value of the pod’s label <KEY> (for example, metadata.labels['mylabel']); available in Kubernetes 1.9+
  • metadata.annotations['<KEY>'] - the value of the pod’s annotation <KEY> (for example, metadata.annotations['myannotation']); available in Kubernetes 1.9+

resourceFieldRef,即可以暴露的container定义中的项目:


  • A Container’s CPU limit
  • A Container’s CPU request
  • A Container’s memory limit
  • A Container’s memory request


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
418人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

5 人收藏了这篇文章
腾讯云数据库性能卓越稳定可靠,为您解决数据库运维难题
广告
扫码APP

扫描使用APP

扫码使用

扫描使用小程序