haproxy使用extern-check支持伪双写配置验证


难度 中等

部署数据库

初始化主库

initdb -D data -A trust -Upostgres

将如下配置文件添加到postgresql.conf,

listen_addresses = '*'
port = 5432

# 复制设置
wal_level = replica
max_wal_senders = 10
wal_keep_size = 1GB
hot_standby = on

启动数据库:(如果是跨机器部署机器还需要修改pg_hba.conf)

pg_ctl -D data -l logfile start

cloen备库

pg_basebackup -h localhost -p 5432 -D data1 -U repl -P -v -R -X stream -C -S standby1_slot

将如下配置文件添加到postgresql.conf,

port = 5433
hot_standby = on
hot_standby_feedback = on
max_standby_streaming_delay = 30s

# 禁止在备库上进行写操作
default_transaction_read_only = on

# 可选:报告为主库(用于监控)
hot_standby_feedback = on

拷贝过来的原来的主库配置要把它删除掉。

启动备库。

pg_ctl -D data1 -l logfile start

可以看到主库运行在5432端口,备库运行在5433端口,然后连接主库查看流复制关系。

psql -h 127.0.0.1 -p 5432 -U postgres
psql (15.12)
Type "help" for help.

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 f
(1 row)

postgres=# select * from pg_stat_replication;
   pid   | usesysid | usename | application_name | client_addr | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_lsn  | write_lsn | flush_lsn |
replay_lsn | write_lag | flush_lag | replay_lag | sync_priority | sync_state |          reply_time
---------+----------+---------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+-----------+-----------+-----------+-
-----------+-----------+-----------+------------+---------------+------------+-------------------------------
 1203834 |    16384 | repl    | walreceiver      | 127.0.0.1   |                 |       48076 | 2026-01-15 08:05:52.885746+00 |              | streaming | 0/7000290 | 0/7000290 | 0/7000290 |
0/7000290  |           |           |            |             0 | async      | 2026-01-15 11:53:44.016787+00
(1 row)

postgres=# \q
root@linux-kernel-test:~# psql -h 127.0.0.1 -p 5433 -U postgres
psql (15.12)
Type "help" for help.

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 t
(1 row)

postgres=#

到这里主备集群就搭建好了。

haproxy

apt install haproxy

或者从源码下载编译。https://github.com/haproxy/haproxy

采用extern-check方式来探测主库,探测脚本check_pg_master.sh的内容如下:

#!/bin/bash
# 检查当前节点是否为 PostgreSQL 主库
# 返回:
# 0 -> 主库 (UP)
# 1 -> 备库 (DOWN)

# 支持两种方式获取 server 地址:
# HAProxy >=2.4 可用参数传递 %s %p
HOST="${HAPROXY_SERVER_ADDR:-$1}"
PORT="${HAPROXY_SERVER_PORT:-$2}"
USER="repl"
PASS=""
DB="postgres"
export LD_LIBRARY_PATH=/var/postgres/lib

# 查询 PostgreSQL 节点角色
STATUS=$(PGPASSWORD="$PASS" /var/postgres/bin/psql -qtAX -h "$HOST" -p "$PORT" -U "$USER" -d${DB} \
        -c "SELECT pg_is_in_recovery();" 2>/tmp/connect.log)

echo "result is $?"
if [[ "$STATUS" == "f" ]]; then
    echo " - $HOST:$PORT is PRIMARY"
    exit 0   # 主库 -> HAProxy UP
else
    echo " - $HOST:$PORT is REPLICA"
    exit 1   # 备库 -> HAProxy DOWN
fi

haproxy的配置如下:

global
    log stdout format raw local0
    maxconn 5000
    external-check  # 启用 external-check

defaults
    mode tcp
    timeout connect 5s
    timeout client  30s
    timeout server  30s

listen pg_write
    bind *:5000
    mode tcp
    option external-check
    external-check command /etc/haproxy/check_pg_master.sh

    # 定义默认 server 健康检查策略
    default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions

    # PostgreSQL 节点
    server pg01 127.0.0.1:5432 check
    server pg02 127.0.0.1:5433 check

使用如下命令启动haproxy,

systemctl start haproxy

验证代理是否生效

  • 代理运行在5000端口,将流量转发到主库
  • 127.0.0.1:5432是主库
  • 127.0.0.1:5432是备库
psql -h 127.0.0.1 -p 5000 -U postgres
psql (15.12)
Type "help" for help.

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery
-------------------
 f
(1 row)

修改haproxy的配置,变更pg1和pg2的位置,继续使用上面的连接串连接数据库,依然能连接到主库。


文章作者: growdu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 growdu !
  目录
分类导航
随笔2 AI27 算法1 计算机基础13 博客搭建7 ChatGPT2 集群63 计算机通信1 数据库34 数据库深入80 DPDK26 Docker11 Elasticsearch4 编辑工具4 FAQ1 Go Web1 hometown2 编程语言16 网络9 OPC1 Linux38 openGauss4 页面12 PostgreSQL54 程序员自我修养1 协议11 成长之路1 stock1 存储5 工具20 VPP18 视频作品1 Vue13 Web1 代码示例11 数据库15 BenchmarkSQL1 PostgreSQL 源码修炼之路14
最热文章
1
13 逻辑复制深入
数据库深入🔥 1570
2
0 Postgresql存储、索引及系统优化、主备切换
PostgreSQL🔥 1495
3
一文读懂openguass dcf网络模块
集群🔥 1420
4
逻辑复制源码分析
数据库深入🔥 1327
5
PostgreSQL 分区表:从一行 `PARTITION BY` 到路由热路径的全链路拆解
数据库🔥 1094
6
applyparallelworker.c 之 LA 端源码深度解析:Leader Apply Worker 的指挥中枢
数据库深入🔥 1082
7
PostgreSQL Background Worker 全解:从 `RegisterBackgroundWorker` 到逻辑复制 4 类 worker 的全生命周期
数据库🔥 1078
8
PostgreSQL的后台进程walsender分析 - 关系型数据库 - 亿速云
PostgreSQL🔥 1033
9
PostgreSQL 逻辑复制的监控:六张视图 + 一组可执行 SQL,把 publisher/subscriber 的速率与健康度彻底看透
数据库🔥 1032
10
PostgreSQL 逻辑复制支持 DDL 之后:DDL 与 DML 的时序难题(重点:分区表)
数据库🔥 999
11
reorderbuffer.c 源码深度解析:PostgreSQL 逻辑复制的"事务重组引擎
数据库深入🔥 953
12
PostgreSQL 内核开发:读取一张表的 9 步标准流程与缓存全景
数据库🔥 938
13
从 `postgres` 二进制到生产级守护 —— PostgreSQL 最外层模块与启动全流程拆解
数据库🔥 936
14
支持逻辑复制同步 DDL 适配 SQL Server 方案
数据库深入🔥 934
15
PostgreSQL 逻辑复制的 ReorderBuffer 与事务机制:从一行 WAL 到一致性变更流的全链路绑定
数据库🔥 913
16
DDL同步架构(美化版)
数据库深入🔥 908
17
PostgreSQL Latch 机制详解:从一行 SetLatch 到 epoll 的内核之旅
数据库🔥 871
18
pgbench 源码全解:一个 C 文件如何撑起 PostgreSQL 官方压测工具
数据库🔥 860
19
PostgreSQL libpq 机制与缓冲区详解
数据库🔥 850
20
PostgreSQL 逻辑复制 spill 文件深度剖析:从 `xid-*.spill` 到 TPC-C 的增长方程
数据库🔥 845