Search code examples
bashparsingkuberneteskubernetes-ingresskubectl

Obtain the hostname served by a Kubernetes Ingress


How to resolve the domain/hostname of a K8s Service, that a specific K8s Ingress is serving?

In the namespace foobar, I want to know where to connect for the service provided by the ingress.

kubectl --namespace foobar get ingress

returns the available ones, and

kubectl --namespace foobar describe ingress/bazbar

returns the details; I can match by name (e.g. barbaz) the one I'm targeting.
But how can I extrapolate the host (and, possibly, also the path) to then launch it in the browser with xdg-open?


Solution

  • The below should solve your query on getting domain per namespace.

    The query below "get ingress" retrieves the domain details from all namespaces and using awk, it prints the 1st column which is the namespace and the 4th column which is the domain in the ingress, you can grep it further to filter down on particular namespace.

    #To get all namespace and domain
    kubectl get ingress --all-namespaces|awk '{print $1 " | " $4 }'
    foobar | foobar.example.com
    barfoo | barfoo.example.com
    
    
    #To filter on namespace from all namespace
    kubectl get ingress --all-namespaces|awk '{print $1 " | " $4 }'|grep -i foobar
    foobar | foobar.example.com
    
    #To get one namespace
    kubectl get ingress -n <namespace-name>|awk '{print $1 " | " $4 }'
    foobar | foobar.example.com