如何在docker swarm中为Postgres数据库容器创建持久数据卷

时间:2019-10-29 07:51:22

标签: docker docker-compose docker-swarm patroni

我有几个Postgresql服务,以及一些对我而言有用的其他服务(用于创建HA Postgresql集群)。该集群在下面的docker-compose中进行了描述:

    version: '3.3'
services:

  haproxy:
    image: haproxy:alpine
    ports:
        - "5000:5000"
        - "5001:5001"
        - "8008:8008"
    configs: 
      - haproxy_cfg
    networks:
      - dbs
    command: haproxy -f /haproxy_cfg

  etcd:
    image: quay.io/coreos/etcd:v3.1.2
    configs:
      - etcd_cfg
    networks: 
      - dbs
    command: /bin/sh /etcd_cfg

  dbnode1:
    image: seocahill/patroni:1.2.5
    secrets: 
      - patroni.yml
    environment:
      - PATRONI_NAME=dbnode1
      - PATRONI_POSTGRESQL_DATA_DIR=data/dbnode1
      - PATRONI_POSTGRESQL_CONNECT_ADDRESS=dbnode1:5432
      - PATRONI_RESTAPI_CONNECT_ADDRESS=dbnode1:8008
    env_file:
      - test.env
    networks:
      - dbs
    entrypoint: patroni
    command: /run/secrets/patroni.yml

  dbnode2:
    image: seocahill/patroni:1.2.5
    secrets: 
      - patroni.yml
    environment:
      - PATRONI_NAME=dbnode2
      - PATRONI_POSTGRESQL_DATA_DIR=data/dbnode2
      - PATRONI_POSTGRESQL_CONNECT_ADDRESS=dbnode2:5432
      - PATRONI_RESTAPI_CONNECT_ADDRESS=dbnode2:8008
    env_file:
      - test.env
    networks:
      - dbs
    entrypoint: patroni
    command: /run/secrets/patroni.yml

  dbnode3:
    image: seocahill/patroni:1.2.5
    secrets: 
      - patroni.yml
    environment:
      - PATRONI_NAME=dbnode3
      - PATRONI_POSTGRESQL_DATA_DIR=data/dbnode3
      - PATRONI_POSTGRESQL_CONNECT_ADDRESS=dbnode3:5432
      - PATRONI_RESTAPI_CONNECT_ADDRESS=dbnode3:8008
    env_file:
      - test.env
    networks:
      - dbs
    entrypoint: patroni
    command: /run/secrets/patroni.yml

networks:
  dbs:
    external: true

configs:
  haproxy_cfg:
    file: config/haproxy.cfg
  etcd_cfg:
    file: config/etcd.sh

secrets:
  patroni.yml:
    file: patroni.test.yml

我从https://github.com/seocahill/ha-postgres-docker-stack.git取得了这个yml代码。我使用下一个命令在docker swarm-docker network create -d overlay --attachable dbs && docker stack deploy -c docker-stack.test.yml test_pg_cluster中部署此服务。但是,如果我创建一些数据库并向其中插入一些数据,然后重新启动服务,则我的数据将丢失。 我知道我需要使用volume在主机上保存数据。 我使用docker命令docker volume create pgdata创建卷,并使用默认docker卷目录,并按以下方式安装它:

dbnode1:
        image: seocahill/patroni:1.2.5
        secrets: 
          - patroni.yml
        environment:
          - PATRONI_NAME=dbnode1
          - PATRONI_POSTGRESQL_DATA_DIR=data/dbnode1
          - PATRONI_POSTGRESQL_CONNECT_ADDRESS=dbnode1:5432
          - PATRONI_RESTAPI_CONNECT_ADDRESS=dbnode1:8008
        env_file:
          - test.env
        volumes:
          pgdata:/data/dbnode1
        networks:
          - dbs
        entrypoint: patroni
        command: /run/secrets/patroni.yml

        volumes:
           pgdata:

容器启动时,它在容器内的数据目录 data / dbnode1 中具有自己的配置。而且,如果我在主机上装载卷pgdata来存储数据,则无法连接到db,并且容器目录 data / dbnode1 中没有空文件夹。如何创建持久数据卷以在PostgerSQL中保存更改的数据?

1 个答案:

答案 0 :(得分:0)

通过直接添加路径来创建volumes更容易。检查此示例。

dbnode1:
        image: seocahill/patroni:1.2.5
        secrets: 
          - patroni.yml
        environment:
          - PATRONI_NAME=dbnode1
          - PATRONI_POSTGRESQL_DATA_DIR=data/dbnode1
          - PATRONI_POSTGRESQL_CONNECT_ADDRESS=dbnode1:5432
          - PATRONI_RESTAPI_CONNECT_ADDRESS=dbnode1:8008
        env_file:
          - test.env
        volumes:
          - /opt/dbnode1/:/data/dbnode1
        networks:
          - dbs
        entrypoint: patroni
        command: /run/secrets/patroni.yml

注意这些行

        volumes:
          - /opt/dbnode1/:/data/dbnode1

我在其中使用路径/opt/dbnode1//data/dbnode1的容器中存储文件系统。

另外请注意,docker swarm 不会为您创建文件夹。因此,您必须在启动服务之前创建文件夹。运行mkdir -p /opt/dbnode1