当前位置:   article > 正文

利用pgpool实现PostgreSQL的高可用

window pgpool

基于流复制的方式,两节点自动切换:

    1、单pgpool

        a.环境:

  1. pgpool:192.168.238.129
  2. data1192.168.238.130
  3. data2192.168.238.131

        b.图例

        c.配置互信

  1. ssh-copy-id ha@node1
  2. ssh-copy-id ha@node2
        d.数据库节点配置,请参照《 使用pg_basebackup搭建PostgreSQL流复制环境 》。


        e.pgpool配置:

  1. listen_addresses = '*'
  2. backend_hostname0 = 'node1'
  3. backend_port0 = 5432
  4. backend_weight0 = 1
  5. backend_data_directory0 = '/home/ha/pgdb/data'
  6. backend_flag0 = 'ALLOW_TO_FAILOVER'
  7. backend_hostname1 = 'node2'
  8. backend_port1 = 5432
  9. backend_weight1 = 1
  10. backend_data_directory1 = '/home/ha/pgdb/data'
  11. backend_flag1 = 'ALLOW_TO_FAILOVER'
  12. enable_pool_hba = on
  13. pool_passwd = 'pool_passwd'
  14. pid_file_name = '/home/ha/pgpool/pgpool.pid'
  15. logdir = '/home/ha/pgpool/log'
  16. health_check_period = 1
  17. health_check_user = 'ha'
  18. health_check_password = 'ha'
  19. failover_command = '/home/ha/pgdb/fail.sh %H'
  20. recovery_user = 'ha'
  21. recovery_password = 'ha'
        f.fail.sh
  1. # Failover command for streaming replication.
  2. # This script assumes that DB node 0 is primary, and 1 is standby.
  3. #
  4. # If standby goes down, do nothing. If primary goes down, create a
  5. # trigger file so that standby takes over primary node.
  6. #
  7. # Arguments: $1: failed node id. $2: new master hostname. $3: path to
  8. # trigger file.
  9. new_master=$1
  10. trigger_command="/home/ha/pgdb/bin/pg_ctl -D /home/ha/pgdb/data promote -m fast"
  11. # Do nothing if standby goes down.
  12. if [ $failed_node = 1 ]; then
  13. exit 0;
  14. fi
  15. # Create the trigger file.
  16. /usr/bin/ssh -T $new_master $trigger_command
  17. exit 0;

        g.建立pool_passwd

pg_md5 -m -p -u postgres pool_passwd
        PS:在9.1之前一直用的是trigger_file,这里建议用promote -m fast的方式,因为
pg_ctl promote -m fast will skip the checkpoint at end of recovery so that we can achieve very fast failover when the apply delay is low. Write new WAL record XLOG_END_OF_RECOVERY to allow us to switch timeline correctly for downstream log readers. If we skip synchronous end of recovery checkpoint we request a normal spread checkpoint so that the window of re-recovery is low. Simon Riggs and Kyotaro Horiguchi, with input from Fujii Masao. Review by Heikki Linnakangas
        h.测试
          pgpool节点
  1. [ha@node0 pgdb]$ pgpool -n -d > /tmp/pgpool.log 2>&1 &
  2. [1] 22928
  3. [ha@node0 pgdb]$ psql -h 192.168.238.129 -p 9999 -d postgres -U ha
  4. Password for user ha:
  5. psql (9.4.5)
  6. Type "help" for help.
  7. postgres=# insert into test values (8);
  8. INSERT 0 1
  9. postgres=# select * from test ;
  10.  id 
  11. ----
  12.   1
  13.   2
  14.   3
  15.   4
  16.   6
  17.   8
  18. (6 rows)
         node1节点:
  1. [ha@localhost pgdb]$ ps -ef | grep post
  2. root 2124 1 0 Dec26 ? 00:00:00 /usr/libexec/postfix/master
  3. postfix 2147 2124 0 Dec26 ? 00:00:00 qmgr -l -t fifo -u
  4. postfix 13295 2124 0 06:01 ? 00:00:00 pickup -l -t fifo -u
  5. ha 13395 1 0 06:06 pts/3 00:00:00 /home/ha/pgdb/bin/postgres
  6. ha 13397 13395 0 06:06 ? 00:00:00 postgres: checkpointer process
  7. ha 13398 13395 0 06:06 ? 00:00:00 postgres: writer process
  8. ha 13399 13395 0 06:06 ? 00:00:00 postgres: wal writer process
  9. ha 13400 13395 0 06:06 ? 00:00:00 postgres: autovacuum launcher process
  10. ha 13401 13395 0 06:06 ? 00:00:00 postgres: stats collector process
  11. ha 13404 13395 0 06:07 ? 00:00:00 postgres: wal sender process rep 192.168.238.131(59415) streaming 0/21000060
  12. ha 13418 4087 0 06:07 pts/3 00:00:00 grep post
  13. [ha@localhost pgdb]$ kill -9 13395
         pgpool节点:
  1. postgres=# insert into test values (8);
  2. server closed the connection unexpectedly
  3. This probably means the server terminated abnormally
  4. before or while processing the request.
  5. The connection to the server was lost. Attempting reset: Succeeded.
  6. postgres=# insert into test values (8);
  7. INSERT 0 1
  8. postgres=# insert into test values (8);
  9. INSERT 0 1
  10. postgres=# select * from test ;
  11.  id 
  12. ----
  13.   1
  14.   2
  15.   3
  16.   4
  17.   6
  18.   8
  19.   8
  20.   8
  21. (8 rows)

    2.两个pgpool节点

        a.环境

  1. pgpool:192.168.238.129 pgpool:192.168.238.131
  2. node1:192.168.238.130
  3. node2:192.168.238.131

        b.图例

        c.配置互信,同上。
        d.数据库节点配置,同上。
        e.pgpool配置
            node1

          f.配置pgpool(主)
  1. listen_addresses = '*'
  2. backend_hostname0 = 'node1'
  3. backend_port0 = 5432
  4. backend_weight0 = 1
  5. backend_data_directory0 = '/home/ha/pgdb/data/'
  6. backend_flag0 = 'ALLOW_TO_FAILOVER'
  7. backend_hostname1 = 'node2'
  8. backend_port1 = 5432
  9. backend_weight1 = 1
  10. backend_data_directory1 = '/home/ha/pgdb/data/'
  11. backend_flag1 = 'ALLOW_TO_FAILOVER'
  12. enable_pool_hba = on
  13. authentication
  14. pool_passwd = 'pool_passwd'
  15. pid_file_name = '/home/ha/pgpool/pgpool.pid'
  16. logdir = '/tmp/log'
  17. master_slave_mode = on
  18. master_slave_sub_mode = 'stream'
  19. sr_check_period =2
  20. sr_check_user = 'ha'
  21. sr_check_password = 'ha'
  22. health_check_period = 1
  23. health_check_timeout = 20
  24. health_check_user = 'ha'
  25. health_check_password = 'ha'
  26. failover_command = '/home/ha/pgpool/fail.sh %H'
  27. recovery_user = 'ha'
  28. recovery_password = 'ha'
  29. use_watchdog = on
  30. wd_hostname = 'node1'    #本端
  31. delegate_IP = '192.168.238.151'
  32. #利用ifconfig,查看网卡
  33. if_up_cmd = 'ifconfig eth1:0 inet $_IP_$ netmask 255.255.255.0'
  34. if_down_cmd = 'ifconfig eth1:0 down'
  35. heartbeat_destination0 = 'node2' #对端
  36. heartbeat_device0 = 'eth0'
  37. other_pgpool_hostname0 = 'node2' #对端
  38. other_pgpool_port0 =9999
  39. other_wd_port0 = 9000

        g.配置pgpool(从)

  1. listen_addresses = '*'
  2. backend_hostname0 = 'node1'
  3. backend_port0 = 5432
  4. backend_weight0 = 1
  5. backend_data_directory0 = '/home/ha/pgdb/data/'
  6. backend_flag0 = 'ALLOW_TO_FAILOVER'
  7. backend_hostname1 = 'node2'
  8. backend_port1 = 5432
  9. backend_weight1 = 1
  10. backend_data_directory1 = '/home/ha/pgdb/data/'
  11. backend_flag1 = 'ALLOW_TO_FAILOVER'
  12. enable_pool_hba = on
  13. authentication
  14. pool_passwd = 'pool_passwd'
  15. pid_file_name = '/home/ha/pgpool/pgpool.pid'
  16. logdir = '/tmp/log'
  17. master_slave_mode = on
  18. master_slave_sub_mode = 'stream'
  19. sr_check_period =2
  20. sr_check_user = 'ha'
  21. sr_check_password = 'ha'
  22. health_check_period = 1
  23. health_check_timeout = 20
  24. health_check_user = 'ha'
  25. health_check_password = 'ha'
  26. failover_command = '/home/ha/pgpool/fail.sh %H'
  27. recovery_user = 'ha'
  28. recovery_password = 'ha'
  29. use_watchdog = on
  30. wd_hostname = 'node2' #本端
  31. delegate_IP = '192.168.238.151'
  32. #利用ifconfig,查看网卡
  33. if_up_cmd = 'ifconfig eth1:0 inet $_IP_$ netmask 255.255.255.0'
  34. if_down_cmd = 'ifconfig eth1:0 down'
  35. heartbeat_destination0 = 'node1' #对端
  36. heartbeat_device0 = 'eth1'
  37. other_pgpool_hostname0 = 'node1' #对端
  38. other_pgpool_port0 =9999
  39. other_wd_port0 = 9000

        h.fail.sh

  1. # Failover command for streaming replication.
  2. # This script assumes that DB node 0 is primary, and 1 is standby.
  3. #
  4. # If standby goes down, do nothing. If primary goes down, create a
  5. # trigger file so that standby takes over primary node.
  6. #
  7. # Arguments: $1: failed node id. $2: new master hostname. $3: path to
  8. # trigger file.
  9. new_master=$1
  10. trigger_command="/home/ha/pgdb/bin/pg_ctl -D /home/ha/data start"
  11. # Do nothing if standby goes down.
  12. if [ $failed_node = 1 ]; then
  13. exit 0;
  14. fi
  15. # Create the trigger file.
  16. /usr/bin/ssh -T $new_master $trigger_command
  17. exit 0;
            i.建立pool_passwd
pg_md5 -m -p -u postgres pool_passwd

        j.测试

  1. #数据库、pgpool启动
  2. [ha@node0 pgdb]$ psql -h 192.168.238.151 -p 9999 -d postgres -U ha
  3. Password for user ha:
  4. psql (9.4.5)
  5. Type "help" for help.
  6. postgres=# insert into test values (9);
  7. INSERT 0 1
  8. postgres=# insert into test values (9);
  9. INSERT 0 1
  10. postgres=#
  11. --杀掉node1的数据库进程
  12. postgres=# insert into test values (9);
  13. server closed the connection unexpectedly
  14. This probably means the server terminated abnormally
  15. before or while processing the request.
  16. The connection to the server was lost. Attempting reset: Succeeded.
  17. postgres=# insert into test values (9);
  18. INSERT 0 1
  19. postgres=# insert into test values (9);
  20. INSERT 0 1
  21. postgres=# insert into test values (9);
  22. INSERT 0 1
  23. postgres=# insert into test values (9);
  24. INSERT 0 1
  25. postgres=# insert into test values (9);
  26. INSERT 0 1
  27. postgres=# insert into test values (9);
  28. INSERT 0 1
  29. postgres=# insert into test values (9);
  30. INSERT 0 1
  31. postgres=# insert into test values (9);
  32. INSERT 0 1
  33. --杀掉node1的pgpool进程
  34. postgres=# insert into test values (9);
  35. server closed the connection unexpectedly
  36. This probably means the server terminated abnormally
  37. before or while processing the request.
  38. The connection to the server was lost. Attempting reset: Succeeded.
  39. postgres=# insert into test values (9);
  40. INSERT 0 1
  41. postgres=# insert into test values (9);
  42. INSERT 0 1
  43. postgres=#




转载于:https://my.oschina.net/Suregogo/blog/552765

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/173352
推荐阅读
相关标签
  

闽ICP备14008679号