22

I'm learning Docker Swarm mode and I managed to create a Swarm locally with a web application and a PostgreSQL database. I can scale them and I see Swarm creating replicas.

I think I understand how Docker Swarm can load balance regular web servers, but how does it deal out of the box with database containers?

Outside of the Swarm context, usually databases have their own ways to deal with replication, in the form of plugins or extended products like MySQL cluster. Other databases like Cassandra have replication built directly into their product. On a Swarm context, do we still need to rely on those database plugins and features?

What is the expected pattern to handle data consistency between replicas of a database container?

I know it's a very open-ended question, but Docker's documentation is very open-ended too and I can't seem to find anything specific to this.

Community
  • 1
  • 1
pfernandom
  • 929
  • 10
  • 22
  • 1
    Not an exact duplicate, but similar in spirit question about Kubernetes [How do I model a PostgreSQL failover cluster with Docker/Kubernetes?](http://stackoverflow.com/questions/29451702/how-do-i-model-a-postgresql-failover-cluster-with-docker-kubernetes) – Roman Nov 03 '16 at 23:14

2 Answers2

19

How does it deal out of the box with database containers?

It doesn't.

There is a pretty good description of Swarm services here: How services work (emphasis mine)

When you deploy the service to the swarm, the swarm manager accepts your service definition as the desired state for the service. Then it schedules the service on nodes in the swarm as one or more replica tasks.

Swarm has no idea what's inside the task, all it knows is how many instances of it there are, whether those instances are passing their health checks, and if there are enough of them to satisfy the task definition you gave it. The word overlap between this and database replicas is a little unfortunate, but they are different concepts.

What is the expected pattern to handle data consistency between replicas of a database container?

Setting up data replication is on you. These are probably as good a place to start as any

Community
  • 1
  • 1
Roman
  • 19,581
  • 6
  • 68
  • 84
2

Docker swarm currently scales well for the stateless applications. For database replication, you have to rely on every database's own replication mechanism. Swarm could not manage the datatbase replication. The volume or file system level replication could provide the protection for a single instance database, but are not aware of database replication/cluster.

For databases such as PostgreSQL, the additional works are required. There are a few options:

  1. Use host's local directory. You will need to create one service for every replica, and use constraint to schedule the container to one specific host. You will also need custom postgresql docker image to set up the postgresql replication among replicas. While, when one node goes down, one PostgreSQL replica will go down. You will need to work to bring up another replica. See crunchydata's example.

  2. Use the volume plugin, such as flocker, REX-Ray. You will still need to create one service for every replica, and bind one volume to one service. You need to create all services in the same overlay network and configure the PostgreSQL replicas to talk with each other via the dns name (the docker service name of the replica). You will still need to set up the postgresql replication among replicas.

CloudStax
  • 649
  • 5
  • 6
  • 2
    If you are affiliated with the FireCamp product (and it appears that you are), then you *must* clearly disclose your affiliation within your answer (https://stackoverflow.com/help/promotion). Please [edit] your answer, and keep this in mind for future answers. – Cody Gray - on strike Sep 26 '17 at 08:18
  • This post is neutral. Just try to summarize the current solutions. So I could mention about other products, but never with FireCamp? – CloudStax Sep 27 '17 at 11:43
  • 1
    You can mention any products you like, including FireCamp, if they are relevant to the question. However, if you suggest using a product with which you are affiliated, you need to disclose your affiliation. – Cody Gray - on strike Sep 27 '17 at 11:46
  • So you think this answer is still affiliated? I didn't not intend to do that. Just try to cover the possible solution. Anyway, I just deleted it. – CloudStax Sep 27 '17 at 12:06