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

9

14

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.

Sloven answered 25/12, 2017 at 14:48 Comment(0)
G
13

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

Grandfatherly answered 25/12, 2017 at 15:9 Comment(3)
Note: use -r on the jq to get the raw value if you want to use it elsewhere (otherwise it includes the quotes in the string it returns)Continue
I prefer using jsonpath kubectl get services -l component=controller,app=nginx-ingress -o jsonpath="{.items[0].status.loadBalancer.ingress[0].hostname}"Coheman
This gave me an error of Cannot index array with string "hostname", instead I used jq -r ".status.loadBalancer.ingress | .[].ip"Maria
C
27

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 \
   ingress-nginx-controller \
   --output jsonpath='{.status.loadBalancer.ingress[0].ip}'

NOTE

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

Continue answered 30/1, 2019 at 19:50 Comment(0)
G
13

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

Grandfatherly answered 25/12, 2017 at 15:9 Comment(3)
Note: use -r on the jq to get the raw value if you want to use it elsewhere (otherwise it includes the quotes in the string it returns)Continue
I prefer using jsonpath kubectl get services -l component=controller,app=nginx-ingress -o jsonpath="{.items[0].status.loadBalancer.ingress[0].hostname}"Coheman
This gave me an error of Cannot index array with string "hostname", instead I used jq -r ".status.loadBalancer.ingress | .[].ip"Maria
T
6

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}"
Twannatwattle answered 21/5, 2019 at 10:58 Comment(0)
A
2

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

Apiculture answered 9/1, 2020 at 11:33 Comment(1)
My two cents: some terminals (like mine running on Mac OS) require single quotes to do the match, otherwise it can't find it kubectl -n default get svc my-service-name -o json | jq -r '.status.loadBalancer.ingress[].hostname'Amaral
F
2

...and yet another way... This will list all the "load-balancer" services

kubectl get services --all-namespaces -o json | jq -r '.items[] | { name: .metadata.name, ns: .metadata.namespace, ip: .status.loadBalancer?|.ingress[]?|.ip }'

Depending on the networkPlugin used by your cluster services/pods may be exposed directly on external-ip. But this will also find an Ingress controllers run in the cluster.

Fading answered 17/5, 2020 at 13:53 Comment(0)
B
2

To get the external-ip on GCP i can use:

kubectl get services --namespace=<your-namespace> -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"
Boehmenism answered 4/3, 2022 at 10:45 Comment(0)
G
0

All previous solutions don't work any more for me (on GCP).

To get the IP:

kubectl get ingress <YOUR_INGRESS_NAME> -o jsonpath="{.status.loadBalancer.ingress[0].ip}"

To get the host-name:

kubectl get ingress <YOUR_INGRESS_NAME> -o jsonpath="{.spec.rules[0].host}"
Graf answered 9/6, 2021 at 17:28 Comment(0)
H
0

Type

minikube tunnel

or

kubectl cluster-info

You can get the public exposed IP of your relevant service.

Handicap answered 21/6, 2022 at 5:53 Comment(0)
E
0

Kubernetes v1.30.1

This is how I get the external IP of my load balancer.

kubectl get svc <service_name> -n <namespace> --no-headers -o custom-columns=":spec.externalIPs[0]"
Elane answered 14/8 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.