Home > Blockchain >  Access dockerized postgres database from minikube
Access dockerized postgres database from minikube

Time:12-30

enter image description here

I have this situation where I want to connect my postgres database that is running on a docker container, to the pgadmin web client running on a local kubernetes cluster (minikube)

I already have the postgres working with docker and the pgadmin working with kubernetes.

I can access the pgadmin through the web browser (pgadminclient.com)

I can access the postgres in the container from outside kubernetes, but I can't access postgres from kubernetes pgadmin, what kind of component could I use to achieve the connection and put the right values here enter image description here

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgadmin-deployment
  labels:
    app: pgadmin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pgadmin
  template:
    metadata:
      labels:
        app: pgadmin
    spec:
      containers:
      - name: pgadmin4
        image: dpage/pgadmin4
        ports:
        - containerPort: 80
        env:
        - name: PGADMIN_DEFAULT_EMAIL
          value: [email protected]
        - name: PGADMIN_DEFAULT_PASSWORD
          value: qwerty

---
apiVersion: v1
kind: Service
metadata:
    name: pgadmin-service
spec:
  selector:
    app: pgadmin
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: pgadmin-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: pgadminclient.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: pgadmin-service
            port:
              number: 80

Also my docker-compose file

version: "3.8"

services:
  postgresdb:
    image: postgres
    volumes:
      - db-data:/var/lib/postgresql/data
    restart: always
    ports:
      - "5432:5432"
    environment:
      - DATABASE_HOST=127.0.0.1
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=qwerty
      - POSTGRES_DB=practicedb
    
volumes:
  db-data:

CodePudding user response:

Your minikube cluster is using a VM which is different from the VM that docker uses on windows/mac to provide the container runtime. That makes access quite tricky.

But since your use case is to simulate a database outside of the cluster it serves quite well. You already exposed the docker port externally so you can use the external ip of your host (from wifi, lan, ...) as host for pgadmin. minikube will reach out to the external ip/port which is then in turn mapped back to the docker vm and container. (I did not test it, but it should work.)

  • Related