The original article is reprinted from liu Yue’s Technology blog v3u.cn/a_id_110

As we know, there are roughly three Redis cluster schemes: 1) Redis cluster scheme; 2) Master /slave scheme; 3) Sentry mode for master/slave replacement and fault recovery.

The Sentinel system can monitor one or more Redis Master services and all the slave services of these Master services. When a master service goes offline, the secondary service of the master is automatically upgraded to the master service to process requests instead of the offline master service.

Again, we use Docker for scaffolding

Create a new mkdir redis-sentinel folder

Go to the project folder CD Redis-sentinel, create a sentinel just for the sentinel script, and then CD sentinel

To create the sentinel.conf configuration file:

sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 5000
Copy the code

This configuration means that port 6379 of the master server is monitored and two instances are started. If the sentry does not receive a heartbeat from the master node within 5 seconds, the sentry considers the master node to be down. The default value is 30 seconds

Then, create the sentinel-entrypoint.sh script file:

#! /bin/sh
sed -i "s/$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /etc/redis/sentinel.conf
sed -i "s/$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /etc/redis/sentinel.conf
sed -i "s/$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /etc/redis/sentinel.conf

exec docker-entrypoint.sh redis-server /etc/redis/sentinel.conf --sentinel
Copy the code

This script file synchronizes the configuration file and is used to start sentinel

Then we create the Dockerfile to specify the base image and copy the configuration file inside the image:

FROM redis

EXPOSE 26379
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
COPY sentinel-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh
ENTRYPOINT ["sentinel-entrypoint.sh"]
Copy the code

Finally, back to the project root directory CD..

Docker-comemess. yml: docker-comemess. yml

master:
  image: redis
  ports:
    - "6379:6379"
slave1:
  image: redis
  command: redis-server --slaveof redis-master 6379
  links:
    - master:redis-master
  ports:
    - "6380:6379"
slave2:
  image: redis
  command: redis-server --slaveof redis-master 6379
  links:
    - master:redis-master
  ports:
    - "6381:6379"
sentinel1:
  build: sentinel
  environment:
    - SENTINEL_DOWN_AFTER=5000
    - SENTINEL_FAILOVER=5000    
  links:
    - master:redis-master
    - slave1
sentinel2:
  build: sentinel
  environment:
    - SENTINEL_DOWN_AFTER=5000
    - SENTINEL_FAILOVER=5000    
  links:
    - master:redis-master
    - slave2
Copy the code

This means that we have three Redis services running at 6379,6380,6381, one master and two slave, and two sentinel instances to monitor them. The final project structure is like this

In the project root directory, start the service:

docker-compose up --force-recreate
Copy the code

You can add -d if you want to run in the background

At this time, we can test whether the synchronization is successful. Open three Windows respectively to log in to Redis: redis-cli -p 6379,redis-cli -P 6380,redis-cli -P 6381

Set 123 123 in primary library 6379 and get 123 in secondary library respectively

We can manually stop the container process of the main library to simulate an outage

docker stop redissentinel_master_1
Copy the code

At this point, the master library is no longer connected, we go to the slave library, use the info command to check the role of the slave library

6380 used to be a slave, but now it has become a master

In fact, this is the so-called high-load and high-availability architecture, which can carry out the high-availability disaster recovery mechanism while using the cluster to bear the high load.

Related video guide:

www.bilibili.com/video/av932…

The original article is reprinted from liu Yue’s Technology blog v3u.cn/a_id_110