1. 程式人生 > 實用技巧 >4、pgpool-II 流複製模式

4、pgpool-II 流複製模式

一、架構圖

  1. PostgreSQL 流複製是基於 wal 日誌複製。主庫產生 wal 日誌併發送給備庫;備庫接收 wal 日誌記錄;並重放這些 wal 日誌。從而達到主備庫資料同步。備庫只讀。
  2. 複製級別是例項級
  3. 在這個模式下:Pgpool 充當連線池作用;
  4. 讀寫查詢智慧分發;可以實現負載均衡;這是其他的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