Server selection timed out after 30000 ms {"name":"MongooseTimeoutError","reason":{"name":"MongoNetworkError"}}

1/19/2020

Context

I'm using GCP, more specifically GKE to deploy my app in container/pod. The app I'm trying to deploy is in node js (express js). This app connects to MongoDB Atlas (free tier M0).

No issue when running the project locally. It connects to the database and I can add/remove documents without any problems.

I allowed my MongoDB Atlas Cluster to get access by anyone (0.0.0.0/0) to make it easier debugging.

When I deploy my project with my CI/CD Deploy to GKE every thing goes smooth.

Problem

Things starts to get tricky once I've deployed my project. I get a CrashLoopBackOff. It keeps crashing after checking the logs here is what I've found:

error: Server selection timed out after 30000 ms {"name":"MongooseTimeoutError","reason":{"name":"MongoNetworkError"}}

Leads

I believe the issue is that my pod can't connect to MongoDB Atlas through its regular port 27017 sending me a timeout error.

Here is what I've tried:

  • Adding in my VPC Network in GCP a new firewall rule: gcloud compute firewall-rules create allow-mongodb --allow tcp:27017

  • Adding in my deployment.yml the following key/value: dnsPolicy: Default

Conclusion

After spending hours on this problem, I still didn't find any solutions and I'm running out of ideas. FYI, I'm new to GCP and to Kubernetes so I might be missing something big here but not sure what.

If some kind person end up on that post and knows the answer I would be glad if he/she could help me out here.

Have a good one.

Cheers,

-- iji
google-cloud-platform
google-kubernetes-engine
kubernetes
kubernetes-pod
mongodb-atlas

1 Answer

1/19/2020

If you think the network policy has been set properly with GCP firewall rules, let's work out it in kubernetes step by step.

  1. check if you can connect to mongodb Atlas from the containers themselves.
kubectl exec <node_app_pod> --command -- curl <mongo_url>:27017
  1. check the network policy in kubernetes. To make it simplified, allow all egress
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-all
spec:
  podSelector: {}
  egress:
  - {}
  policyTypes:
  - Egress

If you want to set port 27017 only, you can adjust with this document :

https://kubernetes.io/docs/concepts/services-networking/network-policies/

Let me know if it is better now.

-- BMW
Source: StackOverflow