keepalived+mysql双主

二、搭建过程
搭建环境(服务器配置忽略)
master1: 196.168.126.6/22
master2: 196.168. 126.7/22
VIP:196.168. 126.67/32
master1和master2上安装keepalived服务,并修改配置文件,如下配置
解压
tar -xvf keepalived-2.0.8.tar.gz
cd keepalived-2.0.8

编译安装
./configure --prefix=/usr/local/keepalived
make && make install
对命令进行添加
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

 

 

192.168.126.6

[root@zxw6 ~]# cat /etc/keepalived/keepalived.conf 

! Configuration File for keepalived
      
global_defs {
notification_email {
kw_wangyang@tech.cebbak.com
gongzidong@tech.cebbak.com
}
      
notification_email_from kw_wangyang@tech.cebbak.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MASTER-HA
}


vrrp_script chk_mysql_port {
        script "/root/chk_mysql.sh"
        interval 2
        weight -5
        fall 2
        rise 1
}
 
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    mcast_src_ip 192.168.126.6
    virtual_router_id 51
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.67
    }
}
track_script {
   chk_mysql_port
}
}
[root@zxw6 ~]# cat chk_mysql.sh 
#!/bin/bash
counter=`ss -tnl | grep "LISTEN" |grep "3306" |wc -l`
if [ ${counter} -eq 0 ]; then
      systemctl stop  keepalived 
fi
exit 0

192.168.126.17

[root@zxw7 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
      
global_defs {
notification_email {
kw_wangyang@tech.cebbak.com
gongzidong@tech.cebbak.com
}
      
notification_email_from kw_wangyang@tech.cebbak.com
 smtp_server 127.0.0.1
 smtp_connect_timeout 30
 router_id MASTER-HA
}
vrrp_script chk_mysql_port {
        script "/root/chk_mysql.sh"
        interval 2
        weight -5
        fall 2
        rise 1
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    mcast_src_ip 192.168.126.7
    virtual_router_id 51
    priority 90
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.67
    }
}
track_script {
   chk_mysql_port
}
}     
[root@zxw7 ~]# cat chk_mysql.sh 
#!/bin/bash
MYSQL=mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=zxw
CHECK_TIME=3
#mysql  is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
  $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
    if [ $? = 0 ] ;then
        MYSQL_OK=1
    else
        MYSQL_OK=0
    fi
        return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
        let "CHECK_TIME -= 1"
        check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
        CHECK_TIME=0
        exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
        pkill keepalived
        exit 1
fi
sleep 1
done

 

posted on 2021-12-06 17:32  我就是我没毛病  阅读(70)  评论(0编辑  收藏  举报

导航