This article shares how to build a Redis Sentinel cluster in Docker.
To build a Redis Sentinel cluster, continue using the bin/ Redis-server image from the previous article. Please refer to the docker environment to build a Redis Cluster
The content of docker-entrypoint.sh is modified as follows
#! /bin/bash
mkdir -p /etc/redis/ /var/log/redis/ /usr/local/redis/data/
chown -R redis:redis /var/log/redis/ /usr/local/redis/data/
if [ "$TYPE" = 'server' ]; then
cat>>/etc/redis/redis.conf<<EOF
protected-mode no
appendonly yes
logfile /var/log/redis/redis.log
dir /usr/local/redis/data/
EOF
exec gosu redis redis-server /etc/redis/redis.conf $@
fi
Copy the code
Start the Redis Master node container
sudo docker run -d --name redis-master -e TYPE=server bin/redis-server
Copy the code
Start the Redis Slave node container
for i in `seq 1 3`; do
master_ip=`sudo docker inspect -f '{{ .NetworkSettings.IPAddress}}' redis-master`
sudo docker run -d --name redis-slave-$i -e TYPE=server bin/redis-server "--port 6379" "--slaveof $master_ip 6379"
done
Copy the code
Log in to the primary Redis server and run the info replication command to view information about the primary and secondary redis services.
$ sudo docker exec-it redis-master /bin/bash root$redis-cli 127.0.0.1:6379> Info replication# Replication
role:master
connected_slaves:3
...
Copy the code
You can see that the current Redis master node is connected to three slave nodes.
Set up sentinel cluster and add the following content to docker-entrypoint.sh
if [ "$TYPE" = 'sentinel' ]; then
cat>>/etc/redis/sentinel.conf<<EOF
sentinel monitor $MASTER_NAME $MASTER_IP $MASTER_PORT $QUORUM
sentinel down-after-milliseconds $MASTER_NAME 5000
sentinel failover-timeout $MASTER_NAME 60000
sentinel parallel-syncs $MASTER_NAME 1
EOF
chown -R redis:redis /etc/redis/
exec gosu redis redis-sentinel /etc/redis/sentinel.conf $@
fi
Copy the code
The master name/master IP address /master port/quorum is configured in the first line of the Sentinel monitor. Quorum means that when at least one of the quorum nodes considers the master offline, In this case, a Sentinel node is selected to perform a failover. Down-after-milliseconds: If the master does not respond to PONG or replies with an error message within that time, the Sentinel considers the master offline. Fail-timeout: If a Sentinel node fails to complete the failover within the specified time (ms), the failover is considered to have failed. In this case, another Sentinel node attempts to perform the parallel syncs failover again: When failover occurs, a maximum of how many slaves can synchronize the new master at the same time.
Note that the Redis Sentinel process modifies the configuration file, so it must have configuration file modification permission.
Start all Sentinel node containers
for i in `seq 1 3`; do
master_ip=`sudo docker inspect -f '{{ .NetworkSettings.IPAddress}}' redis-master`
sudo docker run -d --name redis-sentinel-$i -e TYPE=sentinel -e MASTER_NAME=my_master -e MASTER_IP=$master_ip -e MASTER_PORT=6379 -e QUORUM=2 bin/redis-server
done
Copy the code
Log in to the Sentinel node, and the master node information is displayed through Sentinel Masters, which proves that sentinel monitoring is successful.
$ sudo docker exec- it redis - sentinel - 1 / bin/bash root $: / var/lib/redis - 5.0.7# redis-cli
127.0.0.1:6379> sentinel masters
1) 1) "name"
2) "my_master"
3) "ip"
4) "172.17.0.2"
5) "port"
6) "6379".Copy the code
The following shows the failover operation. Stop the Redis Master node
sudo docker stop redis-master
Copy the code
Look at the master information in the Sentinel cluster
$ sudo docker exec- it redis - sentinel - 1 / bin/bash root $: / var/lib/redis - 5.0.7# redis-cli
127.0.0.1:6379> sentinel masters
1) 1) "name"
2) "my_master"
3) "ip"
4) "172.17.0.3"
5) "port"
6) "6379".Copy the code
Note that the master node has been switched from 172.17.0.2 to 172.17.0.3. In the sentinel. Conf
root$ cat /etc/redis/sentinel.conf sentinel myid 2432497886c8d5543f05b21c142a1dfca84ffe72 sentinel deny-scripts-reconfig Yes Sentinel monitor my_Master 172.17.0.3 6379 2...Copy the code
The master IP address in the configuration file is changed to 172.17.0.3.
If you think this article is good, welcome to pay attention to my wechat public number, your attention is my motivation!