Master: 10.3.20.46
Slave: 10.3.20.47
Virtural IP Address (VIP): 10.3.20.48
Redis port: 17100
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
略。。。。。
略。。。。。
编辑主 keepalived 配置文件:
! Configuration File for keepalived
global_defs {
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 rz01
}
vrrp_script chk_redis {
script "/usr/local/keepalived/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
weight 2
}
vrrp_instance VI_1 {
state MASTER ###设置为MASTER
interface eth0 ###监控网卡
virtual_router_id 51
priority 101 ###权重值
authentication {
auth_type PASS ###加密
auth_pass redis ###密码
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
10.3.20.48 ###VIP
}
notify_master /usr/local/keepalived/scripts/redis_master.sh
notify_backup /usr/local/keepalived/scripts/redis_backup.sh
notify_fault /usr/local/keepalived/scripts/redis_fault.sh
notify_stop /usr/local/keepalived/scripts/redis_stop.sh
}
1.创建主 /usr/local/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/redis/bin/redis-cli -p 17100 PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
service keepalived stop
exit 1
fi
2.创建主 /usr/local/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -p 17100 SLAVEOF 10.3.20.47 17100 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI -p 17100 SLAVEOF NO ONE >> $LOGFILE 2>&1
3.创建主 /usr/local/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -p 17100 SLAVEOF 10.3.20.47 17100 >> $LOGFILE 2>&1
4.创建主 /usr/local/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
5.创建主 /usr/local/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
编辑备 keepalived 配置文件:
! Configuration File for keepalived
global_defs {
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 rz02
}
vrrp_script chk_redis {
script "/usr/local/keepalived/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
weight 2
}
vrrp_instance VI_1 {
state BACKUP ###设置为BACKUP
interface eth0 ###监控网卡
virtual_router_id 51
priority 100 ###比MASTRE权重值低
authentication {
auth_type PASS ###加密
auth_pass redis ###密码与MASTRE相同
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
10.3.20.48 ###VIP
}
notify_master /usr/local/keepalived/scripts/redis_master.sh
notify_backup /usr/local/keepalived/scripts/redis_backup.sh
notify_fault /usr/local/keepalived/scripts/redis_fault.sh
notify_stop /usr/local/keepalived/scripts/redis_stop.sh
}
1.创建备 /usr/local/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/redis/bin/redis-cli -p 17100 PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
service keepalived stop
exit 1
fi
2.创建备 /usr/local/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -p 17100 SLAVEOF 10.3.20.46 17100 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI -p 17100 SLAVEOF NO ONE >> $LOGFILE 2>&1
3.创建备 /usr/local/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -p 17100 SLAVEOF 10.3.20.46 17100 >> $LOGFILE 2>&1
4.创建备 /usr/local/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
5.创建备 /usr/local/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
sudo chmod +x /usr/local/keepalived/scripts/*.sh
1.分别启动Master上的Redis和keepalived
service redis start
service keepalived start
2.尝试通过VIP连接Redis:
./user/local/redis/redis-cli -h 10.3.20.48 -p 17100 INFO
连接成功,Slave也连接上来了。
role:master
slave0:10.6.1.144,6379,online
3.尝试插入一些数据:
./user/local/redis/redis-cli -h 10.3.20.48 -p 17100 SET Hello Redis
OK
从VIP读取数据
./user/local/redis/redis-cli -h 10.3.20.48 -p 17100 GET Hello
"Redis"
从Master读取数据
./user/local/redis/redis-cli -h 10.3.20.46 -p 17100 GET Hello
"Redis"
从Slave读取数据
./user/local/redis/redis-cli -h 10.3.20.47 -p 17100 GET Hello
"Redis"
下面,模拟故障产生:
将Master上的Redis服务停止:
service redis stop
查看Master上的Keepalived日志
tail -f /var/log/keepalived-redis-state.log
[fault]
Thu Sep 27 08:29:01 CST 2015
同时Slave上的日志显示:
tail -f /var/log/keepalived-redis-state.log
[master]
Fri Sep 28 14:14:09 CST 2015
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK
然后我们可以发现,Slave已经接管服务,并且担任Master的角色了。
./user/local/redis/redis-cli -h 10.3.20.48 -p 17100 INFO
./user/local/redis/redis-cli -h 10.3.20.47 -p 17100 INFO
role:master
然后我们恢复Master的Redis进程
service redis start
查看Master上的Keepalived日志
tail -f /var/log/keepalived-redis-state.log
[master]
Thu Sep 27 08:31:33 CST 2015
Being master....
Run SLAVEOF cmd ...
OK
Run SLAVEOF NO ONE cmd ...
OK
同时Slave上的日志显示:
tail -f /var/log/keepalived-redis-state.log
[backup]
Fri Sep 28 14:16:37 CST 2015
Being slave....
Run SLAVEOF cmd ...
OK
参考资料:http://heylinux.com/archives/1942.html