Accessing database with pgAdmin in Kubernetes cluster

1/17/2020

In my current non-Kubernetes environment, if I need to access the Postgres database, I just setup an SSH tunnel with:

ssh -L 5432:localhost:5432 user@domain.com

I'm trying to figure out how to do something similar in a test Kubernetes cluster I am setting up in EKS, that doesn't involve a great security risk. For example, creating a path in the ingress control to the databases port is a terrible idea.

The cluster would be setup where Postgres is in a pod, but all of the data is on persistent volume claim so that the data persists when the pod is destroyed.

How would one use pgAdmin to access the database in this kind of setup?

-- eox.dev
amazon-eks
amazon-web-services
kubernetes
postgresql

1 Answer

1/18/2020

The kubectl command can forward TCP ports into a POD via the kube-api

kubectl port-forward {postgres-pod-ID} 5432:5432

If you are not using a cluster-admin user, the user will need to be bound to a role that allows it to create pods/portforward in the pods namespace.

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: pg-portforward
rules:
  - apiGroups: [""]
    resources: ["pods/portforward"]
    verbs: ["create"]
-- Matt
Source: StackOverflow