Redis installation with three node and sentinel on each

This guide is based on these guides :
- Sean McGary : How to build a fault tolerant redis cluster with sentinel
- Data-Essential : Setup a secured redis cluster on CentOS7
~~-Pivotal : How to setup HAProxy and Redis Sentinal for auto failover and thus How to setp Redis Master Slave replication~~

We have three servers with theses configurations :

All three are on the same subnet

Installation

Configuration Redis

We are going to use the following masterpassword “azerty123456789” (please choose a real password for your configuration !!!)

On the master (redis-01)

Edit the redis configuration

vi /etc/redis.conf

Find and edit the following elements :

bind 172.16.1.43

ports 6379

daemonize yes

masterauth "azerty123456789"

requirepass "azerty123456789"

maxmemory 3584mb
maxmemory-policy noeviction

On the first slave (redis-02)

bind 172.16.1.95

ports 6379

daemonize yes

masterauth "azerty123456789"

slaveof 172.16.1.43

requirepass "azerty123456789"

maxmemory 3584mb
maxmemory-policy noeviction

On the second slave (redis-03)

bind 172.16.1.182

ports 6379

daemonize yes

masterauth "azerty123456789"

slaveof 172.16.1.43

requirepass "azerty123456789"

maxmemory 3584mb
maxmemory-policy noeviction

Start or restart the redis service
service redis restart

We normally have a functional redis master/slave/slave replication.

We now need to add redis-sentinel to have a selection of a new master if redis-01 become not available.

/!\ If you need high availability with HAProxy for example, you need to set the slave as not readonly!

Here the parameters to modify :

Configuration Redis Sentinel

We are going to use the following masterpassword “azerty123456789” (please choose a real password for you configuration !!!)

On the master (redis-01)

Edit the redis configuration

vi /etc/redis.conf

Find and edit the following elements :

bind 172.16.1.43

ports 26379

daemonize yes

sentinel monitor redis-cluster 172.16.1.43 6379 2
sentinel auth-pass redis-cluster azerty123456789
sentinel down-after-milliseconds redis-cluster 3000
sentinel parallel-syncs redis-cluster 1
sentinel failover-timeout redis-cluster 6000

On the first slave (redis-02)

bind 172.16.1.95

ports 26379

daemonize yes

sentinel monitor redis-cluster 172.16.1.43 6379 2
sentinel auth-pass redis-cluster azerty123456789
sentinel down-after-milliseconds redis-cluster 3000
sentinel parallel-syncs redis-cluster 1
sentinel failover-timeout redis-cluster 6000

On the second slave (redis-03)

bind 172.16.1.182

ports 26379

daemonize yes

sentinel monitor redis-cluster 172.16.1.43 6379 2
sentinel auth-pass redis-cluster azerty123456789
sentinel down-after-milliseconds redis-cluster 3000
sentinel parallel-syncs redis-cluster 1
sentinel failover-timeout redis-cluster 6000

Start or restart the redis-sentinel service
service redis-sentinel restart

Testing

Now connect to redis on redis-01 and kill it to test the sentinel reaction (display the log of redis-sentinel on 2&3 at the same time to see the reaction).

redis-cli -h 172.16.1.43 -p 26379 -a azerty123456789

Check redis-cluster status :
172.16.1.43:26379> sentinel ckquorum redis-cluster

List the slaves :
172.16.1.43:26379> sentinel slaves redis-cluster

redis-cli -h 172.16.1.43 -a azerty123456789
Now kill the master :
172.16.1.43:6379> debug segfault

And see the reaction in the log of 2&3.
redis-cli -h 172.16.1.43 -p 26379 -a azerty123456789

Return on sentinel and get the name of the master :
172.16.1.43:26379> sentinel get-master-addr-by-name redis-cluster

Optimizations

From the official Redis setup hints1 :

Set overcommit memory to 1

Make sure to set the Linux kernel overcommit memory setting to 1. Add vm.overcommit_memory = 1 to /etc/sysctl.conf and then reboot or run the command sysctl vm.overcommit_memory=1 for this to take effect immediately.

Disable Transparent huge pages

Make sure to disable Linux kernel feature transparent huge pages, it will affect greatly both memory usage and latency in a negative way. This is accomplished with the following command: echo never > /sys/kernel/mm/transparent_hugepage/enabled.

However, this can be tricky so here the method from the MongoDB guide.

If tuned is available :

Create a new profile.

Create a new tuned profile directory:
sudo mkdir /etc/tuned/no-thp

Edit tuned.conf.

Create and edit vi /etc/tuned/no-thp/tuned.conf so that it contains the following:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

Enable the new profile.

Finally, enable the new profile by issuing:
sudo tuned-adm profile no-thp

Test Your Changes

You can check the status of THP support by issuing the following commands:

cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag

On Red Hat Enterprise Linux, CentOS, and potentially other Red Hat-based derivatives, you may instead need to use the following:

cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
cat /sys/kernel/mm/redhat_transparent_hugepage/defrag

For both files, the correct output resembles:

always madvise [never]

If the defrag doesn’t show up with [never] then add this (courtesy of stackoverflow) :

echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag

[Redis FAQ]
https://redis.io/topics/faq/