1、环境
主机名 | IP | 名称 | 端口 |
---|---|---|---|
master | 192.168.98.155 | 主节点 | 5432 |
master | 192.168.98.155 | pgpool主节点 | 9999 |
node1 | 192.168.98.156 | 备节点 | 5432 |
node1 | 192.168.98.156 | pgpool备节点 | 9999 |
witness | 192.168.98.157 | VIP节点 | 9999 |
2、前期准备
2.1、关闭防火墙和seLinux(master)
# 1、关闭防火墙和seLinux
setenforce 0
sed -ri '/^[ \t]*SELINUX=/cSELINUX=disabled' /etc/selinux/config
systemctl stop firewalld | service firewalld stop
systemctl disable firewalld
2.2、安装配置postgresql14.9
#1、安装
yum install -y postgresql14-server
#自定义安装配置
#自定义目录
#创建数据存放目录
mkdir -p /app/pg/14/data
#指定postgres用户的权限
chown -R postgres:postgres /app/pgsql/14/
# 配置环境变量
vim /etc/profile
# 软件安装目录
export PGHOME=/usr/pgsql-14/
# PG数据目录
export PGDATA=/app/pgsql/14/data/
export PATH=$PGHOME/bin:$PATH
# 更新环境配置
source /etc/profile
#修改启动文件的启动目录
sudo vim /usr/lib/systemd/system/postgresql-14.service
#Enviroment=PGDATA=/var/lib/pgsql/14/data/
Enviroment=PGDATA=/app/pgsql/14/data/
#重新加载配置文件
systemctl daemon-reload
----------------------------下面部分master配置
#修改配置文件
cd /app/pgsql/14/data
vi postgresql.conf
-
listen_addresses="*"
-
vi pg_hba.conf
-
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host replication all 0.0.0.0/0 md5
host replication repl samenet trust
-
2.3、配置免密登录
# 155~157都需要配置
# 基于root
ssh-keygen -t rsa -f id_rsa_pgpool
ssh-copy-id -i id_rsa_pgpool.pub postgres@master
ssh-copy-id -i id_rsa_pgpool.pub postgres@node1
# 基于postgres
passwd postgres
su - postgres
ssh-keygen -t rsa -f id_rsa_pgpool
ssh-copy-id -i id_rsa_pgpool.pub postgres@master
ssh-copy-id -i id_rsa_pgpool.pub postgres@node1
# 验证
ssh postgres@master -i ~/.ssh/id_rsa_pgpool
3、配置master和node1
3.1、初始化数据库(master)
su - postgres
initdb -D /app/pgsql/14/data/
systemctl restart postgresql-14
3.2、新增数据库角色(master)
su - postgres
psql -h 127.0.0.1 -p 5432 -U postgres postgres
CREATE ROLE repl WITH REPLICATION LOGIN;
CREATE ROLE pgpool WITH LOGIN;
\password postgres;
\password repl;
\password pgpool
GRANT pg_monitor TO pgpool;
3.3、配置密码文件
su - postgres
vim .pgpass
# ip:port:repmgr:repmgr:repmgr
#*:*:repmgr:repmgr:123456
192.168.98.156:5432:replication:repl:123456
192.168.98.155:5432:replication:repl:123456
192.168.98.156:5432:postgres:postgres:123456
192.168.98.155:5432:postgres:postgres:123456
192.168.98.157:9999:postgres:pgpool:123456
192.168.98.157:9999:postgres:postgres:123456
chmod 600 .pgpass
3.4、下载pgpool
yum install -y https://www.pgpool.net/yum/rpms/4.3/redhat/rhel-7-x86_64/pgpool-II-release-4.3-1.noarch.rpm
yum install -y pgpool-II-pg14-*
3.5、配置WAL存放地点(master)
su - postgres
mkdir archivedir
vim /app/pgsql/14/data/postgresql.conf
listen_addresses = '*'
archive_mode = on
archive_command = 'cp "%p" "/var/lib/pgsql/archivedir/%f"'
max_wal_senders = 10
max_replication_slots = 10
wal_level = replica
hot_standby = on
wal_log_hints = on
3.5、配置pool_hba.conf访问
vim /etc/pgpool-II/pool_hba.conf
host all pgpool 0.0.0.0/0 md5
host all postgres 0.0.0.0/0 md5
3.6、启动pgpool
systemctl start pgpool
systemctl enable pgpool
3.7、配置节点名称
echo 0 > /etc/pgpool-II/pgpool_node_id # 主服务器为0 备服务器1为1 以此类推
3.8、配置pgpool.conf
# 集群模式 【流复制】
backend_clustering_mode = 'streaming_replication'
# 通讯
listen_addresses = '*'
port = 9999
# 流复制检查
sr_check_user = 'pgpool'
sr_check_password = ''
# 健康检查
health_check_period = 5
health_check_timeout = 30
health_check_user = 'pgpool'
health_check_password = '123456'
health_check_database = 'postgres'
# 设置PCP可以访问
pcp_listen_addresses = '*'
pcp_port = 9898
# 后端设置【配置IP】
backend_hostname0 = '192.168.98.155'
backend_data_directory0 = '/app/pgsql/14/data/'
backend_port0 = 5432
backend_weight0 = 1
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_application_name0 = "master" #用于显示
backend_hostname1 = '192.168.98.156'
backend_data_directory1 = '/app/pgsql/14/data/'
backend_port1 = 5432
backend_weight1 = 1
backend_flag1 = 'ALLOW_TO_FAILOVER'
backend_application_name1 = "node1" #用于显示
# 故障转移配置【脚本可能需要修改PGHOME】
failover_command = '/etc/pgpool-II/failover.sh %d %h %p %D %m %H %M %P %r %R %N %S'
# follow_primary_command = '/etc/pgpool-II/follow_primary.sh %d %h %p %D %m %H %M %P %r %R'
# follow_primary_command = '/etc/pgpool-II/follow_primary.sh %d %h %p %D %m %H %M %P %r %R' #三个及以上需要配置
# 在线恢复配置【主服务器 创建recovery_1st_stage和pgpool_remote_start,并添加执行权限】
recovery_user = 'postgres'
recovery_password = '123456'
recovery_1st_stage_command = 'recovery_1st_stage'
# 客户端认证配置
enable_pool_hba = on
pool_passwd = '/etc/pgpool-II/pool_passwd'
# 看门狗 VIP 【可能需要配置visudo】【网卡】
use_watchdog = on
delegate_IP = '192.168.98.157'
if_up_cmd = '/usr/bin/sudo /sbin/ip addr add $_IP_$/24 dev ens33 label ens33:0'
if_down_cmd = '/usr/bin/sudo /sbin/ip addr del $_IP_$/24 dev ens33'
arping_cmd = '/usr/bin/sudo /usr/sbin/arping -U $_IP_$ -w 1 -I ens33'
# 看门狗停止事件【脚本内需要配置网卡】
wd_escalation_command = '/etc/pgpool-II/escalation.sh'
# 服务器【配置IP】
hostname0 = '192.168.98.155'
wd_port0 = 9000
pgpool_port0 = 9999
hostname1 = '192.168.98.156'
wd_port1 = 9000
pgpool_port1 = 9999
# 偶数节点需要打开
enable_consensus_with_half_votes = on
# 生命检查
wd_lifecheck_method = 'heartbeat'
wd_interval = 10
heartbeat_hostname0 = '192.168.98.155'
heartbeat_port0 = 9694
heartbeat_device0 = 'ens33'
heartbeat_hostname1 = '192.168.98.156'
heartbeat_port1 = 9694
heartbeat_device1 = 'ens33'
wd_heartbeat_keepalive = 2
wd_heartbeat_deadtime = 30
# 日志
log_destination = 'stderr'
logging_collector = on
log_directory = '/var/log/pgpool_log'
log_filename = 'pgpool-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 10MB
#
pid_file_name = '/run/pgpool/pgpool.pid'
3.9、复制需要的配置文件
cp -p /etc/pgpool-II/failover.sh{.sample,}
cp -p /etc/pgpool-II/follow_primary.sh{.sample,}
cp escalation.sh.sample escalation.sh
3.10、配置pcp.conf
echo 'pgpool:'`pg_md5 123456` >> /etc/pgpool-II/pcp.conf
echo 'postgres:'`pg_md5 123456` >> /etc/pgpool-II/pcp.conf
su - postgres
echo 'localhost:9898:pgpool:123456'> ~/.pcppass
chmod 600 .pcppass
3.11、故障恢复指令
cp -p /etc/pgpool-II/recovery_1st_stage.sample /home/pgsql/14/data/recovery_1st_stage
cp -p /etc/pgpool-II/pgpool_remote_start.sample /home/pgsql/14/data/pgpool_remote_start
chown -R postgres:postgres /home/pgsql/14/data/
3.12、使用在线恢复功能,需要 的功能[仅主库执行]
su - postgres
psql template1 -c "CREATE EXTENSION pgpool_recovery"
3.13、为pgpool和postgres生成密码
echo '123456' > .pgpoolkey
chmod 600 .pgpoolkey
pg_enc -m -k .pgpoolkey -u pgpool -p
pg_enc -m -k .pgpoolkey -u postgres -p
cat /etc/pgpool-II/pool_passwd
pgpool:AESKALNNNewCM9MRmlYh5NYqw==
postgres:AESKALNNNewCM9MRmlYh5NYqw==
3.14、配置escalation.sh
vim /etc/pgpool-II/escalation.sh
PGPOOLS=(master node1)
VIP=192.168.98.157
DEVICE=eth0
3.15、配置后hosts
192.168.98.155 master
192.168.98.156 node1
192.168.98.157 vip
3.16、配置/etc/sysconfig/pgpool
OPTS=" -D -n"
3.17、启动pgpool
systemctl start pgpool.service
systemctl stop pgpool.service
systemctl restart pgpool.service
3.18、查看节点信息
psql -h 192.168.98.157 -p 9999 -U pgpool postgres -c "show pool_nodes"
3.19、查看看门狗信息
pcp_watchdog_info -h 192.168.98.157 -p 9898 -U pgpool
3.20、从库初始化,主库恢复
1.从主库恢复数据到从库,从库不要运行,也不需要初始化
pcp_recovery_node -h 192.168.98.157 -p 9898 -U pgpool -n 1
若执行失败,多半是权限问题,可以使用脚本测试一下
./recovery_1st_stage '/app/pgsql/14/data/' '192.168.98.156' '/app/pgsql/14/data/' '5432' 1 '5432' '192.168.98.155'
/app/pgsql/14/data/recovery_1st_stage /app/pgsql/14/data 192.168.98.156 /app/pgsql/14/data/ 5432 1 5432 192.168.98.155
3.21、附 添加插槽
su - postgres
psql
#查询指令:
SELECT slot_name, slot_type, active FROM pg_replication_slots;
#添加指令:
SELECT * FROM pg_create_physical_replication_slot('插槽名称');
插槽名称在从库的数据目录下的 myrecovery.conf
primary_slot_name = '192_168_98_156'
3.22、附 主库恢复后,恢复数据到主库
pcp_recovery_node -h 192.168.98.157 -p 9898 -U pgpool -n 0
3.23、附
其实从服务器和主服务器配置差不多,只需要将主服务器的配置文件拷贝过去即可。
可以使用scp来拷贝
scp xxx root@备服务器:/目录