# Connect from Kubernetes

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

We assume the following prerequisites:

* A datacenter with id `xyz-my-datacenter`.
* A private LAN with id 3 using the network `10.1.1.0/24`.
* A database connected to LAN 3 with IP `10.1.1.5/24`.
* A Kubernetes cluster with id `xyz-my-cluster`.
* [`ionosctl`](https://github.com/ionos-cloud/ionosctl)

In this guide we use DHCP to assign IPs to node pools. Therefore, it is important that the database is in the same subnet that is used by the DHCP server.

To enable connectivity, you must connect the node pools to the private LAN in which the database is exposed:

```
ionosctl k8s nodepool create --cluster-id xyz-my-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 let's create a pod that contains the Postgres tool `pg_isready`. If you have multiple node pools make sure to schedule the pod only the node pools that are attached to the additional LAN.

```
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: connectivity-test
  labels:
    role: connectivity-test
spec:
  containers:
  - name: postgres
    image: postgres
    stdin: true
    tty: true
    command:
      - "/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.
root@connectivity-test:/# pg_isready -h 10.1.1.5
10.1.1.5:5432 - accepting connections
```

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](/cloud/~/revisions/AMiYziCllTKB2y2lmONY/databases/postgresql/troubleshooting.md#network-issues).


---

# 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/~/revisions/AMiYziCllTKB2y2lmONY/databases/postgresql/api-howtos/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.
