❝There is a saying in the role of redis master-slave replication: "Master-slave replication is the cornerstone of high availability." So what is high availability? High availability is to reduce the system The time that cannot be provided is the commonly heard reference to six nines. Sentinels and clusters are essential to achieve high availability. This article mainly introduces the sentinel mechanism.
❞
##❝Kaka compiled a road map to create an interview guide , I plan to write articles according to such a roadmap, and later I found that the knowledge points that have not been added are being added. I also look forward to your partners helping to add them. See you in the comment area!❞
At this time, a big brother named Sentinel came out of the blue. The big brother said that I will help you deal with this problem.
Since the main node master, as the boss, will not let you play. I will select a boss from among the four of you, and then you will play with him.
When the boss who doesn't play with you comes back, his identity will be invalid, and he will no longer be your boss. He can only play with the boss I selected.
The above dialogue process is what exactly is the meaning of configuring sentinels. Whoever plays with them will give data to whom. Once we know the role of sentinels, we will continue.
"Finally, we use professional terms to explain what a sentinel is."
Sentinel, the English name is sentinel, is a distributed system used for master-slave structure Each server in monitors
. When the master node fails, a new master node is selected through the voting mechanism, and all slave nodes are connected to the new master node.
The dialogue process we talked about above is one of the functions of the sentinel. Automatically Failover.
When it comes to its role, it must be exactly what this sentinel does at work. Let’s first describe it using a relatively dry concept, and then we’ll talk about the working principles one by one below.
Three functions of SentinelMonitoring, notification, and automatic fault transfer
One thing to note here is that Sentinel is also a redis server, but it does not provide any services to the outside world.
Configure it as an odd number when configuring the sentinel. So why configure the number of Sentinel servers to be an odd number? With this question you will find the answer you want below.
In this chapter we begin to configure the sentry and prepare for the preliminary work. The picture below shows Kaka’s preparations. Start 8 clients, three sentinels, one master node, two slave nodes, one master node client, and one slave node client.
The configuration file used by sentinel is sentinel.conf
Let's interpret the sentinel.conf configuration informationBut most of them are comments. Here Kaka provides you with a command to filter these useless informationcat sentinel.conf | grep -v '#' | grep -v '^$'
cat sentinel.conf | grep -v '#' | grep -v '^$' > ./data/sentinel-26379.confMove the filtered information in sentinel.conf to
/usr/local/redis/confDownload
Then opensentinel-26379.confModify the information storage directory
Then quickly copy the two sentinel configuration files, the ports are 26380 and 26381. sed 's/26379/26381/g' sentinel-26379.conf > sentinel-26381.conf
Test that the master-slave replication is in normal working condition, start three redis servers, and the ports are 6379, 6380, 6381
Check the master node information. There are two slave nodes connected, and the ports are 6380 and 6381 respectively.
A small point here is that one of the lags is 1 and the other is 0! lag is the delay time. I am testing locally, so there will be a situation of 0. This rarely happens when using a cloud server. The values of lag are 0 and 1, which are normal. Test the master node to add a hash value, hset kaka name kaka
Get the kaka values from slave1 and slave2 respectively, and check whether the master-slave replication is running normally.
After testing, our master-slave structure is running normally. Start a sentinelredis-sentinel 26379-sentinel.conf
Connect to 26379 sentinel, mainly the last line, the monitored master node is named mymaster, the status is normal, and there are two slave nodes The number of sentinels is 1. Let’s check the sentinel configuration information of 26379. It has been changed at this time. Start a sentinel of 26380
, redis-sentinel 26380-sentinel .conf
, please note that there is an extra piece of information in the last line. This ID is the newly added ID of our
26379 configuration file. Then we come to the client of Sentinel 26379, which is also newly added. The ID of 26380 Sentinel. At this time, we are checking the configuration file of 26379 Sentinel. The first time we check the configuration file, 26380 Sentinel is not configured. The second time we check the configuration file, the information added after 26380 Sentinel is configured. Finally we need to start the Sentinel client 3, the port number is 26381. After starting up, our configuration information and server information will also be changed. If we add the information of Sentinel 26380, Sentinel 26381 will also have it.
Up to this point our configuration of Sentinel is over. Next, we will shut down the master node. After waiting for 30 seconds, we will come to the client of 26379 Sentinel. Some new information has been added here. So what does this information do? Let’s break it down.
We need to know a few things about the information here first
When we put the 6379 redis server online again, we can see that the Sentinel server responded with two sentences. One sentence is to remove 6379 offline. The last sentence is to reconnect 6379 to the new master node. At this time, the master node is 6380. Set the value in the redis client of 6380 to check whether the master-slave replication is working properly.
Add list type to new master node 6380Get this value at 6379 and 6381, that’s it! Our sentry mode configuration is complete.
After configuring the sentinel, you need to The working principle has been analyzed. Only by knowing its working process can we have a better understanding of Sentinel.
The principles explained in this article are not so dry! Allows you to read a technical article as a story.
Getting to the point, the role of sentry is monitoring, notification, and failover. Then the working principle also revolves around these three points.
Sentinel will send The command obtains its status and publishes the information to the Sentinel subscription.
sentinel is-master-down-by-address-port
sentinel is-master-down-by-address-port
to their own intranet, confirm that the first one sent sentinel is- The sentry at master-down-by-address-port
said you are right, this guy is indeed dead. When everyone thinks that the master node is down, its status will be modified to odown
. When a sentinel thinks that the master node is hung up, the status is sdown
, and when half of the sentinels think that the master node is hung up, the status is odown
. This is why the sentinel is configured with an odd number.❝At this time, the sentinel has detected the problem, so which sentinel is responsible for selecting the new master node! It cannot be that Zhang San also goes, Li Si also goes, and Wang Wu also goes, it will be chaotic, so We need to choose the leader among all the sentinels, so how to choose! Please see the picture below.
❞
This time! The five sentinels will have a meeting together. All the sentinels are in an intranet, and then one thing they will do is that the five sentinels will send commands at the same time sentinel is-master-down-by-address-port
And bring your election times and runid. Each sentinel is both a candidate and a voter. Each sentinel has one vote, and the envelope represents its voting rights. When sentinel1 and sentinel4 send instructions to the group at the same time to prepare for the election, sentinel2 says at this time that I will vote for whoever receives the instruction first. If sentinel1 is released early, then sentinel2's vote will be cast for sentinel1. Voting will be initiated according to this rule until one sentinel's votes are half of the total number of sentinels. Assume that sentinel1 will be elected after the number of votes sentinel1 reaches more than half of the total number of sentinels. At this time, the next stage is reached. Above, Sentinel has selected sentinel1 as the representative to go to all the slave nodes to find one as the master node. There are certain rules for selecting a master node, not just picking one at random.
Get rid of those who are not online first
Kill the one with slow response. Sentinel will send information to all redis, and the one with slow response will be killed. Kill the one that has been disconnected from the original master node for the longest time. Since the demonstration is not enough here, all A new slave5 has been added, which makes no sense! After judging from the above three points, there are still salve4 and slave5, and they will be filtered according to the priority principle.
After selecting the new master node, instructions must be sent to all nodes.
I have finished talking about all the knowledge points about Sentinel, this article is the most important This is how Sentinel works. Let's briefly review its working principle.
Monitor first, and all sentinels synchronize information
Sentinels publish information to subscriptions
Failover
The above is Kaka’s understanding of Sentinels. If mistakes can be made, Kaka will correct them in time.
❝Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always upheld since his career. I hope that Kaka’s articles in the huge Internet can bring you a little Please help. See you in the next issue.
❞
Recommended: "Redis Tutorial"
The above is the detailed content of Redis Sentinel Principle, I have tolerated you for a long time!. For more information, please follow other related articles on the PHP Chinese website!