Setup HA Redis using Redis Sentinel part 2

Standard

Sentinel are really badass creatures. Whether are they Marvel Sentinel or The Matrix Sentinel, they did a good job to protect their evil masters until the saviour do them in.

In the second part of the high availability set up, I will show how to set up redis sentinel

Step 1: Do Step 2 to step 5 in the part 1 post

Step 2: Copy src/redis-sentinel to /usr/local/sbin

Step 3: Copy sentinel.conf to /etc/redis-sentinel.conf

Step 4: Edit /etc/redis-sentinel.conf. search for sentinel monitor and replace 127.0.0.1 to the master sentinel internal IP.

Do take note that the last number on sentinel monitor is 2. What it means is it needs at least 2 sentinels to confirm that the master redis is down before they switch it to slave. You can set that between 1-3

Step 5: add the following lines to the end of the redis-sentinel.conf. Save the file

daemonize yes
logfile "/var/log/sentinel.log"

Step 6: Create /etc/init.d/redis-sentinel file and paste the following in:

#!/bin/sh
# chkconfig: 2345 95 20
# description: Simple Redis init.d script conceived to work on Linux systems
# processname: redis
# 

REDISPORT=6379
EXEC=/usr/local/sbin/redis-sentinel
CLIEXEC=/usr/local/sbin/redis-cli

PIDFILE=/var/run/redis-sentinel.pid
CONF="/etc/redis-sentinel.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis Sentinel server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis Sentinel to shutdown ..."
                    sleep 1
                done
                echo "Redis Sentinel stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

Step 7: Set up redis-sentinel proper

chmod +x /etc/init.d/redis-sentinel
chkconfig --add redis-sentinel
chkconfig --level 3 redis-sentinel on

Step 8: repeat all these steps for the remaining 2 sentinel

Step 9: run /etc/init.d/redis-sentinel for all

So your redis sentinel is ready for use! Do take note that it operates slightly different from normal standalone redis. You need to take the master value from sentinel. Sentinel will give you the IP from which to use which redis.

Happy developing!

Advertisements

One thought on “Setup HA Redis using Redis Sentinel part 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s