Docker Swarm JDBC connection
Running a Postgresql DB on a Docker Swarm containing multiple nodes where the Database can be deployed. Using Docker version 1.12+.
Using a Data container, the Postgresql failover is working nicely. Now I would like to have a Java client connect to the DB and also survive failover. How should the JDBC connections be managed here? Does the connection string change? Or should it be managed through something like an nginx container running elsewhere? Is there an example of this available for study anywhere? Conceptually, I think I get moving this off to another (nginx-like) container, but can’t quite grok the details!
One Solution collect form web for “Docker Swarm JDBC connection”
In swarm mode, you get service discovery by DNS name for services in the same overlay network, you don’t need to add a proxy layer yourself. The swam networking docs go into detail, but in essence:
docker network create -d overlay app-net docker service create --name app-db --network app-net [etc.] docker service create --name app-web --network app-net [etc.]
Your database server is available by DNS within the network as
app-db, to any service in the same
app-net network. So
app-db is the server name you use in your JDBC connection string. You can have multiple replicas of the Postgres container, or a single container which moves around at failover – the service will always be available at that address.
But: I would be cautious about failover with your data container. You have a single container with your database state there; even if your state is in a volume, it won’t move around the cluster. So if the node with the data fails, your data container will start somwhere else, but the data won’t go with it.