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:


In my case it is:


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 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

Name:	mongodb-service.default.svc.cluster.local
Name:	mongodb-service.default.svc.cluster.local
Name:	mongodb-service.default.svc.cluster.local


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   minikube   <none>           <none>
mongodb-deployment-0             1/1     Running   0          19m   minikube   <none>           <none>
mongodb-deployment-1             1/1     Running   0          18m   minikube   <none>           <none>
mongodb-deployment-2             1/1     Running   0          18m   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      <none>        443/TCP          21m
mongo-express-service   LoadBalancer   <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
Address 1: kube-dns.kube-system.svc.cluster.local

Name:      mongodb-service
Address 1: 10-244-0-5.mongodb-service.default.svc.cluster.local
Address 2: 10-244-0-4.mongodb-service.default.svc.cluster.local
Address 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:


as a hostname.

Try adding below to your StatefulSet manifest file.

  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.