通过Keepalived实现Redis Failover自动故障切换功能
详细可参考/archives/1942.html
设计思路:
redis 主和 redis从 正常同步;
redis主keepalived 启动,执行redis_master.sh脚本,vip在自己身上;
redis 从keepalived 启动,执行redis_slave.sh脚本;
redis主服务关闭,则redis主执行redis_fault.sh脚本, vip落在redis 从上,redis从执行redis_master.sh脚本,变成主;
redis主服务开启,则redis主执行redis_slave.sh脚本变成从,vip还在redis 从上,还是主;
redis从服务关闭,则redis从执行redis_fault.sh脚本, vip落在redis主上,redis主执行redis_master.sh脚本,变成主;
redis从服务开启,则redis从执行redis_slave.sh脚本变成从, vip还在redis主上,redis主继续做主,此时为redis初试主从状态;
环境:
192.168.1.235 redis
192.168.1.236 redis-slave
192.168.1.237 vip
一:安装keepalived
(redis和redis-slave两台服务器都安装)
yum -y install ipvsadm
wget www.keepalived/software/keepalived-1.2.
tar -zxvf keepalived-1.2.
cd keepalived-1.2.2
./configure
configure时若出现以下结果:
魔法禁书目录小说
不推荐使用yum install -y kernel-devel安装 安装完成后建立一下链接在配置:
ln -s /usr/src/kernels/2.6.18-194.el5-i686/ /usr/src/linux/
上述过程完毕后,在进行configure:
make
make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
chmod 755 /etc/init.d/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#运行 由于keepalived配置文件不是在启动时一次性加载并读取完成的,所以必须是完整
路径,可以根据log判断是否加载了正确的配置文件
o holy night
keepalived –f /usr/local/etc/f
运行过程可以查看log:
tail -f /var/log/message
二:安装redis
(redis和redis-slave两台服务器都安装)
brictar -zxvf redis-2.4.
cd redis-2.4.17adverly
./configure
make
make install
redis-slve只需注意一点
[root@redis bin]# pwd
/usr/local/bin
[root@redis bin]# f
116 slaveof 192.168.1.235 6379
三:通过Keepalived实现Redis Failover自动故障切换
修改Master和Slave的/etc/hosts文件
[root@redis rc.d]# cat /etc/hosts
192.168.1.235 redis
192.168.1.236 redis-slave
Master上是
[root@redis rc.d]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=redis
Slave上是
[root@redis-slave linux]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=redis-slave
修改完主机名重启机器生效
默认安装完成keepalived之后是没有配置文件的,因此我们需要手动创建:
首先,在Master上创建如下配置文件:
[root@redis linux]# vim /etc/f
时间曲线
global_defs {majors
notification_email {
}
notification_email_from keepalived@redis
router_id redis
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 1
}
vrrp_instance mes_Redis {
state MASTER
interface eth0
garp_master_delay 10
smtp_alert
virtual_router_id 3
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
outlanders auth_pass 1111
}
virtual_ipaddress {
192.168.1.237
}
track_script {
chk_redis
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_slave.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
然后,在Slave上创建如下配置文件:
[root@redis-slave linux]# vim /etc/f
global_defs {
舞林争霸第五季
notification_email {
}
fzr notification_email_from keepalived@redis-slave催乳师培训
router_id redis-salve
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
interval 1
}
vrrp_instance mes_Redis {
state BACKUP
interface eth0
garp_master_delay 10
smtp_alert
virtual_router_id 3
priority 90
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.237
}
track_script {
chk_redis
}
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_slave.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}
在Master和Slave上创建监控Redis的脚本
mkdir /etc/keepalived/scripts
vim /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/bin/redis-cli PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
el
echo $ALIVE
exit 1
fi
编写以下负责运作的关键脚本:
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_slave.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
因为Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master