简体   繁体   English

如何在dockerized Postgresql中使用repmgr?

[英]How to use repmgr with dockerized Postgresql?

I'm trying to create Postgresql setup with replication and automatic failover. 我正在尝试使用复制和自动故障转移来创建Postgresql安装程序。 I wanted it to be reusable, scalable and portable, so I tried to use docker to run Postgres. 我希望它可重用,可伸缩且可移植,因此我尝试使用docker运行Postgres。 I also didnt want to reinvent the wheel, so I tried to use repmgr as it is recommended tool when setting up postgres replication. 我也不想重新发明轮子,因此在设置postgres复制时尝试使用repmgr作为推荐工具。

On first machine, I started master node using docker-compose and simple Dockerfile. 在第一台机器上,我使用docker-compose和简单的Dockerfile启动了主节点。 Then I added bash script to run after start of container. 然后我添加了bash脚本以在容器启动后运行。 It looks like master was setup properly. 看来母版已正确设置。

Then on second machine I tried to run standby. 然后在第二台计算机上,我尝试运行待机状态。 I started postgres in docker and experimented. 我在docker中启动了postgres并进行了实验。 As we can read on repmgr github repo , to run standby I need to clone master with repmgr command, which uses pg_basebackup internally. 正如我们可以在repmgr github repo上阅读的那样 ,要运行待机状态,我需要使用repmgr命令克隆master,该命令在内部使用pg_basebackup。 Pg_basebackup wont let me clone db into not-empty directory (pgdata), so I need to remove pgdata created by dockerized postgres before cloning. Pg_basebackup不会让我将db克隆到非空目录(pgdata)中,因此在克隆之前,我需要删除由dockerized postgres创建的pgdata。 When I do that, container dies, because of course the only service, postgres, dies without its files. 当我这样做时,容器会死掉,因为当然唯一的服务postgres会死掉而没有文件。 So I can not run repmgr command afterwards, because there is no container to run it inside :) 所以我以后不能运行repmgr命令,因为里面没有容器可以运行它:)

So I tried creating another Dockerfile for standbies only. 因此,我尝试仅为备用对象创建另一个Dockerfile。 Inside pgdata is removed, then repmgr clones master, then postgres is started properly. 在内部删除pgdata,然后repmgr克隆master,然后正确启动postgres。 There are no errors. 没有错误。 But when I log into master machine and verify if replication is running - it is not. 但是,当我登录到主计算机并验证复制是否正在运行时,它不是。

  1. Is it possible to run repmgr for dockerized postgres? 是否可以为dockerized postgres运行repmgr?
  2. If so, how? 如果是这样,怎么办? What am I doing wrong? 我究竟做错了什么?
  3. If not, how can I create postgres cluster replication without manually configuring each server? 如果没有,如何在不手动配置每台服务器的情况下创建postgres集群复制? You know, not treating servers as pets but as cattle etc :) 你知道的,不是把服务器当成宠物,而是像牛一样:)

Files I used and created: docker-compose.yml (both master and standby node): 我使用和创建的文件:docker-compose.yml(主节点和备用节点):

version: '3'
services:
  db:
    build:
      context: .
    volumes:
      - ${DATABASE_STORAGE_PATH}:/var/lib/postgresql/data
    env_file:
      - .env
    environment:
      - PGDATA=/var/lib/postgresql/data/pgdata
    ports:
      - ${DB_PORT}:5432

Master Dockerfile: 主Dockerfile:

FROM postgres:9.6.4

# install repmgr
RUN apt-get update \
    && apt-get install -y repmgr
# COPY conf files
COPY repmgr.conf .
COPY postgres.replication.conf /var/lib/postgresql/data/pgdata/
RUN echo "include '/var/lib/postgresql/data/pgdata/postgres.replication.conf'" >> /var/lib/postgresql/data/pgdata/postgresql.conf

CMD ["postgres"]

Bash script to run when postgres container (master) is alive: 当postgres容器(主服务器)处于活动状态时运行的Bash脚本:

#!/bin/bash
CONTAINER_ID="replicatest2_db_1"

docker exec -d $CONTAINER_ID createuser -s repmgr -U postgres
docker exec -d $CONTAINER_ID createdb repmgr -O repmgr -U postgres
docker exec -d $CONTAINER_ID su postgres -c 'repmgr -f repmgr.conf master register'

Standby docker-compose is the same. 备用docker-compose相同。 Standby Dockerfile: 备用Dockerfile:

FROM postgres:9.6.4

# install repmgr
RUN apt-get update \
    && apt-get install -y repmgr
# COPY repmgr conf
COPY repmgr.conf .

RUN rm -rf /var/lib/postgresql/data/pgdata
RUN su postgres -c "repmgr -h {master-host} -p {master-port} -U repmgr -d repmgr -D /var/lib/postgresql/data/pgdata -f repmgr.conf standby clone"

CMD ["postgres"]

You can use pg-dock , with pg-dock you can run dockerized postgres cluster with repmgr, otherwise you can explore the project and get understand how the things are work. 您可以使用pg-dock ,通过pg-dock可以运行带有repmgr的dockerized postgres集群,否则您可以浏览该项目并了解其工作方式。

Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM