Skip to content

Instantly share code, notes, and snippets.

@rkuzsma
Created January 5, 2017 03:33
Show Gist options
  • Save rkuzsma/2450c1cfe5b27ae2344c0c1c2c769323 to your computer and use it in GitHub Desktop.
Save rkuzsma/2450c1cfe5b27ae2344c0c1c2c769323 to your computer and use it in GitHub Desktop.
Get external IP address of Kubernetes nodes
#!/bin/bash
kubectl get nodes --selector=kubernetes.io/role!=master -o jsonpath={.items[*].status.addresses[?\(@.type==\"ExternalIP\"\)].address}
@liggetm
Copy link

liggetm commented Dec 7, 2018

The addresses structure has changed:

    "status": {
        "addresses": [
            {
                "address": "172.29.29.29",
                "type": "LegacyHostIP"
            },
            {
                "address": "172.29.29.29",
                "type": "InternalIP"
            },
            {
                "address": "atomic62",
                "type": "Hostname"
            }
        ],

So now it's kubectl get nodes --selector=kubernetes.io/role!=master -o jsonpath={.items[*].status.addresses[?\(@.type==\"InternalIP\"\)].address}

@helloguille
Copy link

helloguille commented Apr 23, 2021

For those using zsh, they need to use quotes around the jsonpath argument, and remove the escape characters:

kubectl get nodes -o jsonpath="{.items[*].status.addresses[?(@.type=='ExternalIP')].address}"

@ominidx
Copy link

ominidx commented Oct 21, 2021

kubectl get nodes -o wide | awk '{print $7}'

@h0jeZvgoxFepBQ2C
Copy link

is it possible to get this somehow as an environment variable in a deployment?

Like following from the downward API?

            - name: K8S_HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment