How do you get the Node IP from inside a Pod?

Viewed 12796

I am running a go app that is creating prometheus metrics that are node specific metrics and I want to be able to add the node IP as a label.

Is there a way to capture the Node IP from within the Pod?

3 Answers

The accepted answer didn't work for me, it seems fieldPath is required now:

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

Is there a way to capture the Node IP from within the Pod?

Yes, easily, using the env: valueFrom: fieldRef: status.hostIP; the whole(?) list is presented in the envVarSource docs, I guess because objectFieldSelector can appear in multiple contexts.

so:

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

On a cluster running 1.22 I had to modify mdaniel's answer to be the following:

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

Notice the extra fieldPath.

Related