Check connectivity to MongoDB Atlas from Kubernetes

To check the connectivity to an external MongoDB server from a Kubernetes pod, you can use a simple test pod that includes MongoDB client tools. Here’s how you can set it up:

Create a Test Pod:

Write a Kubernetes YAML file for a pod that includes the MongoDB client. This pod will be used to test the connection to your external MongoDB server. Here’s an example YAML:

apiVersion: v1
kind: Pod
metadata:
  name: mongodb-test
spec:
  containers:
  - name: mongo
    image: mongo
    command: ["sleep"]
    args: ["3600"]

This will create a pod with the MongoDB image and keep it alive for an hour (3600 seconds).

Apply the YAML:

Use kubectl to create the pod:

kubectl apply -f <your-yaml-file.yaml>

Exec into the pod:

kubectl exec -it mongodb-test -- /bin/bash

Test the connection

Inside the pod, use the MongoDB client to test the connection. For example:

mongo --host <external-mongodb-host> --port <port> -u <user> -p

Replace <external-mongodb-host>, <port>, <user>, and <password> with your MongoDB server’s details.

This method allows you to test the connectivity from within your Kubernetes cluster to an external MongoDB server. Remember to delete the test pod when you’re done to clean up resources:

kubectl delete pod mongodb-test