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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…