1

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:
2
  • Can you run the database in Kubernetes too? Or pgadmin in Compose? What motivates this setup using multiple container runtimes at once? Commented Dec 29, 2021 at 22:41
  • Hi David! thank you for the comment. I could run the database in kubernetes, but since I am learning, I would like to experiment and understand how a connection like this could be achieved. Because the next step I want to do is to connect a postgres using AWS RDS, so the database is going to be outside the cluster as well. Commented Dec 29, 2021 at 22:48

1 Answer 1

1

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.)

Sign up to request clarification or add additional context in comments.

6 Comments

Hello Thomas, thank you for your answer! I will try this approach!
To add to this answer, in general it should work like this, however minikube driver should not be docker (it gets very tricky in this case because kubernetes components will be docker in docker) and depends on OS as well. E.g. for such case I'd use minikube driver set to virtualbox. More details here
Oh, that's my case, when I do a docker ps I can see my postgres container and the k8s-minikube as well, so a better approach to set up my environment will be to use VirtualBox to use minikube right?
The minikube-docker setup should not change anything since the containers are exposed on the host the same way.
Thomas and moonkotte thank you! Finally I could achieve the connection. I got the IP of the container since it is exposed, and just with that IP, the db name, user and pass I was able to connect, I deleted my minikube with docker and started a new one using VirtualBox, but I will try again just to practice with the docker minikube. Thank you again for the help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.