2.1.增加配置

master
auto-increment-increment = 2  
auto-increment-offset = 1    



auto-increment-increment = 2     
auto-increment-offset = 2  

1.下载keepalived

http://www.keepalived.org/software/keepalived-1.4.2.tar.gz
yum -y install openssl openssl-devel  
yum -y install libnl libnl-devel
yum install -y libnfnetlink-devel

./configure --prefix=/usr/local/keepalived
make
make install

cp keepalived/etc/init.d/keepalived /etc/init.d/
cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp sbin/keepalived /usr/sbin/

查看vip是否抢占
ip addr show dev ens160

keepalived.conf

! Configuration File for keepalived
       
global_defs {
notification_email {
xxx@xxx.cn
}
       
notification_email_from ops@wangshibo.cn
smtp_server 127.0.0.1 
smtp_connect_timeout 30
router_id MASTER-HA
}
       
vrrp_script chk_mysql_port {     #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/opt/chk_mysql.sh"   #这里通过脚本监测
    interval 2                   #脚本执行间隔,每2s检测一次
    weight -5                    #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
    fall 2                    #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
    rise 1                    #检测1次成功就算成功。但不修改优先级
}
       
vrrp_instance VI_1 {
    state BACKUP  
    interface ens160      #指定虚拟ip的网卡接口
    mcast_src_ip xxxxx
    virtual_router_id 51    #路由器标识,MASTER和BACKUP必须是一致的
    priority 101            #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来 
    advert_int 1         
    authentication {   
        auth_type PASS 
        auth_pass 1111     
    }
    virtual_ipaddress {    
        xxxxxx 没有被分配的ip
    }
      
track_script {               
   chk_mysql_port             
}
}

非抢占模式

! Configuration File for keepalived

global_defs {
   router_id HA_MySQL
}       
vrrp_script chk_mysql_port {     #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/opt/keepalived_check_mysql.sh"   #这里通过脚本监测
    interval 2                   #脚本执行间隔,每2s检测一次
    weight -5                    #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
    fall 2                    #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
    rise 1 
}
vrrp_sync_group VG1 {

    group {

          VI_1

    }

}
vrrp_instance VI_1 {

    state BACKUP         #指定主服务器节点为主节点,备用节点上设置需一致

    interface ens160                  #指定虚拟IP的网络接口

    virtual_router_id 88   #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组

    priority 90                #主节点的优先级(1-254之间),备用节点必须比主节点优先级低。

    advert_int 1                 #组播信息发送间隔,两个节点设置必须一样

    nopreempt

    authentication {             #设置验证信息,两个节点必须一致

        auth_type PASS

        auth_pass 1111

    }   
    virtual_ipaddress {    
        192.168.3.10
    }
      
track_script {               
   chk_mysql_port             
}
}
! Configuration File for keepalived

global_defs {
   router_id HA_MySQL
}       
vrrp_script chk_mysql_port {     #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
    script "/opt/keepalived_check_mysql.sh"   #这里通过脚本监测
    interval 2                   #脚本执行间隔,每2s检测一次
    weight -5 
    fall 2 
    rise 1
}
vrrp_sync_group VG1 {

    group {

          VI_1

    }

}
vrrp_instance VI_1 {

    state BACKUP         #指定主服务器节点为主节点,备用节点上设置需一致

    interface ens160                  #指定虚拟IP的网络接口

    virtual_router_id 88   #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组

    priority 100                #主节点的优先级(1-254之间),备用节点必须比主节点优先级低。

    advert_int 1                 #组播信息发送间隔,两个节点设置必须一样

    nopreempt

    authentication {             #设置验证信息,两个节点必须一致

        auth_type PASS

        auth_pass 1111

    }   
    virtual_ipaddress {    
        192.168.3.10
    }
      
track_script {               
   chk_mysql_port             
}
}
keepalived_check_mysql.sh
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql

MYSQL_HOST=localhost

MYSQL_USER=root

MYSQL_PASSWORD=123456

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 -9 keepalived

    exit 1

fi

sleep 1

Done