1 Pgpool部署前准备
1.1 下载所需软件安装包
1.1.1 Postgresql安装包:postgresql-9.6.7.tar.gz
1.1.2 Pgpool安装包:pgpool-II-3.7.2.tar.gz
1.2 创建所需的文件夹并授权
1.2.1 创建文件夹
a) 软件安装目录:/app/pgpool和/app/pgsql
b) 数据目录:/data/postgresql
1.2.2 授权文件夹
1. 所有目录使用
sudo chown –R 用户名:用户名 路径
命令授权文件夹用户使用权限
2. 在用户目录创建soft文件夹,将软件包复制到文件夹内。
1.3 配置hosts文件
1.3.1 编辑并修改hosts文件
1. 分别在主机1,2上执行sudo vi /etc/hosts
2. 在文件中添加
主机1的IP 主机的名称
主机2的IP 从机的名称
虚拟IP VIP
如:192.168.1.1 postgres1
1.4 配置ssh互信
1.4.1 在要配置互信的机器上,生成各自的经过认证的key文件
[用户名@主机 ~]$ mkdir .ssh
[用户名@主机~]$ chmod 755 .ssh
[用户名@主机~]$ /usr/bin/ssh-keygen -t rsa
[用户名@从机 ~]$ mkdir .ssh
[用户名@从机~]$ chmod 755 .ssh
[用户名@从机~]$ /usr/bin/ssh-keygen -t rsa
1.4.2 将所有的key文件汇总到一个总的认证文件中
[用户名@主机~]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
[用户名@主机~]$ ssh 用户名@主机主机2 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
1.4.3 分发认证文件
[用户名@主机1 ~]$ rcp ~/.ssh/authorized_keys 主机2:~/.ssh/authorized_keys
1.4.4 验证互信
个主机间使用ssh 主机名或ssh 主机IP验证无密登录
2 PostgreSQL安装与流复制配置
2.1 安装并配置postgresql
2.1.1 安装postgresql
3. tar -zxvf postgresql-9.6.7.tar.gz
4. 进入postgresql-9.6.7文件夹
5. 执行./configure --prefix=/app/pgsql
6. 执行make & make install 直到安装完毕
2.1.2 配置postgresql
1. 在用户目录下执行vi .bashrc,添加以下语句:
export PGHOME=/app/pgsql
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
7. 执行source .bashrc使之生效
8. 执行下列命令
sudo cp -r /app/pgsql/bin/* /usr/local/bin/
sudo cp -r /app/pgsql/include/* /usr/local/include/
sudo cp -r /app/pgsql/lib/* /usr/local/lib/
sudo cp -r /app/pgsql/share/* /usr/local/share/
2.2 流复制配置
2.2.1 主机初始化数据库并配置参数
1. 主机使用pg_ctl initdb -D /data/postgresql命令初始化数据库
9. 这时在/data/postgresql文件夹内会初始化出数据库文件,编辑其中的postgresql.conf文件进行参数配置,这里只展示可以达成流复制和pgpool高可用部署的必备配置,更多其他配置可自行调试。
listen_addresses = *
wal_level = hot_standby
max_wal_senders = 1 从机个数,按自己情况填写
wal_keep_segments = 64
10. 编辑pg_hba.conf文件,内容如下
host replication repl 0.0.0.0/0 password
host all all 0.0.0.0/0 password
host all all 0/0 md5
2.2.2 从机初始化及配置
1. 在主机上使用pg_ctl start -D /data/postgresql命令开启主机服务
11. 然后使用psql -U 用户名 -d postgres登录数据库
12. 使用create user repl superuser password ‘repl’;创建带密码超级用户repl
13. 使用alter user 用户名 with password ‘要设置的密码’;命令给用户创建密码
14. 从机上使用pg_basebackup -h 主机IP -U repl -F p -x -P -R -D /data/postgresql/ -l replbackup命令生成从机数据文件。
15. 编辑数据文件夹中的postgresql.conf文件
hot_standby = on
max_standby_archive_delay = 30s
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on
16. 编辑数据文件夹中的recovery.conf文件
添加trigger_file = '/tmp/trigger_file0'
17. 编辑pg_hba.conf文件,修改如下
host replication repl 0.0.0.0/0 password
host all all 0.0.0.0/0 password
host all all 0/0 md5
2.2.3 验证流复制
1. 使用相同命令pg_ctl start -D /data/postgresql开启从机服务
18. 登入主机数据库创建表并插入数据,然后在从机登录查询该表,如果查询得到则流复制配置成功。
3 Pgpool的安装及配置
3.1 Pgpool软件安装与配置
3.1.1 Pgpool安装
1. 使用tar -zxvf pgpool-II-3.7.2.tar.gz命令解压软件包
2. 进入pgpool-II-3.7.2目录执行./configure --prefix=/app/pgpool -with-pgsql=path -with-pgsql=/app/pgpool
3. 执行make & make install 安装软件
4. 进入pgpool-II-3.7.2/src文件夹执行make & make install
5. 进入pgpool-II-3.7.2/src/sql文件夹执行make & make install
6. 执行psql -f insert_lock.sql命令安装pgpool相关函数,非强制,可选安装,提高稳定性。
3.2 Pgpool配置
3.2.1 运行环境配置
1. 进入用户目录执行vi .bashrc,添加下面语句
export PGPOOLHOME=/app/pgpool
export PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin:$PGPOOLHOME/bin
2. 使用source .bashrc使之生效
3.3 Pgpool部署配置
3.3.1 配置pool_hba.conf
1. 进入/app/pgpool/etc文件夹
2. 执行cp pool_hba.conf.sample pool_hba.conf
3. 编辑pool_hba.conf,修改如下
host replication repl 0.0.0.0/0 password
host all all 0.0.0.0/0 password
host all all 0/0 md5
3.3.2 配置pcp.conf
1. 执行cp pcp.conf.sample pcp.conf
2. 执行pg_md5 用户密码,得到密码的MD5加密值
3. 编辑pcp.conf添加如下
用户名:密码MD5加密值,如postgres:6b07583ba8af8e03043a1163147faf6a
19. 使用pg_md5 -p -m -u 用户名 pool_passwd命令,提示输入密码,密码不能输错,否则无法进入数据库,之后会在同一目录生成pool_passwd文件
3.3.3 配置系统命令权限
1. 执行sudo cp /usr/share/bash-completion/completions/arping /usr/sbin
2. 执行sudo chmod u+s /sbin/ifconfig 和sudo chmod u+s /usr/sbin命令
3.3.4 配置pgpool.conf
1. 使用ifconfig命令查看网卡名称,并记录备用
2. 配置主机pgpool.conf
使用命令cp pgool.conf.sample pgpool.conf
编辑pgpool.conf,内容如下
# CONNECTIONS
listen_addresses = '*'
port = 9999
pcp_listen_addresses = '*'
pcp_port = 9898
# - Backend Connection Settings -
backend_hostname0 = '主机'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/data/postgresql '
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_hostname1 = '从机'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/data/postgresql '
backend_flag1 = 'ALLOW_TO_FAILOVER'
# - Authentication -
enable_pool_hba = on
pool_passwd = 'pool_passwd'
# FILE LOCATIONS
pid_file_name = '/app/pgpool/pgpool.pid'
replication_mode = off
load_balance_mode = on
master_slave_mode = on
master_slave_sub_mode = 'stream'
sr_check_period = 5
sr_check_user = 'repl'
sr_check_password = 'repl'
sr_check_database = 'postgres'
#------------------------------------------------------------------------------
# HEALTH CHECK 健康检查
#------------------------------------------------------------------------------
health_check_period = 10 # Health check period
# Disabled (0) by default
health_check_timeout = 20 # Health check timeout
# 0 means no timeout
health_check_user = '数据库用户名' # Health check user
health_check_password = '数据库密码' #数据库密码 # Password for health check user
health_check_database = 'postgres'
#必须设置,否则primary数据库down了,pgpool不知道,不能及时切换。从库流复制还在连接数据,报连接失败。
#只有下次使用pgpool登录时,发现连接不上,然后报错,这时候,才知道挂了,pgpool进行切换。
#主备切换的命令行配置
#------------------------------------------------------------------------------
# FAILOVER AND FAILBACK
#------------------------------------------------------------------------------
failover_command = '/app/pgpool/failover_stream.sh %d %H /tmp/trigger_file0 '
#------------------------------------------------------------------------------
# WATCHDOG
#------------------------------------------------------------------------------
# - Enabling -
use_watchdog = on
# - Watchdog communication Settings -
wd_hostname = '主机' # Host name or IP address of this watchdog
# (change requires restart)
wd_port = 9000 # port number for watchdog service
# (change requires restart)
# - Virtual IP control Setting -
delegate_IP = 'VIP' # delegate IP address
# If this is empty, virtual IP never bring up.
# (change requires restart)
if_cmd_path = '/sbin' # path to the directory where if_up/down_cmd exists
# (change requires restart)
if_up_cmd = 'ifconfig ens33:0 inet $_IP_$ netmask 255.255.255.0'
# startup delegate IP command
# (change requires restart)
# ens33根据现场机器改掉
if_down_cmd = 'ifconfig ens33:0 down' # shutdown delegate IP command
# (change requires restart)
# ens33根据现场机器改掉
# -- heartbeat mode --
wd_heartbeat_port = 9694 # Port number for receiving heartbeat signal
# (change requires restart)
wd_heartbeat_keepalive = 2 # Interval time of sending heartbeat signal (sec)
# (change requires restart)
wd_heartbeat_deadtime = 30 # Deadtime interval for heartbeat signal (sec)
# (change requires restart)
heartbeat_destination0 = '从机' # Host name or IP address of destination 0
# for sending heartbeat signal.
# (change requires restart)
heartbeat_destination_port0 = 9694 # Port number of destination 0 for sending
# heartbeat signal. Usually this is the
# same as wd_heartbeat_port.
# (change requires restart)
heartbeat_device0 = 'ens33' # Name of NIC device (such like 'eth0')
# used for sending/receiving heartbeat
# signal to/from destination 0.
# This works only when this is not empty
# and pgpool has root privilege.
# (change requires restart)
# ens33根据现场机器改掉
# - Other pgpool Connection Settings -
other_pgpool_hostname0 = '从机' #对端
# Host name or IP address to connect to for other pgpool 0
# (change requires restart)
other_pgpool_port0 = 9999 # Port number for othet pgpool 0
# (change requires restart)
other_wd_port0 = 9000 # Port number for othet watchdog 0
# (change requires restart)
20. 配置从机pgpool.conf
# CONNECTIONS listen_addresses = '*' port = 9999 pcp_listen_addresses = '*' pcp_port = 9898 # - Backend Connection Settings - backend_hostname0 = '主机' backend_port0 = 5432 backend_weight0 = 1 backend_data_directory0 = '/data/postgresql' backend_flag0 = 'ALLOW_TO_FAILOVER' backend_hostname1 = '从机' backend_port1 = 5432 backend_weight1 = 1 backend_data_directory1 = '/data/postgresql ' backend_flag1 = 'ALLOW_TO_FAILOVER' # - Authentication - enable_pool_hba = on pool_passwd = 'pool_passwd' # FILE LOCATIONS pid_file_name = '/app/pgpool/pgpool.pid' replication_mode = off load_balance_mode = on master_slave_mode = on master_slave_sub_mode = 'stream' sr_check_period = 5 sr_check_user = 'repl' sr_check_password = 'repl' sr_check_database = 'postgres' #------------------------------------------------------------------------------ # HEALTH CHECK 健康检查 #------------------------------------------------------------------------------ health_check_period = 10 # Health check period # Disabled (0) by default health_check_timeout = 20 # Health check timeout # 0 means no timeout health_check_user = '数据库用户名' # Health check user health_check_password = '数据库密码' #数据库密码 # Password for health check user health_check_database = 'postgres' #必须设置,否则primary数据库down了,pgpool不知道,不能及时切换。从库流复制还在连接数据,报连接失败。 #只有下次使用pgpool登录时,发现连接不上,然后报错,这时候,才知道挂了,pgpool进行切换。 #主备切换的命令行配置 #------------------------------------------------------------------------------ # FAILOVER AND FAILBACK #------------------------------------------------------------------------------ failover_command = '/app/pgpool/failover_stream.sh %d %H /tmp/trigger_file0 ' #------------------------------------------------------------------------------ # WATCHDOG #----------------------------------------------------------------------------- # - Enabling - use_watchdog = on # - Watchdog communication Settings - wd_hostname = '从机' #本端 # Host name or IP address of this watchdog # (change requires restart) wd_port = 9000 # port number for watchdog service # (change requires restart) # - Virtual IP control Setting - delegate_IP = 'VIP' # delegate IP address # If this is empty, virtual IP never bring up. # (change requires restart) if_cmd_path = '/sbin' # path to the directory where if_up/down_cmd exists # (change requires restart) if_up_cmd = 'ifconfig ens33:0 inet $_IP_$ netmask 255.255.255.0' # startup delegate IP command # (change requires restart) # ens33根据现场机器改掉 if_down_cmd = 'ifconfig ens33:0 down' # shutdown delegate IP command # (change requires restart) # ens33根据现场机器改掉 # -- heartbeat mode -- wd_heartbeat_port = 9694 # Port number for receiving heartbeat signal # (change requires restart) wd_heartbeat_keepalive = 2 # Interval time of sending heartbeat signal (sec) # (change requires restart) wd_heartbeat_deadtime = 30 # Deadtime interval for heartbeat signal (sec) # (change requires restart) heartbeat_destination0 = '主机' #对端 # Host name or IP address of destination 0 # for sending heartbeat signal. # (change requires restart) heartbeat_destination_port0 = 9694 # Port number of destination 0 for sending # heartbeat signal. Usually this is the # same as wd_heartbeat_port. # (change requires restart) heartbeat_device0 = 'ens33' # Name of NIC device (such like 'eth0') # used for sending/receiving heartbeat # signal to/from destination 0. # This works only when this is not empty # and pgpool has root privilege. # (change requires restart) # ens33根据现场机器改掉 # - Other pgpool Connection Settings - other_pgpool_hostname0 = '主机' #对端 # Host name or IP address to connect to for other pgpool 0 # (change requires restart) other_pgpool_port0 = 9999 # Port number for othet pgpool 0 # (change requires restart) other_wd_port0 = 9000 # Port number for othet watchdog 0 # (change requires restart) |
21. 进入/app/pgpool文件夹,使用touch failover_stream.sh 创建failover_stream.sh文件
22. 编辑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
# Create the trigger file. /usr/bin/ssh -T $new_master /bin/touch $trigger_file
exit 0; |
23. 使用sudo chown –R 用户名:用户名 /app/pgpool
sudo chmod 777 /app/pgpool/failover_stream.sh
命令给与执行权限。
4 Pgpool集群管理
4.1 启动集群
4.1.1 数据库启动的命令
在主机,从机上先后执行下面命令,开启postgresql数据库
pg_ctl start -D 数据文件夹路径
4.1.2 Pgpool启动的命令
在主机,从机上先后执行下面命令,开启pgpool
pgpool -n -d -D > /app/pgpool/pgpool.log 2>&1 & (该命令为silent启动方式)
pgpool -n& (该命令会显示log信息)
4.1.3 快速停止pgpool的命令
pgpool -m fast stop
psql -d postgres -p 9999 -h ip地址
4.1.4 集群启动后查看状态
使用数据库客户端用虚拟VIP登录数据库,执行show pool_nodes;命令
node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay
---------+----------+------+--------+-----------+---------+------------+-------------------+-------------------
0 | master | 5432 | up | 0.500000 | primary | 0 | false | 0
1 | slave | 5432 | up | 0.500000 | standby | 0 | true | 0
(2 rows)
发现当前主备节点都是正常的up状态