How to expose Postgres at AWS EKS
Given
You have a Kubernetes cluster deployed on AWS EKS using karpenter with PostgreSQL inside.
Goal
The task at hand is to publicly expose the PostgreSQL database. Since the database communicates not via the HTTP protocol, k8s ingress won’t be helpful here. Additionally, setting `spec.type: NodePort` won’t work either.
Solution
Convert the service responsible for PostgreSQL to `spec.type: LoadBalancer`. As a result, you will obtain a public port like `31438`. However, you’ll need to connect not to this port but to the classic PostgreSQL port, 5432. This is because EKS, upon your configuration request for `spec.type: LoadBalancer`, will perform another port mapping. So, in AWS, it will look like this: world <-> 5432 <-> 31438 <-> 31438 <-> 5432. You can verify this by looking at the newly created load balancer under `EC2 > Load Balancers > Listeners`.
Hope this will help you!