Patroni高可用VIP方案
一 机器和vip资源
PostgreSQL
pg1: 192.168.56.105
pg2: 192.168.56.106
etcd
etcd: 192.168.56.104
vip
读写vip: 192.168.56.111
二 通过callback脚本实现vip漂移
客户端通过vip的方式访问PostgreSQL数据库可以多一层防护,Patroni支持用户配置在特定事件发生时触发的回调脚本。因此可以配置一个回调,在主备切换后动态加载vip。
加载vip的回调脚本/pgsql/loadvip.sh
pg1和pg2实例如下
[root@localhost ~]# cat /pgsql/loadvip.sh
#!/bin/bash
VIP=192.168.56.111
GATEWAY=192.168.56.1
DEV=enp0s8
action=$1
role=$2
cluster=$3
log()
{
echo "loadvip: $*"|logger
}
load_vip()
{
ip a|grep -w ${DEV}|grep -w ${VIP} >/dev/null
if [ $? -eq 0 ] ;then
log "vip exists, skip load vip"
else
sudo ip addr add ${VIP}/32 dev ${DEV} >/dev/null
rc=$?
if [ $rc -ne 0 ] ;then
log "fail to add vip ${VIP} at dev ${DEV} rc=$rc"
exit 1
fi
log "added vip ${VIP} at dev ${DEV}"
arping -U -I ${DEV} -s ${VIP} ${GATEWAY} -c 5 >/dev/null
rc=$?
if [ $rc -ne 0 ] ;then
log "fail to call arping to gateway ${GATEWAY} rc=$rc"
exit 1
fi
log "called arping to gateway ${GATEWAY}"
fi
}
unload_vip()
{
ip a|grep -w ${DEV}|grep -w ${VIP} >/dev/null
if [ $? -eq 0 ] ;then
sudo ip addr del ${VIP}/32 dev ${DEV} >/dev/null
rc=$?
if [ $rc -ne 0 ] ;then
log "fail to delete vip ${VIP} at dev ${DEV} rc=$rc"
exit 1
fi
log "deleted vip ${VIP} at dev ${DEV}"
else
log "vip not exists, skip delete vip"
fi
}
log "loadvip start args:'$*'"
case $action in
on_start|on_restart|on_role_change)
case $role in
master)
load_vip
;;
replica)
unload_vip
;;
*)
log "wrong role '$role'"
exit 1
;;
esac
;;
*)
log "wrong action '$action'"
exit 1
;;
esac
修改patroni配置文件/etc/patroni.yml
pg1和pg2实例配置回调函数
postgresql:
...
callbacks:
on_start: /bin/bash /pgsql/loadvip.sh
on_restart: /bin/bash /pgsql/loadvip.sh
on_role_change: /bin/bash /pgsql/loadvip.sh
重新加载patroni配置
pg1和pg2实例重新加载配置信息
patronictl reload pgsql
三 vip发生可漂移
主从状态查看
-bash-4.2$ patronictl list
vip信息查看
执行switchover操作
-bash-4.2$ patronictl switchover
主库vip漂移查看
/var/log/messages系统日志查看
注意:如果直接停止主库上的patroni,vip不会被drop掉。主库上的patroni被停掉后会触发备库failover成为新主库,此时主从2台机器上都有vip,但是由于新主执行了arping,一般不会影响应用访问。
Patroni高可用VIP方案
本文2024-09-23 01:13:23发表“云苍穹知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-cangqiong-144561.html