My organization has Redis running in a Master/Slave configuration, using Keepalived to switch between the two. It was discovered that when the Slave instance dies for some reason, Keepalived will not restart it. I started writing a cron job to check and see if redis is running, and if the slave dies, to restart it.
#!/bin/bash
#redis_cron_restart.sh
######variables########
REDIS_IP="10.19.105.229"
REDIS_CONF="/var/lib/redis/redis.conf"
#Check to see if Redis is running
killall -0 redis-server
if [ $? -eq 1 ]
then
date >> /var/lib/redis/log.txt
echo redis-server not running. Checking redis master >> /var/lib/redis/log.txt
redis-cli -h ${REDIS_IP} PING
if [ $? -eq 1 ]
then
echo redis master not running. Doing nothing. >> /var/lib/redis/log.txt
else
echo redis master is running. I must be the slave. Restarting keepalived >> /var/lib/redis/log.txt
redis-server ${REDIS_CONF}
sleep 1
wait
redis-cli SLAVEOF ${REDIS_IP} 6379
fi
else
echo redis-server running. >> /var/lib/redis/log.txt
fi
It runs, and it restarts Redis. It does not, however, put Redis into slave mode. If I type
redis-cli SLAVEOF 10.19.105.229 6379
at my terminal, however, it goes into slave mode. Any ideas?