Building Redis highly available cluster sentinel mode based on docker compose

The original is reproduced from "Liu Yue's technology blog" https://v3u.cn/a_id_110

As we know, there are three kinds of redis cluster schemes: 1) redis cluster scheme; 2) master/slave scheme; 3) sentinel mode for master-slave replacement and fault recovery.

sentinel system can monitor one or more redis master services and all the slave services of these master services. When a master service is offline, it will automatically upgrade a slave service under the master to a master service to replace the offline master service to continue processing the request.

Similarly, we use docker to build

Create a new MKDIR redis sentinel folder

Enter the project folder CD redis sentinel, create a sentinel to store sentinel scripts, 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

This configuration means to monitor port 6379 of the main server and start two instances. If the sentinel does not receive the heartbeat of the main node within 5s, the sentinel will think that the main node is down. The default is 30 seconds. If the connection fails to synchronize with the main database for more than 5 seconds, the sentinel will vote after 5 seconds to switch roles of other slave servers

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

The script file synchronizes the configuration file to start the sentry

Then we can create a Dockerfile to specify the basic image, and copy the configuration file to the inside of 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"]

Finally, back to the project root cd

To create the docker-compose.yml configuration file:

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

It means that we have three redis services running at 637963806381, one master and two slaves, and two sentinels to monitor them. The final project structure is as follows

In the project root directory, start the service:

docker-compose up --force-recreate

If you want to run in the background, you can add the - d parameter

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

set 123 123 in the main library 6379, and then get 123 in the slave Library

No problem. Now let's test whether the sentinel mode works. If the main database is down for some reason, can the slave database automatically switch roles

We can manually stop the container process of the main database to simulate downtime

docker stop redissentinel_master_1

At this time, the master database is no longer connected. We enter the slave database and use the info command to view the role of the slave database

It was found that 6380 was originally a slave, but now it has become a master

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

Related video strategies:

Redis

The original is reproduced from "Liu Yue's technology blog" https://v3u.cn/a_id_110

Tags: Redis Database Docker Windows

Posted on Sun, 03 May 2020 14:33:27 -0400 by FlipinMonkeyPie