0

我有几个 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. 我使用下一个命令在 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 中保存更改的数据?

4

1 回答 1

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这样做。

于 2019-10-30T05:49:30.993 回答