PostgreSQL叢集管理(一)——repmgr
作者:徐浩
近10年PostgreSQL/MySQL/Oracle/資料庫乙方運維經驗;美創科技運維負責人、資深DBA、資深架構師;中國開源軟體推進聯盟中國PG分會認證講師;美創科技PGCA/PGCE/PGCM認證培訓講師;Oracle OCM、Oracle OCP、MySQL OCP、認證培訓講師;書籍《DBA攻堅指南:左手Oracle,右手MySQL》作者。
repmgr是一個開源工具套件,用於管理 PostgreSQL 伺服器叢集中的複製和故障轉移。它通過設定備用伺服器、監控複製和執行管理任務(例如故障轉移或手動切換操作)的工具增強了 PostgreSQL 的內建熱備用功能。
PostgreSQL在9.0後引入了流複製架構,並且支援hot standby特性,並且在往後的幾個版本中不斷完善和增強流複製架構,repmgr為 PostgreSQL 的流複製機制提供了高階支援,因為它們是在 9.0 中引入的。當前的repmgr系列, repmgr 5,支援從 PostgreSQL 9.3 引入的複製功能的最新發展,例如級聯複製、時間線切換和通過複製協議進行的基本備份
repmgr作為一個開源工具,旨在用於靈活、便捷地管理PostgreSQL叢集
老樣子,我們先來安裝repmgr軟體
我們從https://dl.2ndquadrant.com/中找到對應PostgreSQL版本的RPM儲存庫,下載並安裝,這裡我們以PostgreSQL11為例
curl https://dl.2ndquadrant.com/default/release/get/11/rpm | sudo bash
驗證儲存庫是否已安裝sudo yum repolist,輸出應該包含以下兩個條目
2ndquadrant-dl-default-release-pg11/7/x86_64 2ndQuadrant packages (PG11) for 7 - x86_64 18
2ndquadrant-dl-default-release-pg11-debug/7/x86_64 2ndQuadrant packages (PG11) for 7 - x86_64 - Debug 8
使用yum安裝對應PostgreSQL版本的repmgr版本
sudo yum install repmgr11
當然地,我們需要在兩臺伺服器上都安裝PostgreSQL軟體和repmgr軟體,都完成安裝後,開始配置PostgreSQL
配置postgresql.conf檔案
新增或修改以下選項
max_wal_senders = 10
max_replication_slots = 10
wal_level = 'hot_standby'
hot_standby = on
archive_mode = on
archive_command = '/bin/true'
啟動資料庫,在資料庫中建立repmgr使用者和repmgr資料庫,並進入該資料庫建立repmgr模式,將模式新增到search path中
ALTER USER repmgr SET search_path TO repmgr, "$user", public;
配置ph_hba.conf白名單檔案,允許repmgr有連線訪問和複製的許可權
local replication repmgr trust
host replication repmgr 127.0.0.1/32 trust
host replication repmgr 192.168.22.129/24 trust
local repmgr repmgr trust
host repmgr repmgr 127.0.0.1/32 trust
host repmgr repmgr 192.168.22.129/24 trust
測試一下從節點端能否連通主節點
psql 'host=192.168.22.128 user=repmgr dbname=repmgr connect_timeout=2'
接下來配置repmgr
在/etc/目錄下編輯repmgr.conf
新增以下
node_id=1
node_name='node1'
conninfo='host=192.168.22.128 user=repmgr dbname=repmgr password=repmgr connect_timeout=2'
data_directory='/pgdata/data/' #對應的PG資料目錄
註冊主節點服務
$ repmgr -f /etc/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
註冊好後,我們來檢視一下叢集狀態
$ repmgr -f /etc/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------------
1 | node1 | primary | * running | | default | 100 | 1 | host=192.168.22.128 user=repmgr dbname=repmgr password=repmgr connect_timeout=2
可以看到已經有主節點註冊進來了
緊接著在備端伺服器上註冊從節點服務
在備端/etc/目錄下編輯repmgr.conf,新增以下
node_id=2
node_name='node2'
conninfo='host=192.168.22.129 user=repmgr dbname=repmgr password=repmgr connect_timeout=2'
data_directory='/pgdata/dataano' #選擇一個空目錄,否則會覆蓋原有資料目錄
使用--dry-run引數嘗試克隆備用伺服器
repmgr -h 192.168.22.128 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
NOTICE: destination directory "/pgdata/dataano" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.22.128 user=repmgr dbname=repmgr
DETAIL: current installation size is 31 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: all prerequisites for "standby clone" are met
沒有問題,去掉--dry-run引數再執行命令
repmgr -h 192.168.22.128 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
好了,備伺服器成功搭建,我們直接啟動服務
$ pg_ctl -D $PGDATA start
服務啟動以後,我們去主節點檢視是否建立了流複製模式,可以看到主備已經處於同步狀態
我們可以手動的使用repmgr進行主備切換,我們在備節點執行,使得備節點切換成為主節點
$ repmgr standby switchover -f /etc/repmgr.conf --siblings-follow
…
NOTICE: executing STANDBY FOLLOW on 1 of 1 siblings
INFO: STANDBY FOLLOW successfully executed on all reachable sibling nodes
NOTICE: switchover was successful
DETAIL: node "192.168.22.129" is now primary and node "192.168.22.128" is attached as standby
NOTICE: STANDBY SWITCHOVER has completed successfully
檢視叢集切換狀態
$ repmgr -f /etc/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------------
1 | node1 | standby | * running | | default | 100 | 1 | host=192.168.22.128 user=repmgr dbname=repmgr password=repmgr connect_timeout=2
2 | node2 | primary | * running | | default | 100 | 1 | host=192.168.22.128 user=repmgr dbname=repmgr password=repmgr connect_timeout=2
PostgreSQL的高可用方案,基本上沒有原生的,大多依靠第三方的工具來實現,repmgr來自第二象限公司,其產品的穩定性和支援度也是不錯的,非常適合被考慮用來做生產上的高可用方案。
新聞|Babelfish使PostgreSQL直接相容SQL Server應用程式
中國PostgreSQL分會入選工信部重點領域人才能力評價機構
更多新聞資訊,行業動態,技術熱點,請關注中國PostgreSQL分會官方網站
https://www.postgresqlchina.com
中國PostgreSQL分會生態產品
https://www.pgfans.cn
中國PostgreSQL分會資源下載站
https://www.postgreshub.cn
點贊、在看、分享、收藏
本文分享自微信公眾號 - 開源軟體聯盟PostgreSQL分會(kaiyuanlianmeng)。
如有侵權,請聯絡 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。
- PostgreSQL資料庫匯入大量資料時如何優化
- PostgreSQL 15:一些新特性
- 《PostgreSQL 開發指南》第 08 篇 備份與恢復
- PostgreSQL 15 邏輯複製模式中所有的表
- 為什麼我喜歡 PostgreSQL - 基礎架構工程師的觀點
- PostgreSQL叢集管理(一)——repmgr
- PostgreSQL 實現定時任務的四種方法
- 湖倉一體2.0:資料分析的終局之選
- 【乾貨分享】淺析VACUUM清理
- 基於知識圖譜的PostgreSQL深度分析
- 使用OGG for PG微服務快速雙向同步RDS資料庫(雙主)
- PostgreSQL體系結構概述
- PostgreSQL 從HOT到PHOT
- PostgreSQL分割槽表使用方法及技巧整理
- PG優化-需求實現-大表group by優化
- 高可用環境下的Patroni & etcd
- PostgreSQL之慢SQL語句
- PGConf.Asia2021亞洲大會最新前瞻,有哪些值得關注的亮點?
- PostgreSQL貢獻者指引
- PostgreSQL的checkpoint簡析