Connect to a MongoDB container with kubectl

·

1 min read

Goal: Connect your local computer to a Mongo database running in a kubernetes cluster.

Use case: Use a hosted database for local development. I have used this workflow to connect to databases hosted in Rancher, a container orchestrating service.

You need to know:

  • kubernetes namespace
  • pod name

The command looks like this: kubectl -n namespace port-forward podName 31137:27017

Where namespace is the kubernetes namespace and podName is the name of the pod. Example: kubectl -n namepsace-to-connect-to port-forward name-of-pod-12345-4321 31137:27017

Running this command in your terminal will open up a port connection on 31137, which you can then use to connect to the Mongo database with a GUI such as MongoDB Compass. Be sure to keep the terminal open to run in the background!