issue: My application in GKE is not able to connect with mongo Atlas over VPC peering , however if I run mongosh from a pod in GkE cluster manually using mongodb+srv://cluster0-pri.mongodb.net/test --apiVersion 1 --username user --password pass
it works well.
I did a little more investigation in my case and figured out something un usual. In application properties I am using
uri: mongodb+srv://user:[email protected]/test
URI and because of this it is taking a public route and not going through vpc peering, I test and I am getting same timeout while I try to ssh from a pod to mongo cluster after I remove 0.0.0.0/0 from ip whitelist in atlas.
is there a Better way of making application communicate with mongo in GKE/GCP. or a different way of using URI like below
mongodb+srv://cluster0.mongodb.net/test?apiVersion=1&username=<user>&password=<pass>
when I tried this command it say that apiversion is not supported and if I run the same command like below it works fine
mongodb+srv://cluster0.mongodb.net/test --apiVersion 1 --username user --password pass
2
Answers
The fix was to add
primary
to the Uri that we are using, when you add-pri
it will allow the source to resolve at private ipuri: mongodb+srv://user:[email protected]/test
You can try using cloud NAT instead of VPC peering. From the link shared, you can assign or reserve a static IP address to the router that will be used once you create a cloud NAT.
Since the router’s IP address is static (permanent) it’s safe to use and whitelist the IP address to MongoDB Atlas under Network Access -> IP Access list instead of using 0.0.0.0/0. Its normal that you have a timeout error message once you remove the whitelist, because it only allows client connection once IP address is whitelisted, you can see this information in this link