I've set up an application stack running on Google Kubernetes Engine + Google Cloud SQL. When developing locally, I would like my application to connect to a postgres database server running outside the cluster to simulate the production environment.
It seems that the way to do this is by defining an external endpoint as described here: Minikube expose MySQL running on localhost as service
Unfortunately, I am not able to specify "127.0.0.1" as the Endpoint IP address:
kubectl apply -f kubernetes/local/postgres-service.yaml
service "postgres-db" unchanged
The Endpoints "postgres-db" is invalid: subsets[0].addresses[0].ip:
Invalid value: "127.0.0.1": may not be in the loopback range (127.0.0.0/8)
So I am forced to bind postgres to my actual machine address.
It seems like there MUST be a way to map a port from my localhost into the local kubernetes cluster, but so far I can't find a way to do it.
Anybody know the trick? Or alternatively, can someone suggest an alternative solution which doesn't involve running postgres inside the cluster?
Connecting to Your DatabaseThe PostgreSQL database service is available on localhost and the default PostgreSQL port is 5432 . A default user ( hosting-db ) and database ( postgres ) exist so you can quickly test your connection and perform management tasks.
May not be an answer for Minikube, but I ended up here so I share what I did for Kubernetes in Docker for Mac.
I added a service like this for PostgreSQL:
kind: Service
apiVersion: v1
metadata:
name: postgres
namespace: default
spec:
type: ExternalName
# https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds
externalName: host.docker.internal
ports:
- name: port
port: 5432
My application was able to connect to the locally running postgres server with this setup using the domain name postgres
. The Postgres server can listen to 127.0.0.1
with this setup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With