redis-sentinel

Sentinel can help us solve the single point of failure problem in the master-slave architecture

The sentinel mode is a special mode. First, Redis provides the sentinel command. The sentinel is an independent process. As a process, it will run independently. The principle is that the sentinel can monitor multiple Redis instances running by sending commands and waiting for the Redis server to respond.

The sentry here has two functions

By sending a command, the Redis server returns to monitor its running status, including the master server and the slave server.

When the sentinel detects that the master is down, it will automatically switch the slave to the master, and then notify other slave servers through the publish and subscribe mode , modify the configuration file, and let them switch the master.

Configure the following in the docker-compose.yml file, you can use the sentry configuration inside the container

version: "3.1"services:  redis1:    image: daocloud.io/library/redis:5.0.7    restart: always    container_name: redis1    environment:      - TZ=Asia/Shanghai    network_mode: host    volumes:      - ./conf/redis1.conf:/usr/local/redis/redis.conf      - ./conf/sentinel1.conf:/data/sentinel.conf    command: ["redis-server","/usr/local/redis/redis.conf"]  redis2:    image: daocloud.io/library/redis:5.0.7    restart: always    container_name: redis2    environment:      - TZ=Asia/Shanghai    network_mode: host    volumes:      - ./conf/redis2.conf:/usr/local/redis/redis.conf      - ./conf/sentinel2.conf:/data/sentinel.conf    command: ["redis-server","/usr/local/redis/redis.conf"]  redis3:    image: daocloud.io/library/redis:5.0.7    restart: always    container_name: redis3    environment:      - TZ=Asia/Shanghai    network_mode: host    volumes:      - ./conf/redis3.conf:/usr/local/redis/redis.conf      - ./conf/sentinel3.conf:/data/sentinel.conf    command: ["redis-server","/usr/local/redis/redis.conf"]

docker-compose.yml file location

Create and enter conf

Because the data volume uses path mapping, you have to create your own conf, and the following configuration file

Prepare the sentinel configuration file, and manually start the sentinel inside the container (sentinel)

# Sentinel needs to start
daemonize yes in the background
# Specify the ip and port of the master node, you need to configure the monitoring of the master node, by sending info to the master node to get the information of the slave node, 2 means there are only two or more sentinels Only when the main server is considered unavailable will the switchover operation be performed.
sentinel monitor master 192.168.206.140 6379 2
# Specify the port number of the sentinel
port 26379
# How often does the
sentinel monitor the redis architecture sentinel down-after-milliseconds master 10000

Prepare redis.conf configuration file

# redis1 master node configuration
port 6379
# redis2 slave node configuration
replicaof 192.168.206.140 6379
port 6380
# redis3 slave node configuration
replicaof 192.168.206.140 6379
port 6381

Just start sentinel inside the Redis container, pay attention to entering commands after entering the container

redis-sentinel sentinel.conf

Enter info to view all information

  • info

When I execute this command to deactivate the redis container, the sentry will automatically select the master

  • docker stop redis1

Enter the redis2 container/enter the redis3 container:

  • docker exec -it redis2 bash
  • redis-cli -p 6380

Enter info to view all information

  • info

The sentry deployed successfully! ! !