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实例,可见这个链路比较长,每个组件都会带来一定的性能损耗。
 

图片来源于: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

下载

wget https://www.haproxy.org/download/3.2/src/haproxy-3.2.5.tar.gztar -xzvf haproxy-3.2.5.tar.gzcd 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 glibc    linux-musl     - Modern Linux with musl    freebsd        - FreeBSD    openbsd        - OpenBSD    netbsd         - NetBSD    osx            - macOS    solaris        - SolarisChoose the target which matches your OS the most in order togain 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 LuaUse 'make help' to print a full explanation of supported targetsand features, and 'make ... opts' to show the variables in usefor 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/sbinmake 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
关于HAProxy的另一种验证方式,请参考这里:https://blog.itpub.net/70041375/viewspace-3032961/
 
systemctl启动文件haproxy.service
/etc/systemd/system/haproxy.service
# /etc/systemd/system/haproxy.service[Unit]Description=HAProxy Load BalancerAfter=network.target[Service]Environment="CONFIG=/etc/haproxy/haproxy.conf" "PIDFILE=/var/run/haproxy.pid"ExecStartPre=/usr/local/sbin/haproxy -f $CONFIG -c -qExecStart=/usr/local/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE -dExecReload=/usr/local/sbin/haproxy -f $CONFIG -c -qExecReload=/bin/kill -USR2 $MAINPIDKillMode=mixedRestart=alwaysSuccessExitStatus=143Type=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-reloadsystemctl enable haproxysystemctl start haproxysystemctl 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 OKroot@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 OKroot@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 OKroot@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 -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/ 

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.117root@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.117root@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或者116root@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#安装psmiscapt install -y psmisc

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

[Unit]Description=Keepalive Daemon (LVS and VRRP)After=network-online.targetWants=network-online.target[Service]Type=forkingPIDFile=/run/keepalived.pidKillMode=processEnvironmentFile=/usr/local/keepalived/etc/sysconfig/keepalivedExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONSExecReload=/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.log3,保存后,重启 rsyslog:sudo systemctl restart rsyslog

启动keepalived

systemctl daemon-reloadsystemctl enable keepalivedsystemctl start keepalivedsystemctl status keepalived

 

4.3 keepalived绑定虚拟IP测试

root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# ip addr1: 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 forever2: 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 foreverroot@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 0Sep 28 14:46:43 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 14:46:43 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 14:46:43 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 14:46:43 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.152.119Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 14:46:48 ubuntu08 Keepalived_vrrp[868961]: Sending gratuitous ARP on ens33 for 192.168.152.119root@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 keepalivedroot@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4#root@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# ip addr1: 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 forever2: 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 foreverroot@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 0Sep 28 16:16:51 ubuntu09 Keepalived_vrrp[847325]: (VI_1) Backup received priority 0 advertisementSep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: (VI_1) Receive advertisement timeoutSep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: (VI_1) Entering MASTER STATESep 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.119Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:16:52 ubuntu09 Keepalived_vrrp[847325]: Sending gratuitous ARP on ens33 for 192.168.152.119root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5#root@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# ip addr1: 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 forever2: 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 foreverroot@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 keepalivedroot@ubuntu08:/usr/local/pg_install_package/keepalived-2.3.4# ip addr1: 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 forever2: 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 foreverroot@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 0Sep 28 16:19:11 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:19:11 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:19:11 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:19:11 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: (VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.152.119Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119Sep 28 16:19:16 ubuntu08 Keepalived_vrrp[879358]: Sending gratuitous ARP on ens33 for 192.168.152.119root@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 addr1: 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 forever2: 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 foreverroot@ubuntu09:/usr/local/pg_install_package/haproxy-3.2.5# ip addr1: 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 forever2: 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 foreverroot@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)

 

本站提供的所有下载资源均来自互联网,仅提供学习交流使用,版权归原作者所有。如需商业使用,请联系原作者获得授权。 如您发现有涉嫌侵权的内容,请联系我们 邮箱:[email protected]