在Redis-Sentinel的client-reconfig-script脚本中设置VIP

当使用Redis-Sentinel做冗余时,如何以不同方式使用VIP,我认为使用client-reconfig-script脚本是一种可行方法,让我们试试。


redis1 172.16.171.100/24
redis2 172.16.171.110/24

redis3 172.16.171.120/24

VIP 172.16.171.250/24



sentinel.conf配置:

port 26379
daemonize yes
logfile "/usr/local/redis/sentinel.log"
dir "/usr/local/redis/tmp"
sentinel monitor mymaster 172.16.171.120 6379 2
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 60000
sentinel notification-script mymaster /usr/local/redis/notify.sh
sentinel client-reconfig-script mymaster /usr/local/redis/failover.sh
sentinel auth-pass mymaster skymobi


failover.sh

#!/bin/sh
_DEBUG="on"
DEBUGFILE=/usr/local/redis/sentinel_failover.log
VIP='172.16.171.250'
MASTERIP=${6}
MASK='24'
IFACE='eth0'
MYIP=$(ip -4 -o addr show dev ${IFACE}| grep -v secondary| awk '{split($4,a,"/");print a[1]}')

DEBUG () {
        if [ "$_DEBUG" = "on" ]; then
                $@
        fi
}

rm -rf ${DEBUGFILE}
set -e
DEBUG date >> ${DEBUGFILE}
DEBUG echo $@ >> ${DEBUGFILE}

DEBUG echo "Master: ${MASTERIP} My IP: ${MYIP}" >> ${DEBUGFILE}
if [ ${MASTERIP} == ${MYIP} ]; then
        if [ $(ip addr show ${IFACE} | grep ${VIP} | wc -l) -eq 0 ]; then
                /sbin/ip addr add ${VIP}/${MASK} dev ${IFACE}
                DEBUG echo "sudo /sbin/ip addr add ${VIP}/${MASK} dev ${IFACE}" >> ${DEBUGFILE}
                /usr/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
        fi
        exit 0
else
        if [ $(ip addr show ${IFACE} | grep ${VIP} | wc -l) -ne 0 ]; then
                /sbin/ip addr del ${VIP}/${MASK} dev ${IFACE}
                DEBUG echo "sudo /sbin/ip addr del ${VIP}/${MASK} dev ${IFACE}" >> ${DEBUGFILE}
        fi
        exit 0
fi
exit 1


结论

之后你可以尝试kill master而不是宕机来测试failover,我认为这是个比较好且容易实现的方法。 
sentinel down-after-milliseconds mymaster 3000 
大约在3秒左右会检测到redis的宕机。在更恶劣的环境,可以尝试减小这个值。


你可能感兴趣的:(redis,vip,sentinel)