1:主redis发生故障时,从redis自动切换为主redis

2:主redis恢复正常后,自动降级为从redis

 

 

试验环境:

     redis192.168.189.138 

     redis192.168.189.131

         VIP192.168.189.123

主从redis均安装了keepalivedredisredis已配置好主从。Redis端口44444。

 

 

1:制作redis监控与主备切换脚本:

 Redis监控脚本:(监控redis是否正常)

# cat /usr/local/scripts/redis_monitor.sh
#!/bin/bash    
ALIVE=$(/home/usr/local/redis/bin/redis-cli -h 192.168.189.138 -p 44444 PING)
if [ "$ALIVE" =="PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
killall -9 keepalived
service network restart
exit 1
fi


 redis_master脚本:(用于从redis切换成主redis

# cat /usr/local/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/home/usr/local/redis/bin/redis-cli -h 192.168.189.138 -p 44444 "
LOGFILE="/home/usr/local/redis/var/keepalived-redis-state.log"
echo "[master]" >>$LOGFILE
date >> $LOGFILE
echo "Being master....">> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
#指定自己为131的slave
$REDISCLI SLAVEOF 192.168.189.13144444 >> $LOGFILE  2>&1
#延迟10秒,用于数据同步
sleep 10                                      
echo "Run SLAVEOF NO ONE cmd..." >> $LOGFILE
#解除自己的slave状态,成为master
$REDISCLI SLAVEOF NO ONE >>$LOGFILE 2>&1


 Redis_backup脚本:(用于主redis切换成从redis

# cat /usr/local/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/home/usr/local/redis/bin/redis-cli -h 192.168.189.138 -p 44444"
LOGFILE="/home/usr/local/redis/var/keepalived-redis-state.log"
echo "[backup]" >>$LOGFILE
date >> $LOGFILE
echo "Being slave....">> $LOGFILE 2>&1
sleep 15
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.189.13144444 >> $LOGFILE  2>&1


以上是主redis上的脚本,从redis上的脚本只要更改脚本中相应的IP

 


2:配置keepalived

 主节点:

# cat /usr/local/keepalived/etc/keepalived/keepalived.conf
global_defs {
 router_id LVS_DEVEL
}
vrrp_script redis_monitor {                ------监控脚本
 script"/usr/local/scripts/redis_monitor.sh"
 interval 2 
 weight 2 
}
vrrp_instance VI_1{ 
 state BACKUP                     --------状态同备用节点一致,为BACKUP
 interface eth0
 virtual_router_id 51 
 mcast_src_ip 192.168.189.138
 nopreempt                      ----------设置不抢占IP
 priority 100                   ----------优先级
 advert_int 1
 authentication {
 auth_type PASS
 auth_pass password_123
}
 track_script {
 redis_monitor                   -------与上面的监控脚本一致
}
 virtual_ipaddress {
 192.168.189.123
 }
 notify_master/usr/local/scripts/redis_master.sh   -------keepalived切换为master时运行
 notify_backup/usr/local/scripts/redis_backup.sh   -------keepalived切换为slave时运行
}


 从节点:

# cat /usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
 router_id LVS_DEVEL
}
vrrp_script redis_monitor{
 script"/usr/local/scripts/redis_monitor.sh"
 interval 2 
 weight 2 
}
 
vrrp_instance VI_1{ 
 state BACKUP                     ----------与主节点一致,BAKCUP
 interface eth0
 virtual_router_id 51 
 mcast_src_ip 192.168.189.131
 priority 99                     -----优先级小于主节点
 advert_int 1 
 authentication {
 auth_type PASS
 auth_pass password_123
}
 track_script {
 redis_monitor 
}
 virtual_ipaddress {
 192.168.189.123
 }
notify_master/usr/local/scripts/redis_master.sh
notify_backup/usr/local/scripts/redis_backup.sh
}


 

3:开启rediskeepalived

# /usr/local/redis/bin/redis-server /home/usr/local/redis/etc/redis.conf
# /usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf


 

4:观察测试

 启动redis-server后,可以看到138主机抢占了VIP123,并且redis角色为Master

wKioL1dsnzeySe3YAAAYhenpztE563.png

wKioL1dsnzfQB7bfAAAbr--Osl4448.png

 131主机没抢占到123IPredis角色为Slave

wKioL1dsn3_B1rh4AAAZuHBzfG0297.png

 

 主节点上配置数据,查看从节点是否同步。

 主:

keepalived实现redis主备切换_第1张图片

 从:

keepalived实现redis主备切换_第2张图片

 以上可看出主备数据同步正常。

 

 1,关闭主redis,查看从节点是否抢占IP,从redis是否变为主redis

 #/home/usr/local/redis/bin/redis-cli -h 192.168.189.138 -p 44444 shutdown

 查看从节点日志:

wKioL1dsn8bgu26eAACsz-sQkXA663.png

 以上日志显示131节点抢占到了IP123,成为keepalived主节点。

keepalived实现redis主备切换_第3张图片

 以上日志显示131节点的redis角色变为Master

 

 查看131redis角色状态:

wKiom1dsoB_jU556AAAY_kLHMrw901.png

 131redis已经成功从slave变为master

 

 2,恢复138原主节点的redis状态,测试原来的主redis是否会自动降级为slave

 在主节点上重新开启keepalived,redis(注意要先开启redis,再开启keepalived)。

# /home/usr/local/redis/bin/redis-server /home/usr/local/redis/etc/redis.conf
# /usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf

 查看138节点日志:

wKioL1dsoTOAUvHAAAC9Odo-qh4584.png 


可以看到keepalived原主节点自动进入BACKUP状态,并未抢占123VIP

keepalived实现redis主备切换_第4张图片

 Redis角色也自动变更为Slave

 

 查看主备redis角色:

wKiom1dsoXPhLBB7AAAaJqjsXaU677.png

wKiom1dsoXTydtWKAAAZLcb7Kkk651.png 

 试验目标完成!