Postgresql数据库集群HA方案--pgpool

Posted 蜷缩的蜗牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Postgresql数据库集群HA方案--pgpool相关的知识,希望对你有一定的参考价值。

一、前期准备


1、安装Postgresql数据库主从流复制

请参考《》一文,以下均基于《》部署完成后操作


2、配置hosts

#vim /etc/hosts

192.168.52.101   pgpool-node01

192.168.52.102   pgpool-node02


3、配置双机互信(node01/node02)

# su – postgres

$ ssh-keygen

$ ssh-copy-id postgres@192.168.52.101

$ ssh postgres@192.168.52.101

$ ssh-copy-id postgres@192.168.52.102

$ ssh postgres@192.168.52.102

一定要确保postgres用户访问双机都免密输入,后面涉及到pgpool master主机进行Failover command切换PG主和Follow command恢复PG备的操作,


4、创建目录并修改权限

mkdir /opt/pgsql

chown postgres.postgres /opt/pgsql -R         //pgpool安装目录

chown postgres.postgres /usr/local/src/pgsql   //源码目录


二、Pgpool-II安装和配置

1、Pgpool-II安装

# su - postgres

$ cd /usr/local/src/pgsql

tar zxvf pgpool-II-3.6.3.tar.gz

cd pgpool-II-3.6.3

mkdir /opt/pgpool

./configure --prefix=/opt/pgpool/ \

--with-pgsql=/opt/pgsql-9.3/

make && make install



2、安装相关pgpool函数(Postgresql 主库安装即可同步到备库)

  1. [postgres@node1 ~]$cd /usr/local/src/pgsql/pgpool-II-3.6.3/src/sql/pgpool-regclass

  2. [postgres@node1 pgpool-regclass]$make

  3. [postgres@node1 pgpool-regclass]$make install

  4. [postgres@node1 pgpool-regclass]$psql -f pgpool-regclass.sql template1

  5. [postgres@node1 pgpool-regclass]$cd ../pgpool-recovery

  6. [postgres@node1 pgpool-recovery]$make

  7. [postgres@node1 pgpool-recovery]$make install

  8. [postgres@node1 pgpool-recovery]$psql -f pgpool-recovery.sql template1

  9. [postgres@node1 ~]$psql -U postgres template1

    template1=# \df


3、配置环境变量

  1. #vim /etc/profile

  2. export PGPOOL=/opt/pgpool

  3. export PATH=.:$PGPOOL/bin:$PATH

  4. #source /etc/profile


4、配置ifconfig,arping执行权限(node01/node02)

  1. #vim /etc/sudoers

  2. postgres        ALL=(ALL)       NOPASSWD: /sbin/ifconfig

  3. postgres        ALL=(ALL)       NOPASSWD: /sbin/ifup

  4. postgres        ALL=(ALL)       NOPASSWD: /sbin/ifdown

  5. postgres        ALL=(ALL)       NOPASSWD: /sbin/arping

  6. #chmod u+s /sbin/ifconfig

  7. #chmod u+s /usr/sbin/arping


5、配置pgpool主配置文件(pgpool.conf)

cd /opt/pgpool/etc/

cp pgpool.conf.sample pgpool.conf

vim pgpool.conf    //修改如下选项,其他保持默认即可

       listen_addresses = '*'

port = 9999

socket_dir = '/tmp'

listen_backlog_multiplier = 2

pcp_listen_addresses = '*'

pcp_port = 9898

pcp_socket_dir = '/tmp'

backend_hostname0 = '192.168.52.101'          //配置数据节点PG节点

backend_port0 = 5432

backend_weight0 = 1

backend_data_directory0 = '/var/lib/pgsql/9.3/data'

backend_flag0 = 'ALLOW_TO_FAILOVER'

backend_hostname1 = '192.168.52.102'          //配置数据节点PG节点

backend_port1 = 5432

backend_weight1 = 1

backend_data_directory0 = '/var/lib/pgsql/9.3/data'

backend_flag1 = 'ALLOW_TO_FAILOVER'

enable_pool_hba = on                         //

pool_passwd = 'pool_passwd'

num_init_children = 32                       //初始化子进程数量

log_destination = 'syslog'

log_line_prefix = ' %a: database %d: username %u: %t: pid %p: '   

log_connections = on

print_timestamp = on

log_hostname = on

log_statement = on

log_per_node_statement = on

log_standby_delay = 'none'

syslog_facility = 'LOCAL0'

syslog_ident = 'pgpool'

debug_level = 0

pid_file_name = '/var/log/pgpool/pgpool.pid'

logdir = '/var/log/pgpool'

load_balance_mode = off               // 设置负载均衡模式

master_slave_mode = on             // 设置主从

master_slave_sub_mode = 'stream'   // 设置流复制模式

sr_check_period = 5

sr_check_user = 'replica'

sr_check_password = 'replica'

delay_threshold = 0

follow_master_command = '/opt/pgpool/follw_recovery.sh %d %h /opt/pgpool/pg_recovery.sh'    

health_check_period = 5

health_check_timeout = 20

health_check_user = 'replica'

health_check_password = 'replica'

health_check_max_retries = 3

health_check_retry_delay = 1

failover_command = '/opt/pgpool/failover_stream.sh  %d %H /var/lib/pgsql/9.3/data/trigger_file'

use_watchdog = on

ping_path = '/bin'

wd_port = 9000

wd_authkey = ''

ifconfig_path = '/sbin'

if_up_cmd = 'ifconfig eth0:0 inet $_IP_$ netmask 255.255.255.0'

if_down_cmd = 'ifconfig eth0:0 down'

arping_path = '/usr/sbin'           # arping command path

arping_cmd = 'arping -U $_IP_$ -w 1'

heartbeat_destination0 = '192.168.52.102'      # 配置对端的hostname

heartbeat_destination_port0 = 9694 

heartbeat_device0 = 'eth0'

other_pgpool_hostname0 = '192.168.52.102'    ## 配置对端的 pgpool

other_pgpool_port0 = 9999

other_wd_port0 = 9000


6、配置pcp.conf  //pgpool管理账户密码

  1. $pg_md5 -u postgres -p

  2. password: postgres

  3. e8a48653851e28c69d0506508fb27fc5

  4. $cd /opt/pgpool/etc

  5. $cp pcp.conf.sample pcp.conf

  6. $vim pcp.conf

  7. 编写 pcp.conf 文件,写入以下内容

  8. # USERID:MD5PASSWD

  9. postgres:e8a48653851e28c69d0506508fb27fc

7、配置pool_hba.conf


  1. [postgres@node1 ~]$cd /opt/pgpool/etc

  2. [postgres@node1 etc]$cp pool_hba.conf.sample pool_hba.conf

  3. [postgres@node1 etc]$vim pool_hba.conf

  4. host    all    all    192.168.52.102/32 trust

  5. host    all    all    192.168.52.101/32 trust


8、创建faileover脚本

  1. vim /opt/pgpool/failover_stream.sh

  2. #! /bin/sh

  3. # Failover command for streaming replication.

  4. # This script assumes that DB node 0 is primary, and 1 is standby.

  5. #

  6. # If standby goes down, do nothing. If primary goes down, create a

  7. # trigger file so that standby takes over primary node.

  8. #

  9. # Arguments: $1: failed node id. $2: new master hostname. $3: path to

  10. # trigger file.

  11. failed_node=$1

  12. new_master=$2

  13. trigger_file=$3

  14. `# Do nothing if standby goes down.

  15.    #if [ $failed_node = 1 ]; then`

  16. #        exit 0;

  17. #fi

  18. /usr/bin/ssh -T $new_master /bin/touch $trigger_file

  19. exit 0;


# chmod 775 /opt/pgpool/failover_stream.sh


9、启动pgpool

请使用postgres账户启动

postgres@node1 ~]#pgpool –n & 

Postgresql数据库集群HA方案--pgpool

关闭pgpool: 
[postgres@node1 ~]#pgpool –m fast stop 

Postgresql数据库集群HA方案--pgpool

4.8. 连接测试

检查pgpool启动进程 
On master: 

Postgresql数据库集群HA方案--pgpool

On slave:

Postgresql数据库集群HA方案--pgpool

\\通过pgpool访问数据库

[postgres@node1 ~]$ psql -h 192.168.52.100 -p 9999 -U postgres -d postgres

Postgresql数据库集群HA方案--pgpool


\\查看pgpool集群中nodes的状态

postgres=# show pool_nodes;

关于 status 状态,如下

 
   
   
 
  1. 0 - This state is only used during the initialization. PCP will never display it.

  2. 1 - Node is up. No connections yet.

  3. 2 - Node is up. Connections are pooled.

  4. 3 - Node is down.

查看虚拟IP 
On Master: 

以上是关于Postgresql数据库集群HA方案--pgpool的主要内容,如果未能解决你的问题,请参考以下文章

PostgreSQL技术分享公开课及视频下载(第4期):HA(双机热备)实现方案

PostgreSQL技术分享公开课及视频下载(第4期):HA(双机热备)实现方案

大数据笔记:HDFS集群搭建-HA模式概念

Codis3.2集群HA高可用方案

hadoop 集群HA高可用搭建以及问题解决方案

postgresql集群方案参考答案