Redis-sentinel sentinel mode cluster scheme configuration

Recently, I have studied the clustering scheme of redis. The first scheme is to create a redis cluster, and the second scheme is to use the sentinel mode for master-slave replacement and fault recovery.

1. Sentinel introduction

Redis Sentinel 
Sentinel (Sentinel) is a tool for monitoring the status of the Master in the redis cluster, which has been integrated in the redis2.4+ version

Sentinel functions: 
1): Master status detection 
2): If the Master is abnormal, the Master-Slave switch will be performed, one of the Slaves will be used as the Master, and the previous Master will be used as the Slave 
3): After the Master-Slave switch, master_redis.conf, The contents of slave_redis.conf and sentinel.conf will change, that is, there will be one more line of slaveof configuration in master_redis.conf, and the monitoring target of sentinel.conf will change the 
working mode of Sentinel accordingly: 
1): Each Sentinel has one line per second. The frequency of sending a PING command to the Master, Slave and other Sentinel instances it knows 
2): If an instance (instance) has been away from the last valid reply to the PING command more than the value specified by the down-after-milliseconds option, then this Instances will be marked as subjective offline by Sentinel. 
3): If a Master is marked as subjective offline, all Sentinels monitoring the Master should confirm that the Master has indeed entered the subjective offline state at a frequency of once per second. 
4): When a sufficient number of Sentinels (greater than or equal to the value specified in the configuration file) confirm that the Master has indeed entered the subjective offline state within the specified time range, the Master will be marked as an objective offline 
5): Under normal circumstances , each Sentinel will send INFO commands to all known Masters and Slaves every 10 seconds 
6): When the Master is marked as objectively offline by Sentinel, the frequency of sending INFO commands to all slaves of the offline Master by Sentinel will be changed from once every 10 seconds to once per second 
7): If there is not enough Sentinel to agree that the Master has After going offline, the objective offline status of the Master will be removed. 
If the Master returns a valid reply to Sentinel's PING command, the Master's subjective offline status will be removed.

Subjective offline and objective offline 
Subjective offline: Subjectively Down, referred to as SDOWN, refers to the offline judgment made by the current Sentinel instance on a redis server. 
Objective offline: Objectively Down, referred to as ODOWN, refers to the Master Server offline after multiple Sentinel instances make SDOWN judgments on the Master Server and communicate with each other through the SENTINEL is-master-down-by-addr command Judge, then open failover.

In layman's terms, 
the sentinel system of redis is used to manage multiple redis servers, and can implement a functionally HA cluster. The system mainly performs three tasks: 
①Monitoring (Monitoring): Redis Sentinel monitors the running status of the master server and the slave server in real time. 
②Notification: When there is a problem with a Redis server being monitored, Redis Sentinel can send a notification to the system administrator, or send notifications to other programs through the API 
. The architecture diagram of a simple master-slave structure plus sentinel cluster is as follows : 
write picture description here
The above picture shows one master and one slave node, plus two clusters where Sentinel is deployed. Sentinel clusters will communicate with each other, communicate the status of redis nodes, make corresponding judgments and deal with them. The subjective offline status here and the objective offline state are more important states, they determine whether to perform failover or not. 
You can subscribe to the specified channel information. When the server fails, the administrator is notified. 
The client can regard Sentinel as a service that only provides subscription functions. Redis server, you can not use PUBLISH command to send information to this server, but you can use SUBSCRIBE command or PSUBSCRIBE command to get corresponding event alerts by subscribing to a given channel. 
A channel can receive events with the same name as the channel. For example, a channel named +sdown can receive events for all instances to enter the subjective downline (SDOWN) state.

2. Build a redis-sentinel cluster environment

1. Create a new directory redis-sentinel under /usr/local/, and then create six directories under this directory: 7501/ 7502/ 7503/ 7504/ 7505/ 7506/.

2.、将redis安装目录下的reids.conf,拷贝到前4个目录下,分别命名为: 
Redis-7501.conf redis-7502.conf redis-7503.conf redis-7504.conf 
修改配置文件内容(以redis-7501.conf为例):

  1. daemonizeyes
  2. Port 7501
  3. Bind 192.168.12.90
  4. logfile "./redis-7501.log"

3、 将redis安装目录下的sentinel.conf拷贝到7505/和7506/目录下分别命名: 
Sentinel-7505.conf sentinel-7506.conf 
修改配置文件(以sentinel-7505.conf为例):

  1. port7505
  2. sentinel monitor mymaster 192.168.12.9075012

注:我们稍后要启动四个redis实例,其中端口为7501的redis设为master,其他三个设为slave 。所以my mymaster 后跟的是master的ip和端口,最后一个’2’代表我要启动只要有2个sentinel认为master下线,就认为该master客观下线,启动failover并选举产生新的master。通常最后一个参数不能多于启动的sentinel实例数。

4、启动redis和sentinel 
分别启动4个redis实例:

  1. redis-server redis-7501.conf
  2. ...
然后分别登陆7502 7503 7504三个实例,动态改变主从关系,成为7501的slave:
  1. redis-cli-h192.168.12.90-p7502
  2. 192.168.12.90:7502> SLAVEOF 192.168.12.907501

以后台启动模式启动两个sentinel(哨兵):

  1. redis-sentinel sentinel-7505.conf &

5、sentinel一些命令介绍 
要使用sentinel的命令,我们需要用redis-cli命令进入到sentinel:

  1. redis-cli-h192.168.12.90-p7505

① INFO 
sentinel的基本状态信息 
②SENTINEL masters 
列出所有被监视的主服务器,以及这些主服务器的当前状态 
③ SENTINEL slaves 
列出给定主服务器的所有从服务器,以及这些从服务器的当前状态 
④SENTINEL get-master-addr-by-name 
返回给定名字的主服务器的 IP 地址和端口号 
⑤SENTINEL reset 
重置所有名字和给定模式 pattern 相匹配的主服务器。重置操作清除主服务器目前的所有状态, 包括正在执行中的故障转移, 并移除目前已经发现和关联的, 主服务器的所有从服务器和 Sentinel 。 
⑥SENTINEL failover 
当主服务器失效时, 在不询问其他 Sentinel 意见的情况下, 强制开始一次自动故障迁移,但是它会给其他sentinel发送一个最新的配置,其他sentinel会根据这个配置进行更新


6、测试: 
(1)登陆到 master:

  1. redis-cli -h 192.168.12.90 -p 7501
  2. 192.168.12.90:7501> setname"zhangsan"
  3. [root@localhost redis-sentinel]# redis-cli -h 192.168.12.90 -p 7502
  4. 192.168.12.90:7502> getname
  5. "zhangsan"
  6. 192.168.12.90:7502> set age 24
  7. (error) READONLY You can't writeagainst a read only slave.
It can be seen that in our master-slave mode, the slave is read-only by default.

(2) At present, 7501 is the master. After we forcefully kill the process of 7501, we can see the information sent by sentinel: 
write picture description here

It can be seen that sentinel has promoted the redis instance 7504 to the new master, and later started the instance 7501 as the slave of 7504, thus manually restoring the redis cluster.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326607637&siteId=291194637