Home > Mobile >  Access to localhost Postgres from the minikube
Access to localhost Postgres from the minikube

Time:04-01

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

CodePudding user response:

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

CodePudding user response:

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

  • Related