<p>I set up a Kubernetes Cluster on Hetzner following theses steps: <a href="https://github.com/kube-hetzner/terraform-hcloud-kube-hetzner" rel="nofollow noreferrer">https://github.com/kube-hetzner/terraform-hcloud-kube-hetzner</a></p>
<pre><code>Client Version: v1.26.3
Kustomize Version: v4.5.7
Server Version: v1.26.4+k3s1
Mongosh Version: 1.8.1
</code></pre>
<p>I am unable to connect to either my own mongodb server (docker deployment) or a hosted one on <code>mongodb.net</code>:</p>
<pre><code>root@trustsigner-frontend-deployment-59644b6b55-pqgmm:/usr/share/nginx/html# mongosh mongodb+srv://&lt;removed-user&gt;:&lt;removed-password&gt;@cluster0.fdofntt.mongodb.net/test
Current Mongosh Log ID: 6447807561ebcee04b00165d
Connecting to:          mongodb+srv://&lt;credentials&gt;@cluster0.fdofntt.mongodb.net/test?appName=mongosh+1.8.1
MongoServerSelectionError: Server selection timed out after 30000 ms
</code></pre>
<p>Same error when using my own one with <code>mongodb://</code> instead of <code>mongodb+srv//</code>.</p>
<p>But surprisingly it is possible to use the same connection string with mongodb compass or mongosh that is installed on my machine (not in a kubernetes pod)</p>
<p>Ping to 8.8.8.8 or to any other side works and I can fetch via curl. But no chance to establish a mongodb connection...</p>

<p>Experiencing the same issue while using <a href="https://github.com/kube-hetzner/terraform-hcloud-kube-hetzner" rel="nofollow noreferrer">kube-hetzner</a> as you, I looked at the configuration file and found the culprit: by default, outbound traffic is filtered, except for a few popular ports like HTTP or HTTPS.</p>
<p>There are at least 2 solutions:</p>
<ol>
<li><p>you set the variable <code>restrict_outbound_traffic</code> to <em>false</em>:
<a href="https://github.com/kube-hetzner/terraform-hcloud-kube-hetzner/blob/master/kube.tf.example#L395" rel="nofollow noreferrer">https://github.com/kube-hetzner/terraform-hcloud-kube-hetzner/blob/master/kube.tf.example#L395</a></p>
</li>
<li><p>You add a firewall rule to allow outbound traffic on port 27017.</p>
</li>
</ol>
<p>Once done, the connection to a Mongo Atlas cluster is working perfectly!</p>
