Connect from Kubernetes

This guide shows you how to connect to a MongoDB cluster from your managed Kubernetes cluster.
We assume the following prerequisites:
  • A data center with id xyz-my-datacenter.
  • A private LAN with id 3.
  • A MongoDB cluster connected to LAN 3, with the connection string mongodb+srv://
  • A Kubernetes cluster with id xyz-my-k8s-cluster.
  • ionosctl set up with your IONOS credentials.
In this guide, we use DHCP to assign IP addresses to node pools. Therefore, it is important that the database is in the same subnet that's used by the DHCP server.
To enable connectivity, you must connect the node pools to the private LAN with the MongoDB cluster:
ionosctl k8s nodepool create --cluster-id xyz-my-k8s-cluster --datacenter-id xyz-my-datacenter --lan-ids 3 --dhcp=true --name=my_nodepool
Wait for the node pool to become available. To test the connectivity you can create a pod that contains the MongoDB tool mongosh. If you have multiple node pools, make sure to schedule the pod on one of the node of the node pools that are attached to the private LAN.
# pod.yaml
apiVersion: v1
kind: Pod
name: connectivity-test
role: connectivity-test
- name: mongo
image: mongo
stdin: true
tty: true
- "/bin/bash"
Let's create the pod...
kubectl apply -f pod.yaml
... and attach to it.
kubectl attach -it connectivity-test
If you don't see a command prompt, try pressing enter.
[email protected]:/# mongosh "mongodb+srv://"
Current Mongosh Log ID: 631063ca901a9459bab0b4d4
Connecting to: mongodb+srv://
Using MongoDB: 5.0.10
Using Mongosh: 1.5.4
For mongosh info see:
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (
You can opt-out by running the disableTelemetry() command.
Enterprise a0def940-2455-11ed-a564-7a0f508690ac [primary] test>
If everything works, we should see that the database is accepting connections. If you see connection issues, make sure that the node is properly connected to the LAN. To debug the node start a debugging container ...
kubectl debug node/$(kubectl get po connectivity-test -o jsonpath="{.spec.nodeName}") -it --image=busybox
... and follow the network troubleshooting guide.