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}
@sethus-git
Copy link

It not displaying any output.
[root@master001 ~]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master001 Ready master 5d v1.9.4 CentOS Linux 7 (Core) 3.10.0-862.el7.x86_64 docker://17.3.2
worker001 Ready node 4d v1.9.4 CentOS Linux 7 (Core) 3.10.0-862.el7.x86_64 docker://17.3.2
worker002 Ready node 7h v1.9.4 CentOS Linux 7 (Core) 3.10.0-862.el7.x86_64 docker://17.3.2

@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