GithubHelp home page GithubHelp logo

postgres-forward-pod's Introduction

Postgres Forward Pod

Set up a forwarder-pod

This will create a new pod with the sole purpose to connect to the database via port forwarding from local to this pod:

export NAMESPACE=...
# either your name or the context/purpose of your action, e.g. migration.
export SUFFIX=...
# find this value either in the database configuration in Argo CD or via inspecting the output of 'kubectl -n $NAMESPACE describe configmap'
export DATABASE_HOST=...
# default for postgres is 5432, but could be something else
export DATABASE_PORT=...

# cd into this repository

# create the pod
envsubst < manifest.yaml | kubectl apply -n $NAMESPACE -f -

# port mapping example: 5000:5432
kubectl port-forward postgres-forward-pod-$SUFFIX <local-port>:<remote-port> -n $NAMESPACE

Connect to the database

Now you need to retrieve the username and password to access the database via the forwarded port:

# see available secrets
kubectl -n $NAMESPACE get secret
# use the desired secret name to retrieve it
kubectl -n $NAMESPACE get secret <secret-name> -o jsonpath='{.data}'

This gives you something like: {"db.password":"","db.user":""}, which now needs to be decoded:

echo "<db.user>" | base64 --decode
echo "<db.password>" | base64 --decode
# note that the last "%" is not part of the decoded string

Now you have the credentials and can connect to the database by using the UI in IntelliJ or via command line:

# 'brew install postgresql' if you don't have psql yet
# example for local-port from above: 5000
PGPASSWORD=<password> psql -U <username> -h localhost -d <databasename> -p <local-port>

Shut down the forwarder-pod

Once you are done, it's important you remember to shut down the pod you created:

envsubst < manifest.yaml | kubectl delete -n $NAMESPACE -f -

Why using a forwarder-pod?

The command kubectl port-forward can only connect to pods. The other option to connect by a service is used as a pod selector and does not connect to a service at all. Therefore port-forwarding does not work with services of type ExternalName or by using a combination of Serviceand EndpointSlice. For details see the following sources:

Everything put together in the script db-forward.sh

All the steps above are combined into the script db-forward.sh.

For each database to forward a separate config file with the following variables must be created:

KUBE_CONTEXT=dev
NAMESPACE=ris-staging
SUFFIX=janedoe
DATABASE_HOST=10.1.2.3
DATABASE_PORT=5432
DATABASE_LOCAL_PORT=50001

To start port forwarding run ./db-forward.sh example-database.cfg up.

To end port forwarding run ./db-forward.sh example-database.cfg down.

If you run ./db-forward.sh without any parameter it does not only show usage information but also lists all processes identified as port forwards.

postgres-forward-pod's People

Contributors

carhartl avatar danielburgmann avatar hprinz avatar victordelcampo avatar benjaminaaron avatar

Stargazers

 avatar

Watchers

Christian Schuhmann avatar Christian Kaatz avatar Eli Flores avatar  avatar  avatar Magdalena Noffke avatar  avatar Carl Gödecken avatar Tarek Hamaoui avatar Simon Kempendorf avatar Pram Gurusinga avatar Alex August Schlote avatar Lucas Solcher avatar Julian avatar Kai Bernhard avatar  avatar

postgres-forward-pod's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.