暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL企业级高可用repmgr架构部署

呆呆的私房菜 2024-09-05
415
    Whoami:5年+金融、政府、医疗领域工作经验的DBA
    Certificate:OCP、PCP
    Skill:Oracle、Mysql、PostgreSQL
    Platform:CSDN、墨天伦、公众号(呆呆的私房菜)


    阅读本文可以了解使用repmgr扩展实现PostgreSQL数据库高可用的相关内容,包含repmgr高可用架构基本原理、搭建部署及主备切换等。


    01

    repmgr概述
    • repmgr是什么?

    • repmgr是一个开源工具套件,用于管理PostgreSQL服务器集群中的复制和故障转移。

    • 它增强了PostgreSQL内置的热备用能力,提供了设置备用服务器、监控复制和执行故障转移或手动切换操作等管理任务的工具。


    • repmgr收费吗?

    • repmgr 由全球PostgreSQL支持、培训、开发、迁移和咨询领域的专家EDB开发,并由其他个人和组织贡献。

    • 它是免费且开源的软件,根据GNU公共许可证(GPL)v3许可。这意味着您可以自由使用和修改repmgr,但您所做的任何修改只能在同一条款下分发。


    02

    repmgr基本原理


    • repmgr通过监控PostgreSQL集群中的节点状态并自动执行故障转移来实现高可用。当主机点发生故障时,repmgr能够自动提升一个备用节点作为主节点,从而确保服务的连续性和数据的一致性。

    • 1. 节点管理:repmgr将每个服务器定义为一个节点,每个节点都有唯一的ID和配置文件,记录了节点的详细信息和连接参数;

    • 2. 自动故障转移:repmgrd守护进程实时监控所有节点的健康状态,一旦检测到主节点不可用,它将根据预选的选举机制选择一个备用节点并自动提升其为新的主节点;

    • 3. 数据同步:repmgr通过流复制的方式确保主节点和备用节点之间的数据保持同步,支持同步或异步复制模式以满足不同的数据一致性需求;

    • 4. 元数据管理:repmgr在数据库中维护集群的元数据,包括节点状态和配置信息,这些信息对于故障转移和集群管理至关重要;

    • 5. 手动切换与故障恢复:repmgr提供了命令行工具,允许管理员手动执行主备切换和故障恢复操作,以应对特定的维护场景;

    • 6. witness节点:为了提高故障转移的准确性,repmgr可以配置witness节点,它可以作为一个独立的监控点,帮助避免因网络问题导致的不正确的故障转移决策;

    • 7. 日志和通知:repmgrd记录详细的操作日期,并通过可配置的脚本发送通知,使管理员能及时响应集群中的任何变化。


    03

    repmgr高可用架构搭建


    • 高可用搭建环境介绍:

    • 本章高可用部署采用的是1主节点+2从节点+1见证服务器的架构。


    序号主机名
    IP地址

    介质版本操作系统版本主机资源
    1pgdb01
    192.168.56.11

    postgresql-11.18.tar.gz

    repmgr-5.3.1.tar.gz

    centos7.64C 8G 500G
    2
    pgdb02192.168.56.12
    3
    pgdb03192.168.56.13
    4
    pgdb04192.168.56.142c 4G 100G
      1. 主机环境配置
      cat >> etc/sysctl.conf <<-EOF
      kernel.shmmax = 4294967296
      kernel.shmall = 2097152
      kernel.shmmni = 4096
      kernel.sem = 250 32000 100 128
      fs.aio-max-nr = 1048576
      net.ipv4.ip_local_port_range = 9000 65500
      net.core.rmem_default = 262144
      net.core.rmem_max = 4194304
      net.core.wmem_default = 262144
      net.core.wmem_max = 4194304
      fs.file-max = 6815744
      EOF


      2. 配置主机资源限制
      cat >> etc/security/limits.conf <<-EOF
      * soft nofile 1024000
      * hard nofile 1024000
      * soft nproc unlimited
      * hard nproc unlimited
      * soft core unlimited
      * hard core unlimited
      * soft memlock unlimited
      * hard memlock unlimited
      EOF


      3. 安装系统依赖
      yum -y install coreutils glib2 lrzsz mpstat dstat sysstat e4fsprogs xfsprogs ntp readline-devel zlib-devel openssl-devel pam-devel libxml2-devel libxslt-devel python-devel tcl-devel gcc make smartmontools flex bison perl-devel perl-ExtUtils* openldap-devel jadetex openjade bzip2 gettext json-c json-c-devel curl-devel


      4. 源码安装PostgreSQL数据库和repmgr扩展(所有主机执行)
      -- 创建安装用户
      useradd postgres
      echo "postgres#2024" | passwd --stdin postgres
      -- 配置用户环境变量
      cat >> home/postgres/.bash_profile <<-EOF
      export PGHOME=/pg/pg11
      export PGDATA=/pg/pg11/data
      export PATH=\$PATH:\$PGHOME/bin
      EOF
      chown postgres.postgres ~/.bash_profile
      -- 上传postgresql-11.18.tar.gz安装包到/pg/pg11下
      cd pg/pg11/
      tar xzf postgresql-11.18.tar.gz
      cd postgresql-11.18
      ./configure --prefix=/pg/pg11 --enable-nls --with-perl --with-python --with-tcl --with-gssapi --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt
      make -j 4 && make install
      -- 上传repmgr-5.3.1.tar.gz安装包到 /pg/pg11/postgresql-11.18/contrib下
      cd /pg/pg11/postgresql-11.18/contrib
      tar xzf repmgr-5.3.1.tar.gz
      cd repmgr-5.3.1
      ./configure && make && make install


      5. 主机互信创建(所有主机执行)
      cat >> /etc/hosts <<-EOF
      192.168.56.11 pgdb01
      192.168.56.12 pgdb02
      192.168.56.13 pgdb03
      192.168.56.14 pgdb04
      EOF
      ssh-keygen -t rsa
      -- 主节点执行
      ssh pgdb01 "cat ~/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
      ssh pgdb02 "cat ~/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
      ssh pgdb03 "cat ~/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
      ssh pgdb04 "cat ~/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
      scp ~/.ssh/authorized_keys pgdb-02:~/.ssh/
      scp ~/.ssh/authorized_keys pgdb-03:~/.ssh/
      scp ~/.ssh/authorized_keys pgdb-04:~/.ssh/
      -- 所有主机执行
      chmod 644 ~/.ssh/authorized_keys
      chmod 700 ~/.ssh


      6. 初始化主库
      initdb -E UTF8 --locale=en_US.utf8 -U postgres


      7. 主库配置
      -- 修改数据库参数配置
      cat >> $PGDATA/postgresql.conf <<EOF
      listen_addresses = '*'
      port=5432
      unix_socket_directories='/pg/pg11/data'
      logging_collector = on
      log_directory = 'pg_log'
      log_filename = 'postgresql-%a.log'
      log_truncate_on_rotation = on
      wal_level='replica'
      archive_mode='on'
      archive_command='test ! -f pg/pg11/archive/%f && cp %p pg/pg11/archive/%f'
      hot_standby=on
      max_wal_senders=10
      wal_sender_timeout=60s
      wal_keep_segments = 128
      max_replication_slots=10
      wal_log_hints=on
      shared_preload_libraries ='repmgr'
      EOF
      -- 修改访问白名单配置
      cat >> $PGDATA/pg_hba.conf <<EOF
      host replication all 0.0.0.0/0 md5
      local repmgr repmgr md5
      host repmgr repmgr 127.0.0.1/32 md5
      host    repmgr          repmgr          192.168.56.0/24         md5
      local replication repmgr md5
      host replication repmgr 127.0.0.1/32 md5
      host    replication     repmgr          192.168.56.0/24         md5
      EOF
      -- 主库创建用户
      createuser -s repmgr -h127.0.0.1 -Upostgres
      createdb repmgr -O repmgr -h127.0.0.1 -Upostgres
      psql -Upostgres -h 127.0.0.1 -c "alter user repmgr with password 'repmgr#2024';"
      psql -Upostgres -h 127.0.0.1 -c "select name, setting from pg_settings where name in ('wal_level','archive_mode','archive_command');"


      8. repmgr配置
      -- 主库执行
      cat > $PGHOME/repmgr.conf <<EOF
      node_id=1
      node_name=pgdb01
      conninfo='host=192.168.56.11 user=repmgr password=repmgr#2024 dbname=repmgr connect_timeout=2'
      data_directory='/pg/pg11/data'
      pg_bindir='/pg/pg11/bin'
      EOF
      -- 从库1执行
      cat > $PGHOME/repmgr.conf <<EOF
      node_id=2
      node_name=pgdb02
      conninfo='host=192.168.56.12 user=repmgr password=repmgr#2024 dbname=repmgr connect_timeout=2'
      data_directory='/pg/pg11/data'
      pg_bindir='/pg/pg11/bin'
      EOF
      -- 从库2执行
      cat > $PGHOME/repmgr.conf <<EOF
      node_id=3
      node_name=pgdb03
      conninfo='host=192.168.56.13 user=repmgr password=repmgr#2024 dbname=repmgr connect_timeout=2'
      data_directory='/pg/pg11/data'
      pg_bindir='/pg/pg11/bin'
      EOF
      -- 所有节点都配~/.pgpass密码文件
      cat >> ~/.pgpass <<EOF
      192.168.56.11:5432:repmgr:repmgr:repmgr#2024
      192.168.56.12:5432:repmgr:repmgr:repmgr#2024
      192.168.56.13:5432:repmgr:repmgr:repmgr#2024
      192.168.56.14:5432:repmgr:repmgr:repmgr#2024
      192.168.56.11:5432:replication:repmgr:repmgr#2024
      192.168.56.12:5432:replication:repmgr:repmgr#2024
      192.168.56.13:5432:replication:repmgr:repmgr#2024
      192.168.56.14:5432:replication:repmgr:repmgr#2024
      EOF
      chmod 0600 ~/.pgpass
      -- 注册主库服务
      repmgr -f $PGHOME/repmgr.conf primary register
      psql -Urepmgr -h127.0.0.1 -c "select * from repmgr.nodes;"
      -- 注册从库1服务
      repmgr -h 192.168.56.11 -U repmgr -d repmgr -f $PGHOME/repmgr.conf standby clone --dry-run
      repmgr -h 192.168.56.11 -U repmgr -d repmgr -f $PGHOME/repmgr.conf standby clone
      pg_ctl start
      repmgr -h 192.168.56.11 -U repmgr -d repmgr -f $PGHOME/repmgr.conf standby register
      psql -Urepmgr -h127.0.0.1 -c "select * from repmgr.nodes;"
      repmgr -f $PGHOME/repmgr.conf cluster show
      -- 注册从库2服务
      repmgr -h 192.168.56.11 -U repmgr -d repmgr -f $PGHOME/repmgr.conf standby clone --dry-run
      repmgr -h 192.168.56.11 -U repmgr -d repmgr -f $PGHOME/repmgr.conf standby clone
      pg_ctl start
      repmgr -h 192.168.56.11 -U repmgr -d repmgr -f $PGHOME/repmgr.conf standby register
      psql -Urepmgr -h127.0.0.1 -c "select * from repmgr.nodes;"
      repmgr -f $PGHOME/repmgr.conf cluster show
      -- 见证节点配置
      -- 注意:见证节点不需要克隆,初始化数据库即可
      initdb -E UTF8 --locale=en_US.utf8 -U postgres
      -- 修改数据库配置
      cat >> $PGDATA/postgresql.conf <<EOF
      listen_addresses = '*'
      port=5432
      unix_socket_directories='/pg/pg11/pgdata'
      logging_collector = on
      log_directory = 'pg_log'
      log_filename = 'postgresql-%a.log'
      log_truncate_on_rotation = on
      wal_level='replica'
      archive_mode='on'
      archive_command='test ! -f /pg/pg11/archive/%f && cp %p /pg/pg11/archive/%f'
      hot_standby=on
      max_wal_senders=10
      wal_sender_timeout=60s
      wal_keep_segments = 128
      max_replication_slots=10
      wal_log_hints=on
      shared_preload_libraries ='repmgr'
      -- 修改访问白名单配置
      host replication all 0.0.0.0/0 md5
      local repmgr repmgr md5
      host repmgr repmgr 127.0.0.1/32 md5
      host    repmgr          repmgr          192.168.56.0/24        md5
      local replication repmgr md5
      host replication repmgr 127.0.0.1/32 md5
      host    replication     repmgr          192.168.56.0/24        md5
      -- 见证节点创建用户
      createuser -s repmgr -h127.0.0.1 -Upostgres
      createdb repmgr -O repmgr -h127.0.0.1 -Upostgres
      psql -Upostgres -h 127.0.0.1 -c "alter user repmgr with password 'repmgr#2024';"
      psql -Upostgres -h 127.0.0.1 -c "select * from pg_settings where name in ('wal_level','archive_mode','archive_command');"
      -- 见证节点repmgr配置文件
      cat > $PGHOME/repmgr.conf <<EOF
      node_id=4
      node_name=pgdb-04
      conninfo='host=192.168.56.14 user=repmgr password=repmgr#2024 dbname=repmgr connect_timeout=2'
      data_directory='/pg/pg11/data'
      pg_bindir='/pg/pg11/bin'
      EOF
      -- 注册见证节点服务
      repmgr -h 192.168.56.11 -U repmgr -d repmgr -f $PGHOME/repmgr.conf witness register
      repmgr -f $PGHOME/repmgr.conf cluster show


      9. 检查repmgr服务
      repmgr -f $PGHOME/repmgr.conf cluster matrix
      repmgr -f $PGHOME/repmgr.conf cluster crosscheck
      repmgr -f $PGHOME/repmgr.conf node status
      repmgr -f $PGHOME/repmgr.conf node check


      10. 自动故障转移配置
      cat >> /pg/pg11/repmgr.conf <<-EOF
      monitoring_history=yes
      monitor_interval_secs=5
      failover=automatic
      reconnect_attempts=6
      reconnect_interval=5
      promote_command='repmgr standby promote -f /pg/pg11/repmgr.conf --log-to-file'
      follow_command='repmgr standby follow -f /pg/pg11/repmgr.conf --log-to-file --upstream-node-id=%n'
      log_level=INFO
      log_status_interval=10
      log_file='/pg/pg11/repmgr.log'
      EOF


      11. 日志轮询配置
      cat >> /etc/logrotate.conf <<"EOF"
      /pg/pg11/repmgr.log {
      missingok
      compress
      rotate 30
      daily
      dateext
      create 0600 postgres postgres
      }
      EOF


      12. 所有节点启动repmgrd进程
      echo "repmgrd -f /pg/pg11/repmgr.conf --pid-file /tmp/repmgrd.pid --daemonize" >> /etc/rc.local
      chmod +x /etc/rc.d/rc.local
      repmgrd -f /pg/pg11/repmgr.conf --pid-file /tmp/repmgrd.pid --daemonize


      04

      repmgr切换


      • repmgr切换分为两种,一种是手动主备切换,一种是故障自动切换。

      • 1. 手动主备切换:

        切换目标:主节点切换成从节点,从节点1切换成主节点
        1. 检查所有节点状态
        repmgr -f /pg/pg11/repmgr.conf cluster show
        2. 预执行,从节点1执行切换,必须保证ssh通讯正常
        repmgr -f /pg/pg11/repmgr.conf standby switchover --siblings-follow --dry-run --log-level DEBUG --force-rewind
        3. 从节点1执行切换
        repmgr -f /pg/pg11/repmgr.conf standby switchover --siblings-follow --force-rewind --log-level DEBUG --verbose
        • 2. 故障自动切换:

          目标: 模拟主库宕机,从节点自动切换成主节点
          1. 停止主库
          pg_ctl stop
          2. 查看从节点是否切换为主节点
          repmgr -f /pg/pg11/repmgr.conf cluster show
          • 3. 故障切换后切回原主库:

            目标: 宕机的主库重新加入集群,并切回正式主库
            1. 重新加入集群
            repmgr -f $PGHOME/repmgr.conf node rejoin -d 'host=192.168.56.12 dbname=repmgr user=repmgr' --force-rewind --config-files=postgresql.conf,postgresql.auto.conf --verbose --dry-run
            2. 查看是否成功加入集群
            repmgr -f /pg/pg11/repmgr.conf cluster show
            3. 原主库执行主备切换
            repmgr -f /pg/pg11/repmgr.conf standby switchover --siblings-follow --force-rewind --log-level DEBUG --dry-run 



            本文内容就到这啦,阅读完本篇,相信你对PostgreSQL高可用架构repmgr相关知识有了一定的认识了吧!我们下篇再见!

            点击上方公众号,关注我吧!

            文章转载自呆呆的私房菜,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

            评论