Connect to host postgres db from minishift

3/1/2017

Im trying to connect to a postgres database, from a springboot application deployed in minishift.

The postgres server is running on the same host that minishift is running on.

I've tried setting the postgres serve to listen on a specific IP address, and use this same address in the springboot jdbc connection url but I still get org.postgresql.util.PSQLException: Connection to 172.99.0.1:5432 refused

I've also tried using 10.0.2.2

Also tried, in /etc/postgresql/9.5/main/postgresql.conf, setting:

listen_addresses = '*'  

How can I connect to a database external to minishift, running on same host?

-- Magick
fabric8
kubernetes
minikube
openshift

2 Answers

3/2/2017

If a pod inside kubernetes can't see the IP address from the host then I guess its an underlying firewall or networking issue. Try opening a shell inside the pod...

kubectl exec -it mypodname bash

Then trying to ping, telnet, curl, wget or whatever to see if you can see the IP address.

It sounds like something's wrong with the networking setup of your minishift. It might be worth raising an issue with minishift: https://github.com/minishift/minishift/issues/new

If you can find an IP address on the host which is accessible from a docker pod you can create a Kubernetes Service and then an Endpoint for the service with the IP address of the database on your host; then you can use the usual DNS discovery of kubernetes services (i.e. using the service name as the DNS name) which will then resolve to the IP address. Over time you could have multiple IP addresses for failover etc.

See: https://kubernetes.io/docs/user-guide/services/#without-selectors

Then you can use Services to talk to all your actual network endpoints with your application code completely decoupled on if the endpoints are implemented inside kubernetes, outside with load balancing baked in!

-- James Strachan
Source: StackOverflow

3/1/2017

Besides the answer referenced in my comment, which suggests to make your database listen on the IP address of the Docker bridge, you could make your pod use the network stack of your host. This way you could reach Postgres on the loopback. This works only if can guarantee that the pod will always run on the same host as the database.

The Kubernetes documentation discourages using hostNetwork. If you understand the consequences you can enable it as in this example.

-- Antoine Cotten
Source: StackOverflow