基於Pgpool-II4.1.0搭建PostgreSQL11集羣node
Pgpool介紹git
pgpool相關介紹不在該文章中,須要者能夠參考個人另外一篇文章。傳送門:pgpool介紹github
環境準備sql
本次使用三臺CentOS7.3的服務器來搭建集羣,首先在三臺服務器上部署PostgreSQL11.6版本的數據庫,部署教程你們能夠網上自行查找。具體規劃以下:
數據庫
架構圖參考官網:
後端
搭建配置服務器
01服務器配置架構
故障轉移、在線恢復時須要ssh到其它服務器執行命令,故須要配置服務器之間無密碼ssh登陸(如服務器之間已經配置ssh能夠跳過該節,可是須要修改failover.sh等腳本)負載均衡
[all servers]# cd ~/.ssh [all servers]# ssh-keygen -t rsa -f id_rsa_pgpool [all servers]# ssh-copy-id -i id_rsa_pgpool.pub postgres@server1 [all servers]# ssh-copy-id -i id_rsa_pgpool.pub postgres@server2 [all servers]# ssh-copy-id -i id_rsa_pgpool.pub postgres@server3 [all servers]# su postgres [all servers]$ cd ~/.ssh [all servers]$ ssh-keygen -t rsa -f id_rsa_pgpool [all servers]$ ssh-copy-id -i id_rsa_pgpool.pub postgres@server1 [all servers]$ ssh-copy-id -i id_rsa_pgpool.pub postgres@server2 [all servers]$ ssh-copy-id -i id_rsa_pgpool.pub postgres@server3
02pgpool安裝ssh
pgpool安裝不在該文章中介紹,須要者能夠參考我另外一篇文章。傳送門:pgpool安裝。
03PostgreSQL數據庫配置
- WAL歸檔,須要WAL歸檔的可自行配置,該示例暫時未使用WAL歸檔
[all servers]# su - postgres [all servers]$ mkdir /var/lib/pgsql/archivedir ## postgres.conf 配置 archive_mode = on archive_command = 'cp "%p" "/var/lib/pgsql/archivedir/%f"'
這兒有一個歸檔的腳本,使用該腳本能夠自行修改保留多少天歸檔 pg_archive.sh
- postgres.conf配置,該配置僅在主節點配置,從節點使用pgpool的在線恢復功能配置
listen_addresses = '*' port = 5432 max_wal_senders = 10 max_replication_slots = 10 wal_level = replica hot_standby = on wal_log_hints = on
- 數據庫用戶以及密碼配置
[server1]# psql -U postgres -p 5432 postgres=# CREATE ROLE pgpool WITH LOGIN PASSWORD 'pgpool'; postgres=# CREATE ROLE repl WITH REPLICATION LOGIN PASSWORD 'repl'; ## 用於SHOW POOL_NODES 展現 "replication_state" and "replication_sync_state" postgres=# GRANT pg_monitor TO pgpool;
- pg_hba.con配置,添加用戶驗證配置,將repl複製用戶添加到pg_hba中。此文檔中全部密碼使用md5驗證。
host all all 0.0.0.0/0 md5 host replication repl 0.0.0.0/0 md5
- 配置.pgpass文件用於無密碼操做
因爲在故障轉移、在線恢復時使用腳本進行操做,腳本中使用pg_basebakup、pg_rewind等命令,因此須要配置無密碼操做
在postgres用戶的home目錄下建立.pgpass文件,而且文件權限爲600。
[all servers]# su - postgres [all servers]$ vi ~/.pgpass ## 格式爲:hostname:port:database:username:password server1:5432:replication:repl:<repl user password> server2:5432:replication:repl:<repl user passowrd> server3:5432:replication:repl:<repl user passowrd> server1:5432:postgres:postgres:<postgres user passowrd> server2:5432:postgres:postgres:<postgres user passowrd> server3:5432:postgres:postgres:<postgres user passowrd> [all servers]$ chmod 600 ~/.pgpass
04pgpool配置
一、# cp -p /usr/local/pgpool/etc/pgpool.conf.sample-stream /usr/local/pgpool/etc/pgpool.conf
二、配置PostgreSQL數據庫信息
# - Backend Connection Settings - # 有幾臺PostgreSQL數據庫,配置幾個後端信息,使用後綴名0、一、2…… backend_hostname0 = 'server1' # Host name or IP address to connect to for backend 0 backend_port0 = 5432 # Port number for backend 0 backend_weight0 = 1 # Weight for backend 0 (only in load balancing mode) backend_data_directory0 = '/data/pgsql/sport/' # Data directory for backend 0 # Controls various backend behavior # ALLOW_TO_FAILOVER or DISALLOW_TO_FAILOVER backend_flag0 = 'ALLOW_TO_FAILOVER' backend_hostname1 = 'server2' backend_port1 = 5432 backend_weight1 = 1 backend_data_directory1 = '/var/lib/pgsql/11/data' backend_flag1 = 'ALLOW_TO_FAILOVER' backend_hostname2 = 'server3' backend_port2 = 5432 backend_weight2 = 1 backend_data_directory2 = '/var/lib/pgsql/11/data' backend_flag2 = 'ALLOW_TO_FAILOVER'
三、基礎配置
listen_addresses = '*' pid_file_name = '/var/run/pgpool/pgpool.pid' # PID file name # Can be specified as relative to the" # location of pgpool.conf file or # as an absolute path # (change requires restart) logdir = '/var/run/pgpool' # Directory of pgPool status file # (change requires restart)
建立pgpool運行須要目錄mkdir -p /var/run/pgpool
四、配置複製延遲檢查
sr_check_user = 'pgpool' ## 自從4.0版本後,若是密碼設置爲的話,pgpool會首先從`pool_passwd`文件中獲取密碼,而後在使用空密碼 sr_check_password = 'pgpool'
五、配置健康檢查
# Health check period # Disabled (0) by default health_check_period = 5 # Health check timeout # 0 means no timeout health_check_timeout = 30 health_check_user = 'pgpool' health_check_password = 'pgpool' health_check_max_retries = 3
六、配置故障轉移
failover_command = '/usr/locla/pgpool/etc/failover.sh %d %h %p %D %m %H %M %P %r %R %N %S' follow_master_command = '/usr/locla/pgpool/etc/follow_master.sh %d %h %p %D %m %H %M %P %r %R'
關於腳本會在後面給出下載地址以及腳本使用說明
# 給腳本設置執行權限,注意:該腳本須要在三臺pgpool服務器中都須要建立 chmod +x /usr/local/pgpoll/etc/{failover.sh,follow_master.sh}
七、配置在線恢復
爲了使用Pgpool-II執行在線恢復,咱們須要配置PostgreSQL用戶名和在線恢復命令recovery_1st_stage。因爲執行在線恢復須要PostgreSQL中的超級用戶特權,所以咱們在recovery_user中指定postgres用戶。而後,咱們在PostgreSQL主服務器(server1)的數據庫目錄中建立recovery_1st_stage和pgpool_remote_start,並添加執行權限。
recovery_user = 'postgres' # Online recovery user recovery_password = 'postgres' # Online recovery password recovery_1st_stage_command = 'recovery_1st_stage'
## 該腳本只須要在數據庫主節點建立,後續使用在線恢復時會複製過去 [server1]# su - postgres [server1]$ vi /data/pgsql/sport/recovery_1st_stage [server1]$ vi /data/pgsql/sport/pgpool_remote_start [server1]$ chmod +x /data/pgsql/sport/{recovery_1st_stage,pgpool_remote_start}
數據庫建立擴展,該擴展是爲了可以執行在線恢復(若是在安裝時已經建立該擴展則能夠跳過該步驟)
[server1]# su - postgres [server1]$ psql template1 -c "CREATE EXTENSION pgpool_recovery"
八、配置客戶端身份驗證
## pgpool.conf中 enable_pool_hba = on
身份驗證文件爲/usr/local/pgpool/etc/pool_hba.conf,配置方式與PostgreSQL基本同樣。(scram-sha-256方式可參考pgpool官網)
host all pgpool 0.0.0.0/0 md5 host all postgres 0.0.0.0/0 md5
cd /usr/local/pgpool/etc ../bin/pg_md5 -p -m -u postgres pool_passwd ../bin/pg_md5 -p -m -u pgpool pool_passwd cat /etc/pgpool-II/pool_passwd # pgpool:AESheq2ZMZjynddMWk5sKP/Rw== # postgres:AESHs/pWL5rtXy2IwuzroHfqg==
九、看門狗配置
use_watchdog = on delegate_IP = '192.168.111.6' ##vip配置 ## 網卡名字須要正確配置 if_up_cmd = '/usr/bin/sudo /sbin/ip addr add $_IP_$/24 dev enp0s8 label enp0s8:0' if_down_cmd = '/usr/bin/sudo /sbin/ip addr del $_IP_$/24 dev enp0s8' arping_cmd = '/usr/bin/sudo /usr/sbin/arping -U $_IP_$ -w 1 -I enp0s8' 配置其它pgpool信息,注意:該配置在三臺服務器不同,只須要配置另外幾臺便可 [server1配置以下] # - Other pgpool Connection Settings - other_pgpool_hostname0 = 'server2' other_pgpool_port0 = 9999 other_wd_port0 = 9000 other_pgpool_hostname1 = 'server3' other_pgpool_port1 = 9999 other_wd_port1 = 9000 heartbeat_destination0 = 'server2' heartbeat_destination_port0 = 9694 heartbeat_device0 = '' heartbeat_destination1 = 'server3' heartbeat_destination_port1 = 9694 heartbeat_device1 = '' [server2配置以下] # - Other pgpool Connection Settings - other_pgpool_hostname0 = 'server1' other_pgpool_port0 = 9999 other_wd_port0 = 9000 other_pgpool_hostname1 = 'server3' other_pgpool_port1 = 9999 other_wd_port1 = 9000 heartbeat_destination0 = 'server1' heartbeat_destination_port0 = 9694 heartbeat_device0 = '' heartbeat_destination1 = 'server3' heartbeat_destination_port1 = 9694 heartbeat_device1 = '' [server3配置以下] # - Other pgpool Connection Settings - other_pgpool_hostname0 = 'server1' other_pgpool_port0 = 9999 other_wd_port0 = 9000 other_pgpool_hostname1 = 'server2' other_pgpool_port1 = 9999 other_wd_port1 = 9000 heartbeat_destination0 = 'server1' heartbeat_destination_port0 = 9694 heartbeat_device0 = '' heartbeat_destination1 = 'server2' heartbeat_destination_port1 = 9694 heartbeat_device1 = ''
十、pcp命令配置
cd /usr/local/pgpool/bin echo 'pgpool:'`pg_md5 pgpool` >> /usr/local/pgpool/etc/pcp.conf
以上pgpool相關配置都配置完成,最終會生成pgpool.conf、pool_hba.conf、pool_passwd、pcp.conf,能夠配置完一臺後,在其它服務器copy配置文件便可。注意:pgpool.conf中看門狗配置須要在其它服務器修改一下。
05相關腳本
此處一共須要四個腳本分別爲:
- failover.sh 目錄爲: /usr/local/pgpool/etc/下
- follow_master.sh 目錄爲: /usr/local/pgpool/etc/下
- recovery_1st_stage 目錄爲:PostgreSQL數據庫數據目錄下
- pgpool_remote_start目錄爲:PostgreSQL數據庫數據目錄下
腳本地址:https://github.com/MrSmallLiu/pgpool
腳本參考於官網,可是稍有改動:
- 建立與刪除複製槽時,若是使用ip地址,則會報錯,由於名字不容許有.,修改:${FAILED_NODE_HOST//./_}
- follow_master.sh腳本中一處bug
# drop replication slot ssh -T -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null postgres@${NEW_MASTER_NODE_HOST} -i ~/.ssh/id_rsa_pgpool " ${PGHOME}/bin/psql -p ${NEW_MASTER_NODE_PORT} -c \"SELECT pg_drop_replication_slot('${FAILED_NODE_HOST//./_}')\" "
注意:
- 注意全部腳本中 PGHOME變量爲PostgreSQL安裝路徑
- 注意全部腳本中ARCHIVEDIR,若是未使用,須要將其註釋,而且修改相應使用地方
- 注意全部腳本中PGPOOL_PATH變量爲pgpool的bin路徑
- 注意recovery_1st_stage腳本中PRIMARY_NODE_HOST變量,該變量取hostname值,能夠先在本身服務器測試一下該命令是否符合預期。不符合者使用hostnamectl set-hostname server1
啓動
後臺運行
/usr/local/pgpool/bin/pgpool
debug運行
/usr/local/pgpool/bin/pgpool -n -d
/usr/local/pgpool/bin/pgpool -n
standby 數據庫啓動
-n 表示在pgpool.conf配置文件中PostgreSQL配置的後面的序號 /usr/local/pgpool/bin/pcp_recovery_node -h 192.168.111.6 -p 9898 -U pgpool -n 1 Password: pcp_recovery_node -- Command Successful /usr/local/pgpool/bin/pcp_recovery_node -h 192.168.111.6 -p 9898 -U pgpool -n 2 Password: pcp_recovery_node -- Command Successful
中止
/usr/local/pgpool/bin/pgpool -m fast stop
測試
使用客戶端鏈接(navicat、pgAdmin等)
psql -h 192.168.111.6 -p 9999 -U passwd:postgres
執行show pool_nodes
負載均衡測試
能夠配置PostgreSQL日誌打印sql語句,使用vip地址鏈接上後測試輸出便可看到查詢會負載到三臺服務器(默認根據鏈接使用負載均衡,須要幾回新建查詢來測試)
自動故障轉移測試
可使用PostgreSQL命令停掉主節點數據庫來測試故障轉移
寫在最後
- 以上配置通過咱們部署測試是能夠正常運行的,可是是後續整理才寫的該篇文章,可能存在遺漏不足的地方。小夥伴在配置過程當中有疑惑、配置後未能正常啓動、有文檔意見均可以與我聯繫。隨時歡迎提給我,而後我進行修改文檔,以幫助更多人。
- 後面抽時間整理一個pgpool的部署流程圖,方便你們理解部署流程。
- 關於配置文件的具體講解以及優化會在後面再寫相關文檔,歡迎持續關注
關於該文章,歡迎各位批評指正
關於做者
GISer
小劉先森
QQ: 1016817543
郵箱:1016817543@qq.com
github:https://github.com/MrSmallLiu (歡迎star)
劉航,國信司南(北京)地理信息技術有限公司,技術經理。負責過多個基於 PostgreSQL 數據庫以及 PostGIS 的大中型項目。公司矢量切片產品(基於 PostgreSQL 數據庫)研發核心人員。