您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# 如何使用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。