28

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?

Juliano Costa
  • 2,175
  • 2
  • 18
  • 30

2 Answers2

42

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.

Shashank V
  • 10,007
  • 2
  • 25
  • 41
  • 1
    Lets say that I have the service db-service running in `-n dev` and `-n prod`. I know that the FQDN would be `db-service.dev.svc.cluster.local` and `db-service.prod.svc.cluster.local` respectively, but how would I use the nslookup in this scenario? – Juliano Costa Jan 02 '20 at 07:19
  • You need to know the namespace at least. You can do `nslookup db-service.dev` or `nslookup db-service.prod` – Shashank V Jan 02 '20 at 07:21
  • 1
    I think I'm doing something wrong, cause the only result that I'm able to get is: `** server can't find xxx: NXDOMAIN`, where xxx is the service name. – Juliano Costa Jan 02 '20 at 08:18
  • 1
    Where are you running the nslookup command? You should be running it inside a container in a pod. – Shashank V Jan 02 '20 at 08:35
  • 2
    It worked, thanks @aviator.`master $ kubectl exec -it redis -- nslookup redis-service.default nslookup: can't resolve '(null)': Name does not resolve Name: redis-service.default Address 1: 10.107.229.159 redis-service.default.svc.cluster.local` – Juliano Costa Jan 02 '20 at 09:15
  • My pod doesn't have nslookup ... – Adonis Dec 14 '21 at 16:14
  • Note that `api-server` is actually the name of the service in the node, and not a constant command – Eido95 Jun 10 '23 at 18:07
14

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>
bwl1289
  • 1,655
  • 1
  • 12
  • 10