<p>I deployed a MongoDB replica set in my Kubernetes cluster. The <strong>MongoDB replica set can be easily connected with the help of internal ClusterIP</strong> within the cluster. I even connect it to my mongo-express client.</p>
<pre><code>//Successfull and working fine internally
mongodb://db-mongodb-0.mycompany-mongodb-headless:27017/db
</code></pre>
<p>But I have to establish a remote connection for local testing and other services. I'm using the MongoDB helm chart provided by Bitnami. I opened a NodePort for both of my replica set at 30001 and 30002.</p>
<p>My first attempt to establish a remote connection was:</p>
<p>I <strong>tried to connect with my previously opened NodePort</strong>. I'm currently using Mongoose Client. The available node ports of my two replica sets are - 30001, 30002.</p>
<pre><code>//Unsuccessful
mongoose
  .connect(
    `mongodb://username:password@&lt;EXTERNAL_IP&gt;:30001,&lt;EXTERNAL_IP&gt;:30002/mydb`,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    }
  )
</code></pre>
<p>Then my second attempt was -</p>
<p>I tried <strong>port forwarding and tried to connect with my localhost</strong> like this -</p>
<pre><code>kubectl port-forward svc/database-mongodb-0-external 27017:27017
</code></pre>
<p>Then I tried to establish a connection like this and failed too</p>
<pre><code>//Unsuccessful 
//MongoError: Authentication failed.
mongodb://username:password@localhost:27017/database
</code></pre>
<p>I tried ?replicaSet=rs0 query parameters in both of the cases too. But no luck.</p>
<p>I didn't try opening a LoadBalancer. Is there any way to <strong>establish a remote connection with my Ingress-Nginx controller to my MongoDB services</strong>? I don't know if it is possible but I tried to forward my headless services to a subdomain. But that subdomain is saying that - <em><strong>It looks like you are trying to access MongoDB over HTTP on the native driver port.</strong></em> I am not sure if it's possible!</p>
<p>But finally, my only question is, how can I establish or expose a remote connection with my MongoDB replica set which is set up at a Kubernetes Cluster?</p>
<p>My latest Bitnami/Mongodb values are</p>
<pre><code>architecture: replicaset
auth:
  rootPassword: 12341234
  username: abcd
  password: 1234234
  database: abcd
  replicaSetKey: abcd
persistence:
  enabled: true
  size: 3Gi
externalAccess:
  enabled: true
  autoDiscovery:
    enabled: true
  service:
    type: NodePort
    nodePorts: ['30001', '30002']
rbac:
  create: true
</code></pre>

Md Fazlul Karim
<p>If I understood correctly, you want to expose your MongoDB replicaset to a <strong>remote</strong> connection.</p>
<p>I am actively working with the Bitnami MongoDB helm chart and I can easly connect <strong>locally</strong> to the helm chart using your first attempt:</p>
<ol>
<li>I port-forward the headless service mapping the ports 27017:27017</li>
<li>I simply connect to the localhost using username and password, the only difference with my connection string is that I do not specify the database: <code>mongodb://&lt;user&gt;:&lt;password&gt;@localhost:27017</code></li>
</ol>
<p>To answer your final question, you need to look up in the <a href="https://github.com/bitnami/charts/tree/master/bitnami/mongodb" rel="nofollow noreferrer">Official documentation page</a>, there are few values for the helm chart to expose your mongodb:</p>
<ul>
<li><code>service.type=LoadBalancer</code></li>
<li><code>externalAccess.enabled=true</code></li>
<li><code>externalAccess.autoDiscovery.enabled</code></li>
</ul>
<p>You can play with those variables to find the most suitable way of exposing your MongoDB remotely.</p>
<p>Please, note that if you set LoadBalancer and autoDiscovery enabled, you need to make sure that your k8s cluster has a LoadBalancer in front of it (<a href="https://aws.amazon.com/it/elasticloadbalancing/?elb-whats-new.sort-by=item.additionalFields.postDateTime&amp;elb-whats-new.sort-order=desc" rel="nofollow noreferrer">ELB</a> in case of <a href="https://aws.amazon.com/it/eks/?whats-new-cards.sort-by=item.additionalFields.postDateTime&amp;whats-new-cards.sort-order=desc&amp;eks-blogs.sort-by=item.additionalFields.createdDate&amp;eks-blogs.sort-order=desc" rel="nofollow noreferrer">Amazon EKS</a>).
Do not worry about this last point if you are using a cloud managed kubernetes service since you will have this feature out of the box.</p>
