Addition for Kubernetes remote database access
If you like to connect to a remote database that runs in a cluster you have to configure the host ip address differently (not localhost).
Kubernetes Database setup - Example Keycloak k8
Simple example setup of keycloak running in a kubernetes cluster.
- Database resource of type StatefulSet, since the db shall be persisted.
- Service resource, used by the StatefulSet (link).
- Keycloak instance (app), runs the actual keycloak application (Quarkus App)
Diagram of components inside cluster
+--------------------------+
| Kubernetes |
| Cluster Nodes |
| |
| +--------------------+ |
| | StatefulSet | |
| | (Postgres Database)| |
| | | |
| | +---------------+ | |
| | | Pod (DB) | | |
| | +---------------+ | |
| +--------------------+ |
| |
| +--------------------+ |
| | Deployment | |
| | (Keycloak App) | |
| | | |
| | +---------------+ | |
| | | Pod (App) | | |
| | +---------------+ | |
| +--------------------+ |
| |
| +--------------------+ |
| | Service | |
| | (keycloak-database)| |
| | | |
| | +---------------+ | |
| | | ClusterIP: | | |
| | | 10.152.183.65 | | |
| | +---------------+ | |
| +--------------------+ |
| |
+--------------------------+
Traffic / comunication flow
Brief summary of the communication between keycloak and database with the service.
- Kecloak (app): runs inside a pod in the cluster
- Service (discovery): the service used to provide a stable IP address and DNS name for accesing pods, in this example keycloak uses it to gain access to the database
- Database (stateful set): the actual instance of the database also running in a pod.
Communicaiton diagram - keycloak inside a cluster
+--------------------------+
| Kubernetes Cluster |
| (Nodes) |
| |
| +----------------------+ |
| | Keycloak Deployment | |
| | +------------------+ | |
| | | Keycloak Pod | | |
| | | (App) | | |
| | +------------------+ | |
| +----------------------+ |
| | |
| | Service Discovery
| v |
| +----------------------+ |
| | keycloak-database | |
| | Service | |
| | ClusterIP: | |
| | 10.152.183.65:5432 | |
| +----------------------+ |
| | |
| v |
| +----------------------+ |
| | StatefulSet (DB) | |
| | +------------------+ | |
| | | PostgreSQL Pod | | |
| | | (DB) | | |
| | +------------------+ | |
| +----------------------+ |
| |
+--------------------------+
Example communication summary:
1. Keycloak:
- pod runs applicaiton inside pod.
- Keycloak wants to connect to the postgres db to do stuff.
2. Service discovery:
- Kubernetes services provide stable IP for accessing pods.
- The keycloak-database service acts as an intermediary that directs traffic to the pgsql database pod.
3. Service to pod communication:
- When the keycloak pod needs to interact with the database it sends the request to the keycloak-database service.
- The keycloak-database service has a ClusterIP and is configured to forward requests on port 5432 to the pgsql database pod.
4. Service selector:
- The keycloak-database service uses a selector to match labels on the database pod. Ensures the service knows where to forward the traffic (to the correct pod only).
5. Database pod receives request:
- meaning the service forwards the request from keycloak to the statefulSet database pod and returns the reponse the same way back to the caller.
In this setup the Service: keycloak-database is your entrypoint for communicating to your database. In the example of @payne the MySQL Database runs on his server on localhost:.
Knowing your stuff
This is just an example setup as the important part here is merely the fact that knowing what and where to type stuff is explained now easier on an example.
Where you host your database determines the value of the host in DataGrip
So only if your postgres database runs on your server directly you also would use localhost: in your configuration. But this is NOT the case when you do Cloud / Cluster db hosting.
How does the tunneling then work in a client to cluster scenario?
Remark:
It doesn´t matter what tool you use, since the overall workflow is the same.
1. Establish a ssh tunnel:
to your server, where your database is running on (in this case the cluster the db is deployed to)
- DataGrip gives you this option as @payne showcased
2. Configure database connection in DataGrip
Host: needs to be Cluster Ip of your service that is used to communicate to your cluster database
Port: is the actual port of the service, not the port of the database pod. The service defines whcih interal port is mapped to the service port.
Username + password + database name: your actual credentials
Client to cluster database communication
+--------------------------+
| Local Machine (Client) |
| |
| +--------------------+ |
| | DataGrip | |
| | | |
| | Host: localhost | |
| | Port: 5432 | |
| | Username: | |
| | Password: | |
| | Database: keycloak | |
| +--------------------+ |
| | |
| | SSH Tunnel |
| | |
| +--------------------+ |
| | ssh command: | |
| | ssh -L 5432: | |
| | 10.152.183.65:5432 | |
| | your-username@ | |
| | your-ssh-server.com| |
| +--------------------+ |
| | |
+---------|----------------+
|
+---------|----------------+
| Remote Server |
| (Kubernetes Cluster) |
| |
| +--------------------+ |
| | Kubernetes Node | |
| | | |
| | +--------------+ | |
| | | StatefulSet | | |
| | | (Postgres DB)| | |
| | | | | |
| | +--------------+ | |
| | ClusterIP: | | |
| | 10.152.183.65 | | |
| +--------------------+ |
+--------------------------+
Putting it all together
TL;DR How to connect to a cluster (remote) database with DataGrip
General settings
Tunnel settings
Simply leave the Local port blank, so DataGrip can assign a free port itself for the tunnel. Or if you want a specific port choose one.
Simply enter your clusters host ip or domain name here, choose authentication type etc. Is the actual IP of the server that runs the cluster, or lets you enter the cluster from outside
Summary
Since this little detail was missing on how to connect to a remote cluster based database, here you go. This might be very obvious for seasoned folks, but can be usefull for beginners in cloud tech, since it´s way more comfortable to have access to your remote databases with a more suffisticated tool than the terminal.
alter user postgres password 'postgres'
Things magically worked after that – Polygynist