Search code examples
postgresqldocker-swarmdocker-stack

Docker stack deploy postgresql slave container restart in few minutes


Run this YML with docker stack in swarm. Docker stack file to create Postgresql master/slave setup with 2 slaves.

The slave container will restart in few minutes repeat and repeat....

How to fix it ?

docker stack deploy -c docker-stack-postgresql.yml post

docker-stacl-postgresql.yml

<pre>version: "3"
services:
  db1:
    image: bitnami/postgresql:latest
    ports:
      - "127.0.0.1:5432:5432"
    networks:
      - awesomenet
    deploy:
      placement:
        constraints: [node.hostname == node-docker-1]
    volumes:
      - pgdata:/bitnami/postgresql
    env_file: .env.master
  db2:
    image: bitnami/postgresql:latest
    depends_on:
      - db1
    ports:
     - "127.0.0.1:5433:5432"
    networks:
      - awesomenet
    deploy:
      placement:
        constraints: [node.hostname == node-docker-2]
    volumes:
      - pgdata:/bitnami/postgresql
    env_file: .env.slave
  db3:
    image: bitnami/postgresql:latest
    depends_on:
      - db1
    ports:
      - "127.0.0.1:5434:5432"
    networks:
      - awesomenet
    deploy:
      placement:
        constraints: [node.hostname == node-docker-3]
    volumes:
      - pgdata:/bitnami/postgresql
    env_file: .env.slave
volumes:
  pgdata:

networks:
  awesomenet:
    driver: overlay
    driver_opts:
      encrypted: "true"
    </pre>

env file of master : .env.master

<pre>
POSTGRESQL_REPLICATION_MODE=master
POSTGRESQL_REPLICATION_USER=replication_user
POSTGRESQL_REPLICATION_PASSWORD= replication_password
POSTGRESQL_USERNAME=postgres
POSTGRESQL_PASSWORD=password
POSTGRESQL_DATABASE=monkey_db
</pre>

env file : .env.slave

<pre>
POSTGRESQL_REPLICATION_MODE=slave
POSTGRESQL_REPLICATION_USER=replication_user
POSTGRESQL_REPLICATION_PASSWORD= replication_password
POSTGRESQL_MASTER_HOST=db1
POSTGRESQL_MASTER_PORT=5432
</pre>

PostgreSQL log file :

<pre>
nami    INFO  Initializing postgresql
postgre INFO   This installation requires no credentials.
nami    INFO  postgresql successfully initialized
</pre>

PostgreSQL error.log

<pre>
2018-01-16T00:22:47.426512118Z Starting postgresql... 
2018-01-16T00:26:11.369732522Z ERROR Unable to start com.bitnami.postgresql: pg_ctl: directory "/opt/bitnami/postgresql/data" is not a database cluster directory
</pre>

Solution

  • This worked for me:

    version: "3"
    services:
      db1:
        image: bitnami/postgresql:latest
        volumes:
          - pgdata:/bitnami/postgresql
        env_file: .env.master
      db2:
        image: bitnami/postgresql:latest
        env_file: .env.slave
      db3:
        image: bitnami/postgresql:latest
        env_file: .env.slave
    volumes:
      pgdata:
    

    So It's likely you trying to use the same volume for all three services. So either 1. create a different named volume for each or 2. don't use volumes for db2/3 since they are just copies of the primary.

    Also, no need to use depends_on, it doesn't work in Swarm Stacks.