I deployed a MongoDB replica set in my Kubernetes cluster. The MongoDB replica set can be easily connected with the help of internal ClusterIP within the cluster. I even connect it to my mongo-express client.
//Successfull and working fine internally
mongodb://db-mongodb-0.mycompany-mongodb-headless:27017/db
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.
My first attempt to establish a remote connection was:
I tried to connect with my previously opened NodePort. I'm currently using Mongoose Client. The available node ports of my two replica sets are - 30001, 30002.
//Unsuccessful
mongoose
.connect(
`mongodb://username:password@<EXTERNAL_IP>:30001,<EXTERNAL_IP>:30002/mydb`,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
Then my second attempt was -
I tried port forwarding and tried to connect with my localhost like this -
kubectl port-forward svc/database-mongodb-0-external 27017:27017
Then I tried to establish a connection like this and failed too
//Unsuccessful
//MongoError: Authentication failed.
mongodb://username:password@localhost:27017/database
I tried ?replicaSet=rs0 query parameters in both of the cases too. But no luck.
I didn't try opening a LoadBalancer. Is there any way to establish a remote connection with my Ingress-Nginx controller to my MongoDB services? 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 - It looks like you are trying to access MongoDB over HTTP on the native driver port. I am not sure if it's possible!
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?
My latest Bitnami/Mongodb values are
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