6

Cluster created in Rancher with Amazon EKS.

MongoDB replicaset was created as a catalog app in Rancher.

Services in the cluster can successfully connect the the database with this connection string.

mongodb://mongodb-replicaset.mongodb-replicaset.svc.cluster.local:27017/tradeit_system?replicaSet=rs

I want to view and edit data in the db. In a local db you can do it easily by the command mongo --port 27017.

Similarly is there a way to connect to the one one on kubernetes. Either from the terminal or using an application like Robo 3t?

EDIT

The replicaset doesn't show when I do.

kubectl get deployments --all-namespace

kubectl get pods --all-namespaces

Show that it runs in 3 pods mongodb-replicaset-0, mongodb-replicaset-1, mongodb-replicaset-2.

enzio902
  • 457
  • 6
  • 14

3 Answers3

6
  1. run kubectl get services -n <namespace>. this will list the replicaset service
  2. execute kubectl port-forward svc/mongodb-replicaset -n mongoNamespace 27018:27017

where

mongodb-replicaset = mongodb service name

mongoNamespace = namespace

and 27018 = your local port

As best practice, you should always connect on services not on pods. Since pods are automatically recreated/restarted, it will give you a new pod name. Connecting to a service saves you from reconnecting and finding the primary pod of your mongodb replicaset.

  • @may you right, connecting to the service is the best rather than the pods. – Kamal Oct 02 '19 at 19:41
  • Should I use a service to access my mongodb cluster on my microservice too? – Vanderson Assis Apr 07 '20 at 23:46
  • 1
    @VandersonAssis yes especially if you connect your db server on your microservices. rather than connecting them to a specific pod. there are 3types of services on k8s, ClusterIP, NodePort and LoadBalancer. see https://matthewpalmer.net/kubernetes-app-developer/articles/service-kubernetes-example-tutorial.html – May Anne Luyun Apr 08 '20 at 05:46
  • With this answer I get this error `getaddrinfo ENOTFOUND example-mongodb-0.example-mongodb-svc.mongodb.svc.cluster.local`. EDIT: this error only seems to happen with the MongoDB Compass GUI. Using e.g. the C# nuget client it works. – David S. Jun 02 '21 at 15:46
  • @DavidS. Try setting `useUnifiedTopology` as `false` and only port-forward the replicaset primary pod. Then in the connection string only pass the primary pod's hostname and no replicaset name. – Kartik Shah Jul 25 '21 at 07:15
2

kubectl port-forward mongodb-replicaset-0 --namespace mongodb-replicaset 27017:27017

mongodb-replicaset-0 - pod that runs primary set.

This forwards the traffic to localhost:27017 on your machine.

Github discussion

Documentation on port-forward

enzio902
  • 457
  • 6
  • 14
  • Here you are forwarding to the pod called `mongodb-replicaset-0`. A pod doesn't "run" a replica set, and there is no such thing as a "primary set". The pod is the thing that is replicated. It's also good to know that the replica/pod that is primary may change. – Phil Feb 04 '22 at 08:38
0

After you execute kubectl get pods, you will see the name of your pods.

You can connect to any pod using this command: kubectl exec -it POD_NAME /bin/bash. Also check docs for Execute a command in a container.

Also, after connecting to a POD, you can check where is mongo located using whereis mongo.

I think default installation is located at /usr/bin/mongo, so while being connected to your cluster you can call mongo directly from any pod you like using kubectl exec -it POD_NAME /usr/bin/mongo.

Crou
  • 10,232
  • 2
  • 26
  • 31