利用keepalived实现redis高可用
环境介绍:
master: 192.168.0.100
slave: 192.168.0.101
vip(keepvlied虚拟IP): 192.168.0.200
注:这里有一个坑
就是在同一网段之间的Keepalived的配置文件中需要把 virtual_router_id 后面的值设置不同(范围是0-255),否则,keepalived在主从切换的时间会有问题,而且在/var/log/messages日志中
会不断的写入日志,会把服务器写爆。
virtual_router_id 每一套都需要设置不同(范围是0-255)
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失
在Master和Slave上安装Keepalived
yum install -y keepalived
首先,在Master上创建如下配置文件:
! Configuration File for keepalivedglobal_defs { router_id redis100}vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379" interval 2 timeout 2 fall 3}vrrp_instance redis { state MASTER # master set to SLAVE also interface eth0 virtual_router_id 50 priority 150 nopreempt # 非抢占模式,注:主与从必须同时设置为BACKUP advert_int 1authentication { #all node must same auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.200/24 } track_script { chk_redis } notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.0.101 6379" notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.0.101 6379" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
然后,在Slave上创建如下配置文件:
! Configuration File for keepalivedglobal_defs { router_id redis101}vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379" interval 2 timeout 2 fall 3}vrrp_instance redis { state BACKUP interface eth0 virtual_router_id 50 priority 100 advert_int 1authentication { #all node must same auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.200/24 } track_script { chk_redis } notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.0.100 6379" notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.0.100 6379" 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/redis/redis-cli -h $1 -p $2 PING` LOGFILE="/var/log/keepalived-redis-check.log" echo "[CHECK]" >> $LOGFILEdate >> $LOGFILEif [ $ALIVE == "PONG" ]; then : echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1 exit 0 else echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1 exit 1 fi
编写以下负责运作的关键脚本
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
因为Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop
首先,在Redis Master上创建notity_master与notify_backup脚本:
cat /etc/keepalived/scripts/redis_master.sh
#!/bin/bash REDISCLI="/usr/redis/redis-cli -h $1 -p $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run MASTER cmd ..." >> $LOGFILE 2>&1$REDISCLI SLAVEOF $2 $3 >> $LOGFILE sleep 10 #delay 10 s wait data async cancel syncecho "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
cat /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash REDISCLI="/usr/redis/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1 # echo "Being slave...." >> $LOGFILE 2>&1 sleep 15 #delay 15 s wait data sync exchange role
接着,在Redis Slave上创建notity_master与notify_backup脚本:
cat /etc/keepalived/scripts/redis_master.sh
#!/bin/bash REDISCLI="/usr/redis/redis-cli -h $1 -p $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ... " >> $LOGFILE $REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1#echo "SLAVEOF $2 cmd can't excute ... " >> $LOGFILE sleep 10 ##delay 15 s wait data sync exchange roleecho "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
cat /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash REDISCLI="/usr/redis/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[BACKUP]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1$REDISCLI SLAVEOF $2 $3 >> $LOGFILE sleep 100 #delay 10 s wait data async cancel sync exit(0)
然后在Master与Slave创建如下相同的脚本:
cat /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[fault]" >> $LOGFILEdate >> $LOGFILE
cat /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
给脚本都加上可执行权限:
chmod +x /etc/keepalived/scripts/*.sh
脚本创建完成以后,我们开始按照如下流程进行测试:1.启动Master上的Redis
$ /etc/init.d/redis start
2.启动Slave上的Redis
$ /etc/init.d/redis start
3.启动Master上的Keepalived
$ /etc/init.d/keepalived start
4.启动Slave上的Keepalived
$ /etc/init.d/keepalived start
5.尝试通过VIP连接Redis:
$ redis-cli -h 10.6.1.200 INFO
连接成功,Slave也连接上来了。
role:masterslave0:192.168.0.101,6379,online
6.尝试插入一些数据:
$ redis-cli -h 10.6.1.200 SET Hello RedisOK
从VIP读取数据
$ redis-cli -h 10.6.1.200 GET Hello"Redis"
从Master读取数据
$ redis-cli -h 10.6.1.143 GET Hello"Redis"
从Slave读取数据
$ redis-cli -h 10.6.1.144 GET Hello"Redis"
下面,模拟故障产生:
将Master上的Redis停了
本方来自:http://blog.csdn.net/huwei2003/article/details/41894197