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 主库安装即可同步到备库)
[postgres@node1 ~]$cd /usr/local/src/pgsql/pgpool-II-3.6.3/src/sql/pgpool-regclass
[postgres@node1 pgpool-regclass]$make
[postgres@node1 pgpool-regclass]$make install
[postgres@node1 pgpool-regclass]$psql -f pgpool-regclass.sql template1
[postgres@node1 pgpool-regclass]$cd ../pgpool-recovery
[postgres@node1 pgpool-recovery]$make
[postgres@node1 pgpool-recovery]$make install
[postgres@node1 pgpool-recovery]$psql -f pgpool-recovery.sql template1
[postgres@node1 ~]$psql -U postgres template1
template1=# \df
3、配置环境变量
#vim /etc/profile
export PGPOOL=/opt/pgpool
export PATH=.:$PGPOOL/bin:$PATH
#source /etc/profile
4、配置ifconfig,arping执行权限(node01/node02)
#vim /etc/sudoers
postgres ALL=(ALL) NOPASSWD: /sbin/ifconfig
postgres ALL=(ALL) NOPASSWD: /sbin/ifup
postgres ALL=(ALL) NOPASSWD: /sbin/ifdown
postgres ALL=(ALL) NOPASSWD: /sbin/arping
#chmod u+s /sbin/ifconfig
#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管理账户密码
$pg_md5 -u postgres -p
password: postgres
e8a48653851e28c69d0506508fb27fc5
$cd /opt/pgpool/etc
$cp pcp.conf.sample pcp.conf
$vim pcp.conf
编写 pcp.conf 文件,写入以下内容
# USERID:MD5PASSWD
postgres:e8a48653851e28c69d0506508fb27fc
7、配置pool_hba.conf
[postgres@node1 ~]$cd /opt/pgpool/etc
[postgres@node1 etc]$cp pool_hba.conf.sample pool_hba.conf
[postgres@node1 etc]$vim pool_hba.conf
host all all 192.168.52.102/32 trust
host all all 192.168.52.101/32 trust
8、创建faileover脚本
vim /opt/pgpool/failover_stream.sh
#! /bin/sh
# Failover command for streaming replication.
# This script assumes that DB node 0 is primary, and 1 is standby.
#
# If standby goes down, do nothing. If primary goes down, create a
# trigger file so that standby takes over primary node.
#
# Arguments: $1: failed node id. $2: new master hostname. $3: path to
# trigger file.
failed_node=$1
new_master=$2
trigger_file=$3
`# Do nothing if standby goes down.
#if [ $failed_node = 1 ]; then`
# exit 0;
#fi
/usr/bin/ssh -T $new_master /bin/touch $trigger_file
exit 0;
# chmod 775 /opt/pgpool/failover_stream.sh
9、启动pgpool
请使用postgres账户启动
postgres@node1 ~]#pgpool –n &
关闭pgpool:
[postgres@node1 ~]#pgpool –m fast stop
4.8. 连接测试
检查pgpool启动进程
On master:
On slave:
\\通过pgpool访问数据库
[postgres@node1 ~]$ psql -h 192.168.52.100 -p 9999 -U postgres -d postgres
\\查看pgpool集群中nodes的状态
postgres=# show pool_nodes;
关于 status 状态,如下
0 - This state is only used during the initialization. PCP will never display it.
1 - Node is up. No connections yet.
2 - Node is up. Connections are pooled.
3 - Node is down.
查看虚拟IP
On Master:
以上是关于Postgresql数据库集群HA方案--pgpool的主要内容,如果未能解决你的问题,请参考以下文章
PostgreSQL技术分享公开课及视频下载(第4期):HA(双机热备)实现方案