0

I am trying to set up MongoDB and MongoDB monitoring agent on a kubernetes cluster.

The monitoring agent first queries the service endpoint for the mongodb instance, and receives the hostname as a response. It then stops using the service endpoint, and starts to use the hostname to connect to the instance which fails as there is no resolution to get the container name resolved.

I think I can use a headless service to achieve this, although using headless service is not an option.

Is there any way to enable hostname resolution for containers/pods in Kubernetes or inject custom DNS records in kube-dns?

krish7919
  • 892
  • 2
  • 13
  • 30
  • Can you use IPs instead of hostname? Why not headless service is an option? Did you try `hostNetwork` option? You need to give more details for people to understand the issue. – Buchi Mar 25 '17 at 19:00
  • The agent in question does not use IPs to connect to the database, but the host name. A headless service is not an option as I also have an nginx frontend to the database with access policies, and nginx crashes when the DB instance goes down if I use a hostname. So I want hostname for nginx, but IP address or name resolution for the monitoring agent, which puts me in a catch 22 situation. I didn't know about the hostNetwork option, I will read up on it. Thanks for the hint. :) – krish7919 Mar 25 '17 at 19:49
  • I tried the `hostNetwork` option. It still does not resolve the internal cluster IP. Also, I have a mongodb replics set (not the same as a k8s replica set), and that is highly coupled with FQDN and needs name resolution. Ideally I would love it if there is some way to add a dns record to kube-dns - something that says x.y.z points to a cluster internal service at 10.a.b.c. – krish7919 Mar 27 '17 at 12:59
  • Did you try this: "With v1.3, the PodSpec has a hostname field, which can be used to specify the Pod’s hostname." – Alen Komljen Apr 03 '17 at 22:37

1 Answers1

0

You should create a StatefulSet for your use case. Because you need your pod to have a unique identifier. To quote the docs, StatefulSets have:

StatefulSet Pods have a unique identity that is comprised of an ordinal, a stable network identity, and stable storage. The identity sticks to the Pod, regardless of which node it’s (re)scheduled on.

So if you are using Deployment object for MongoDB modify it to StatefulSet object type.

Your pods will be name resolved as well.

Docs:

surajd
  • 1,627
  • 13
  • 13
  • I finally did this with the headless service. But I would have wanted a normal service with a clusterIP in this case. – krish7919 Apr 05 '17 at 07:14