如何获取 pod 内 kubernetes 部署的系统主机名?

How to get the System HostName of kubernetes deployment inside pod?

在kubernetes中我们可以使用环境变量来传递hostIP使用

 env:
    - name: NODE_IP
      valueFrom:
        fieldRef:
          fieldPath: status.hostIP

那么类似地如何获取主机名而不是主机 IP?

env:
- name: MY_NODE_NAME
  valueFrom:
    fieldRef:
      fieldPath: spec.nodeName

参见:https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api

正如已经回答的那样,应该是 spec.nodeName

我还想提一下,您可以使用的字段数量有限。检查 Capabilities of the Downward API.

Information available via fieldRef:
metadata.name 
metadata.namespace
metadata.uid 
metadata.labels['<KEY>']
metadata.annotations['<KEY>'] 

In addition, the following information is available through downwardAPI volume fieldRef:    
metadata.labels
metadata.annotations 

The following information is available through environment variables:    
status.podIP - the pod's IP address
spec.serviceAccountName 
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