How can I connect MongoDB which is running on Kubernetes cluster through UI tools like MongoDB Compass or RoboMongo?
Asked Answered
S

4

8

I have multiple instances of MongoDB deployed inside my Kubernetes cluster through Helm packages. They are running as a service, in NodePort.

How do I connect to those MongoDB instances through UI tools like MongoDB Compass and RoboMongo from outside the cluster?

Shearwater answered 6/8, 2018 at 15:3 Comment(0)
G
16

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

  1. 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

  1. 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

Granddaddy answered 6/8, 2018 at 15:27 Comment(1)
Is this secure?Cravat
A
1

Fetch the service associated with MongoDB:

kubectl get services -n <namespace>

Port forward using:

kubectl port-forward service/<service_name> -n <namespace> 27018:27017

Open Robomongo on localhost:27018.

Acotyledon answered 21/10, 2021 at 6:43 Comment(1)
Error Exception opening socket Unknown hostIndiscrimination
M
0

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

Misdeem answered 23/5, 2019 at 7:31 Comment(1)
The qustion says "UI".Sideling
G
0

If not resolved, expose your mongo workload as a load balancer and use the IP address provided by the service. Copy the LB IP address and use the same in the robo3T. If it requires authentication, check my YAML file below:

apiVersion: apps/v1
kind: Deployment
metadata:
    name: mongodb
    labels:
        app: mongodb
spec:
    replicas: 1
    selector:
        matchLabels:
            app: mongodb
    template:
        metadata:
            labels:
                app: mongodb
        spec:
            containers:
                - name: mongodb
                  image: mongo
                  volumeMounts:
                      - name: data
                        mountPath: "/data/db"
                        subPath: "mongodb_data"
                  ports:
                      - containerPort: 27017
                        protocol: TCP
                  env:
                      - name: MONGO_INITDB_ROOT_USERNAME
                        value: xxxx
                      - name: MONGO_INITDB_ROOT_PASSWORD
                        value: xxxx
            imagePullSecrets:
                - name: xxxx
            volumes:
                - name: data
                  persistentVolumeClaim:
                      claimName: xxx

Set the same values in the authentication tab in ROBO3T

Note: I haven't mentioned the service section in the YAML since I directly exposed as an LB in the GCP UI itself.

Gameto answered 23/7, 2020 at 12:5 Comment(1)
What is "LB"?Paresthesia

© 2022 - 2024 — McMap. All rights reserved.