15

I created a Mongodb service according to the Kubernetes tutorial.

Now my question is how do I gain access to the database itself, with a client like Robomongo or similar clients? Just for making backups or exploring what data have been entered.

The mongo-pod and service only have an internal endpoint, and a single mount.

Is there any way to safely access this instance with no public endpoint?

Internally URI is mongo:27***

Rot-man
  • 18,045
  • 12
  • 118
  • 124
g4challenge
  • 421
  • 1
  • 4
  • 11
  • I'd eagerly encourage to use the mongo shell (https://docs.mongodb.org/getting-started/shell/client/) as mongo client and `mongodump` and `mongorestore` for backups. Use GUIs like Robomongo as a complement, not as a main client. – Héctor Valverde Apr 13 '16 at 10:32
  • You can access to shell of running container through docker exec command if You know container name (something like k8s_mongo): `docker ps --format "{{.ID}}\t{{.Names}}" | grep "${name}" | cut -f1 | xargs -I NAME docker exec -i NAME echo 1` – ainlolcat Apr 13 '16 at 17:05

4 Answers4

24

You can use kubectl port-forward mypod 27017:27017 and then just connect your mongodb client to localhost:27017.

If you want to stop, just hit Ctrl+C on the same cmd window to stop the process.

lmcarreiro
  • 5,312
  • 7
  • 36
  • 63
  • 1
    Perfect for accessing from an external client like Robomongo running in your computer – Jesferman Nov 09 '18 at 10:34
  • 11
    you can also connect to a pod using the service name using it as `kubectl port-forward svc/mongodb 27018:27017` instead of podname so you don't have to look up the ever changing pod name – magnusson Jan 29 '19 at 16:06
  • Hello, how can I use this service on this port outside my cluster? – Saki Osive Jul 16 '21 at 17:20
15

The kubernetes cmd-line tool provides this functionality as @ainlolcat stated

kubectl get pods

Retrieves the pod names currently running and with:

kubectl exec -i mongo-controller-* bash

you get a basic bash, which lets you execute

mongo

to get into the database to create dumps, and so on. The bash is very basic and has no features like completion and so on. I have not found a solution for better shell but it does the job

g4challenge
  • 421
  • 1
  • 4
  • 11
8

when you create a service in kubernetes you give it a name, say for example "mymongo". After the service is created then

The DNS service of kubernetes (by default is on) will ensure that any pod can discover this servixe simply by its name. so you can set your uri like

uri: mongodb://**mymongo**:27017/mong

In addition the service IP and port will be set as environment variables at the running pod.

MYMONGO_SERVICE_HOST

MYMONGO_SERVICE_PORT

I have in fact wrote a blog that show a step by step example of an app with nodejs web server and mongo that can explain further

http://codefresh.io/blog/kubernetes-snowboarding-everything-intro-kubernetes/

feedback welcome!

Raziel Tabib
  • 176
  • 2
  • 2
2

Answer from @grchallenge is correct but it is deprecated as of in 2021

All new comers please use

kubectl exec  mongo-pod-name -i -- bash