2018年7月29日 星期日

在 PostgreSQL 10 上安裝與設定 repmgr

目標設定:
  • 在 PostgreSQL 10 設定 repmgr !
  • 在各節點上設定 repmgrd 服務!

快速操作流程
    Primary Server 上的設定!
  1. 安裝 repmgr 10 軟體:
    #yum install repmgr10
    
  2. 修改設定檔 postgresql.conf:
    #su - postgres
    $vim /var/lib/pgsql/10/data/postgresql.conf
    max_wal_senders = 10
    wal_level = 'replica'
    hot_standby = on
    archive_mode = on
    archive_command = '/bin/true'
    
  3. 新增 repmgr 系統使用者:
    $ createuser -s repmgr
    $ createdb repmgr -O repmgr
    $ psql
    postgres=# ALTER USER repmgr SET search_path TO repmgr, "$user", public;
    postgres=# \q
    
  4. 修改 pg_hba.conf 設定檔:
    $ cd /var/lib/pgsql/10/data
    $ vim pg_hba.conf
    local   replication   repmgr                              trust
    host    replication   repmgr      127.0.0.1/32            trust
    host    replication   repmgr      192.168.1.0/24          trust
    
    local   repmgr        repmgr                              trust
    host    repmgr        repmgr      127.0.0.1/32            trust
    host    repmgr        repmgr      192.168.1.0/24          trust
    
  5. 修改 /etc/hosts 設定:
    # vim /etc/hosts
    192.168.1.1 node1
    192.168.1.2 node2
    
  6. 測試 repmgr 是否可連到 node1 :(在 standby 上測試)
    # su - postgres
    $psql 'host=node1 user=repmgr dbname=repmgr connect_timeout=2'
    
  7. 修改 repmgr.conf 設定檔:
    #vim /etc/repmgr/10/repmgr.conf
    node_id=1
    node_name='node1'
    conninfo='host=node1 user=repmgr dbname=repmgr connect_timeout=2'
    data_directory='/var/lib/pgsql/10/data'
    pg_bindir='/usr/pgsql-10/bin/'
    ssh_options='-q -o ConnectTimeout=10'
    
  8. 註冊 node1 成為主要節點:
    # su - postgres
    $/usr/pgsql-10/bin/repmgr -f /etc/repmgr/10/repmgr.conf primary register
    
  9. 查詢註冊狀況:
    $/usr/pgsql-10/bin/repmgr -f /etc/repmgr/10/repmgr.conf cluster show
    
    Standby Server 上的設定!
  1. 安裝 repmgr 10 軟體:
    #yum install repmgr10
    
  2. 修改 repmgr.conf 設定檔:
    #vim /etc/repmgr/10/repmgr.conf
    node_id=2
    node_name='node2'
    conninfo='host=node2 user=repmgr dbname=repmgr connect_timeout=2'
    data_directory='/var/lib/pgsql/10/data'
    pg_bindir='/usr/pgsql-10/bin/'
    ssh_options='-q -o ConnectTimeout=10'
    
  3. 測試 node2 是否可複製主機內容:
    # su - postgres
    $/usr/pgsql-10/bin/repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr/10/repmgr.conf standby clone --dry-run
    
  4. 測試後如果没問題,可直接複製 Primary 主機內容:
    $/usr/pgsql-10/bin/repmgr -h node1 -U repmgr -d repmgr -f /etc/repmgr/10/repmgr.conf standby clone
    
  5. 修改 postgresql.conf 設定檔:
    $vim /var/lib/pgsql/10/data/postgresql.conf
    listen_addresses = '*'
    
  6. 啟動 PostgreSQL Server:
    # systemctl restart postgresql-10.service
    
  7. 連進 Primary Server 進行狀況查詢:
    # su - postgres
    $psql -u repmgr -h node1 -d repmgr
    repmgr=# SELECT * FROM pg_stat_replication;
    
  8. 註冊 node2 成為 Standby 節點:
    # su - postgres
    $/usr/pgsql-10/bin/repmgr -f /etc/repmgr/10/repmgr.conf primary register
    $ /usr/pgsql-10/bin/repmgr -f /etc/repmgr/10/repmgr.conf cluster show
    
    設定 repmgrd 機制
  1. 修改在各節點的設定檔 postgresql.conf 內容:
    # su - postgres
    $ vim /var/lib/pgsql/10/data/postgresql.conf
    shared_preload_libraries = 'repmgr'
    
  2. 重新啟動 PostgreSQL 服務:
    # systemctl restart postgresql-10.service
    
  3. 修改設定檔 regmpr.conf,啟動失效自動接管功能:
    # vim /etc/repmgr/10/repmgr.conf
    failover=automatic
    promote_command='/usr/pgsql-10/bin/repmgr standby promote -f /etc/repmgr/10/repmgr.conf --log-to-file'
    follow_command='/usr/pgsql-10/bin/repmgr standby follow -f /etc/repmgr/10/repmgr.conf --log-to-file --upstream-node-id=%n'
    monitoring_history=yes
    monitor_interval_secs=2
    
  4. 啟動 repmgr 服務:
    # systemctl enable repmgr10.service
    # systemctl start repmgr10.service
    

參考文獻:
  1. https://repmgr.org/docs/4.0/quickstart-postgresql-configuration.html