Ubuntu 20.04: Setup redis master – slave replication and redis sentinel


Example we have 3 server with IP: 5.161.72.222 5.161.61.47 5.161.97.150

redis sentinel

With each server:

Step 1*: install redis-server
apt install redis-server -y

Step 2*:edit file /etc/redis/redis.conf

nano /etc/redis/redis.conf

add server ip, e.g.

bind 5.161.61.47 127.0.0.1

For each server, we setup a redis server (port 6379) and a redis sentinel (port 26379)

Step 3*:
disable cluster mode

sed -i -e 's@^cluster-enabled@#cluster-enabled@' /etc/redis/redis.conf

Step 4*: start redis server on port 6379

systemctl start redis-server.service
systemctl enable redis-server.service

Step 5: Set slave on slave server ( 5.161.61.47 5.161.97.150 )

redis-cli replicaof 5.161.72.222 6379

Setup Sentinel

Step 6*: create file /etc/redis/sentinel.conf

nano /etc/redis/sentinel.conf

With content:

sentinel monitor redis-test 5.161.72.222 6379 2
sentinel down-after-milliseconds redis-test 6001
sentinel failover-timeout redis-test 60000
sentinel parallel-syncs redis-test 1
bind 0.0.0.0

Note: sentinel monitor [master-group-name] [master-ip] [master-port] [quorum]

quorum = number-of-sentinels / 2 + 1

Step 7*:
chown redis:redis /etc/redis/sentinel.conf

Step 8*:
Clone file /usr/lib/systemd/system/redis-server.service to /usr/lib/systemd/system/redis-sentinel.service

cp /usr/lib/systemd/system/redis-server.service /usr/lib/systemd/system/redis-sentinel.service

edit two lines:

ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
PIDFile=/run/redis/redis-server.pid

To:

ExecStart=/usr/bin/redis-server /etc/redis/sentinel.conf --sentinel
PIDFile=/run/redis/redis-sentinel.pid

We can do with two command lines:
sed -i -e 's@redis.conf@sentinel.conf --sentinel@' /usr/lib/systemd/system/redis-sentinel.service
sed -i -e 's@redis-server.pid@redis-sentinel.pid@' /usr/lib/systemd/system/redis-sentinel.service

Change line
Restart=always
to
Restart=on-failure

By command:
sed -i -e 's@Restart=always@Restart=on-failure@' /usr/lib/systemd/system/redis-sentinel.service

Or you can use simple file:

[Unit]
Description=Sentinel for Redis
After=network.target

[Service]
Type=forking
User=redis
Group=redis
PIDFile=/var/run/redis/sentinel.pid
ExecStart=/usr/bin/redis-server /etc/redis/sentinel.conf --sentinel
ExecStop=/bin/kill -s TERM $MAINPID
Restart=always

[Install]
WantedBy=multi-user.target

Step 9*:
systemctl start redis-sentinel.service
systemctl enable redis-sentinel.service

Check sentinel status

redis-cli -p 26379 info sentinel

root@tutorialspots-1:~# redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=redis-test,status=ok,address=5.161.72.222:6379,slaves=2,sentinels=1

sentinels will increase to 3 when we run 3 redis sentinels

Check master IP:

root@tutorialspots-1:~# redis-cli -p 26379 sentinel get-master-addr-by-name redis-test
1) "5.161.72.222"
2) "6379"

Check sentinel settings on slaves:

cat /etc/redis/sentinel.conf

root@tutorialspots-2:~# cat /etc/redis/sentinel.conf
sentinel myid ae7026e91caf9b34e1475c7159133f710642ad2f
sentinel deny-scripts-reconfig yes
sentinel monitor redis-test 5.161.72.222 6379 2
sentinel down-after-milliseconds redis-test 6001
bind 0.0.0.0
# Generated by CONFIG REWRITE
port 26379
dir "/"
protected-mode no
sentinel failover-timeout redis-test 60000
sentinel config-epoch redis-test 1
sentinel leader-epoch redis-test 1
sentinel known-replica redis-test 5.161.97.150 6379
sentinel known-replica redis-test 5.161.61.47 6379
sentinel known-sentinel redis-test 5.161.61.47 26379 7319b26af330fbfe40b8d27ca3ee8e69c832cc78
sentinel known-sentinel redis-test 5.161.97.150 26379 be30fab9759fdc8f9f1f05a535e3d82f8b7beb65
sentinel current-epoch 1

Redis Sentinel Automatic Failover

Run on master redis:
redis-cli -a foobared DEBUG sleep 30

Check master IP:

root@tutorialspots-2:~# redis-cli -p 26379 sentinel get-master-addr-by-name redis-test
1) "5.161.61.47"
2) "6379"

Sentinel commands

COMMAND	DESCRIPTION
sentinel masters	list all masters and their statuses
sentinel master	one master’s status
sentinel slaves	list all slaves and their statuses
sentinel sentinels	list all Sentinel instances and their statuses
sentinel failover	run failover manually
sentinel flushconfig	force Sentinel to rewrite its configuration on disk
sentinel monitor	add a new master
sentinel remove	remove master from being watched

Recent search terms:

  • how to configure redis replication on ubuntu 20 04

Leave a Reply