本文建立在數據庫主從復制(Master-Slave)的基礎上。
Master:10.19.100.2 port:5432 數據庫版本PostgreSQL-10.3
Slave:10.19.100.3 port:5432 數據庫版本PostgreSQL-10.3
pgpool-II使用虛擬IP:10.19.100.4 軟件版本 pgpool-II 3.7.2
一 pgpool中間件及HA簡介
在沒有中間件的情況下,在Master數據庫宕機,需要手工提升Standby為Master,還需要應用主動把連接切換到新Master庫上才能保證整個系統能繼續對外提供服務。為了實現自動切換,增加系統可靠性,這里可以考慮pgpool中間件。pgpool可以檢測數據庫集群中的Master節點是否存活,在Master節點失效時可以自動提升Standby庫,並且pgpool本身可以做高可用部署,搭建多個pgpool,使用vip在多個pgpool上漂移。
pgpool-II 3.7對原來舊版本的幾個影響用戶使用的問題進行了fix,增加了新的pgpool主從模式和參數設置,通過pgpool主動切換主備后,備庫如果沒有問題,也可以直接加入集群了(實測pg 9.6.8也可用)。而之前版本的pgpool,脫離集群的節點無法重新加入進去。
注意1:如果需要輸出pgpool日志,需要設置服務器的selinux為disable。且做以下修改
# vi /etc/rsyslog.conf local0.* /PostgreSQL/pgpool/pgpool.log
# /etc/init.d/rsyslog restart
注意2:通過pgpool登錄數據庫目前僅支持數據庫白名單pg_hba.conf中的password驗證方式。
二 pgpool安裝
2.1 編譯安裝軟
# tar zxvf pgpool-II-3.7.2.tar.gz # cd pgpool-II-3.7.2 # ./configure --prefix=/PostgreSQL/pgpool # make # make install
2.2 配置pgpool管理用戶和密碼
Pgpool管理員用戶密碼位於其配置文件pcp.conf中,手工編輯即可。其中密碼需要使用pg_md5加密。
# /PostgreSQL/pgpool/bin/pg_md5 123456 e10adc3949ba59abbe56e057f20f883e # vi /PostgreSQL/pgpool/etc/pcp.conf postgres:e10adc3949ba59abbe56e057f20f883e
2.3 創建Master/Standby切換腳本
# vi /PostgreSQL/pgpool/failover.sh #! /bin/sh new_master=$1 pathdir=$2 /usr/bin/ssh -T $new_master /PostgreSQL/10/bin/pg_ctl promote -D $pathdir exit 0;
切換腳本權限
# chmod 755 /PostgreSQL/pgpool/failover.sh
2.4 軟件權限
# chown -r postgres:postgres /PostgreSQL/pgpool
三 配置pgpool
3.1配置master節點的pgpool
pgpool沒有必要和數據庫部署在同一台服務器上,我這里為了配置方便pgpool master節點和master庫放在同一台服務器上。
在Master節點上安裝pgpool
安裝方法見第一節。
修改配置文件pgpool.conf
Pgpool的etc目錄下自帶多種配置文件范例,我這里使用最基礎的版本pgpool.conf.sample進行修改,僅為數據庫高可用考慮,兩套pgpool間為主從關系。如果需要負載均衡等其他功能,請詳細閱讀pgpool.conf.sample內的說明后進行配置。
$ cd /PostgreSQL/pgpool/etc
$ cp pgpool.conf.sample pgpool.conf
$ vi pgpool.conf
這里單獨列出要修改的內容:
listen_addresses = '*' backend_hostname0 = '10.19.100.2' backend_port0 = 5432 backend_weight0 = 1 backend_data_directory0 = '/PostgreSQL/10/data' backend_flag0 = 'ALLOW_TO_FAILOVER' backend_hostname1 = '10.19.100.3' backend_port1 = 5432 backend_weight1 = 1 backend_data_directory1 = '/PostgreSQL/10/data' backend_flag1 = 'ALLOW_TO_FAILOVER' log_destination = 'syslog' pid_file_name = '/PostgreSQL/pgpool/pgpool.pid' logdir = '/PostgreSQL/pgpool' master_slave_mode = on sr_check_user = 'postgres' sr_check_password = '123456' sr_check_database = 'postgres' health_check_user = 'postgres' health_check_password = '123456' health_check_database = 'postgres' failover_command = '/PostgreSQL/pgpool/failover.sh %H %R' use_watchdog = on wd_hostname = '10.19.100.2' delegate_IP = '10.19.100.4' heartbeat_destination0 = '10.19.100.3' wd_lifecheck_dbname = 'postgres' wd_lifecheck_user = 'postgres' wd_lifecheck_password = '123456' other_pgpool_hostname0 = '10.19.100.3' other_pgpool_port0 = 9999 other_wd_port0 = 9000
ifconfig權限
如果需要非root用戶啟動Pgpool時能啟動虛擬IP,需要配置該權限
$ su -
Password:
# chmod u+s /sbin/ip
ssh信任
$ su - postgres $ ssh-keygen $ ssh-copy-id postgres@10.19.100.3 $ ssh-copy-id postgres@10.19.100.2
3.2配置Standby節點的pgpool
在Standby節點上安裝pgpool
安裝方法見第一節。
修改配置文件pgpool.conf
修改內容和Master節點基本相同,這里僅列出不同的部分
wd_hostname = '10.19.100.3' heartbeat_destination0 = '10.19.100.2' other_pgpool_hostname0 = '10.19.100.2'
ifconfig權限
如果需要非root用戶啟動pgpool時能啟動虛擬IP,需要配置該權限
$ su -
Password:
# chmod u+s /sbin/ip
ssh信任
$ su - postgres $ ssh-keygen $ ssh-copy-id postgres@10.19.100.2 $ ssh-copy-id postgres@10.19.100.3
四 啟動pgpool
啟動Master節點的pgpool
$ /PostgreSQL/pgpool/bin/pgpool -f /PostgreSQL/pgpool/etc/pgpool.conf
啟動后可以觀察到虛擬IP生效
$ ifconfig eth0:0 eth0:0 Link encap:Ethernet HWaddr 08:00:27:56:B5:91 inet addr:10.19.100.4 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
啟動Standby節點的pgpool
$ /PostgreSQL/pgpool/bin/pgpool -f /PostgreSQL/pgpool/etc/pgpool.conf
五 使用pgpool連接數據庫
通過pgpool的虛擬IP和端口即可連接數據庫,整個過程對用戶透明,不需要更改任何連接工具:
$ psql -U postgres -d postgres -h 10.19.100.2 -p 9999 Password for user postgres: psql.bin (10.3) Type "help" for help. postgres=#
可以通過命令看到數據庫各個節點狀態
postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replic ation_delay ---------+-------------+------+--------+-----------+---------+------------+-------------------+------- ------------ 0 | 10.19.100.2 | 5432 | up | 0.500000 | primary | 0 |false | 0 1 | 10.19.100.3 | 5432 | up | 0.500000 | standby | 0 | true | 0 (2 rows)
六 pgpool切換
這里把10.19.100.2拔電,模擬Master節點宕機,數據庫Master節點和pgpool Master節點全部失去的情況。
Master節點下電后在Standby節點服務器上可以看到:
虛擬IP發生漂移
$ ifconfig eth0:0 eth0:0 Link encap:Ethernet HWaddr 08:00:27:56:B5:91 inet addr:10.19.100.4 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Standby庫變為Master庫
postgres=# show pool_nodes; node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replic ation_delay ---------+-------------+------+--------+-----------+---------+------------+-------------------+------- ------------ 0 | 10.19.100.2 | 5432 | down | 0.500000 | standby| 0 | true | 0 1 | 10.19.100.3 | 5432 | up | 0.500000 | primary | 0 | false | 0 (2 rows)
我這里因為只是模擬故障,因此重啟10.19.100.2,將主庫的recovery.done文件重命名為recovery.conf后,重新啟動主庫即可重新恢復流復制。流復制恢復后使用下面的命令把節點重新加入pgpool集群。
$ /PostgreSQL/pgpool/bin/pcp_attach_node -h 10.19.100.4 -p 9898 -U postgres -n 1 Password: pcp_attach_node -- Command Successful