1.下载:
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
2.解压安装:
yum install ipvsadm
tar -zxvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --prefix=/usr/local/keepalived-1.2.15 --disable-fwmark --sysconfdir=/etc
make && make install
cd /usr/local;
ln -s keepalived-1.2.15 keepalived
3.配置keepalive:
[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
lvs_id LVS_nginx
notification_email {
[email protected]
}
notification_email_from
[email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/scripts/nginx_check.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 61
nopreempt
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass nginx
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.16.13/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/scripts/nginx_script.sh master"
notify_backup "/etc/keepalived/scripts/nginx_script.sh backup"
notify_fault "/etc/keepalived/scripts/nginx_script.sh master"
}
[root@localhost keepalived]# cat /etc/keepalived/scripts/nginx_script.sh
#!/bin/bash
#auth desheng
NGINXCMD=/usr/local/nginx/sbin/nginx;
HTTP_STATE=`curl -I http://localhost/check_ha_200.html 2>/dev/null |head -1|awk '{print $2}'`;
MASTER_CNT=`ps aux|grep "nginx:\ master\ process"|grep -v grep|wc -l`;
WORKER_CNT=`ps aux|grep "nginx:\ worker\ process"|grep -v grep|wc -l`;
VIP_CNT=`ifconfig |grep eth0:1|wc -l`;
LOGFILE=/var/log/keepalived_nginx_state.log;
if [ "$HTTP_STATE" == "200" -a "$MASTER_CNT" -eq "1" -a "$WORKER_CNT" -ge "1" ] ;then
echo "[$1]sucess...">>$LOGFILE
exit 0;
else
sleep 1;
HTTP_STATE=`curl -I http://localhost/check_ha_200.html 2>/dev/null |head -1|awk '{print $2}'`;
MASTER_CNT=`ps aux|grep "nginx:\ master\ process"|grep -v grep|wc -l`;
WORKER_CNT=`ps aux|grep "nginx:\ worker\ process"|grep -v grep|wc -l`;
VIP_CNT=`ifconfig |grep eth0:1|wc -l`;
if [ "$HTTP_STATE" == "200" -a "$MASTER_CNT" -eq "1" -a "$WORKER_CNT" -ge "1" ] ;then
echo "[$1]sucess...">>$LOGFILE
exit 0;
elif [ "$VIP_CNT" -eq "0" ] ;then
ps aux|grep 'nginx: worker process'|grep -v grep|awk '{print "kill -9 "$2}' | sh
ps aux|grep 'nginx: master process'|grep -v grep|awk '{print "kill -9 "$2}' | sh
$NGINXCMD
echo "[$1]restart...">>$LOGFILE;
fi;
fi;
[root@localhost keepalived]# cat /etc/keepalived/scripts/nginx_check.sh
#!/bin/bash
#auth desheng
NGINXCMD=/usr/local/nginx/sbin/nginx;
HTTP_STATE=`curl -I http://localhost/check_ha_200.html 2>/dev/null |head -1|awk '{print $2}'`;
MASTER_CNT=`ps aux|grep "nginx:\ master\ process"|grep -v grep|wc -l`;
WORKER_CNT=`ps aux|grep "nginx:\ worker\ process"|grep -v grep|wc -l`;
VIP_CNT=`ifconfig |grep eth0:1|wc -l`;
LOGFILE=/var/log/keepalived_nginx_state.log;
if [ "$HTTP_STATE" -eq "200" -a "$MASTER_CNT" -eq "1" -a "$WORKER_CNT" -ge "1" ] ;then
echo "[check]sucess...">>$LOGFILE
exit 0;
else
sleep 1;
HTTP_STATE=`curl -I http://localhost/check_ha_200.html 2>/dev/null |head -1|awk '{print $2}'`;
MASTER_CNT=`ps aux|grep "nginx:\ master\ process"|grep -v grep|wc -l`;
WORKER_CNT=`ps aux|grep "nginx:\ worker\ process"|grep -v grep|wc -l`;
if [ "$HTTP_STATE" -eq "200" -a "$MASTER_CNT" -eq "1" -a "$WORKER_CNT" -ge "1" ] ;then
echo "[check]sucess...">>$LOGFILE
exit 0;
else
echo "[check]failed...">>$LOGFILE
exit 1;
fi;
fi;
4.测试:
service keepalived start
ifconfig
pkill -9 nginx
ifconfig
ps aux|grep nginx