Home >Database >Redis >Redis sentry mode principle

Redis sentry mode principle

Guanhui
GuanhuiOriginal
2020-06-01 15:03:463708browse

Redis sentry mode principle

Redis Sentry Mode Principle

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

Linux - redis sentinel cluster instance

Command arrangement

Official website address: http://redisdoc.com/

redis-cli info #查看redis数据库信息
redis-cli info replication #查看redis的复制授权信息
redis-cli info sentinel   #查看redis的哨兵信息

Configuration process

Ideas:

redis master-slave

一Master and two slaves plan

1. Environment preparation, prepare the redis architecture of one master and two slaves

redis-6379.conf

port 6379
daemonize yes
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/opt/redis/6379/"
redis-6380.conf
port 6380
daemonize yes
logfile "6380.log"
dbfilename "dump-6380.rdb"
dir "/opt/redis/6380/"
slaveof  127.0.0.1  6379
redis-6381.conf
port 6381
daemonize yes
logfile "6381.log"
dbfilename "dump-6381.rdb"
dir "/opt/redis/6381/"
slaveof  127.0.0.1  6379

2. Prepare three data folders

mkdir -p  /opt/redis/{6379,6380,6381}

3. Start three databases respectively

[root@master sbredis]# redis-server redis-6379.conf 
[root@master sbredis]# redis-server redis-6380.conf 
[root@master sbredis]# redis-server redis-6381.conf

4. Detect the master-slave status

redis-cli -p 6379   info replication
redis-cli -p 6380   info replication
redis-cli -p 6381   info replication

5. Prepare three redis sentinels to detect the master-slave status

Prepare the configuration of the three sentinels File

redis-26379.conf

// Sentinel节点的端口
port 26379  
dir /var/redis/data/
logfile "26379.log"
// 当前Sentinel节点监控 192.168.119.10:6379 这个主节点
// 2代表判断主节点失败至少需要2个Sentinel节点节点同意
// mymaster是主节点的别名
sentinel monitor mymaster 192.168.119.10 6379 2
//每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过30000毫秒30s且没有回复,则判定不可达
sentinel down-after-milliseconds mymaster 30000
//当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,
原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1
sentinel parallel-syncs mymaster 1
//故障转移超时时间为180000毫秒
sentinel failover-timeout mymaster 180000
redis-26380.conf
port 7000
daemonize yes
dir "/opt/data"
logfile "7000.log"
dbfilename "dump-7000.rdb"
cluster-enabled yes   
cluster-config-file nodes-7000.conf
cluster-require-full-coverage no
redis-26381.conf

Three configuration files, only the port is different, quickly generate the configuration file through the command

[root@master sbredis]# sed "s/26379/26380/g"  redis-26379.conf   >  redis-26380.conf  
[root@master sbredis]# sed "s/26379/26381/g"  redis-26379.conf   >  redis-26381.conf

6. Start three sentinels respectively

[root@master sbredis]# redis-sentinel redis-26379.conf 
[root@master sbredis]# redis-sentinel redis-26380.conf 
[root@master sbredis]# redis-sentinel redis-26381.conf

7. Detect the sentinel, master-slave status

redis-cli -p 26379  info sentinel

When you see the following information, you are like me

[root@master sbredis]# 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=s17ms,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3

8. Test, kill the master redis, and see if it switches automatically

ps -ef|grep redis
    kill 进程
    ..

9. Start redis 6379 again to see if it has joined the master-slave cluster

redis-server redis-6379.conf

Recommended tutorial: "Redis Tutorial"



The above is the detailed content of Redis sentry mode principle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn