keepalived高可用
1、keepalived
通过vrrp协议实现的高可用. 虚拟路由冗余协议
[root@lb01 ~]# yum install keepalived -y [root@lb02 ~]# yum install keepalived -y
global_defs { router_id lb01 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 50 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } }
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 } }
[root@lb01 ~]# systemctl start keepalived [root@lb01 ~]# systemctl enable keepalived
4、主配置文件和备配置文件区别
priority(优先级) 150 100
if [ $? -eq 0 -a `ip add|grep "$vip"|wc -l` -eq 1 ];then echo "ha is split brain.warning." else echo "ha is ok" fi sleep 5 done-------------------------------------------------------------
[root@lb02 scripts]# cat check_split.sh vip=10.0.0.3 master_ip=10.0.0.5 while true;do ping -c 2 -W 3 $master_ip &>/dev/null if [ $? -eq 0 -a `ip add|grep "$vip"|wc -l` -eq 1 ];then echo "ha is split brain.warning." pkill keepalived &>/dev/null if [ $? -eq 0 ];then echo "Keepalived pkill" else exit fi else echo "ha is ok" fi sleep 5 done
[root@lb01 ~]# cat /scripts/check_web.sh #!/bin/sh nginxpid=$(ps -C nginx --no-header|wc -l) #1.判断Nginx是否存活,如果不存活则尝试启动Nginx if [ $nginxpid -eq 0 ];then systemctl start nginx sleep 3 #2.等待3秒后再次获取一次Nginx状态 nginxpid=$(ps -C nginx --no-header|wc -l) #3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本 if [ $nginxpid -eq 0 ];then systemctl stop keepalived fi fi
[root@lb01 conf.d]# chmod +x /scripts/check_web.sh
[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 } }