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 27017:27017
.
Now point your UI tool to localhost:27017
and kubectl
will forward all connections to the pod/service 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 27017:27017 --namespace mongodb-namespace
Example:
If your connectionstring looks like this:
mongodb://username:[email protected]:27017,mongodb-1.mongodb-svc.mongodb.svc.cluster.local:27017,mongodb-2.mongodb-svc.mongodb.svc.cluster.local:27017/admin?replicaSet=mongodb&ssl=false
- Add the hostnames to you hosts file:
127.0.0.1 mongodb-0.mongodb-svc.mongodb.svc.cluster.local
127.0.0.1 mongodb-1.mongodb-svc.mongodb.svc.cluster.local
127.0.0.1 mongodb-2.mongodb-svc.mongodb.svc.cluster.local
- Use MongoDB Compass or Robo 3T and use the following connectionstring:
mongodb://username:[email protected]:27017,mongodb-1.mongodb-svc.mongodb.svc.cluster.local:27017,mongodb-2.mongodb-svc.mongodb.svc.cluster.local:27017/admin?replicaSet=mongodb&ssl=false