记录keepalived搭建HA服务提供IP或者服务的HA
安装:
安装一般会有2种方式:
1,rpm包安装;
2,源码编译安装。
这里不详细做安装配置的详解,仅记录,方便后期环境的快速构建和回忆。
Keepalived配置
主:
! Configuration File for keepalived
global_defs {
router_id LVS_NODE1
}
vrrp_script chk_mysql {
script "/test/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_script chk_nginx {
script "/test/chk_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.20
}
track_script {
chk_mysql
chk_nginx
}
}
关闭防火墙
启动keepalived服务
验证VIP的有效性,可以ping 192.168.1.20验证
备:
! Configuration File for keepalived
global_defs {
router_id LVS_NODE18
}
vrrp_script chk_mysql {
script "/test/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_script chk_nginx {
script "/test/chk_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.20
}
track_script {
chk_mysql
chk_nginx
}
}
关闭防火墙
启动keepalived服务
验证VIP的有效性,可以ping 192.168.1.20验证。
注意:该VIP在主宕机恢复不对vip进行抢占,该种情况是由于state状态是backup,并且nopreempt 配置,其中track_script为监控的服务,如没有可以删除。
chk_mysql脚本
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306" |wc -l)
if [ "${counter}" -eq 0 ];then
echo "mysql 3306 down"
systemctl stop keepalived
else
echo "mysql is running"
fi
chk_nginx脚本
#!/bin/bash
counter=$(ps -C nginx --no-header | wc -l)
if [ "$counter" -eq 0 ];then
systemctl start nginx
sleep 2
if [ "$(ps -C nginx --no-header | wc -l)" -eq 0 ];then
systemctl stop nginx
fi
fi