Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to Mongodb in Kubernetes

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***

like image 217
g4challenge Avatar asked Apr 13 '16 08:04

g4challenge


People also ask

How do I connect to mongod in Kubernetes?

Kubernetes exposes mongod on port 27017 within the Kubernetes container. The NodePort service exposes the mongod via port 30994 . NodePorts range from 30000 to 32767, inclusive. To connect to your deployment from outside of the Kubernetes cluster, run the mongod command with the external FQDN of a node as the --host flag.

How do I find the external FQDN of my Kubernetes cluster?

If a node in the Kubernetes cluster has an external FQDN of ec2-54-212-23-143.us-west-2.compute.amazonaws.com, you can connect to this standalone instance from outside of the Kubernetes cluster using the following command: To obtain the external DNS of your Kubernetes cluster, you can run the following command:

What is the best way to expose a port in Kubernetes?

Type LoadBalancer is usually used on cloud providers since they provide external load balancers for Kubernetes. So, in your case, NodePort is the easiest way to expose the Port. Here is an example of Service YAML: In line port: 27017, we specified your MongoDB port, it is also usually specified in Deployment for MongoDB.

What are Kubernetes operators?

Use Kubernetes to control fully managed MongoDB Atlas deployments on AWS, Azure, and GCP Kubernetes Operators are application-specific controllers that extend the Kubernetes API to create, configure, and manage instances of stateful applications such as databases.


2 Answers

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

like image 38
g4challenge Avatar answered Oct 16 '22 03:10

g4challenge


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.

like image 94
lmcarreiro Avatar answered Oct 16 '22 03:10

lmcarreiro