downwardAPI
downwardAPI
把pod和containers 的信息挂载到容器中
通过变量传递
fieldRef
可用字段 | 注释 | |
---|---|---|
node级别 | spec.nodeName |
|
status.hostIP |
||
pod级别 | metadata.namespace |
|
metadata.name |
||
status.podIP |
||
spec.serviceAccountName |
||
metadata.uid |
||
metadata.labels['app'] |
获取app标签的值 | |
metadata.annotations['deployment.kubernetes.io/revision'] |
样例:
# pod.spec.containers.env.valueFrom
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
resourceFieldRef
- name: MY_POD_cpu_limit
valueFrom:
resourceFieldRef:
resource: limits.cpu
divisor: 1m #作为被除数,决定了显示的单位
- name: MY_POD_cpu_request
valueFrom:
resourceFieldRef:
resource: requests.cpu
divisor: 1m #作为被除数,决定了显示的单位
- name: MY_POD_mem_request
valueFrom:
resourceFieldRef:
resource: requests.memory
divisor: 1Mi #作为被除数,决定了显示的单位
- name: MY_POD_mem_limits
valueFrom:
resourceFieldRef:
resource: limits.memory
divisor: 1Mi #作为被除数,决定了显示的单位
通过卷挂载
# pod.spec.volumes.downwardAPI
valuems:
- name: pod_info
downwardAPI:
items:
fieldRef:
fieldPath: spec.nodeName
resourceFieldRef:
resource: limits.cpu
containerName: cpu_limit
功能是什么意思
volumes:
- downwardAPI:
defaultMode: 420
items:
- path: pod_name
fieldRef:
fieldPath: metadata.name
- path: pod_cpu_limit
resourceFieldRef:
containerName: cpu_limit
divisor: "0"
resource: requests.cpu
name: podinfo
Information available via resourceFieldRef
:
A Container's hugepages limit (providing that the DownwardAPIHugePages feature gate is enabled)
A Container's hugepages request (providing that the DownwardAPIHugePages feature gate is enabled)
A Container's ephemeral-storage limit
A Container's ephemeral-storage request
这些属性只能在env 中引用
status.podIP
- the pod's IP address
spec.serviceAccountName
- the pod's service account name, available since v1.4.0-alpha.3
spec.nodeName
- the node's name, available since v1.4.0-alpha.3
status.hostIP
- the node's IP, available since v1.7.0-alpha.1
使用场景
较为典型的是在Pod中运⾏Java应⽤程序时,若未使⽤“-Xmx”选项指
定JVM的堆内存可⽤总量,它默认会设置为主机内存总量的⼀个空间⽐例
(如30%),这会导致容器中的应⽤程序申请内存资源时将会达到上限⽽
转为OOMKilled。另外,即便使⽤了“-Xmx”选项设置其堆内存上限,但它
对于⾮堆内存的可⽤空间不会产⽣任何限制作⽤,结果是仍然存在达到容
器内存资源上限的可能性。
另⼀个颇具代表性的场景是于Pod中运⾏的nginx应⽤,在配置参数
worker_processes的值为“auto”时