Automatic failover using sentinel, a Redis operation and maintenance monitoring tool

In a cluster, we often have to consider its security to prevent the master node from going down. Of course, this cannot be guaranteed, it is inevitable, but we can take preventive measures. For example, in a cluster, the master node hangs down. Now, we want to perform a quick transfer and switch another slave node to the master node to prevent a single point of failure, then this article will teach how to manually failover, and finally use automated tools for automatic failover.

Scenario requirements: There are three redis servers, port 6379 is the master, and ports 6380 and 6381 are slaves. For how to build a Redis cluster, you can see the previous article [ redis master-slave replication [master-slave cluster] ], here, we Assuming that the master is down, you need to switch to another slave as the master.

Failover manually

The following is what we need to do:
change the master-slave at runtime, and modify a slave (set to A) as a new master
1) Command the service not to do the slave
commands of other redis services: slaveof no one
2) Modify its readonly to no

Other slaves point to new master A
1) Command the service to be the slave
command format of new master A slaveof IP port

Let's get started:
First, we can use the info replication command to view the status of each slave node. At the beginning, they are all slave nodes of 6379, and the master is in the up state:
write picture description here
write picture description here

Next, we use the shutdown command to shut down the 6379 master to simulate the master downtime. How to switch manually?

127.0.0.1:6380> slaveof no one
127.0.0.1:6380> config set slave-read-only no
127.0.0.1:6381> slaveof localhost 6380

Run the above command to switch successfully, and the result is as follows:
write picture description here
write picture description here
From the above screenshot, we can see that we successfully failed over when the master failed, but in practice, we cannot always switch manually, because it takes time to switch manually. Data is easy to lose, so we can use Redis's operation and maintenance tool, sentinel, for automatic failover.

Failover with sentinel

The schematic diagram of sentinel is as follows:
write picture description here

Its principle is: Sentinel communicates with the master at all times. When the communication message from the master is not received within a certain period of time, it is considered that the master has failed. The host points to the new master, and sentinel continues to monitor the new master to communicate with it.

The principle is very simple, how to use it? The sentinel.conf file needs to be configured:

sentinel monitor def_master 127.0.0.1 6379 1
##master被当前sentinel实例认定为“失效”的间隔时间  
##如果当前sentinel与master直接的通讯中,在指定时间内没有响应或者响应错误代码,那么  
##当前sentinel就认为master失效(SDOWN,“主观”失效)  
##<mastername> <millseconds>  
##默认为30秒  
sentinel down-after-milliseconds def_master 30000
#默认超过这个时间认为故障转移失败,需要人工进行故障转移  
sentinel failover-timeout mymaster 180000
##当前sentinel实例是否允许实施“failover”(故障转移)  
##no表示当前sentinel为“观察者”(只参与"投票".不参与实施failover),  
##全局中至少有一个为yes  
sentinel can-failover def_master yes
#一次性修改几个slave指向新的new master,这个参数防止当master宕机后,多个sentinel同时
#一下子指向new master致使new master负载过重
sentinel parallel-syncs mymaster 1 

After adding the above configuration, you can start sentinel:
if you don't know how to start, you can first use the help command to view:
write picture description here
use the following command to start sentinel:

redis-server /myredis/sentinel.conf --sentinel

write picture description here
As shown in the figure above, after starting sentinel, it will monitor that the master has two slaves running. Now we shut down the master to see the changes in sentinel:
write picture description here
write picture description here

It can be seen from the above screenshot that sentinel monitors the failure of the master and automatically performs failover, but it determines which slave to use as the master according to the priority of the slave. We can set the priority to specify, the value of the parameter Smaller means higher priority.

So far, we have seen how to automate failover and use the sentinel tool to automate failover. If forwarding, please indicate the original address, thank you! ! !


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325198372&siteId=291194637