<p><strong>Context</strong></p>

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

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

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

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

<p><strong>Problem</strong></p>

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

<p><code>error: Server selection timed out after 30000 ms {"name":"MongooseTimeoutError","reason":{"name":"MongoNetworkError"}}</code></p>

<p><strong>Leads</strong></p>

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

<p>Here is what I've tried:</p>

<ul>
<li><p>Adding in my VPC Network in GCP a new <strong>firewall rule</strong>: 
<code>gcloud compute firewall-rules create allow-mongodb --allow tcp:27017</code></p></li>
<li><p>Adding in my deployment.yml the following key/value: <code>dnsPolicy: Default</code></p></li>
</ul>

<p><strong>Conclusion</strong></p>

<p>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.</p>

<p>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.</p>

<p>Have a good one.</p>

<p>Cheers,</p>

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

<ol>
<li>check if you can connect to mongodb Atlas from the containers themselves. </li>
</ol>

<pre><code>kubectl exec &lt;node_app_pod&gt; --command -- curl &lt;mongo_url&gt;:27017
</code></pre>

<ol start="2">
<li>check the network policy in kubernetes. To make it simplified, allow all egress</li>
</ol>

<pre><code>apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-all
spec:
  podSelector: {}
  egress:
  - {}
  policyTypes:
  - Egress
</code></pre>

<p>If you want to set port 27017 only, you can adjust with this document : </p>

<p><a href="https://kubernetes.io/docs/concepts/services-networking/network-policies/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/network-policies/</a></p>

<p>Let me know if it is better now. </p>
