大网数据广告活动
服务器运维问答

How does docker swarm handle volumes mounted on replicated services and How would one integrate this with S3?

· 52CE 我爱测

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 …

问题原文

原帖链接 · 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/data on manager node) mounted to persist data from primary postgres service.
    • Only 1 instance will be running.
  • 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 replica service instances.
    • Has 5 instances running via replicas: 5 under deploy: key of docker-stack.yml.
  • This setup works and doing select * from pg_stat_replication; shows 5 wal recievers on primary.
  • 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 replica service in the same way as I mounted for my primary service. 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,

  1. 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.
  2. 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.yaml and then individually define the state your application for each replica (which of course is not worth the effort!)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2024-01-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。