Retrieve the full name of a service in Kubernetes
Asked Answered
P

2

37

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?

Parvis answered 2/1, 2020 at 6:55 Comment(1)
nslookup service name will give you thisSceptic
G
58

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.

Glaze answered 2/1, 2020 at 7:10 Comment(7)
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?Parvis
You need to know the namespace at least. You can do nslookup db-service.dev or nslookup db-service.prodGlaze
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.Parvis
Where are you running the nslookup command? You should be running it inside a container in a pod.Glaze
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.localParvis
My pod doesn't have nslookup ...Berdichev
Note that api-server is actually the name of the service in the node, and not a constant commandAnimalcule
A
16

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>
Abstracted answered 15/11, 2021 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.