问题原文
原帖链接 · Ayush · CC BY-SA 4.0
Setup: A docker swarm with one manager and one worker node (two virtual machines at play). The docker-stack.yml contains two services
primary: bitnami/postgresql image setup as the master postgres server.- Has a local directory (
./docker/primary/dataon manager node) mounted to persist data from primary postgres service. - Only 1 instance will be running.
- Has a local directory (
replica: bitnami/postgresql image setup as the replicated postgres server.- Does not have any volume mounted. This means the replicated data is ephemeral and restarting the container would lead to data loss on
replicaservice instances. - Has 5 instances running via
replicas: 5underdeploy:key ofdocker-stack.yml.
- Does not have any volume mounted. This means the replicated data is ephemeral and restarting the container would lead to data loss on
- This setup works and doing
select * from pg_stat_replication;shows 5 wal recievers onprimary. - Both virtual machines within the swarm are running locally on my computer on debian. In a real production environment, these two machines would be two individual ec2 instances. I'm only running it locally to learn HA deployments.
Goal: Save the data for each replicated instances of 'replica' service to a separate directory (maybe locally on the worker node itself in a unique directory or on aws s3 bucket for backup; this should total to 5 unique backup's from each replicated instance of 'replica' service for the above setup).
Problem:
- If I mount a local volume on the
replicaservice in the same way as I mounted for myprimaryservice. I think this would lead to write races since each 'replica' instance of the service would try to write to the same folder and the local folder's aren't guaurrented to exist worker and manager nodes in the swam. How do you overcome this? - If the above local backups work, how do you then take this local directory and back that up to aws s3 bucket?
Research: I've searched around this problem and came to a conclusion that swarm isn't really good at state-full replication and swarm is designed more for stateless replication of a given service.
docker-stack.yml
version: "3.9"
# temporary env vars.
services:
primary:
image: "bitnami/postgresql:latest"
environment:
postgresql_username: "primary"
postgresql_password: "password"
postgresql_database: "lune"
postgresql_replication_mode: "master"
postgresql_replication_user: "replica"
postgresql_replication_password: "password"
postgresql_synchronous_commit_mode: "on"
postgresql_num_synchronous_replicas: "1"
volumes:
- ./docker/primary/data:/bitnami/postgresql
ports:
- 5432:5432
networks:
- backend
deploy:
restart_policy:
condition: on-failure
replica:
image: "bitnami/postgresql:latest"
depends_on:
- primary
environment:
postgresql_password: "password"
postgresql_replication_mode: "slave"
postgresql_replication_user: "replica"
postgresql_replication_password: "password"
postgresql_master_host: "primary"
postgresql_master_port_number: "5432"
ports:
- 5433:5432
networks:
- backend
deploy:
replicas: 5
restart_policy:
condition: on-failure
networks:
backend:
已采纳解答
原帖链接 · Ayush · CC BY-SA 4.0
About 4 months later, I'm better equipped at answering this question. We first need to establish a difference between types of applications and there are generally only two kinds,
- Stateless Applications:
- These are applications that can be removed, killed, added, restarted without any problems. These apps do not rely on external state such as the state of the environment they're being run on.
- Ex. Web Servers, Servers etc.
- Statefull Applications:
- These are applications that might cause a problem if they're removed, killed, added, restarted. They rely on external state such as volumes/disk etc.
- Ex Databases etc.
There is unfortunately no workaround for replication of statefull applications in docker swarm. In other words, docker swarm is not designed to handle scaling of statefull applications and workloads. I recommend you to google and research about Kubernetes which can handle scaling of statefull applications very well. This is very well what I did for my case.
If you're curious: The only workaround available to scale your statefull applications in docker is to create independent services in your
docker-stack.yamland then individually define the state your application for each replica (which of course is not worth the effort!)
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
