keepalived实现redis主备切换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived实现redis主备切换相关的知识,希望对你有一定的参考价值。
试验目标:
1:主redis发生故障时,从redis自动切换为主redis。
2:主redis恢复正常后,自动降级为从redis。
试验环境:
主redis:192.168.189.138
从redis:192.168.189.131
VIP:192.168.189.123
主从redis均安装了keepalived与redis,redis已配置好主从。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:开启redis与keepalived
# /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
131主机没抢占到123IP,redis角色为Slave
主节点上配置数据,查看从节点是否同步。
主:
从:
以上可看出主备数据同步正常。
1,关闭主redis,查看从节点是否抢占IP,从redis是否变为主redis
#/home/usr/local/redis/bin/redis-cli -h 192.168.189.138 -p 44444 shutdown
查看从节点日志:
以上日志显示131节点抢占到了IP123,成为keepalived主节点。
以上日志显示131节点的redis角色变为Master。
查看131redis角色状态:
131的redis已经成功从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节点日志:
可以看到keepalived原主节点自动进入BACKUP状态,并未抢占123VIP。
Redis角色也自动变更为Slave。
查看主备redis角色:
试验目标完成!
本文出自 “扮演上帝的小丑” 博客,转载请与作者联系!
以上是关于keepalived实现redis主备切换的主要内容,如果未能解决你的问题,请参考以下文章