How to connect MongoDB which is running on Kubernetes cluster through UI tools like MongoDB Compass or RoboMongo?

8/6/2018

I have multiple instances of Mongo db deployed inside my kubernetes cluster through helm packages. They are running as a service, in NodePort. How do I connect to those Mongo db instances through UI tools like MongoDB Compass and RoboMongo from outside the cluster? Any help is appreciated.

-- Shruthi Bhaskar
kubernetes
mongodb
mongodb-compass
robo3t

2 Answers

5/23/2019

If it is not your production database you can expose it through a NodePort service:

# find mongo pod name
kubectl get pods
kubectl expose pod <<pod name>> --type=NodePort
# find new mongo service
kubectl get services

Last command will output something like

mongodb-0   10.0.0.45    <nodes>       27017:32151/TCP   30s

Now you can access your mongo instance with mongo <<node-ip>>:32151

-- Andrey Serebryanskiy
Source: StackOverflow

8/6/2018

You can use kubectl port-forward to connect to MongoDB from outside the cluster.

Run kubectl port-forward << name of a mongodb pod >> --namespace << mongodb namespace >> 27018:27018.
Now point your UI tool to localhost:27018 and kubectl will forward all connections to the pod inside the cluster.

Starting with Kubernetes 1.10+ you can also use this syntax to connect to a service (you don't have to find a pod name first):
kubectl port-forward svc/<< mongodb service name >> 27018:27018 --namespace << mongodb namespace>>

-- Oliver
Source: StackOverflow