Setup HA Redis using Redis Sentinel part 1

Standard

In this blog post i will walk through setting up a HA redis cluster using Redis Sentinel. Just like NDB cluster, it is LONG so I break it up to 2 blog post.

Part 1 focus on setting up a master and slave. Part 2 focus on setting up the sentinel

Step 1: Set up 5 instances. I will name them as:

redis-master
redis-slave
redis-sentinel-1
redis-sentinel-2
redis-sentinel-3

Step 2: Install make, gcc and cc in all the five instances

yum install make gcc cc

Step 3: Download the redis source code in both the redis-master and redis-slave

wget http://download.redis.io/releases/redis-2.8.17.tar.gz

Step 4: SSH into redis-master, unzip the tar file

tar -zxvf redis-2.8.17.tar.gz

Step 5: cd into the folder and make

cd redis-2.8.17
make
yum install -y tcl
make test

Step 6: Move redis-server and redis-cli into /usr/local/bin, and move your main redis files to /var/lib

cp src/redis-server /usr/local/sbin
cp src/redis-cli /usr/local/sbin
cp -R /opt/redis-2.8.17 /var/lib
cp redis.conf /etc/

Step 8a: change the redis daemon in redis.conf from no to yes

Step 8b: set the logfile=”<your logfile location>”

BTW, redis have a very nice log file

[28412] 06 Jan 12:57:54.385 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 2.8.17 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in stand alone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 28412
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'
[28412] 06 Jan 12:57:54.386 # Server started, Redis version 2.8.17

Step 9: Copy the following code into /etc/init.d/redis

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

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

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis 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 to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

Step 10: run Redis by command /etc/init.d/redis start

Step 11: Check that redis is running by running “redis-cli ping”. you should get a pong answer

Step 12: Image / Snapshot the master disk and use it to create a slave instance

Step 13: set the slaveof setting of the slave instance

slaveof <masterip> <masterport>

That is all to set up a master and slave redis. Next part 2 is to set up a sentinel

Advertisements

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

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