How do I get the External IP of a Kubernetes service as a raw value?

14,448

Solution 1

Maybe not GKE as my clusters are on AWS, but I assume logic will be similar. When you kubectl get svc you can select output format and it will show more then just the "normal" get. For me, with ELB based services to het LB hostname it's enough to run ie. kubectl -n kube-system get svc cluster-nginx-ingress-controller -o json | jq .status.loadBalancer.ingress.hostname

Solution 2

You can use the jsonpath output type to get the data directly without needing the additional jq to process the json:

kubectl get services --namespace ingress-nginx nginx-ingress-controller --output jsonpath='{.status.loadBalancer.ingress[0].ip}'

(Be sure to replace the namespace and service name, respectively, with yours.)

Solution 3

In my case 'kubectl get services' returns array of items, but not just one service.

So then such jsonpath works fine to me:

kubectl get services -l component=controller,app=nginx-ingress -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"

Solution 4

The answers above do not provide the output the user asked. The correct command would be: kubectl -n $namespace get svc $ingressServiceName -o json | jq -r .status.loadBalancer.ingress[].hostname

Share:
14,448
stm
Author by

stm

Updated on July 08, 2022

Comments

  • stm
    stm almost 2 years

    I am running an application with GKE. It works fine but I can not figure out how to get the external IP of the service in a machine readable format. So i am searching a gcloud or kubectl command that gives me only the external IP or a url of the format http://192.168.0.2:80 so that I can cut out the IP.