Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

Reverse proxy mongodb from kubernetes with nginx

I have 3 mongodb instances running inside a kubernetes cluster. I use a service to access the pods via the built-in kubernetes dns.

I already exposed them through nginx (which is running inside the cluster) and I can connect via the mongo shell to each one from outside.

However, when I try to connect via a mongodb connection string like this:

mongodb://mydomain.com:27017,mydomain.com:27027,mydomain.com:27037/?replicaSet=rs0&readPreference=primary

I get an ENOTFOUND error, saying that mongo0.mongo could not be resolved. Like I said, if I connect to them one by one from outside it works, so I assume there is some further configuration with nginx needed.

Maybe there are some headers I missed?

My nginx configuration looks like this:

stream {
  server {
    listen  27017;
    proxy_connect_timeout 1s;
    proxy_timeout 3s;
    proxy_pass    stream_mongo_0;
  }
  ...

  upstream stream_mongo_0 {
    server mongo-0.mongo:27017;
  }
  ...
}

EDIT:

The strange thing is that connecting with a connection string that has only one host (like mongodb://mydomain.com:27017) works like a charm as long as it's the primary node. So maybe it's not an issue with dns but with my replica set?

question from:https://stackoverflow.com/questions/65934001/reverse-proxy-mongodb-from-kubernetes-with-nginx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...