'Access to localhost Postgres from the minikube
I am new to Kubernetes and I am trying to setup a Java-app locally in minikube. And I want it to be able to connect to locally hosted Postgres DB, connection URL looks like jdbc:postgresql://localhost:5432/my_db, not inside k8s. That sounds like something simple but I just can't find a solution. Could you pls give me a piece of advice on where to look at? Thanks.
Btw, external connections to the DB server are allowed
My deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
name: my-app
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app-image
Solution 1:[1]
Seems, I found the solution. host.minikube.internal is exactly the alias we can use in this case. In my example just referring DB URI to jdbc:postgresql://host.minikube.internal:5432/my_db solved the problem.
More info here
Solution 2:[2]
Localhost in your app would be from the containers pov which is not the same as your local system. Option A is to host the postgres in K8s and access it through kubernetes DNS or Option B is to give the proper IP of the system where the database is running
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vit Ias |
| Solution 2 | Pierre |
