使用keepalived实现高可用

通常情况下,都是启动2台相同的业务系统,一台故障,另外一台自动接管。后端一般情况下我们使用集群,排除了单点故障造成的影响,前段7层负载的高可用就尤为重要了
keepalived使用vrrp协议,虚拟路由冗余协议。依靠vmac和vip实现高可用技术方案,俗称地址漂移。

抢占式高可用配置

1.实践环境,配置实现虚IP转移
状态  IP  角色
节点1 10.0.0.5    Master
节点2 10.0.0.6    Backup
VIP     10.0.0.3    
 
安装keepalived
yum install keepalived -y
 
配置Master的Keepalived
global_defs {                   #全局配置
    router_id lb01              #表示身份->名称
}
 
vrrp_instance VI_1 {
    state MASTER                #标识角色状态
    interface eth0              #网卡绑定接口
    virtual_router_id 50        #虚拟路由id
    priority 150                #优先级
    advert_int 1                #监测间隔时间
    authentication {            #认证
        auth_type PASS          #明文认证
        auth_pass 1111          #明文密码
}
    virtual_ipaddress {         
        10.0.0.3                #虚拟的VIP地址
    }
}
 
配置Backup的Keepalived
 
global_defs {
    router_id lb02
}
 
vrrp_instance VI_1 {
    state BACKUP        
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {    
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        10.0.0.3
    }
}
#注意事项优先级要设置的不一样,vip地址是虚拟出来的,当然公网地址要真实存在
 
非抢占式区别
 
1、两个节点的state都必须配置为BACKUP
    2、两个节点在vrrp层加上配置 nopreempt
    3、其中一个节点的优先级必须要高于另外一个节点的优先级。
两台服务器都角色状态启用nopreempt后,必须修改角色状态统一为BACKUP,唯一的区分就是优先级。
 

为keepalived嵌入脚本运行

2.修改keepalived配置文件
[root@lb01 conf.d]# cat /etc/keepalived/keepalived.conf 
global_defs {     
    router_id lb01   
}
 
#定义脚本存放的位置
vrrp_script check_web {
   script "/scripts/check_web.sh"
   interval 5     #脚本执行间隔时间。一定要注意,这个时间要设置的比脚本执行时间长
}
 
vrrp_instance VI_1 {
    state BACKUP
    priority 150
    nopreempt
    interface eth0
    virtual_router_id 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        10.0.0.3
    }
 
    #调用并运行该脚本
    track_script {
        check_web
    }
}
 

检测nginx并试图拉起的脚本

#!/bin/bash
Nginx_status=systemctl status nginx|awk  '/Active/ {print $2}'
if [ ! $Nginx_status == "active" ];then
 
        systemctl start nginx
        sleep 3
        Nginx_status=systemctl status nginx|awk  '/Active/ {print $2}'
        case $Nginx_status in
        active)
                echo "start nginx succes,nginx is running"
        ;;
        inactive)
                pkill keepalived
                if [ $? -eq 0 ]; then
                        echo "nginx is down ,kill keepalived"
                else
                        echo "keepalived cannot killed ,please go go"
                fi
        ;;
        *)
                pkill keepalived
                if [ $? -eq 0 ]; then
                        echo "nginx is down ,kill keepalived"
                else
                        echo "keepalived cannot killed ,please go go"
                fi
        esac
fi
 

备用机上,监控脑裂,并解决的脚本

#!/bin/bash
vip=10.0.0.3
master_ip=10.0.0.5
Date=date +%F--%X
IP=ifconfig eth0|awk '/inet /{print $2}'
echo "$Date$hostname${IP}_Spiltbarin,do kill suessed" >/scripts/spilt.txt
while true;
do  
    ping -c 2 -W 2 $master_ip &>/dev/null
    Result=$?
    ip add|grep $vip &>/dev/null
    if [ $Result -eq 0 -a $? -eq 0  ];then
    pkill keepalived &>/dev/null
    echo "warining..spilt barin"
        if [ $? -eq 0 ];then
            mail -s " warning" "[email protected]" < /scripts/spilt.txt
        else 
            mail -s "spiltbrain ,please go go go" "[email protected]" < /scripts/spilt.txt
        fi      
    fi  
    sleep 5
    ping -c 2 -W 2 $vip &>/dev/null 
    if [ ! $? -eq 0  ];then
    systemctl start keepalived 
    echo "$vip laqi"
    fi
done
 

你可能感兴趣的:(使用keepalived实现高可用)