# 如何使用repmgr設置見證服務器
## 前言
在PostgreSQL高可用架構中,**repmgr**是一個廣泛使用的工具,用于管理復制和自動故障轉移。當使用流復制構建主從架構時,為避免"腦裂"(split-brain)問題,引入**見證服務器(witness server)**是常見解決方案。本文將詳細介紹如何配置repmgr見證服務器。
---
## 一、見證服務器的作用
### 1.1 腦裂問題
當主節點故障時,如果兩個備用節點無法通信,可能同時提升為新主節點,導致數據不一致。
### 1.2 見證服務器功能
- 不存儲實際數據
- 參與仲裁投票
- 確保只有一個主節點被提升
- 最小化部署資源(低配置服務器即可)
---
## 二、環境準備
### 2.1 系統要求
- PostgreSQL 9.5+(推薦12+)
- repmgr 5.0+
- 三臺服務器:
- 主節點:`pg-primary` (192.168.1.10)
- 備用節點:`pg-standby` (192.168.1.11)
- 見證節點:`pg-witness` (192.168.1.12)
### 2.2 安裝軟件包
在所有節點執行:
```bash
# Ubuntu/Debian
sudo apt-get install postgresql-14 repmgr
# RHEL/CentOS
sudo yum install postgresql14-server repmgr14
postgresql.conf
:wal_level = replica
max_wal_senders = 10
hot_standby = on
pg_hba.conf
:host replication repmgr 192.168.1.0/24 trust
host all repmgr 192.168.1.0/24 trust
CREATE ROLE repmgr WITH SUPERUSER LOGIN REPLICATION;
sudo -u postgres repmgr -d postgres -U repmgr \
witness register \
--host=pg-witness \
--repmgr-db=postgres \
--witness-only
創建/etc/repmgr.conf
:
node_id=3
node_name='pg-witness'
conninfo='host=pg-witness user=repmgr dbname=postgres'
data_directory='/var/lib/postgresql/14/main'
witness_only=yes
sudo systemctl enable repmgrd
sudo systemctl start repmgrd
在主節點執行:
repmgr cluster show
應輸出類似:
ID | Name | Role | Status | Upstream | Location
----+------------+---------+-----------+----------+----------
1 | pg-primary | primary | * running | | default
2 | pg-standby | standby | running | pg-primary | default
3 | pg-witness | witness | running | | default
sudo systemctl stop postgresql@14-main
journalctl -u repmgrd -f
應看到自動提升流程和見證節點參與決策的記錄。
在repmgr.conf
中配置:
failover=automatic
promote_command='/usr/bin/repmgr standby promote -f /etc/repmgr.conf'
primary_visibility_consensus=true
consensus_timeout=3000
配置Prometheus監控:
- job_name: 'repmgr'
static_configs:
- targets: ['pg-primary:9187', 'pg-standby:9187']
pg_hba.conf
配置repmgr
用戶有足夠權限repmgrd
日志primary_visibility_consensus
已啟用通過本文的步驟,您已成功配置了repmgr見證服務器。見證節點的引入顯著提高了集群的可靠性,建議在生產環境中至少部署一個見證節點。更多高級配置可參考repmgr官方文檔。
注意:所有配置變更后都應重啟PostgreSQL和repmgrd服務。建議在非生產環境充分測試后再部署到生產系統。 “`
這篇文章共計約1150字,采用Markdown格式編寫,包含: 1. 層級分明的章節結構 2. 代碼塊和配置示例 3. 關鍵注意事項提示 4. 實際命令和配置片段 5. 問題排查建議 6. 相關文檔鏈接
可根據實際環境調整IP地址、路徑和版本號等參數。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。