当前位置: 首页 > news >正文

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

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

PostgreSQL patroni高可用

PostgreSQL patroni 高可用 1:ectd 安装和配置
PostgreSQL patroni 高可用 2:patroni安装和配置
PostgreSQL patroni 高可用 3:patroni 运维
PostgreSQL patroni 高可用 4:HAProxy和Keepalived实现读写分离

 

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

需要特别说明的是:
1,HAProxy只是一个请求转发的功能,可以单独安装在一台独立的机器上,也可以跟PostgreSQL实例安装在一台机器上。
2,HAProxy并不是只能适配于Patroni,可以是任意类型的集群,比如基础的流复制,repmgr,PostgreSQL auto failover集群等等。
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安装

版本选择
https://www.haproxy.org/,版本信息如下,这里找一个长期支持版本(LTS)3.2

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# makeBuilding 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 glibclinux-musl     - Modern Linux with muslfreebsd        - FreeBSDopenbsd        - OpenBSDnetbsd         - NetBSDosx            - macOSsolaris        - SolarisChoose 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 connectionsUSE_ZLIB=1    - Support for HTTP response compressionUSE_PCRE=1    - Support for PCRE regular expressionsUSE_LUA=1     - Support for dynamic processing using LuaUse '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

globallog         127.0.0.1 local2       pidfile     /var/run/haproxy.pid   maxconn     1000                   daemon                            defaultsmode                    tcpretries                 3timeout client          10mtimeout connect         10stimeout server          10mtimeout check           10slisten  statsstats uri /mode httpbind *:8080stats enablestats auth admin:adminstats refresh 10slisten  pg_rwbind *:6432option httpchkhttp-check expect status 200default-server inter 3s rise 3 fall 2 on-marked-down shutdown-sessionsserver ubuntu05 192.168.152.115:9000 check port 8008server ubuntu06 192.168.152.116:9000 check port 8008server ubuntu07 192.168.152.117:9000 check port 8008listen  pg_robind *:6433option httpchk GET /replica	http-check expect status 200default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessionsbalance roundrobinserver ubuntu05 192.168.152.115:9000 check port 8008server ubuntu06 192.168.152.116:9000 check port 8008server 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 BalancerLoaded: loaded (/etc/systemd/system/haproxy.service; disabled; vendor preset: enabled)Active: active (running) since Sun 2025-09-28 13:47:47 CST; 10s agoProcess: 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.7MCGroup: /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 -dSep 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安装

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 ubunt08script_user rootenable_script_securitynotification_syslog facility local1
}vrrp_script chk_haproxy {script "/usr/bin/killall -0 haproxy"interval 2weight 5fall 30rise 5timeout 2
}vrrp_instance VI_1 {state MASTER	#抢占模式interface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_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 facilitynotification_syslog facility local1
}
这里的 local1 可以换成 local0 ~ local7 任意一个,但要和 rsyslog 里对应。2,编辑 /etc/rsyslog.d/keepalived.conf,增加一条规则,把 local1.* 的日志写到独立文件里:
local1.*    /var/log/keepalived.log3,保存后,重启 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 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:af:4a:a4 brd ff:ff:ff:ff:ff:ffinet 192.168.152.115/24 brd 192.168.152.255 scope global ens33valid_lft forever preferred_lft foreverinet 192.168.152.119/32 scope global ens33valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:feaf:4aa4/64 scope linkvalid_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 agoProcess: 868947 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)Main PID: 868960 (keepalived)Tasks: 2 (limit: 4550)Memory: 1.8MCGroup: /system.slice/keepalived.service├─868960 /usr/local/keepalived/sbin/keepalived -D -S 0└─868961 /usr/local/keepalived/sbin/keepalived -D -S 0Sep 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 ubunt09script_user rootenable_script_securitynotification_syslog facility local1
}vrrp_script chk_haproxy {script "/usr/bin/killall -0 haproxy"interval 2weight 5fall 3rise 5timeout 2
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 90advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.152.119}track_script {chk_haproxy}
}

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

global_defs {router_id ubunt10script_user rootenable_script_securitynotification_syslog facility local1
}vrrp_script chk_haproxy {script "/usr/bin/killall -0 haproxy"interval 2weight 5fall 3rise 5timeout 2
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_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 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:af:4a:a4 brd ff:ff:ff:ff:ff:ffinet 192.168.152.115/24 brd 192.168.152.255 scope global ens33valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:feaf:4aa4/64 scope linkvalid_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 agoProcess: 847309 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)Main PID: 847324 (keepalived)Tasks: 2 (limit: 4550)Memory: 2.5MCGroup: /system.slice/keepalived.service├─847324 /usr/local/keepalived/sbin/keepalived -D -S 0└─847325 /usr/local/keepalived/sbin/keepalived -D -S 0Sep 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 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:4e:c2:b0 brd ff:ff:ff:ff:ff:ffinet 192.168.152.116/24 brd 192.168.152.255 scope global ens33valid_lft forever preferred_lft foreverinet 192.168.152.119/32 scope global ens33valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe4e:c2b0/64 scope linkvalid_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 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:af:4a:a4 brd ff:ff:ff:ff:ff:ffinet 192.168.152.115/24 brd 192.168.152.255 scope global ens33valid_lft forever preferred_lft foreverinet 192.168.152.119/32 scope global ens33valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:feaf:4aa4/64 scope linkvalid_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 agoProcess: 879342 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)Main PID: 879356 (keepalived)Tasks: 2 (limit: 4550)Memory: 1.6MCGroup: /system.slice/keepalived.service├─879356 /usr/local/keepalived/sbin/keepalived -D -S 0└─879358 /usr/local/keepalived/sbin/keepalived -D -S 0Sep 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 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:4e:c2:b0 brd ff:ff:ff:ff:ff:ffinet 192.168.152.116/24 brd 192.168.152.255 scope global ens33valid_lft forever preferred_lft foreverinet 192.168.152.119/32 scope global ens33valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe4e:c2b0/64 scope linkvalid_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 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:4e:c2:b0 brd ff:ff:ff:ff:ff:ffinet 192.168.152.116/24 brd 192.168.152.255 scope global ens33valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe4e:c2b0/64 scope linkvalid_lft forever preferred_lft forever
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5#
 

4.5 通过虚拟IP连接至PostgreSQL集群测试

#通过虚拟IP,RW端口号,总是转发到主节点
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# psql "host=192.168.152.119 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)#通过虚拟IP,RW端口号,总是转发到主节点
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# psql "host=192.168.152.119 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)#通过虚拟IP,RW端口号,总是转发到主节点
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# psql "host=192.168.152.119 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)#通过虚拟IP,RO端口号,轮训转发到从节点
root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# psql "host=192.168.152.119 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@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# psql "host=192.168.152.119 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@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# psql "host=192.168.152.119 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@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# psql "host=192.168.152.119 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)