postgresql通过repmgr搭建standby

 一    准备

       1. 两台机机器,操作系统ubuntu

            node1: ip 192.168.4.32

            node2: ip 192.168.8.68

        2. pg和repmgr安装

                                这两个我是通过源码,从pg官网下载,repmgr在github下载。pg编译步骤./configure;make;make install,

                    把repmgr源码下载后,copy到pg源码的src/contrib/下,直接make,make install即可。

    二    搭建standby

        1.  node1配置

               a) 修改pg配置文件

                        postgresql.conf: 具体配置如下

                                listen_addresses='*'
                                wal_level = 'hot_standby'
                                archive_mode = on
                                archive_command = 'cd .'   # we can also use exit 0, anything that
                                                                                   # just does nothing
                                max_wal_senders = 10
                                wal_keep_segments = 5000   # 80 GB required on pg_xlog
                                hot_standby = on
                                shared_preload_libraries = 'repmgr_funcs'
                                listen_addresses = '*'

                        pg_hba.conf: 具体配置如下  

                                host    all             all             192.168.4.32/32            trust
                                host    all             all             192.168.8.68/32            trust
                                host    replication     hli        192.168.4.32/32            trust
                                host    replication     hli        192.168.8.68/32            trust
                b)启动服务

                      pg_ctl start -D ../data
                             执行psql -f ../share/contrib/repmgr_funcs.sql posgres
                             重启服务:pg_ctl -D $PGDATA restart

                c)node1、node2交换key
                            生成key:ssh-keygen -t rsa
                            交换key:
                                        node1执行:ssh-copy-id [email protected]
                                                      ssh-copy-id [email protected]
                                        node2执行
                                                      ssh-copy-id [email protected]
                                                        ssh-copy-id [email protected]
                        
            2. node2配置
                    a)clone master:
                            repmgr -d postgres -U hli -h 192.168.4.32 standby clone -D $PGDATA
                    b)启动服务:
                            pg_ctl -D $PGDATA start
                        
            3. 生成repmgr.conf
                    node1 的配置文件repmgr.conf内容:
                        cluster=my_cluster
                        node=1
                        node_name=node1
                        conninfo='host=192.168.4.32 dbname=postgres user=hli'
                        master_response_timeout=10
                        reconnect_attempts=3
                        reconnect_interval=5
                        failover=automatic
                        promote_command='repmgr standby promote -f /home/hli/postgresql-9.3.5/release/bin/repmgr.conf'
                        follow_command='repmgr standby follow -f /home/hli/postgresql-9.3.5/release/bin/repmgr.conf'
                    node2的配置文件repmgr.conf内容:
                        cluster=my_cluster
                        node=2
                        node_name=node2
                        conninfo='host=192.168.8.68 dbname=postgres user=hli'
                        master_response_timeout=30
                        reconnect_attempts=3
                        reconnect_interval=5
                        failover=automatic
                        promote_command='repmgr standby promote -f /home/hli/release/bin/repmgr.conf'
                        follow_command='repmgr standby follow -f /home/hli/release/bin/repmgr.conf'

            4. 注册master和standby
                    node1上执行:
                        repmgr -f ./repmgr.conf --verbose master register
                    node2上执行:
                        repmgr -f ./repmgr.conf standby register

            5. 初始化wintess服务
                    node2上注册该服务,也可以在第三台机器上做,不过本次实验只有两台机器,所以该witness服务最好在node2上,这样不管node1还是node2服务死掉,都能保证有服务可以正常使用
                    node2上执行:
                                    repmgr -d postgres -U hli -h 192.168.4.32 -D ./witness -f ./repmgr_witness.conf witness create
                            repmgr_witness.conf内容如下:
                                    cluster=my_cluster
                                    node=3
                                    node_name=node3
                                    conninfo='host=192.168.8.68 dbname=postgres user=hli port=5499'
                                    master_response_timeout=5
                                    reconnect_attempts=2
                                    reconnect_interval=2
                                    failover=automatic
                                    promote_command='repmgr standby promote -f /home/hli/sn/zy/release/repmgr.conf'
                                    follow_command='repmgr standby follow -f /home/hli/sn/zy/release/repmgr.conf'

            6. 在node2上注册repmgrd daemons
                    node2上执行:
                        repmgrd -f ./repmgr.conf --daemonize -> /var/log/repmgr.log 2>&1

            至此配置完成,当node1掉电等各种异常导致服务挂掉后,node2上的服务就会成为新的master



































你可能感兴趣的:(postgres,Failover,standby)