Reputation: 324
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.
Upvotes: 15
Views: 53846
Reputation: 1714
For the sake of completeness and those wondering why
kubectl get svc -o jsonpath='{.items[].spec.loadBalancerIP}'
does not work as-expected (if you have more than one service) while
kubectl get svc -o json | jq '.items[].spec.loadBalancerIP'
does => apparently jq
uses a slightly different dialect. The correct way to fetch this from spec.LoadBalancerIP
instead of fetching in from .status
would be
kubectl get svc -o jsonpath='{.items[?(@.spec.loadBalancerIP)].spec.loadBalancerIP}'
(side-note: It has no benefit over the other answers provided here, im just writing this here because i ran into this and had the feeling that the jsonpath
is evaluated against a different json than what i get when passing -o json
. But i was wrong, its just different notation. If you pass []
to jsonpath it will only give the first entry. passing [*]
would be equivalent to jq's []
. Here we say "iterate over all list-items that have a spec.loadBalancerIP
element)
Its in the docs at https://kubernetes.io/docs/reference/kubectl/jsonpath/
Upvotes: 0
Reputation: 174
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]"
Upvotes: 0
Reputation: 3649
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}'
Be sure to replace the namespace and service name, respectively, with yours.
Upvotes: 29
Reputation: 842
Type
minikube tunnel
or
kubectl cluster-info
You can get the public exposed IP of your relevant service.
Upvotes: 0
Reputation: 66
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}"
Upvotes: 2
Reputation: 1874
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}"
Upvotes: 0
Reputation: 2889
...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.
Upvotes: 2
Reputation: 59
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
Upvotes: 2
Reputation: 484
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}"
Upvotes: 6
Reputation: 22884
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
Upvotes: 13