kubernetes 自从1.7开始,可以在pod 的container 内获取pod的spec,metadata 等信息。
具体方法可以通过env获取:
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: MY_POD_SERVICE_ACCOUNT
valueFrom:
fieldRef:
fieldPath: spec.serviceAccountName
spec.nodeName : pod所在节点的IP、宿主主机IP
status.podIP :pod IP
metadata.namespace : pod 所在的namespace
更多参数:https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
https://github.com/kubernetes/kubernetes/issues/24657