Last active
April 30, 2024 15:51
-
-
Save waja/301c5ca1b532669a9b6e to your computer and use it in GitHub Desktop.
Shell script to migrate a VIP to the actual redis master when managing them with redis-sentinel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
_DEBUG="on" | |
DEBUGFILE=/tmp/sentinel_failover.log | |
VIP='10.125.125.30' | |
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 | |
} | |
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) = 0 ]; then | |
sudo /sbin/ip addr add ${VIP}/${MASK} dev ${IFACE} | |
DEBUG echo "sudo /sbin/ip addr add ${VIP}/${MASK} dev ${IFACE}" >> ${DEBUGFILE} | |
sudo /usr/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE} | |
fi | |
exit 0 | |
else | |
if [ $(ip addr show ${IFACE} | grep ${VIP} | wc -l) != 0 ]; then | |
sudo /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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sudo /usr/sbin/arping -q -c 3 -A${VIP} -I $ {INTERFACE}. should be {IFACE} i think.
otherwise you get the error in sentinel.log:
-script-error /var/lib/redis/redis-sentinel-switch-state.sh 0 2