How to figure out pod DNS name for StatefulSet and Headless service (MongoDB)?

Hi there,

I’d like to configure StatefulSet and Headless service for MongoDB with Mongo-Express.

However, I cannot figure out the hostname of my master node which could be resolved to an IP address.

I’d like to pass the hostname to mongo-express deployment file, that mongo-express can connect to the master node.

I found this thread, where it is recommended to use:

podname-{replica-index}.{serviceName}.default.svc.cluster.local

In my case it is:

mongodb-deployment-0.mongodb-service.default.svc.cluster.local

however, when I type:

kubectl run -it --rm debug --image=tutum/dnsutils --restart=Never nslookup mongodb-deployment-0.mongodb-service.default.svc.cluster.local

I get:

Server:		10.96.0.10
Address:	10.96.0.10#53

** server can't find mongodb-deployment-0.mongodb-service.default.svc.cluster.local: NXDOMAIN

When I call nslookup for my service, then I get:

kubectl run -it --rm debug --image=tutum/dnsutils --restart=Never nslookup mongodb-service
Server:		10.96.0.10
Address:	10.96.0.10#53

Name:	mongodb-service.default.svc.cluster.local
Address: 10.244.0.4
Name:	mongodb-service.default.svc.cluster.local
Address: 10.244.0.5
Name:	mongodb-service.default.svc.cluster.local
Address: 10.244.0.3

Others:

kubectl get pods -o wide
NAME                             READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
mongo-express-859f75dd4f-ndt9j   1/1     Running   0          15m   10.244.0.6   minikube   <none>           <none>
mongodb-deployment-0             1/1     Running   0          19m   10.244.0.3   minikube   <none>           <none>
mongodb-deployment-1             1/1     Running   0          18m   10.244.0.4   minikube   <none>           <none>
mongodb-deployment-2             1/1     Running   0          18m   10.244.0.5   minikube   <none>           <none>

mongo-express-859f75dd4f-ndt9j - it is running because I hardcoded IP address in the yaml

kubectl get services
NAME                    TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes              ClusterIP      10.96.0.1      <none>        443/TCP          21m
mongo-express-service   LoadBalancer   10.110.59.92   <pending>     8081:30000/TCP   16m
mongodb-service         ClusterIP      None           <none>        27017/TCP        20m

I’d appreciate any help or comment. I have spent a lot of time researching this and reading following materials:

Cluster information:

Kubernetes version: minikube version: v1.32.0
Cloud being used: (put bare-metal if not on a public cloud): bare-metal
Installation method: home-brew
Host OS: MacOS M1
CNI and version:
CRI and version:

You can format your yaml by highlighting it and pressing Ctrl-Shift-C, it will make your output easier to read.

In addition to that a figured out some DNS names of my pods:

kubectl run -i --tty --image busybox:1.28 dns-test --restart=Never --rm
If you don't see a command prompt, try pressing enter.
/ # nslookup mongodb-service
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      mongodb-service
Address 1: 10.244.0.5 10-244-0-5.mongodb-service.default.svc.cluster.local
Address 2: 10.244.0.4 10-244-0-4.mongodb-service.default.svc.cluster.local
Address 3: 10.244.0.3 10-244-0-3.mongodb-service.default.svc.cluster.local

However, taking into consideration that the IP address of the pods can change in future, it is not wise to use something like this:

10-244-0-5.mongodb-service.default.svc.cluster.local

as a hostname.

Hi,
Try adding below to your StatefulSet manifest file.

spec:
  serviceName: mongodb-service

Please note that service name should match the name of your headless service!

Once that is done, you should be able to resolve mongodb-deployment-0.mongodb-service.default.svc.cluster.local DNS A record.

1 Like