[Redis Notes 02] Sentinel Sentinel Mode of Redis Operating Environment

This article mainly introduces the Sentinel sentinel mode of the Redis operating environment.

Table of contents

1. Sentinel Sentinel Mode

1.1. Principle of sentinel mode

1.2. Sentinel environment construction

(1) Build master-slave mode

(2) Create a sentinel configuration file

(3) Start the sentinel sentinel node

(4) View sentinel node information

(5) Simulated failover

1.3. Principle of sentinel mode

(1) Communication mechanism

(2) Subjective offline

(3) Objective offline

(4) Election strategy


1. Sentinel Sentinel Mode

1.1. Principle of sentinel mode

The previous article introduced the master-slave mode. Although the master-slave mode can improve the read and write performance of Redis, it may still be unavailable. When the master node fails, the master-slave mode cannot provide external services normally. At this time, the functions related to the redis write operation in the entire project cannot be used.

In order to solve the problem that the master-slave mode is unavailable, we need to actively restart the master node after the master node fails, or select a slave node to become a new master node. This method requires Human intervention can restore the Redis master-slave mode to normal.

Is there any way to automatically make the master node work again without human intervention? ? ? This requires the introduction of the Sentinel sentinel mode. The sentinel mode is actually the process of using an application to replace human intervention. This application is called: the sentinel process.

The general principle of sentinel mode is as follows:

  • First of all, it is still a master-slave mode architecture, but a sentinel process will be introduced at this time.
  • The sentinel process is a separate redis service process. Its main function is to monitor the running status of all master and slave nodes.
  • When the sentinel process finds that the master node fails, it will adopt a certain strategy to select a node from all the slave nodes as the new master node.
  • Then reconnect other slave nodes to the new master node, and the original master node will become the slave node of the new master node.

Some people may ask, if the sentinel process also fails, isn't the master-slave mode also unavailable? ? ?

  • Therefore, in order to ensure the high availability of the sentinel process, under normal circumstances, multiple sentinel nodes will be set up, and set to an odd number, for example: 3, 5.
  • The general pattern is: [one master, two slaves and three sentinels].

Here's how to build Sentinel Mode.

1.2. Sentinel environment construction

Here, the mode of [one master, two slaves and three sentries] is used to build the Sentinel sentinel environment.

Sentinel sentinel mode (one master, two slaves and three sentinels)
node name IP address start port
master master node 127.0.0.1 6379
slave from node 1 127.0.0.1 6380
slave from node 2 127.0.0.1 6381
sentinel sentinel node 1 127.0.0.1 26379
sentinel sentinel node 2 127.0.0.1 26380
sentinel sentinel node 3 127.0.0.1 26381

The sentry mode is built on the basis of the master-slave mode, so the environment of the master-slave mode will not be built repeatedly here. If you don’t know how to build it, you can read this article [ Master-Slave Mode ].

(1) Build master-slave mode

First build a master-slave mode, refer to the [ Redis master-slave mode ] article.

(2) Create a sentinel configuration file

In the Redis installation directory, create three sentinel configuration files, the file names are:

  • sentinel26379.conf、sentinel26380.conf、sentinel26381.conf。

The content of the configuration file is as follows:

# 启动端口
port 26379
# 禁止保护模式
protected-mode no
# 配置监听的主服务器,这里sentinel monitor代表监控
# master-name代表主结点服务器的名称,可以自定义
# master-ip代表监控的主服务器,master-port代表端口
# quornum代表只有quornum个以上的哨兵认为主服务器不可用的时候,才会进行failover故障转移操作。
# sentinel monitor <master-name> <master-ip> <master-port> <quornum>
sentinel monitor custom-master 127.0.0.1 6379 2
# sentinel auth-pass定义服务的密码,custom-master是服务名称,123456是Redis服务器密码
# sentinel auth-pass <master-name> <password>
# sentinel auth-pass custom-master 123456
# 设置 sentinel 的唯一ID标识,不设置会自动生成一个,不同的sentinel结点的ID不同
sentinel myid 24daf4ea2601497b129141742020b34fd749021c

Note: The three sentinel configuration files are the same except for the port port and myid.

(3) Start the sentinel sentinel node

Windows opens the CMD command line window, and uses the [redis-server.exe sentinel26379.conf --sentinel] directory to start the sentinel node. Start three sentinel sentinel services in turn, as follows:

(4) View sentinel node information

After the sentinel node is successfully started, you can connect to the sentinel node [redis-cli.exe -h 127.0.0.1 -p 26379] , and then view the node information through the [info sentinel] command.

Note: Start the master and slave nodes in the master-slave mode first, and then view the sentinel sentinel node information.

(5) Simulated failover

The previous steps have successfully set up the Sentinel sentinel mode environment. Next, we will simulate whether the Sentinel will actively promote the slave node to the new master node when the master node is down.

  • Under windows, end the master node service by [Ctrl+C].

  • Connect a sentinel sentinel node, and then check the node information through the [info sentinel] command.

  •  When the original master node is restarted, it can be found that the original master node will become the slave node of the new master node.

1.3. Principle of sentinel mode

(1) Communication mechanism

The core function of the sentinel sentinel node is to monitor all redis master-slave nodes. When the master fails, select a slave node to become the master node. How can we know whether a node has failed? ? ?

The sentinel sentinel node will send a [ping] command to all redis nodes every second. If the node is normal, then the node will return a [PONG] response to the sentinel node, and the sentinel node will receive it. After the PONG response, it is known that the current node is working normally.

(2) Subjective offline

When the sentinel sentinel node does not receive the PONG response returned by a redis node within the specified time (default is 30 seconds), then the current sentinel sentinel node will consider that the redis node service is unavailable, and then Mark the redis node as unavailable, that is: offline state, this way of marking the redis node as offline is called: subjective offline.

Subjective offline will not immediately offline the redis node, but will modify the node status maintained in the current sentinel sentinel node. The sentinel sentinel node also needs to ask other sentinel sentinel nodes to finally determine the redis node. Whether the point is really offline.

(3) Objective offline

A single sentinel node thinks that a redis node is offline. This situation is not very accurate and too subjective, so when a sentinel sentinel finds that the redis node is offline, it will report to other sentinel sentinels. Click to send a command to determine whether the current redis node is offline. After receiving the command, other sentinel sentinel nodes will detect the status of the redis node. If more than half of the sentinel sentinel nodes consider this The redis node is offline, so at this time, the redis node will be marked as offline. This offline method is called: objective offline.

(4) Election strategy

If the sentinel sentinel node judges that the master master node is offline, then at this time, a certain election strategy will be adopted between the sentinel nodes, and a slave node will be selected from all available slave nodes. And promote the slave node to the master node, notify other slave slave nodes by sending subscriptions, associate with the new master node, and finally modify the original master node to slave slave node, do this The advantage is that when the original master node restarts, it can normally join the master-slave mode.

The above operation is failover (by default, it is completed within 3 minutes, and the execution fails if it exceeds 3 minutes). The failover is completed by a sentinel sentinel node. How to determine which sentinel sentinel node will perform the failover? ? ? All sentinel sentinel nodes will use a certain election algorithm to select a sentinel sentinel node as the leader node, and the leader node will perform a failover operation.

So far, the sentinel mode in Redis has been introduced.

In summary, this article is over, mainly introducing the Sentinel sentinel mode of the Redis operating environment.

Guess you like

Origin blog.csdn.net/qq_39826207/article/details/129775834