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:
cp /usr/local/keepalived/sbin/keepalived /sbin/
echo 'export PATH=/usr/local/keepalived/sbin:$PATH;' >/etc/profile.d/keepalived.sh
具体keepalived配置文件如下:
-----------------------------------------
! Configuration File for keepalived
global_defs {
lvs_id LVS_redis
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from
[email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
weight -20
interval 2
}
vrrp_instance VI_1 {
state backup
interface eth0
virtual_router_id 51
#设置原主机恢复正常时,服务不漂移;
nopreempt
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_redis
}
virtual_ipaddress {
192.168.16.77/24 dev eth0 label eth0:1
}
notify_master "/etc/keepalived/scripts/redis_script.sh master"
notify_backup "/etc/keepalived/scripts/redis_script.sh backup"
notify_fault "/etc/keepalived/scripts/redis_script.sh fault"
}
----------------------------------------
SLAVER上与MASTER上大致相同,改下这两个就可以了 state BACKUP
priority 80
4.keepalive自定义监控功能说明:
notify_master keepalived切换到master时执行的脚本
notify_backup keepalived切换到backup时执行的脚本
notify_fault keepalived出现故障时执行的脚本
vrrp_script代码块是用来定义监控脚本,脚本执行时间间隔以及脚本的执行结果导致优先级变更幅度的。
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" #指定执行脚本的路径
interval 1 #指定脚本的执行时间间隔
weight 10 #脚本结果导致的优先级变更:10表示优先级+10;-10则表示优先级-10
}
#定义好vrrp_script代码块之后,就可以在instance中使用了
track_script {
chk_redis
}
注意:VRRP脚本(vrrp_script)和VRRP实例(vrrp_instance)属于同一个级别
5.建立相关脚本:
redis服务状态检测脚本redis_check.sh
#!/bin/bash
#auth desheng
export REDIS_CLI="/usr/local/bin/redis-cli"
export REDIS_SER="/usr/local/bin/redis-server"
export LOGFILE="/var/log/keepalived-redis-state.log"
export MASTER_HOST="192.168.16.131"
export MASTER_PORT="6379"
export ETH_FLAG='eth0:3'
REDIS_ROLE=`$REDIS_CLI info|grep role|sed 's/\r//g'`;
REDIS_STATUS=`$REDIS_CLI PING 2>&1`;
VIP_CNT=`ifconfig |grep ${ETH_FLAG}|wc -l`;
PRINT_LOG() {
LAST_MSG=`tail -1 $LOGFILE|awk -F']' '{print $2}'`;
if [ "${LAST_MSG}" != $1 ] ;then
echo "`date '+%F %T'`[check]$1" >>$LOGFILE;
fi;
}
if [ "$REDIS_STATUS" == "PONG" ] ;then
PRINT_LOG "sucess...$REDIS_STATUS";
if [ "$VIP_CNT" -eq 0 -a "${REDIS_ROLE}" != "role:slave" ] ;then
$REDIS_CLI SLAVEOF $MASTER_HOST $MASTER_PORT;
echo "`date '+%F %T'`[check]Starting Slaveof $MASTER_HOST $MASTER_PORT...">>$LOGFILE;
fi;
exit 0;
elif [ "$REDIS_STATUS" != "PONG" -a ${VIP_CNT} -eq 1 ] ;then
sleep 1;
REDIS_STATUS=`$REDIS_CLI PING 2>&1`;
if [ "$REDIS_STATUS" == "PONG" ] ;then
PRINT_LOG "sucess...$REDIS_STATUS";
exit 0;
else
PRINT_LOG "faild...$REDIS_STATUS";
ps aux|grep redis-server|grep -v grep|awk '{print "kill -9 " $2}'| sh
exit 1;
fi;
elif [ "$REDIS_STATUS" != "PONG" -a ${VIP_CNT} -eq 0 ] ;then
sleep 1;
REDIS_STATUS=`$REDIS_CLI PING 2>&1`;
if [ "$REDIS_STATUS" == "PONG" ] ;then
PRINT_LOG "sucess...$REDIS_STATUS";
exit 0;
elif [ "$REDIS_STATUS" == "LOADING Redis is loading the dataset in memory" ] ;then
while [ "$REDIS_STATUS" == "LOADING Redis is loading the dataset in memory" ] ;do
sleep 2;
REDIS_STATUS=`$REDIS_CLI PING 2>&1`;
done;
REDIS_STATUS=`$REDIS_CLI PING 2>&1`;
if [ "$REDIS_STATUS" == "PONG" ] ;then
PRINT_LOG "sucess...$REDIS_STATUS";
exit 0;
else
PRINT_LOG "faild...$REDIS_STATUS";
ps aux|grep redis-server|grep -v grep|awk '{print "kill -9 " $2}'| sh
$REDIS_SER /etc/redis/6379.conf
exit 1;
fi;
else
PRINT_LOG "faild...$REDIS_STATUS";
ps aux|grep redis-server|grep -v grep|awk '{print "kill -9 " $2}'| sh
$REDIS_SER /etc/redis/6379.conf
exit 1;
fi;
fi;
redis服务状态处理脚本redis_script.sh
#!/bin/bash
#auth desheng
export REDIS_CLI="/usr/local/bin/redis-cli"
export REDIS_SER="/usr/local/bin/redis-server"
export LOGFILE="/var/log/keepalived-redis-state.log"
export MASTER_HOST="192.168.16.131"
export MASTER_PORT="6379"
check_redis(){
REDIS_STATUS=`$REDIS_CLI PING 2>&1`;
if [ "$REDIS_STATUS" != "PONG" ] ;then
sleep 1;
REDIS_STATUS=`$REDIS_CLI PING 2>&1`;
if [ "$REDIS_STATUS" != "PONG" ] ;then
ps aux|grep redis-server|grep -v grep|awk '{print "kill -9 " $2}'| sh
$REDIS_SER /etc/redis/6379.conf
echo "`date '+%F %T'`[$1]restart redis...">>$LOGFILE;
fi;
elif [ "$REDIS_STATUS" == "PONG" ] ;then
echo "`date '+%F %T'`[$1]redis is running...">>$LOGFILE;
fi;
}
echo "[$1]...">>$LOGFILE;
case $1 in
master)
check_redis master;
$REDIS_CLI SLAVEOF NO ONE >> $LOGFILE 2>&1;
echo "`date '+%F %T'`[master]Slaveof no one...">>$LOGFILE;
;;
backup)
check_redis backup;
REDIS_ROLE="role:slave";
REDIS_MASTER_HOST="master_host:$MASTER_HOST";
NOW_REDIS_ROLE=`${REDIS_CLI} info|grep role|sed 's/\\r//g'`;
NOW_REDIS_MASTER=`${REDIS_CLI} info|grep master_host|sed 's/\\r//g'`;
if [ "$REDIS_ROLE" == "$NOW_REDIS_ROLE" -a "$REDIS_MASTER_HOST" == "$NOW_REDIS_MASTER" ] ;then
echo "`date '+%F %T'`[backup]Slaveof $MASTER_HOST $MASTER_PORT...">>$LOGFILE;
else
$REDIS_CLI SLAVEOF $MASTER_HOST $MASTER_PORT >> $LOGFILE 2>&1;
echo "`date '+%F %T'`[backup]Starting Slaveof $MASTER_HOST $MASTER_PORT...">>$LOGFILE;
echo "`date '+%F %T'`[backup]Redis status is `$REDIS_CLI info|grep role`" >> $LOGFILE;
fi;
;;
fault)
check_redis fault;
;;
*)
echo "####error">>$LOGFILE;
;;
esac
6.验证测试:
service keepalived start
ifconfig
pkill -9 redis-server
ifconfig