一、keepalived安装
#下载安装包:
wget http://www.keepalived.org/software/keepalived-2.0.11.tar.gz
#下载安装依赖:
yum install -y openssl-devel popt-devel
yum install libnl*
yum install y popt-static
#解压并编译安装:
tar -zxvf keepalived-2.0.11.tar.gz
cd keepalived-2.0.11
./configure --prefix=/var/local/keepalived --sysconf=/etc
make && make install
#为命令创建软连接:
cp /var/local/keepalived/sbin/keepalived /usr/sbin/
#备份配置文件
cd /etc/keepalived
mv keepalived.conf keepalived.conf.bak
二、配置文件修改(MySQL)
[root@hx-crm-ddb-data1 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_mysql {
script "etc//etc/keepalived/check_mysql.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 90/80
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_mysql
}
virtual_ipaddress {
10.0.0.100
}
}
/etc/keepalived/check_mysql.sh脚本:
#!/bin/bash
mysql_id=`ps -C mysqld --noheader |wc -l`
echo $mysql_id
echo "enter"
if [ "$mysql_id" = "0" ];then
echo "0"
#pkill keepalived
systemctl stop keepalived.service
fi
chmod u+x check_mysql.sh
systemctl restart keepalived.service
三、配置文件参考(nginx)
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
######监控nginx的脚本###############
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER ####主用MASTER,从用BACKUP ####
interface eth0 ####监测的网络接口
virtual_router_id 51 ####主从上要设置一样的值
priority 100 ####权重,备机比主机稍微调小一点就行,备机50
advert_int 1
authentication {
auth_type PASS ####VRRP认证方式
auth_pass 1111
}
track_script {
check_nginx ####调用的脚本
}
virtual_ipaddress {
10.0.0.200 #####负载VIP的IP地址
}
/etc/keepalived/check_nginx.sh脚本:
cat /etc/keepalived/check_nginx.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/check_nginx.sh
/etc/init.d/keepalived start || systemctl restart keepalived.service