4、pgpool-II 流复制模式
一、架构图
- PostgreSQL 流复制是基于 wal 日志复制。主库产生 wal 日志并发送给备库;备库接收 wal 日志记录;并重放这些 wal 日志。从而达到主备库数据同步。备库只读。
- 复制级别是实例级
- 在这个模式下:Pgpool 充当连接池作用;
- 读写查询智能分发;可以实现负载均衡;这是其他的HA软件不具有的
二、配置示例
本文是为后续文章做铺垫
Pgpool-II 流复制示例环境
|角色|ip|端口|数据目录|
|:----😐:----|:----😐:----|:----😐:----|:----😐:----|
|pgpool|192.168.1.221|9999| |
|primary|192.168.1.221|6000|/data/postgres/data|
|standby|192.168.1.221|6001|/data/postgres/data1|
1、搭建流复制
#1、搭建备库
[postgres@node3 data1]$ pg_basebackup -F p -R --progress -D /data1/postgres/data1 -h 192.168.1.221 -p 6000 -U replica
Password:
66660/66660 kB (100%), 2/2 tablespaces
#2、启动备库
# 由于在同一台服务器上搭建;需要修改端口pgport:6001
[postgres@node3 data1]$ pg_ctl start -D /data1/postgres/data1
#3、查看是否搭建成功
[postgres@node3 ~]$ psql
psql (12.2)
Type "help" for help.
postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid | 4072
usesysid | 73745
usename | replica
application_name | walreceiver
client_addr | 192.168.1.221
client_hostname |
client_port | 52720
backend_start | 2020-11-17 11:28:20.906093+08
backend_xmin |
state | streaming
sent_lsn | 0/1A000148
write_lsn | 0/1A000148
flush_lsn | 0/1A000148
replay_lsn | 0/1A000148
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
reply_time | 2020-11-17 11:29:11.113346+08
2、配置 pgpool.conf 文件
#1、 复制配置文件
$ cp pgpool.conf.sample-stream pgpool.conf
#2、修改pgpool.conf文件内容
listen_addresses = '*'
#配置后端信息
backend_hostname0 = '192.168.1.221'
backend_port0 = 6000
backend_weight0 = 1
backend_data_directory0 = '/data1/postgres/data'
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_application_name0 = 'server0'
backend_hostname1 = '192.168.1.221'
backend_port1 = 6001
backend_weight1 = 1
backend_data_directory1 = '/data1/postgres/data1'
backend_flag1 = 'ALLOW_TO_FAILOVER'
backend_application_name1 = 'server1'
#管理密码文件
pool_passwd = 'pool_passwd'
#pid文件存放炉具
pid_file_name = '/opt/pgpool/pgpool.pid'
# 管理日志存放路径
logdir = '/opt/pgpool'
#目前模式是流复制;非内置复制;所以该参数关闭
replication_mode = off
load_balance_mode = on
# MASTER/SLAVE MODE
master_slave_mode = on
master_slave_sub_mode = 'stream'
sr_check_user = 'pgpool'
sr_check_password = '123456'
3、启动 pgpool
[pgpool@node3 etc]$ pgpool -n > /tmp/pgpool.log &
[1] 1386
[pgpool@node3 etc]$ 2020-11-25 10:32:26: pid 1386: LOG: Backend status file /opt/pgpool/pgpool_status does not exist
2020-11-25 10:32:26: pid 1386: LOG: memory cache initialized
2020-11-25 10:32:26: pid 1386: DETAIL: memcache blocks :64
2020-11-25 10:32:26: pid 1386: LOG: pool_discard_oid_maps: discarded memqcache oid maps
2020-11-25 10:32:26: pid 1386: LOG: Setting up socket for 0.0.0.0:9999
2020-11-25 10:32:26: pid 1386: LOG: Setting up socket for :::9999
2020-11-25 10:32:26: pid 1386: LOG: find_primary_node_repeatedly: waiting for finding a primary node
2020-11-25 10:32:26: pid 1386: LOG: find_primary_node: primary node is 0
2020-11-25 10:32:26: pid 1386: LOG: find_primary_node: standby node is 1
2020-11-25 10:32:26: pid 1386: LOG: pgpool-II successfully started. version 4.1.4 (karasukiboshi)
2020-11-25 10:32:26: pid 1386: LOG: node status[0]: 1
2020-11-25 10:32:26: pid 1386: LOG: node status[1]: 2
2020-11-25 10:32:26: pid 1422: LOG: PCP process: 1422 started
三、参数配置
#1、打开主备模式,设置流复制必须打开
master_slave_mode = on
#2、模式是stream模式;还可以设置slony;
master_slave_sub_mode = 'stream'
#下面参数为了探测后端哪个是主库
# - Streaming -
#3、探测频率10s;0为不探测
sr_check_period = 10
sr_check_user = 'pgpool'
sr_check_password = '123456'
sr_check_database = 'postgres'
# 该参数表明备库延迟多少个字节;读的请求不在发往备库;
delay_threshold = 10000000
四、查看节点信息
pgpool=# show pool_nodes;
node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay | replication_state | replication_sync_state | last_status_change
---------+-----------+------+--------+-----------+---------+------------+-------------------+-------------------+-------------------+------------------------+-------------------
--
0 | localhost | 6000 | up | 0.500000 | primary | 0 | false | 0 | | | 2020-11-25 10:50:0
9
1 | localhost | 6001 | up | 0.500000 | standby | 0 | true | 0 | | | 2020-11-25 10:50:0
9