Kubernetes 向容器中添加数据 Downward API

2020-05-22 0 By admin

使用 Kubernetes 容器编辑工具在配置 Deployment、Statefulset 等对象时,会遇到向容器添加环境变量的情况。
这些环境变量可能是一个固定的字符串、也可能是容器所在的环境信息、或者容器自身运行中的环境(如容器的IP地址和Service 名称等)。

一、可通过环境变量和downwardAPI卷提供给容器

1.1、Information available via fieldRef

  1. metadata.name – the pod’s name
  2. metadata.namespace – the pod’s namespace
  3. metadata.uid – the pod’s UID, available since v1.8.0-alpha.2
  4. metadata.labels['key'] – the value of the pod’s label key(for example, metadata.labels[‘mylabel’]); available in Kubernetes 1.9+
  5. metadata.annotations['key'] – the value of the pod’s annotation key (for example, metadata.annotations[‘myannotation’]); available in Kubernetes 1.9+

1.2、Information available via resourceFieldRef

  1. A Container’s CPU limit
  2. A Container’s CPU request
  3. A Container’s memory limit
  4. A Container’s memory request
  5. A Container’s ephemeral-storage limit, available since v1.8.0-beta.0
  6. A Container’s ephemeral-storage request, available since v1.8.0-beta.0

1.3、he following information is available through downwardAPI volume fieldRef

  1. metadata.labels – all of the pod’s labels, formatted as label-key=”escaped-label-value” with one label per line
  2. metadata.annotations – all of the pod’s annotations, formatted as annotation-key=”escaped-annotation-value” with one annotation per line

二、information is available through environment variables

  1. status.podIP – the pod’s IP address
  2. spec.serviceAccountName – the pod’s service account name, available since v1.4.0-alpha.3
  3. spec.nodeName – the node’s name, available since v1.4.0-alpha.3
  4. status.hostIP – the node’s IP, available since v1.7.0-alpha.1