MySQL高可用架构之Keepalived+主从架构部署(3)


vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 43
    priority  100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

virtual_ipaddress {
        192.168.1.27 label eth1:1
    }
    track_script {
        check_local
    }

}
virtual_server 192.168.1.27 3306 {
    delay_loop 6
    lb_algo wrr          #lvs调度算法rr|wrr|lc|wlc|lblc|sh|dh
    lb_kind DR          #负载均衡转发规则NAT|DR|RUN
    persistence_timeout 50
    protocol TCP

real_server 192.168.1.23 3306 {
      weight 3
      notify_down /etc/keepalived/mysql.sh
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 3306
        }
    }
}

备库配置:

keepalived.conf

! Configuration File for keepalived
global_defs {
      notification_email {
      abc@163.com
      }
      notification_email_from root@localhost
      smtp_server root@localhost
      smtp_connect_timeout 30
      router_id node2
    }

vrrp_script check_local {
    script "/etc/keepalived/check_gateway.sh "
    interval 5
    fall    4
    rise    4
    weight 3
}


vrrp_instance VI_1 {
      state BACKUP
      interface eth1
      virtual_router_id 43
      priority 90
      advert_int 1
      authentication {
      auth_type PASS
      auth_pass 1111
      }

virtual_ipaddress {
      192.168.1.27 label eth1:1
      }
    track_script {
        check_local
    }

}


 virtual_server 192.168.1.27 3306 {
      delay_loop 2
      lb_algo wrr
      lb_kind DR
      persistence_timeout 60
      protocol TCP

real_server 192.168.1.24 3306{
      weight 3
      notify_down /etc/keepalived/mysql.sh
      echo '3' >  /etc/keepalived/t.log
      TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3   
      delay_before_retry 3
      connect_port 3306
      }
      }
}

脑裂检查脚本:

/etc/keepalived/check_gateway.sh

#!/bin/sh
VIP=192.168.1.27
GATEWAY=192.168.1.254

/sbin/arping -I eth1 -c 5 -s $VIP $GATEWAY  &>/dev/null

gateway_status=$?
keepalived_status=`ps -ef |grep keepalived|grep -v grep |wc -l`


if [ ${gateway_status} != 0 ] && [ ${keepalived_status} = 0 ]
then
  service keepalived start
elif [ ${gateway_status} != 0 ] && [ ${keepalived_status} != 0 ]
  service keepalived stop
fi

数据库服务宕机转移脚本

/etc/keepalived/mysql.sh

#!/bin/bash

run_status=`service keepalived status|grep running|wc -l`
pro_status=`ps -ef |grep keepalived|grep -v grep |wc -l`

service keepalived stop

if [ ${run_status} != 0 ] || [ ${pro_status} != 0 ]
then
  pkill keepalived
fi

从库状态检查脚本(在备库做crontab定时任务*/1 * * * * sh /etc/keepalived/check_slave.sh >/dev/null 2>&1)

#!/bin/bash

VIP="192.168.1.27"

vip_status=`ip add |egrep "${VIP}"|wc -l`
keepalived_status=`ps -ef |grep keepalived|grep -v grep|wc -l`

slave_status=`mysql -uroot -e "show slave status \G"|egrep  "Slave.*Running|Seconds_Behind_Master|Last_Errno"|grep -v "Slave_SQL_Running_State"|awk -F ':' '{printf("%s",$NF)}'`
io_status=`echo "${slave_status}"|awk '{print $1}'|grep "Yes"|wc -l`
sql_status=`echo "${slave_status}"|awk '{print $2}'|grep "Yes"|wc -l`
sync_status=`echo "${slave_status}"|awk '{print $3}'`
sync_time=`echo "${slave_status}"|awk '{print $4}'`

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/a110e7963ffde840823e5a868ac7f306.html