惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
K
Kaspersky official blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Recorded Future
Recorded Future
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
S
Schneier on Security
P
Palo Alto Networks Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
N
Netflix TechBlog - Medium
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
C
Check Point Blog
L
LangChain Blog
腾讯CDC
小众软件
小众软件
T
Tenable Blog
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
About on SuperTechFans
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
Recent Announcements
Recent Announcements
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
雷峰网
雷峰网
美团技术团队
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
博客园_首页

博客园 - MSSQL123

PostgreSQL 18 新特性 skip scan,一个鸡肋的功能 PostgreSQL 17 流复制中开启逻辑复制槽同步,以及逻辑槽的故障转移 记一次PostgreSQL交叉表crosstab行转列导致的OOM TiDB 扩容与缩容 PostgreSQL 高可用集群 patroni 自动故障转移测试 Ubuntu 20 环境下 patroni 自动化安装,一分钟快速搭建 patroni 集群 Ubuntu 20 环境下 pg_auto_failover 自动化安装,一分钟快速搭建pg_auto_failover集群 PostgreSQL的消息队列扩展pgmq 磁盘IO延迟和队列深度的关系 TiDB 最小拓扑架构集群安装 PostgreSQL 逻辑复制中的同步和异步模式以及其表现 prometheus 的 altermanager Silences静默告警,优雅地抑制告警 “世界上百分之99的博士和他们的论文都是垃圾”,原本以为这个观点偏激 pg_auto_failover 在多种场景下自动故障转移的验证 pgbouncer连接池设置与压力测试的最大连接数测试 pg_auto_failover 自动故障转移参数 博文阅读密码验证 - 博客园 pg_auto_failover 高可用中,PostgreSQL实例配置文件的加载步骤 pg_auto_failover集群monitor节点的高可用 prometheus监控Linux Server node_exporter代理安装和配置 prometheus监控windows window_exporter代理安装和配置 Prometheus 和 Grafana 监控 PostgreSQL 记一次MySQL binlog日志导致磁盘空间占满的问题 SQLServer 2019 标准版在虚拟机上无法充分利用CPU的问题诊断 Windows Failover Cluster集群中的EventId 1196错误日志 pg_auto_failover 环境变量导致的show命令错误 SqlServer 事务复制(transaction replication)的复制位点信息 SqlServer 事务复制的两个参数immediate_sync,allow_anonymous MySQL,SqlServer,PostgreSQL中,如何实现锁定一张表 PostgreSQL pg_auto_failover 高可用 2:pg_auto_failover集群运维 PostgreSQL pg_auto_failover 高可用 1:pg_auto_failover集群搭建 PostgreSQL patroni 高可用 3:patroni 运维
PostgreSQL patroni 高可用 4:HAProxy和Keepalived实现读写分离
MSSQL123 · 2025-09-28 · via 博客园 - MSSQL123

PostgreSQL patroni 高可用 4:HAProxy和Keepalived实现读写分离

PostgreSQL ptroni的高可用架构图如下所示,本文完成如下架构图中红色标记内的HAProxy安装和配置,实际上是在每个节点都安装了HAProxy,然后用keepalived的方式,实现HAProxy自身的高可用。

1,HAProxy只是一个请求转发功能的中间件,可以单独安装在一台独立的机器上,也可以跟PostgreSQL实例安装在一台机器上。

2,HAProxy并不是只能适配于Patroni,可以是任意类型的集群,比如基础的流复制,repmgr,pg_auto_failover 集群,或者实现MySQL集群的代理等等。

3,HAproxy自身也是一个单点的应用,所以其自身也需要高可用,因此本文会基于keepalived对HAproxy做高可用。

4,HAProxy在patroni高可用环境中,客户端的访问路径为:Application---》keepalived虚拟IP---》HAProxy---》patroni实例---》etcd存储---》PostgreSQL实例,可见这个链路比较长,每个组件都会带来一定的性能损耗。

image

图片来源于:https://docs.percona.com/postgresql/12/solutions/high-availability.html#architecture-layout

1,环境

Ubuntu08:192.168.152.115

Ubuntu09:192.168.152.116

Ubuntu10:192.168.152.117

patroni集群环境:

root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# patronictl -c /usr/local/pgsql16/patroni/patroni.yml list
+ Cluster: pg_cluster_wy_prod (7553485872297570126) ----+----+-----------+
| Member   | Host                 | Role    | State     | TL | Lag in MB |
+----------+----------------------+---------+-----------+----+-----------+
| ubuntu08 | 192.168.152.115:9000 | Replica | streaming |  5 |         0 |
| ubuntu09 | 192.168.152.116:9000 | Replica | streaming |  5 |         0 |
| ubuntu10 | 192.168.152.117:9000 | Leader  | running   |  5 |           |
+----------+----------------------+---------+-----------+----+-----------+
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#

2,AHProxy安装

版本选择

image

下载

wget https://www.haproxy.org/download/3.2/src/haproxy-3.2.5.tar.gz
tar -xzvf haproxy-3.2.5.tar.gz
cd haproxy-3.2.5/

编译安装

#编译选项,make编译会报错,提示出编译选项
root@ubuntu08:/usr/local/pg_install_package/haproxy-3.2.5# make

Building HAProxy without specifying a TARGET is not supported.
Usage:

    make help                       # To print a full explanation.
    make TARGET=xxx USE_<feature>=1 # To build HAProxy.

The most commonly used targets are:

    linux-glibc    - Modern Linux with glibc
    linux-musl     - Modern Linux with musl
    freebsd        - FreeBSD
    openbsd        - OpenBSD
    netbsd         - NetBSD
    osx            - macOS
    solaris        - Solaris

Choose the target which matches your OS the most in order to
gain the maximum performance out of it.

Common features you might want to include in your build are:

    USE_OPENSSL=1 - Support for TLS encrypted connections
    USE_ZLIB=1    - Support for HTTP response compression
    USE_PCRE=1    - Support for PCRE regular expressions
    USE_LUA=1     - Support for dynamic processing using Lua

Use 'make help' to print a full explanation of supported targets
and features, and 'make ... opts' to show the variables in use
for a given set of build options, in a reusable form.

make: *** [Makefile:933: all] Error 1
#编译
make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_QUIC=1 USE_QUIC_OPENSSL_COMPAT=1

#安装,安装位置为:/usr/local/sbin
make install

3,HAProxy配置

haproxy三个节点完全一致,不需要修改,/etc/haproxy/haproxy.conf

global
    log         127.0.0.1 local2       
    pidfile     /var/run/haproxy.pid   
    maxconn     1000                   
    daemon                            

defaults
    mode                    tcp
    retries                 3
    timeout client          10m
    timeout connect         10s
    timeout server          10m
    timeout check           10s
    
listen  stats
        stats uri /
        mode http
        bind *:8080
        stats enable
        stats auth admin:admin
        stats refresh 10s
    
listen  pg_rw
        bind *:6432
        option httpchk
        http-check expect status 200
        default-server inter 3s rise 3 fall 2 on-marked-down shutdown-sessions
        server ubuntu05 192.168.152.115:9000 check port 8008
        server ubuntu06 192.168.152.116:9000 check port 8008
        server ubuntu07 192.168.152.117:9000 check port 8008
        
listen  pg_ro
        bind *:6433
        option httpchk GET /replica	
        http-check expect status 200
        default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
        balance roundrobin
        server ubuntu05 192.168.152.115:9000 check port 8008
        server ubuntu06 192.168.152.116:9000 check port 8008
        server ubuntu07 192.168.152.117:9000 check port 8008

systemctl启动文件haproxy.service

/etc/systemd/system/haproxy.service

# /etc/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=network.target

[Service]
Environment="CONFIG=/etc/haproxy/haproxy.conf" "PIDFILE=/var/run/haproxy.pid"
ExecStartPre=/usr/local/sbin/haproxy -f $CONFIG -c -q
ExecStart=/usr/local/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE -d
ExecReload=/usr/local/sbin/haproxy -f $CONFIG -c -q
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify

# The following lines leverage SystemD's sandboxing options to provide
# defense in depth protection at the expense of restricting some flexibility
# in your setup (e.g. placement of your configuration files) or possibly
# reduced performance. See systemd.service(5) and systemd.exec(5) for further
# information.

# NoNewPrivileges=true
# ProtectHome=true
# If you want to use 'ProtectSystem=strict' you should whitelist the PIDFILE,
# any state files and any other files written using 'ReadWritePaths' or
# 'RuntimeDirectory'.
# ProtectSystem=true
# ProtectKernelTunables=true
# ProtectKernelModules=true
# ProtectControlGroups=true
# If your SystemD version supports them, you can add: @reboot, @swap, @sync
# SystemCallFilter=~@cpu-emulation @keyring @module @obsolete @raw-io

[Install]
WantedBy=multi-user.target

启动服务

systemctl daemon-reload
systemctl enable haproxy
systemctl start haproxy
systemctl status haproxy

如果有异常,可以直接启动调试验证配置文件是否正常

/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.conf -c -V

3,HAProxy代理使用

先从Ubuntu08:192.168.152.115开始安装,目前集群角色如下

root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# patronictl -c /usr/local/pgsql16/patroni/patroni.yml list
+ Cluster: pg_cluster_wy_prod (7553485872297570126) ----+----+-----------+
| Member   | Host                 | Role    | State     | TL | Lag in MB |
+----------+----------------------+---------+-----------+----+-----------+
| ubuntu08 | 192.168.152.115:9000 | Replica | streaming |  5 |         0 |
| ubuntu09 | 192.168.152.116:9000 | Replica | streaming |  5 |         0 |
| ubuntu10 | 192.168.152.117:9000 | Leader  | running   |  5 |           |
+----------+----------------------+---------+-----------+----+-----------+
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#

3.1,PostgreSQL集群的patroni状态检查

root@ubuntu08:/usr/local/pg_install_package#
root@ubuntu08:/usr/local/pg_install_package# curl -s "http://192.168.152.117:8008/leader" -v 2>&1|grep '200 OK'		#主节点检查正常
< HTTP/1.0 200 OK
root@ubuntu08:/usr/local/pg_install_package# curl -s "http://192.168.152.117:8008/replica" -v 2>&1|grep '200 OK'
root@ubuntu08:/usr/local/pg_install_package#
root@ubuntu08:/usr/local/pg_install_package# curl -s "http://192.168.152.115:8008/replica" -v 2>&1|grep '200 OK'	#从节点1检查正常
< HTTP/1.0 200 OK
root@ubuntu08:/usr/local/pg_install_package#
root@ubuntu08:/usr/local/pg_install_package# curl -s "http://192.168.152.116:8008/replica" -v 2>&1|grep '200 OK'	#从节点2检查正常
< HTTP/1.0 200 OK
root@ubuntu08:/usr/local/pg_install_package#

3.2,启动HAproxy

root@ubuntu08:/usr/local/pg_install_package# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
     Loaded: loaded (/etc/systemd/system/haproxy.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2025-09-28 13:47:47 CST; 10s ago
    Process: 858613 ExecStartPre=/usr/local/sbin/haproxy -f $CONFIG -c -q (code=exited, status=0/SUCCESS)
   Main PID: 858635 (haproxy)
     Status: "Ready."
      Tasks: 3 (limit: 4550)
     Memory: 8.7M
     CGroup: /system.slice/haproxy.service
             ├─858635 /usr/local/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -d
             └─858639 /usr/local/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -d

Sep 28 13:47:47 ubuntu08 haproxy[858639]: Using epoll() as the polling mechanism.
Sep 28 13:47:47 ubuntu08 haproxy[858635]: 00000000:MASTER.accept(0003)=0007 from [unix:1] ALPN=<none>
Sep 28 13:47:47 ubuntu08 haproxy[858635]: [NOTICE]   (858635) : Loading success.
Sep 28 13:47:47 ubuntu08 haproxy[858635]: 00000000:MASTER.srvcls[0007:ffff]
Sep 28 13:47:47 ubuntu08 haproxy[858635]: 00000001:MASTER.clicls[0007:ffff]
Sep 28 13:47:47 ubuntu08 haproxy[858635]: 00000001:MASTER.closed[0007:ffff]
Sep 28 13:47:47 ubuntu08 systemd[1]: Started HAProxy Load Balancer.
Sep 28 13:47:47 ubuntu08 haproxy[858639]: [WARNING]  (858639) : Server pg_rw/ubuntu08 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 7ms. 2 active and 0>
Sep 28 13:47:47 ubuntu08 haproxy[858639]: [WARNING]  (858639) : Server pg_rw/ubuntu09 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 1ms. 1 active and 0>
Sep 28 13:47:49 ubuntu08 haproxy[858639]: [WARNING]  (858639) : Server pg_ro/ubuntu10 is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 3ms. 2 active and 0>

root@ubuntu08:/usr/local/pg_install_package#

3.3,HAproxy管理后台

HAproxy管理后台:http://192.168.152.115:8080/ 

image

3.4,读写分离测试

patronictl -c /usr/local/pgsql16/patroni/patroni.yml list查看集群状态

root@ubuntu10:/usr/local/pg_install_package# patronictl -c /usr/local/pgsql16/patroni/patroni.yml list
+ Cluster: pg_cluster_wy_prod (7553485872297570126) ----+----+-----------+
| Member   | Host                 | Role    | State     | TL | Lag in MB |
+----------+----------------------+---------+-----------+----+-----------+
| ubuntu08 | 192.168.152.115:9000 | Replica | streaming |  5 |         0 |
| ubuntu09 | 192.168.152.116:9000 | Replica | streaming |  5 |         0 |
| ubuntu10 | 192.168.152.117:9000 | Leader  | running   |  5 |           |
+----------+----------------------+---------+-----------+----+-----------+
root@ubuntu10:/usr/local/pg_install_package#

测试读写分析

#6432 读写端口号,一直重定向到主节点 192.168.152.117
root@ubuntu10:/usr/local/pg_install_package# psql "host=192.168.152.115 port=6432 user=postgres dbname=postgres password=******" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery
------------------+-------------------
 192.168.152.117  | f
(1 row)
#6432 读写端口号,一直重定向到主节点 192.168.152.117
root@ubuntu10:/usr/local/pg_install_package# psql "host=192.168.152.115 port=6432 user=postgres dbname=postgres password=******" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery
------------------+-------------------
 192.168.152.117  | f
(1 row)
#6433 只读端口号,一直重定向到主节点 192.168.152.115或者116
root@ubuntu10:/usr/local/pg_install_package#
root@ubuntu10:/usr/local/pg_install_package# psql "host=192.168.152.115 port=6433 user=postgres dbname=postgres password=******" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery
------------------+-------------------
 192.168.152.115  | t
(1 row)

root@ubuntu10:/usr/local/pg_install_package#
root@ubuntu10:/usr/local/pg_install_package# psql "host=192.168.152.115 port=6433 user=postgres dbname=postgres password=******" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery
------------------+-------------------
 192.168.152.116  | t
(1 row)

root@ubuntu10:/usr/local/pg_install_package#
root@ubuntu10:/usr/local/pg_install_package# psql "host=192.168.152.115 port=6433 user=postgres dbname=postgres password=******" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery
------------------+-------------------
 192.168.152.115  | t
(1 row)

root@ubuntu10:/usr/local/pg_install_package# psql "host=192.168.152.115 port=6433 user=postgres dbname=postgres password=******" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery
------------------+-------------------
 192.168.152.116  | t
(1 row)

root@ubuntu10:/usr/local/pg_install_package# psql "host=192.168.152.115 port=6433 user=postgres dbname=postgres password=******" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery
------------------+-------------------
 192.168.152.115  | t
(1 row)

4,keepalived安装

很多高可用环境中,把故障的判断以及VIP,都由keepalived实现,keepalived在网络分区的情况下存在脑裂的问题,这里的keepalived 只做VIP 高可用(接入层),不做故障判断,这里的故障判断交由实际上是patroni实现。

4.1 下载和安装

首先从Ubuntu08这台主机开始安装

wget https://keepalived.org/software/keepalived-2.3.4.tar.gz
#config
./configure --prefix=/usr/local/
#编译和安装
make && make install

#安装psmisc
apt install -y psmisc

keepalived服务文件:/etc/systemd/system/keepalived.server

[Unit]
Description=Keepalive Daemon (LVS and VRRP)
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target

Ubuntu 08 keepalived配置文件:/usr/local/keepalived/etc/keepalived/keepalived.conf

global_defs {
    router_id ubunt08
    script_user root
    enable_script_security
    notification_syslog facility local1
}

vrrp_script chk_haproxy {
    script "/usr/bin/killall -0 haproxy"
    interval 2
    weight 5
    fall 30
    rise 5
    timeout 2
}

vrrp_instance VI_1 {
    state MASTER	#抢占模式
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.152.119
    }
    track_script {
        chk_haproxy
    }
}

4.2 keepalived日志设置

keepalived的环境变量配置默认在 yum/apt 安装的在 /etc/sysconfig/keepalived ,源码编译安装的在/usr/local/keepalived/etc/sysconfig/keepalived

1,修改keepalived.conf配置文件
global_defs {
    # 设置 syslog facility
    notification_syslog facility local1
}
这里的 local1 可以换成 local0 ~ local7 任意一个,但要和 rsyslog 里对应。


2,编辑 /etc/rsyslog.d/keepalived.conf,增加一条规则,把 local1.* 的日志写到独立文件里:
local1.*    /var/log/keepalived.log


3,保存后,重启 rsyslog:
sudo systemctl restart rsyslog

启动keepalived

systemctl daemon-reload
systemctl enable keepalived
systemctl start keepalived
systemctl status keepalived

4.3 keepalived绑定虚拟IP测试

root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:af:4a:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.152.115/24 brd 192.168.152.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.152.119/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feaf:4aa4/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# systemctl status keepalived
● keepalived.service - Keepalive Daemon (LVS and VRRP)
     Loaded: loaded (/etc/systemd/system/keepalived.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2025-09-28 14:46:40 CST; 2min 9s ago
    Process: 868947 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
   Main PID: 868960 (keepalived)
      Tasks: 2 (limit: 4550)
     Memory: 1.8M
     CGroup: /system.slice/keepalived.service
             ├─868960 /usr/local/keepalived/sbin/keepalived -D -S 0
             └─868961 /usr/local/keepalived/sbin/keepalived -D -S 0

Sep 28 14:46:43 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 14:46:43 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 14:46:43 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 14:46:43 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.152.119
Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#

ubunt09 keepalived配置文件(修改router_id,state,priority)

global_defs {
    router_id ubunt09
    script_user root
    enable_script_security
        notification_syslog facility local1
}

vrrp_script chk_haproxy {
    script "/usr/bin/killall -0 haproxy"
    interval 2
    weight 5
    fall 3
    rise 5
    timeout 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.152.119
    }
    track_script {
        chk_haproxy
    }
}

ubunt10 keepalived配置文件(修改router_id,state,priority)

global_defs {
    router_id ubunt10
    script_user root
    enable_script_security
        notification_syslog facility local1
}

vrrp_script chk_haproxy {
    script "/usr/bin/killall -0 haproxy"
    interval 2
    weight 5
    fall 3
    rise 5
    timeout 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.152.119
    }
    track_script {
        chk_haproxy
    }
}

4.4 keepalived虚拟IP飘移测试

1,Ubuntu08主节点关闭keepalived

root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# systemctl stop keepalived
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:af:4a:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.152.115/24 brd 192.168.152.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feaf:4aa4/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#

2,Ubuntu09节点接替keepalived 

root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5#
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# systemctl status keepalived
● keepalived.service - Keepalive Daemon (LVS and VRRP)
     Loaded: loaded (/etc/systemd/system/keepalived.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2025-09-28 16:16:21 CST; 33s ago
    Process: 847309 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
   Main PID: 847324 (keepalived)
      Tasks: 2 (limit: 4550)
     Memory: 2.5M
     CGroup: /system.slice/keepalived.service
             ├─847324 /usr/local/keepalived/sbin/keepalived -D -S 0
             └─847325 /usr/local/keepalived/sbin/keepalived -D -S 0

Sep 28 16:16:51 ubuntu09 Keepalived_vrrp[847325]: (VI_1) Backup received priority 0 advertisement
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: (VI_1) Receive advertisement timeout
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: (VI_1) Entering MASTER STATE
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: (VI_1) setting VIPs.
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.152.119
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5#
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:c2:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.152.116/24 brd 192.168.152.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.152.119/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe4e:c2b0/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5#

3,Ubuntu08主节点启动keepalived,抢回虚拟ip

root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# systemctl start keepalived
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:af:4a:a4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.152.115/24 brd 192.168.152.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.152.119/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feaf:4aa4/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# systemctl status keepalived
● keepalived.service - Keepalive Daemon (LVS and VRRP)
     Loaded: loaded (/etc/systemd/system/keepalived.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2025-09-28 16:19:07 CST; 18s ago
    Process: 879342 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
   Main PID: 879356 (keepalived)
      Tasks: 2 (limit: 4550)
     Memory: 1.6M
     CGroup: /system.slice/keepalived.service
             ├─879356 /usr/local/keepalived/sbin/keepalived -D -S 0
             └─879358 /usr/local/keepalived/sbin/keepalived -D -S 0

Sep 28 16:19:11 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:19:11 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:19:11 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:19:11 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.152.119
Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119
root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#

4,Ubuntu09上的虚拟IP被抢回(Ubuntu08主节点启动keepalived,抢回虚拟ip)

oot@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:c2:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.152.116/24 brd 192.168.152.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.152.119/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe4e:c2b0/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:c2:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.152.116/24 brd 192.168.152.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe4e:c2b0/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5#