作者:顏博 青云科技資料庫研發工程師
目前從事 PostgreSQL 產品開發作業,熱衷于 PostgreSQL 資料庫的學習和研究
上一期我們介紹了 PG 集群復制管理工具 repmgr,能夠輕松的搭建出 PostgreSQL 的高可用集群,在主節點宕機后,挑選備機提升為主節點,繼續提供服務,
本文將詳細介紹 repmgr 構建 PostgreSQL 高可用集群的部署程序,
準備作業
- 集群所有服務器安裝 repmgr 工具
- 主服務器安裝 PostgreSQL 資料庫,初始化完成并正常啟動資料庫(Primary)
| 1 主庫部分
1.1 修改 postgresql.conf 檔案
$ vim postgresql.conf
max_wal_senders = 10
max_replication_slots = 10
wal_level = 'hot_standby'
hot_standby = on
archive_mode = on # repmgr 本身不需要 WAL 檔案歸檔,
archive_command = '/bin/true'
在 PG9.6 之前的版本中,wal_level 允許設定為archive和hot_standby,新版本中,仍然接受這些值,但是它們會被映射成replica,
1.2 創建 repmgr 用戶和庫
為 repmgr 元資料資訊創建 PostgreSQL 超級用戶和資料庫
# su - postgres
$ /usr/lib/postgresql/11/bin/createuser -s repmgr
$ /usr/lib/postgresql/11/bin/createdb repmgr -O repmgr
alter user repmgr with password 'test1234';
1.3 修改 pg_hba.conf 檔案
repmgr 用戶作為 repmgr 工具默認使用的資料庫用戶
$ vim pg_hba.conf
local replication repmgr trust
host replication repmgr 127.0.0.1/32 trust
host replication repmgr 0.0.0.0/0 trust
local repmgr repmgr trust
host repmgr repmgr 127.0.0.1/32 trust
host repmgr repmgr 0.0.0.0/0 trust
repmgr 免密登錄設定
# 修改 pg_hba.conf 檔案后 reload 資料庫生效
$ su - postgres -c "/usr/lib/postgresql/11/bin/pg_ctl reload"
# su postgres
$ vim ~/.pgpass
# 添加以下內容到 ~/.pgpass 檔案,用戶、資料庫和密碼修改為自己的即可
*:*:repmgr:repmgr:test1234
# 修改 ~/.pgpass 檔案權限
chmod 600 ~/.pgpass
1.4 創建 repmgr.conf 檔案
在主服務器上創建一個 repmgr.conf 檔案
node_id、node_name、conninfo 需要與從庫不同
node_id=1 # 節點ID,高可用集群各節點標識
node_name='node1' # 節點名稱,高可用集群各節點名稱,對應集群中 select * from pg_stat_replication; 中查到的 application_name
conninfo='host=192.168.100.2 port=5432 user=repmgr dbname=repmgr connect_timeout=2' # 集群中的所有服務器都必須能夠使用此字串連接到本地節點
data_directory='/data/pgsql/main' # pg資料目錄
replication_user='repmgr' # 流復制資料庫用戶,默認使用repmgr
repmgr_bindir='/usr/lib/postgresql/11/bin' # repmgr軟體目錄
pg_bindir='/usr/lib/postgresql/11/bin' # pg軟體目錄
# 日志管理
log_level=INFO
log_file='/data/pglog/repmgr/repmgrd.log' # log 檔案需要提前創建
log_status_interval=10 # 此設定導致 repmgrd 以指定的時間間隔(以秒為單位,默認為 300)發出狀態日志行,描述 repmgrd 的當前狀態,
# 例如: [2021-09-28 17:51:15] [INFO] monitoring primary node "node1" (ID: 1) in normal state
# pg、repmgr服務管理命令
service_start_command='/usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ start -o \'-c config_file=/etc/postgresql/11/main/postgresql.conf\' -l /data/pglog/start.log'
service_stop_command='/usr/lib/postgresql/11/bin/pg_ctl stop'
service_restart_command='/usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ restart -o \'-c config_file=/etc/postgresql/11/main/postgresql.conf\' -l /data/pglog/start.log'
service_reload_command='su - postgres -c \'/usr/lib/postgresql/11/bin/pg_ctl reload\' '
repmgrd_pid_file='/tmp/repmgrd.pid' # repmgrd 運行時的 pid 檔案
repmgrd_service_start_command='/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start'
repmgrd_service_stop_command='kill -9 `cat /tmp/repmgrd.pid`'
# failover設定
failover=automatic
promote_command='/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf standby promote --log-to-file' #當 repmgrd 確定當前節點將成為新的主節點時 ,將在故障轉移情況下執行 promote_command 中定義的程式或腳本
follow_command='/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf standby follow --log-to-file --upstream-node-id=%n' # %n將被替換 repmgrd與新的主節點的ID, 如果沒有提供, repmgr standby follow將嘗試自行確定新的主repmgr standby follow節點,
# 但如果在新主節點提升后原主節點重新上線,則存在導致節點繼續跟隨原主節點的風險 ,
# 高可用引數設定
location='location1' # 定義節點位置的任意字串,在故障轉移期間用于檢查當前主節點的可見性
priority=100 # 節點優先級,選主時可能使用到,(lsn > priority > node_id)
# 0 代表該節點不會被提升為主節點
monitoring_history=yes # 是否將監控資料寫入“monitoring_history”表
reconnect_interval=10 # 故障轉移之前,嘗試重新連接的間隔(以秒為單位)
reconnect_attempts=6 # 故障轉移之前,嘗試重新連接的次數
connection_check_type=ping # ping: repmg 使用PQPing() 方法測驗連接
# connection: 嘗試與節點建立新的連接
# query: 通過現有連接在節點上執行 SQL 陳述句
monitor_interval_secs=5 # 寫入監控資料的間隔
use_replication_slots=true
# failover_validation_command= # %n (node_id), %a (node_name),
# 自定義腳本,以驗證 repmgrd 做出的故障轉移決策
# 此腳本必須回傳退出代碼 0 以指示節點應提升自身為主節點,
本次示例 repmgr.conf 檔案放到以下位置:/etc/postgresql/11/main/repmgr.conf ,
【使用注意】
repmgr.conf不應存盤在 PostgreSQL 資料目錄中,因為在設定或重新初始化 PostgreSQL 服務器時它可能會被覆寫;- 如果將 repmgr 二進制檔案放置在 PostgreSQL 安裝目錄以外的位置,指定
repmgr_bindir以啟用 repmgr 在其他節點上執行操作(例如:repmgr cluster crosscheck),
1.5 注冊主服務器
要使 repmgr 支持復制集群,必須使用 repmgr 注冊主節點(repmgr primary register),這將安裝 repmgr擴展和元資料物件,并為主服務器添加元資料記錄,
# su - postgres -c "/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf primary register"
INFO: connecting to primary database...
NOTICE: attempting to install extension "repmgr"
NOTICE: "repmgr" extension successfully installed
NOTICE: primary node record (ID: 1) registered
- 查看集群資訊
# su - postgres -c "/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf cluster show"
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------------------------
1 | node1 | primary | * running | | default | 100 | 1 | host=127.0.0.1 port=5432 user=repmgr dbname=repmgr connect_timeout=2 password=test1234
- 查看 repmgr 元資料表
repmgr=# SELECT * FROM repmgr.nodes;
node_id | upstream_node_id | active | node_name | type | location | priority | conninfo | repluser | slot_name | config_file
---------+------------------+--------+-----------+---------+----------+----------+---------------------------------------------------------------------------------------------+----------+-----------+-------------------------------------
1 | | t | node1 | primary | default | 100 | host=127.0.0.1 port=5432 user=repmgr dbname=repmgr connect_timeout=2 password=test1234 | repmgr | | /etc/postgresql/11/main/repmgr.conf
- 組態檔發生改變,需要在每個節點執行
$ repmgr primary register --force -f /path/to/repmgr.conf
$ repmgr standby register --force -f /path/to/repmgr.conf
$ repmgr witness register --force -f /path/to/repmgr.conf -h primary_host
【使用注意】
repmgr 不能以 root 用戶運行,
1.6 啟動 repmgrd
1、修改 postgresql.conf 檔案
加入 repmgr 共享庫(在之前的共享庫中在加入 repmgr 即可),
shared_preload_libraries = 'passwordcheck, repmgr'
2、重啟資料庫
/usr/lib/postgresql/11/bin/pg_ctl restart
3、啟動 repmgrd 服務
# 創建日志檔案,repmgrd 的日志檔案需要手動創建
su postgres
mkdir -p /data/pglog/repmgr/
touch /data/pglog/repmgr/repmgrd.log
# 啟動 repmgrd 服務
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start
1.7 repmgrd 日志輪換
為確保當前的 repmgrd 日志檔案(repmgr.conf組態檔中用引數log_file指定的檔案)不會無限增長,請將您的系統配置logrotate為定期輪換它,
vim /etc/logrotate.d/repmgr
/data/pglog/repmgr/repmgrd.log {
missingok
compress
rotate 52
maxsize 100M
weekly
create 0600 postgres postgres
postrotate
/usr/bin/killall -HUP repmgrd
endscript
}
1.8 repmgrd 多載配置
# 1、kill 舊行程
kill -9 `cat /tmp/repmgrd.pid`
# 2、start
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start
| 2 備庫部分
【使用注意】
在備用資料庫上,不要創建 PostgreSQL 實體(即不要執行 initdb 或任何包提供的資料庫創建腳本),但要確保目標資料目錄(以及您希望 PostgreSQL 使用的任何其他目錄)存在并歸其所有 postgres 系統用戶,權限必須設定為 0700 (drwx------),
2.1 創建 repmgr.com 檔案
在備用服務器上創建一個 repmgr.conf 檔案,repmgr 組態檔與主庫相同,注意修改其中的 node_id、node_name、conninfo 為本節點即可,
2.2 檢查備庫是否可克隆
備服務器節點注冊前,不需要對 PostgreSQL 資料庫進行初始化,可通過 repmgr 工具“一鍵式”部署,在對備用服務器進行克隆前,可以使用以下命令測驗是否可以克隆,
使用--dry-run選項來檢查備庫是否可以克隆
$ su - postgres -c "/usr/lib/postgresql/11/bin/repmgr -h 192.168.100.2 -U repmgr -d repmgr -f /etc/postgresql/11/main/repmgr.conf standby clone --dry-run"
NOTICE: destination directory "/data/pgsql/main" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.100.2 user=repmgr dbname=repmgr
DETAIL: current installation size is 38 MB
INFO: "repmgr" extension is installed in database "repmgr"
INFO: replication slot usage not requested; no replication slot will be set up for this standby
INFO: parameter "max_wal_senders" set to 10
NOTICE: checking for available walsenders on the source node (2 required)
INFO: sufficient walsenders available on the source node
DETAIL: 2 required, 10 available
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: required number of replication connections could be made to the source server
DETAIL: 2 replication connections required
NOTICE: standby will attach to upstream node 1
HINT: consider using the -c/--fast-checkpoint option
INFO: would execute:
pg_basebackup -l "repmgr base backup" -D /data/pgsql/main -h 192.168.100.2 -p 5432 -U repmgr -X stream
INFO: all prerequisites for "standby clone" are met
報錯以下內容證明:primary 節點的免密登錄未配置好!
NOTICE: destination directory "/data/pgsql/main" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.100.2 user=repmgr dbname=repmgr
ERROR: connection to database failed
DETAIL:
fe_sendauth: no password supplied
2.3 克隆備庫
$ su - postgres -c "/usr/lib/postgresql/11/bin/repmgr -h 192.168.100.2 -U repmgr -d repmgr -f /etc/postgresql/11/main/repmgr.conf standby clone"
NOTICE: destination directory "/data/pgsql/main" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.100.2 user=repmgr dbname=repmgr
DETAIL: current installation size is 38 MB
INFO: replication slot usage not requested; no replication slot will be set up for this standby
NOTICE: checking for available walsenders on the source node (2 required)
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: checking and correcting permissions on existing directory "/data/pgsql/main"
NOTICE: starting backup (using pg_basebackup)...
HINT: this may take some time; consider using the -c/--fast-checkpoint option
INFO: executing:
pg_basebackup -l "repmgr base backup" -D /data/pgsql/main -h 192.168.100.2 -p 5432 -U repmgr -X stream
NOTICE: standby clone (using pg_basebackup) complete
NOTICE: you can now start your PostgreSQL server
HINT: for example: pg_ctl -D /data/pgsql/main start
HINT: after starting the server, you need to register this standby with "repmgr standby register"
這代表使用 PostgreSQL 的pg_basebackup工具從 192.168.100.2 克隆了PostgreSQL 資料目錄檔案,將自動創建包含從該主服務器開始流式傳輸的正確引數的 recovery.conf 檔案,默認情況下,主資料目錄中的任何組態檔都將復制到備用,通常這些將是 postgresql.conf、postgresql.auto.conf、pg_hba.conf 和 pg_ident.conf,這些可能需要在待機啟動之前進行修改,
2.4 修改組態檔
修改 postgresql.conf、pg_hba.conf 組態檔,配置免密登錄,
2.5 啟動備庫
# su postgres
$ /usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ start -o '-c config_file=/etc/postgresql/11/main/postgresql.conf' -l /data/pglog/start.log
2.6 注冊從庫為備用服務器
# su postgres
$ /usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf --upstream-node-id=1 standby register
2.7 啟動 repmgrd
1、修改 postgresql.conf 檔案,加入repmgr 共享庫
shared_preload_libraries = 'passwordcheck, repmgr'
2、重啟資料庫
/usr/lib/postgresql/11/bin/pg_ctl restart
3、啟動 repmgrd
# 創建日志檔案
su postgres
mkdir -p /data/pglog/repmgr/
touch /data/pglog/repmgr/repmgrd.log
# 啟動 repmgrd 服務
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start
2.8 repmgrd 日志輪換
為確保當前的 repmgrd 日志檔案(repmgr.conf組態檔中用引數log_file指定的檔案)不會無限增長,請將您的系統配置logrotate為定期輪換它,
# vim /etc/logrotate.d/repmgr
/data/pglog/repmgr/repmgrd.log {
missingok
compress
rotate 52
maxsize 100M
weekly
create 0600 postgres postgres
postrotate
/usr/bin/killall -HUP repmgrd
endscript
}
2.9 repmgrd 多載配置
# 1、kill 舊行程
kill -9 `cat /tmp/repmgrd.pid`
# 2、start
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start
| 3 見證服務器(witness)
【使用注意】
- 只有在使用 repmgrd 時,見證服務器才有用;
- 發生故障轉移的情況下,見證服務器提供證據表明是主服務器本身是不可用的,而不是例如不同的物理位置之間的網路分離(防止腦裂問題出現) ;
- 請在與集群主服務器位于同一網段的服務器上設定一個普通 PostgreSQL 實體,并安裝 repmgr、repmgrd,注冊該實體為 witness(repmgr witness register)(見證服務器 Database system identifier 不能與集群主服務器相同),
3.1 啟動節點 postgres 服務
/usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ start
3.2 添加 repmgr.conf 配置
基本配置與主庫相同,保持 node_id、node_name、conninfo 與主庫不同即可,
3.3 啟動 repmgrd
1、修改 postgresql.conf 檔案,加入repmgr 共享庫
shared_preload_libraries = 'passwordcheck, repmgr'
2、重啟資料庫
/usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ restart -o '-c config_file=/etc/postgresql/11/main/postgresql.conf' -l /data/pglog/start.log
3、啟動 repmgrd
# 創建日志檔案
su postgres
mkdir -p /data/pglog/repmgr/
touch /data/pglog/repmgr/repmgrd.log
# 啟動 repmgrd 服務
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start
4、為確保當前的 repmgrd 日志檔案(repmgr.conf組態檔中用引數log_file指定的檔案)不會無限增長,請將您的系統配置logrotate為定期輪換它
# vim /etc/logrotate.d/repmgr
/data/pglog/repmgr/repmgrd.log {
missingok
compress
rotate 52
maxsize 100M
weekly
create 0600 postgres postgres
postrotate
/usr/bin/killall -HUP repmgrd
endscript
}
3.4 注冊 witness
/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf witness register -h 192.168.100.2
| 總結
至此,基于 repmgr 搭建出了一個 PostgreSQL 高可用集群(repmgr 本身不提供虛擬 ip 服務,如果需要虛擬 ip 服務,請使用 keepalived 或其它工具),它具有集群狀態監控、故障檢測、故障轉移等功能,更多 repmgr 高級功能及原理,例如處理網路分裂、主要可見性共識、級聯復制、監控連接數、事件通知等,請參照官方檔案進一步學習,
參考
[1]. repmgr.conf 配置: https://raw.githubusercontent.com/EnterpriseDB/repmgr/master/repmgr.conf.sample
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/374461.html
標籤:其他
下一篇:Oracle資料庫入門(二)
