repmgrd監控與容錯移轉

本章節將介紹在repmgr的基礎上,加入repmgrd監控來實現自動化failover,在某一節點失連後能自動化將standby promote成master。

在設定repmgrd自動failover之前,請先確認所有節點的postgres.conf是否有包含以下設定,若沒有,加入後記得重新啟動postgresql服務:

shared_preload_libraries = 'repmgr_funcs'

並在所有節點的repmgr.conf中加入以下設定:

# 是否自動failover,預設為manual
failover=automatic

# 需要promote時所執行的command,只會被執行在standby
promote_command='repmgr standby promote -f /etc/repmgr.conf --log-to-file'

# 當節點有兩個以上時才會執行follow
follow_command='repmgr standby follow -f /etc/repmgr.conf --log-to-file'

# 多久確認狀態,預設為2秒,建議稍微拉長一點避免因網路或主機太忙誤判導致亂拉master
monitor_interval_secs=5

# 若master在timeout時間內沒有回應,standby將會執行promote
master_response_timeout=60

repmgrd執行上非常簡單,請在所有standby節點執行該服務,master則是可有可無,因為當master節點掛掉時,實際執行promote的還是standby,master本來就沒有他出場的機會。

postgres@node1 repmgrd -m --verbose
[2017-11-01 10:57:25] [NOTICE] looking for configuration file in current directory
[2017-11-01 10:57:25] [NOTICE] looking for configuration file in /etc
[2017-11-01 10:57:25] [NOTICE] configuration file found at: /etc/repmgr.conf
[2017-11-01 10:57:25] [NOTICE] Redirecting logging output to '/var/log/repmgr/repmgr.log'

-m為monitor參數,加上該參數後,可在repmgr_dbc.repl_status拉到repmgrd即時監控資料。

repmgr=# SELECT * FROM repmgr_dbc.repl_status;
 primary_node | standby_node | standby_name | node_type | active |       last_monitor_time       | last_wal_primary_location | last_wal_standby_location | replication_lag | replication_time_lag | apply_lag | communication_time_lag 
--------------+--------------+--------------+-----------+--------+-------------------------------+---------------------------+---------------------------+-----------------+----------------------+-----------+------------------------
            1 |            2 | node2        | standby   | t      | 2017-11-01 11:12:13.837996+08 | 0/E02FC40                 | 0/E02FC40                 | 0 bytes         | 00:00:06.446686      | 0 bytes   | 
(1 row)

repmgrd監控cluster的實際情況,可在repmgr log中看到,請根據實際情況,自行調整監控參數。另外建議配合-d/daemonize 以及 -p/pidfile選項,結合systemd並讓其自動於開機時啟動,後面章節會再介紹該如何與systemd整合。

results matching ""

    No results matching ""