Score:0

Connect to a postgres running internally within a cluster through a kubernetes pod

cn flag

I've got a kubernetes pod myapp-2390458f-kfjgd I can get access to with kubectl and an instance of a PostgreSQL that can be accessed from within the kubernetes cluster by the name mypos.tgres.com, but it cannot be accessed outside of the cluster.

I'm trying to connect to the PostgreSQL from my laptop, but to my knowledge kubectl port-forward would not work in that case since I can only forward my local port to some pod's port. But it would not forward traffic further to mypos.tgres.com.

Is there a way to forward all traffic to the actual db instance mypos.tgres.com from the pod that can be accessed from my laptop?

What would be the possible solution?

The PostgreSQL is not running as a pod, but on an internal network that is only accessible from the Kubernetes cluster's pods. The Kubernetes cluster is hosted in our own infrastracture, not in a cloud.

Score:2
pt flag

Create a Pod that acts as a proxy to the internal Postgres service; then you can kubectl port-forward to that Pod to get access to Postgres from your laptop.

We can set up a simple proxy like this:

kubectl run postgres-proxy \
  --image docker.io/alpine/socat \
  -- \
  tcp-listen:5432,fork,reuseaddr \
  tcp-connect:mypos.tgres.com:5432

Now you can expose that proxy on your local machine:

kubectl port-forward pod/postgres-proxy 5432:5432

And then access postgres via your local machine:

psql -h localhost ...

Delete the proxy pod when you're done:

kubectl delete pod/postgres-proxy
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.