using kubectl or kubernetes api to fetch external ip of a service

7/9/2017

Folks, Is there an easier method to grab the external ip address of a service in Kubernetes, than to parsing the output of a kubectl output?

kubectl get services/foo --namespace=foo -o json

Thanks!

-- Cmag
google-cloud-platform
kubectl
kubernetes

2 Answers

7/10/2017

Using kubectl is the easiest way to get the ingress IP addresses of your services. If you are looking to get just the IP addresses then you can do most of the parsing as part of the kubectl command itself.

kubectl get svc foo -n foo \
    -o jsonpath="{.status.loadBalancer.ingress[*].ip}"

This may not apply to you but some cloud load balancers (like AWS ELB) give you a hostname rather than IP address so you will need to look for that instead.

kubectl get svc foo -n foo \
    -o jsonpath="{.status.loadBalancer.ingress[*].hostname}"

You can get both by using the jsonpath union operator if you like.

kubectl get svc foo -n foo \
    -o jsonpath="{.status.loadBalancer.ingress[*]['ip', 'hostname']}"

If you want a human readable output you can use the custom-columns output format.

kubectl get svc foo -n foo \
    -o custom-columns="NAME:.metadata.name,IP ADDRESS:.status.loadBalancer.ingress[*].ip"
-- Ian Lewis
Source: StackOverflow

7/10/2017

Can't you just use jq to do something like

kubectl get services/foo --namespace=foo -o json| jq '.items[0].status.hostIP'

https://stedolan.github.io/jq/

-- tommybananas
Source: StackOverflow