测试前环境准备:
PostgreSQL主备搭建参考下面三篇文章
PostgreSQL高可用之Repmgr两节点搭建及switchover操作
PostgreSQL高可用之Repmgr自动failover
PostgreSQL高可用之Repmgr见证服务器搭建
应用层JDBC配置
jdbc.url=jdbc:postgresql://192.168.99.200:6000,192.168.99.202:6000/DB
jdbc.username=your user name
jdbc.password=your password
node1
[postgres@ha1 ~]$ psql -h192.168.99.200 -p6000 -Upostgres
psql (12.1)
Type "help" for help.
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "192.168.99.200" at port "6000".
node2
[postgres@ha2 ~]$ psql -h192.168.99.202 -p6000 -Upostgres
psql (12.1)
Type "help" for help.
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "192.168.99.202" at port "6000".
Repmgr显示集群状态
$ /opt/pgsql/bin/repmgr -f /opt/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------
1 | node1 | primary | * running | | default | 100 | 21 | host=192.168.99.200 port=6000 user=repmgr dbname=repmgr connect_timeout=2
2 | node2 | standby | running | node1 | default | 100 | 21 | host=192.168.99.202 port=6000 user=repmgr dbname=repmgr connect_timeout=2
3 | node3 | witness | * running | node1 | default | 0 | 1 | host=192.168.99.222 port=6000 user=repmgr dbname=repmgr connect_timeout=2
测试一:模拟node2故障
[postgres@ha2 ~]$ /opt/pgsql/bin/pg_ctl -D /opt/data6000/ stop
测试完成后rejoin恢复集群
[postgres@ha2 ~]$ /opt/pgsql/bin/repmgr -f /opt/repmgr.conf \
node rejoin -d'host=192.168.99.200 port=6000 user=repmgr dbname=repmgr connect_timeout=2'
测试二:模拟node1故障
[postgres@ha1 ~]$ /opt/pgsql/bin/pg_ctl -D /opt/data6000/ stop
测试完成后rejoin恢复集群
[postgres@ha1 ~]$ /opt/pgsql/bin/repmgr -f /opt/repmgr.conf \
node rejoin -d'host=192.168.99.202 port=6000 user=repmgr dbname=repmgr connect_timeout=2'
本文基于Java Web应用进行node1和node2交替测试,应用层数据读写一切正常,不过基于数据库序列主键有跳变现象。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。