0

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.

larsks
  • 43,623
  • 14
  • 121
  • 180
Some Name
  • 143
  • 4

1 Answers1

2

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
larsks
  • 43,623
  • 14
  • 121
  • 180