前提准备 基于流复制部署,keepalived
检测postgresql
状态,如果pgsql
死了,那么keepalived
的VIP
漂移到备库,同时提升备库为主库,可以进行读写操作。(默认流复制的主库为可读可写,备库为只读)
主库:10.211.55.8
从库:10.211.55.11
Vip: 10.211.55.15
安装keepalived
主备都需要安装keepalived
yum install -y keepalived
修改配置文件 $ vim /etc/keepalived/keepalived.conf
配置文件位置有可能不一样,可以通过whereis keepalived
查找一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 ! Configuration File for keepalived global_defs { notification_email { acassen@firewall .loc failover@firewall .loc sysadmin@firewall .loc } notification_email_from Alexandre.Cassen@firewall .loc smtp_server 192.168 .200.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script check_pg_alived { script "/etc/keepalived/scripts/check_pg.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_pg_alived } virtual_ipaddress { 10.211 .55.15 } }
主备脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 $ vim /etc/keepalived/scripts/check_pg.sh A=`ps -C postgres --no-header | wc -l` B=`ip a | grep 10.211.55.15 | wc -l` C=`ps -ef | grep postgres | grep 'startup process' | wc -l` D=`ps -ef | grep postgres | grep 'receiver' | wc -l` E=`ps -ef | grep postgres | grep 'sender' | wc -l` if [ $A -eq 0 ];then echo "`date " +%Y-%m-%d--%H:%M:%S"` postgresql stop so vip stop " >> /etc/ keepalived/log /check_pg.log systemctl stop keepalivedelse if [ $B -eq 1 -a $C -eq 1 -a $D -eq 0 ];then su - postgres -c "pg_ctl promote -D /var/lib/pgsql/9.5/data/" echo "`date " +%Y-%m-%d--%H:%M:%S"` standby promote " >> /etc/ keepalived/log /check_pg.log fi if [ $B -eq 1 -a $C -eq 0 -a $D -eq 0 -a $E -eq 0 ];then echo "`date " +%Y-%m-%d--%H:%M:%S"` can't find standby " >> /etc/ keepalived/log /check_pg.log fi fi
然后需要给与相应的可执行权限
chomd a+x /etc/keepalived/scripts/check_pg.sh
修改keepalived配置 vim /etc/sysconfig/keepalived
把KEEPALIVED_OPTIONS="-D"
修改为KEEPALIVED_OPTIONS="-D -d -S 0"
vim /etc/rsyslog.conf
加入如下配置:
1 2 # keepalived -S 0 local0.* /var/log/keepalived.log
关闭SELINUX和防火墙 1 2 $ systemctl stop firewalld && systemctl disable firewalld $ setenforce 0
启动keepalived服务检查VIP情况 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ systemctl start keepalived && systemctl enable keepalived $ ip addr show eth02 : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00 :1 c:42 :fe:68 :b6 brd ff:ff:ff:ff:ff:ff inet 10.211 .55.8 /24 brd 10.211 .55.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 10.211 .55.15 /32 scope global eth0 valid_lft forever preferred_lft forever 2 : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00 :1 c:42 :da:8 a:7 a brd ff:ff:ff:ff:ff:ff inet 10.211 .55.11 /24 brd 10.211 .55.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever
检测 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 )查看主库与备库的数据库是否一致,并在主库上写入数据,再到备库上查看是否同步过去了。确定同步没有问题(流复制检查) 2 )使用vip连接数据库并写入数据 $ psql - h 10.211 .55 .15 - U postgres create table test(id int ); 创建test表 insert into test values (1 ),(2 ); 插入数据 3 )主备查看数据 select * from test; 4 )模拟主库宕机,检测备库情况 systemctl stop postgresql #停掉主库(10.211 .55 .8 ) 检查vip在哪一台主机,连接vip再次插入数据,如果可以写入数据则表示备库切换主库成功; insert into test values (3 ); 现在只有一台运行(74 )