1

I am connecting a Nodejs app running on the GCP Kubernetes cluster. Before, it worked fine, when I enabled VPC Peering and I was using the connection string (Private). It is recommended on the Atlas dashboard:

mongodb+srv://(username):(password)@<cluster_name>-pri.m6tkv.gcp.mongodb.net/<database_name>?retryWrites=true&w=majority

I followed this tutorial to setup: https://docs.atlas.mongodb.com/security-vpc-peering/

I cannot understand since my app has been running for quite a while with this configuration. But one fine day, the app running on a pod in my GCP cluster is unable to connect.

Thats the error message:

Connected mongodb failed MongoNetworkError: failed to connect to server [<cluster_name>-00-00-pri.m6tkv.gcp.mongodb.net:27017] on first connect [MongoNetworkError: connection timed out  
at connectionFailureError (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:406:14)                                                                  
at TLSSocket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:294:16)                                                                   
at Object.onceWrapper (events.js:299:28)                                                                                                                                           
at TLSSocket.emit (events.js:210:5)                                                                                                                                                
at TLSSocket.Socket._onTimeout (net.js:469:8)                                                                                                                                      
at listOnTimeout (internal/timers.js:531:17)                                                                                                                                       
at processTimers (internal/timers.js:475:7) {name: 'MongoNetworkError'                                                                                                                                                           }]                                                                                                                                                                                    at Pool.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:438:11)                                                                         
at Pool.emit (events.js:210:5)                                                                                                                                                     
at /app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:562:14                                                                                              
at /app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:1009:9                                                                                              
at /app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:31:7                                                                                             
at callback (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:264:5)                                                                                 
at TLSSocket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:294:7)                                                                    
at Object.onceWrapper (events.js:299:28)                                                                                                                                           
at TLSSocket.emit (events.js:210:5)                                                                                                                                                
at TLSSocket.Socket._onTimeout (net.js:469:8)                                                                                                                                      
at listOnTimeout (internal/timers.js:531:17)                                                                                                                                       
at processTimers (internal/timers.js:475:7)    

I switched back to the old connection (Standard), my app works again. The connection string:

mongodb+srv://(username):(password)@<cluster_name>.m6tkv.gcp.mongodb.net/<database_name>?retryWrites=true&w=majority

So connection via VPC Peering is having some problem, I couldn't find any clue.

TuanNA
  • 11
  • 1
  • I solved this problem by adding pod address range in my cluster to the IP whitelist on Atlas – TuanNA Aug 17 '20 at 09:49
  • 1
    Hello, I'm glad that you resolved your issue. Please provide your comment as an answer with an explanation how you've managed to solve it for a better visibility. – Dawid Kruk Aug 17 '20 at 13:01

1 Answers1

1

Posting this answer as a community wiki for better visibility.

I solved this problem by adding the pod address range in my cluster to the IP whitelist on Atlas.

The solution to this problem was to add the pod address range to the IP whitelist of the Atlas server.

It's also mentioned in the official documentation:

You must add your VPC CIDR block address (or subset) associated with the peer VPC to the whitelist before your new VPC peer can connect to your Atlas cluster. To learn about Auto mode IP ranges that GCP uses, see Auto mode IP ranges.

Docs.atlas.mongodb.com: Security VPC Peering

You will need to follow below link with a guide inside:

Dawid Kruk
  • 588
  • 2
  • 8