Kubernetes Operator - Expose DB to outside users

If I wanted to connect to the DB from outside of kubernetes aka another machine, what would the best way to handle it be? Do outside users connect to the operator in order to make queries? I just followed Install Percona Distribution for PostgreSQL on Kubernetes and for step 6, i just connected to the database with the command below. Should I expose the kubernetes operator port for other users to connect to the database or should I use a load balancer like HAProxy? Thanks!

$ kubectl run -i --rm --tty pg-client --image=perconalab/percona-distribution-postgresql:13.2 --restart=Never -- bash -il
[postgres@pg-client /]$ PGPASSWORD='pguser_password' psql -h cluster1-pgbouncer -p 5432 -U pguser pgdb

Also is there documentation on the 4 containers in the operator pod? apiserver, operator, scheduler, event?

1 Like

Hello @jason123 ,

Operator is just tracking the Custom Resources and creates necessary k8s primitives to create the database.
To connect to database you should expose pgbouncer.

  pgBouncer:
    image: perconalab/percona-postgresql-operator:main-ppg14-pgbouncer
#    imagePullPolicy: Always
    size: 3
    resources:
      requests:
        cpu: "1"
        memory: "128Mi"
      limits:
        cpu: "2"
        memory: "512Mi"
    expose:
      serviceType: LoadBalancer

This will create a LoadBalancer service for pgbouncer. You can also expose it as a NodePort.

For me on GKE it looks like this:

$ kubectl get services
cluster1-pgbouncer              LoadBalancer   10.52.9.49     35.238.XX.YY  5432:32508/TCP               26m

So now I connect from my machine to my PG cluster:

PGPASSWORD='pguser_password' psql -h 35.238.XX.YY  -p 5432 -U pguser pgdb

Please let me know if it helps.

2 Likes