By default,according to k8s documentation, Services are assigned a DNS A record for a name of the form my-svc.my-namespace.svc.cluster-domain.example
.
Is there a command to retrieve the full name of a service?
You can do a DNS query from any pod and you would get the FQDN. cluster-domain. example is just a example in the documentation. cluster.
Pod's setHostnameAsFQDN field FEATURE STATE: Kubernetes v1.22 [stable] When a Pod is configured to have fully qualified domain name (FQDN), its hostname is the short hostname. For example, if you have a Pod with the fully qualified domain name busybox-1. default-subdomain.
Using kubectl describe pods to check kube-system If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system . The Status field should be "Running" - any other status will indicate issues with the environment.
You can do a DNS query from any pod and you would get the FQDN.
# nslookup api-server
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: api-server.default.svc.cluster.local
Address: 10.104.225.18
root@api-server-6ff8c8b9c-6pgkb:/#
cluster-domain.example
is just a example in the documentation. cluster.local
is the default cluster domain assigned. So the FQDN of any service by default would be <service-name>.<namespace>.svc.cluster.local
.
You don't need to use the FQDN to access services - for services in same namespace, just the service name would be enough. For services in other namespaces, <service-name>.<namespace>
would be enough as kubernetes would automatically set up the DNS search domains.
TLDR; skip the below if you just want an automated way to do this; here's a quick bash script I wrote to do this automatically. This assumes bash is installed in the container:
k8s_shell_pod_svc_nslookup () {
kubectl exec -it $1 --container $2 -- /bin/bash -c "apt update;apt-get -y install dnsutils;nslookup $3"
}
Sample use:
k8s_shell_pod_svc_nslookup example_pod example_container example_service
Longer Explanation:
First, get the name of the service you're interested in getting the FQDN (fully qualified domain name) for by listing all of your services in the appropriate namespace:
kubectl get svc -n <namespace>
Second, get the name of the pod associated with the service you're interested in by listing all of your pods:
kubectl get pods
Third, get the container in the pod you're interested in by listing all of the containers in that pod:
kubectl get pods <pod_name> -o jsonpath='{.spec.containers[*].name}'
Fourth, you need to access the pod (and the container running in the pod) and start a bash shell. Note: I use Istio, so I'll always have multiple containers running in my pods, so I also specify my container. This assumes bash is installed in the container.
kubectl exec -it <pod_name> --container <container_name> -- /bin/bash
Fifth, once your bash shell has started, if you're running a debian
container, you'll need to use apt
to install dnsutils
before you do the nslookup. If you're not using debian
, use the appropriate alternative:
apt update && apt-get -y install dnsutils
Sixth, you can perform the nslookup
:
nslookup <service_name>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With