# Connect from Kubernetes

This guide shows you how to connect to a MongoDB cluster from your managed Kubernetes cluster.

## Prerequisites

Before connecting to a MongoDB cluster from your managed Kubernetes, make sure you have:

* 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://m-xyz-example.mongodb.de-txl.ionos.com`.
* A Kubernetes cluster with id `xyz-my-k8s-cluster`.
* [<mark style="color:blue;">\`ionosctl</mark>](https://github.com/ionos-cloud/ionosctl) set up with your <code class="expression">space.vars.ionos\_cloud</code> credentials.

{% hint style="info" %} **Note:** 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 is used by the DHCP server. {% endhint %}

## Procedure

1.To enable connectivity, connect the node pools to the private LAN with the MongoDB cluster.

```bash
ionosctl k8s nodepool create --cluster-id xyz-my-k8s-cluster --datacenter-id xyz-my-datacenter --lan-ids 3 --dhcp=true --name=my_nodepool
```

2\. 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.

```yaml
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: connectivity-test
  labels:
    role: connectivity-test
spec:
  containers:
  - name: mongo
    image: mongo
    stdin: true
    tty: true
    command:
      - "/bin/bash"
```

3\. Create the pod by using the following command:

```bash
kubectl apply -f pod.yaml
```

4\. Attach the pod to the cluster by using the following command:

```bash
kubectl attach -it connectivity-test
If you do not see a command prompt, try pressing enter.
root@connectivity-test:/# mongosh "mongodb+srv://m-xyz-example.mongodb.de-txl.ionos.com"
Current Mongosh Log ID: `631063ca901a9459bab0b4d4`
Connecting to: `mongodb+srv://m-xyz-example.mongodb.de-txl.ionos.com/?appName=mongosh+1.5.4
Using MongoDB: 5.0.10
Using Mongosh: 1.5.4

For mongosh information, see: https://docs.mongodb.com/mongodb-shell/

To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

Enterprise a0def940-2455-11ed-a564-7a0f508690ac [primary] test> 
```

{% hint style="success" %}
**Result:** You see the database is accepting connections.
{% endhint %}

If you see connection issues, make sure that the node is properly connected to the LAN. To debug the node, use a debugging container.

```bash
kubectl debug node/$(kubectl get po connectivity-test -o jsonpath="{.spec.nodeName}") -it --image=busybox
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ionos.com/cloud/databases/mongodb/api/v1-api/connect-from-kubernetes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
