全部产品
云市场

Downward API

更新时间:2019-10-22 09:39:10

k8s Downward API

标准的k8s有两种方式可以将Pod和Container字段呈现给运行中的容器:

  • 环境变量
  • DownwardAPIVolumeFile

这两种呈现Pod和Container字段的方式都称为Downward API

目前ACK/ASK+ECI已经支持了Downward API的大部分的常用字段。具体的使用文档见下文。

环境变量

环境变量能通过fieldRef获得如下值: metadata.name : Pod名称 、 metadata.namespace : Pod名字空间 、 metadata.uid : Pod的UID、 metadata.labels['<KEY>'] : 单个 pod 标签值 <KEY> (例如, metadata.labels['mylabel'])、 metadata.annotations['<KEY>'] : 单个 pod 的标注值 <KEY>

除此之外,环境变量fieldRef还支持spec以及status字段:

spec.serviceAccountName

spec.nodeName

status.podIP

样例如下:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: vk-downward-env
  5. annotations:
  6. regionId: cn-beijing
  7. platform: Aliyun ECI
  8. labels:
  9. app: nginx
  10. env: test
  11. spec:
  12. nodeName: virtual-kubelet
  13. containers:
  14. - image: nginx
  15. name: nginx
  16. env:
  17. - name: MY_metadata.name
  18. valueFrom:
  19. fieldRef:
  20. fieldPath: metadata.name
  21. - name: MY_metadata.namespace
  22. valueFrom:
  23. fieldRef:
  24. fieldPath: metadata.namespace
  25. - name: MY_metadata.uid
  26. valueFrom:
  27. fieldRef:
  28. fieldPath: metadata.uid
  29. - name: MY_metadata.labels
  30. valueFrom:
  31. fieldRef:
  32. fieldPath: metadata.labels['env']
  33. - name: MY_metadata.annotations
  34. valueFrom:
  35. fieldRef:
  36. fieldPath: metadata.annotations['regionId']
  37. - name: MY_status.podIP
  38. valueFrom:
  39. fieldRef:
  40. fieldPath: status.podIP
  41. - name: MY_spec.serviceAccountName
  42. valueFrom:
  43. fieldRef:
  44. fieldPath: spec.serviceAccountName
  45. - name: MY_spec.nodeName
  46. valueFrom:
  47. fieldRef:
  48. fieldPath: spec.nodeName

登入容器查看环境变量,可以看到fieldRef都生效了:

  1. root@default-vk-downward-env:/# env
  2. MY_spec.nodeName=virtual-kubelet
  3. MY_spec.serviceAccountName=default
  4. MY_metadata.annotations=cn-beijing
  5. MY_metadata.namespace=default
  6. MY_metadata.uid=f4881309-f3dd-11e9-bcf9-9efaf54dcfa7
  7. MY_metadata.name=vk-downward-env
  8. MY_metadata.labels=test
  9. MY_status.podIP=192.168.6.245
  10. KUBERNETES_SERVICE_PORT_HTTPS=443
  11. KUBERNETES_SERVICE_PORT=6443
  12. PWD=/
  13. PKG_RELEASE=1~buster
  14. HOME=/root
  15. KUBERNETES_PORT_443_TCP=tcp://172.22.128.1:443
  16. NJS_VERSION=0.3.5
  17. TERM=xterm
  18. SHLVL=1
  19. KUBERNETES_PORT_443_TCP_PROTO=tcp
  20. KUBERNETES_PORT_443_TCP_ADDR=172.22.128.1
  21. KUBERNETES_SERVICE_HOST=192.168.6.120
  22. KUBERNETES_PORT=tcp://172.22.128.1:443
  23. KUBERNETES_PORT_443_TCP_PORT=443
  24. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  25. NGINX_VERSION=1.17.4
  26. _=/usr/bin/env

DownwardAPIVolumeFile

DownwardAPIVolumeFile能通过fieldRef获得如下的值: metadata.name - Pod名称 、 metadata.namespace - Pod名字空间 、 metadata.uid - Pod的UID、 metadata.labels['<KEY>'] - 单个 pod 标签值 <KEY> (例如, metadata.labels['mylabel'])、 metadata.annotations['<KEY>'] - 单个 pod 的标注值 <KEY>。该部分和环境变量支持的范围及用法是一致的。

除此之外,DownwardAPIVolumeFile的fieldRef还支持存储所有的labels以及annotations:

metadata.annotations: 所有Pod的标签,以label-key="label-value"格式显示,每行显示一个label。

metadata.labels: Pod的注释,以annotation-key="annotation-value"格式显示,每行显示一个标签。

样例如下:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: vk-downward-down-volume
  5. annotations:
  6. regionId: cn-beijing
  7. platform: Aliyun ECI
  8. labels:
  9. app: nginx
  10. env: test
  11. spec:
  12. nodeName: virtual-kubelet
  13. containers:
  14. - image: nginx
  15. name: nginx
  16. volumeMounts:
  17. - name: podinfo
  18. mountPath: /etc/podinfo
  19. readOnly: false
  20. volumes:
  21. - name: podinfo
  22. downwardAPI:
  23. items:
  24. - path: "metadata.name"
  25. fieldRef:
  26. fieldPath: metadata.name
  27. - path: "metadata.namespace"
  28. fieldRef:
  29. fieldPath: metadata.namespace
  30. - path: "metadata.uid"
  31. fieldRef:
  32. fieldPath: metadata.uid
  33. - path: "metadata.labels"
  34. fieldRef:
  35. fieldPath: metadata.labels
  36. - path: "metadata.annotations"
  37. fieldRef:
  38. fieldPath: metadata.annotations

登入容器查看volume的挂载目录,可以看到volume的fieldRef都生效了,并存储在了容器指定的目录下:

  1. Welcome to Alibaba Cloud Elastic Container Instance!
  2. root@default-vk-downward-down-volume:/# cd /etc/podinfo/
  3. root@default-vk-downward-down-volume:/etc/podinfo# ls
  4. metadata.annotations metadata.labels metadata.name metadata.namespace metadata.uid
  5. root@default-vk-downward-down-volume:/etc/podinfo# cat metadata.namespace
  6. default
  7. root@default-vk-downward-down-volume:/etc/podinfo# cat metadata.name
  8. vk-downward-down-volume
  9. root@default-vk-downward-down-volume:/etc/podinfo# cat metadata.uid
  10. fa50b2b2-f3e3-11e9-bcf9-9efaf54dcfa7
  11. root@default-vk-downward-down-volume:/etc/podinfo# cat metadata.annotations
  12. platform="Aliyun ECI"
  13. regionId="cn-beijing"
  14. root@default-vk-downward-down-volume:/etc/podinfo# cat metadata.labels
  15. app="nginx"
  16. env="test"
  17. root@default-vk-downward-down-volume:/etc/podinfo#

注:目前仅支持pod字段,而容器的字段还不支持,比如:

  1. limits.cpu
  2. requests.cpu
  3. limits.memory
  4. requests.memory
  5. limits.ephemeral-storage
  6. requests.ephemeral-storage