Test mongo Atlas connection from a kubernetes pod with mongosh

Viewed 396

I'm trying to connect a GKE pod to mongodb Atlas.

I have a VPC Peering Network connection set up and Active between Atlas and a GCP VPC Network, but I can't configure mongosh either in the gcloud terminal or on a GKE pod to test my connection.

How do I do this? Completely stuck!

2 Answers

To install mongosh in your Cloud Shell you need first to find out which Operating System is running with the command lsb_release -dc; for this example the running OS is "Debian GNU/Linux 10 (buster)". Here you can find the documentation about how to install mongodb on the different Operating Systems [1].

These are the steps to install mongosh community edition on a Debian system:

  1. Import public key with the command wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
  2. Create a list file for the mongodb packages: echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
  3. Reload local package database: sudo apt-get update
  4. Install mongosh: sudo apt-get install -y mongodb-mongosh

Now, for the installation on a GKE pod you have a couple of options here; the first, is to open a bash command shell in your running pod with the command kubectl exec -it pod-name -- /bin/bash and from there install mongosh with the provided steps (these can vary depending on the operating system the pod image is based on). The second option is to run a mongodb docker image in your pod [2], which is already loaded with all the dependencies needed to run mongodb, including mongosh, in this case you would only need to open a bash command shell in your pod and test.

[1] https://docs.mongodb.com/manual/installation/

[2] https://hub.docker.com/_/mongo

You don't need to install/configure mongosh. You can use any already configured docker image like rtsp/mongosh

Steps to verify mongodb connectivity.

  1. Run temp mongosh container
kubectl run tmp-mongosh --image=rtsp/mongosh --rm -it -- bash
  1. use mongosh
mongosh "<connection string>"

You will get atlas prompt after successfully connecting with atlas enter image description here

Related