Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes node.js container cannot connect to MongoDB Atlas

So I've been struggling with this all afternoon. I can't at all get my NodeJS application running on kubernetes to connect to my MongoDB Atlas database.

In my application I've tried running

mongoose.connect('mongodb+srv://admin:<password>@<project>.gcp.mongodb.net/project_prod?retryWrites=true&w=majority', { useNewUrlParser: true })

but I simply get the error

UnhandledPromiseRejectionWarning: Error: querySrv ETIMEOUT _mongodb._tcp.<project>.gcp.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:196:19)
(node:32) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 17)

I've tried setting up a ExternalName service too, but using either the URL or the ExternalName results in me not being able to connect to the database.

I've whitelisted my IP on MongoDB Atlas, so I know that isn't the issue.

It also seems to work on my local machine, but not in the kubernetes pod. What am I doing wrong?

like image 668
Matthew Weeks Avatar asked Feb 03 '23 18:02

Matthew Weeks


1 Answers

I figured out the issue, my pod DNS was not configured to allow external connections, so I set dnsPolicy: Default in my YML, because oddly enough Default is not actually the default value

like image 54
Matthew Weeks Avatar answered Feb 06 '23 07:02

Matthew Weeks