当前位置:网站首页>Redis sentry

Redis sentry

2022-06-23 06:12:00 BugMaker-shen

One 、 Brief introduction to sentry

 Insert picture description here

If master It's down. , The Sentry will find one slave As master, Notify all others slave Connect new master, Start a new master And slave, Data synchronization ( Copy in full *N+ Partial reproduction *N)

There are several problems with this process : Who will confirm master It's down. ? How to find one master? After modifying the configuration , The original master What if it's restored ?

sentry (sentinel) : It monitors each server in the master-slave structure Distributed systems , When there is a failure, through The voting mechanism chooses a new one master, And put all slave Connect to the new master; The sentry is also a redis The server , It just doesn't provide data services , Usually the number of sentinels is odd ( Prevent a tie in the voting )

 Insert picture description here

The role of a sentry

  • monitor : Keep going on master Survival tests 、master And slave Operation detection
  • notice ( remind ): When there is a problem with the monitored server , To the other ( The sentry room , client ) Sending notice
  • Automatic failover : Disconnect the downtime master And slave Connect , Pick one slave As master, Will others slave Connect to the new master, And inform the client of the new server address

Two 、 Configure sentinel mode

Configure the master-slave structure of one drag two , Three sentries ( The configuration is the same , Different ports ), The configuration file is Redis In the root directory sentinel.conf

Activate the sentry

redis-sentinel sentinel- Port number .conf

1. Write the sentry's configuration file

 Insert picture description here

#  Port occupied by sentinel service 
port 26379
# dir Keep the sentry log 
dir /tmp
# mymaster: It means that the sentinel monitors master, You can call it whatever you want  
# 2: If there is 2 A sentinel thought mymaster Hang up , So this one master It really hung up , Usually set to half the total number of sentinels  + 1
sentinel monitor mymaster 127.0.0.1 6379 2
# mymaster 30000ms No response , The sentry thought mymaster Hang up 
sentinel down-after-milliseconds mymaster 30000
#  new master When it's chosen , A few at a time slave Data synchronization 
#  The smaller the value , The less pressure on the server , The slower the synchronization ; The bigger this is , The more pressure you put on the server , The faster the synchronization 
sentinel parallel-syncs mymaster 1
#  If the synchronization time exceeds 180000ms, Data synchronization timeout 
sentinel failover-timeout mymaster 180000

 Insert picture description here

Modify the sentinel log directory

 Insert picture description here
Modify the port , Generate configuration files for the other two sentinels . The ports used by the three sentinels are 26379、26380、26381

 Insert picture description here

2. To write redis The configuration file for the server

see redis Server profile ,6379 by master,6380 by slave

 Insert picture description here
Generate 6381 Of slave

 Insert picture description here

3. start-up redis Servers and sentinels

Start one master, two slaves, three redis The server

 Insert picture description here
start-up 26379 sentry

 Insert picture description here
Log in to the started through the client 26379 Sentinel server

 Insert picture description here
Data operations cannot be performed on the sentinel server , Only some instructions corresponding to the Sentry can be executed , We type in info Command to view information

 Insert picture description here
info In the output information , We can see the Sentinels watching master as well as master Corresponding slave Number , And currently monitoring this master Number of sentinels

Check out the currently started 26379 The configuration file corresponding to the sentry , Found that the configuration file has changed

 Insert picture description here
start-up 26380 sentry

 Insert picture description here
Let's check again 26379 Sentinel profile
 Insert picture description here

here 26379 Sentinel server also has 2638 Sentinel surveillance master A reminder of . We now know , Every time a new Sentry is activated to monitor the same master, Sentinels can recognize each other

 Insert picture description here
Empathy ,3 Sentinels watch the same master, The configuration files of the three sentinels and the terminal prompt information are all mutual , Can detect each other

4. Verify the role of the sentry

If master It's down. , The Sentry will find one slave As master, Notify all others slave Connect new master. Let's demonstrate this function

Let's quit first master
 Insert picture description here

What we set in the sentry configuration file is ,30000ms Inside master No response , The sentry thought master It's down ,30000ms after , sentry 1 The terminal of has the following prompt information :

 Insert picture description here

As master Of 6379 After offline , The main process is for all sentinels to confirm 6379 Is it really offline , I'm sure 6379 Start voting after going offline , from slave Choose a new master, Then for the others slave change master. And take the old ones off the line 6379master Set to slave, Back 6379 After going online, it is directly slave

We started 6379 redis The server
 Insert picture description here

see 26379 A reminder of , Find out 6379 Become slave
 Insert picture description here

3、 ... and 、 How the sentry works

The sentry goes through three stages in the process of master-slave switching : monitor 、 notice 、 Fail over

  • monitor : Sync information
  • notice : Stay connected
  • Fail over : Find the problem 、 campaign for sentinel、 Choose new master、 new master In office , other slave Switch master, primary master When you recover, you become slave

1. monitor

It is used to synchronize the status information of each node

 Insert picture description here

  • The sentry sent a message to the other sentries ping, Get each sentinel The state of ( Whether online )

  • obtain master The state and master attribute , Include runid、role etc.

  • according to master Medium slave Information , Look for each one slave Get their details , Include slave Of runid、role、master_host、master_port、offset etc.

Let's talk about it in detail , sentry 、master and slave How to exchange information between

 Insert picture description here

At this time, the first sentry goes online to connect master, Get master Of info, meanwhile master And the sentry info. And then according to master I got something about slave Information about , De link slave, And then get it slave Of info, So the first sentry got master And all slave Of info

 Insert picture description here
Then the second sentry goes online to connect master, from master Get info, from master Of info You can know the current master-slave environment master、slave、sentinel The number of ip:port, Then the sentry went to find others slave and sentinel Exchange information

While taking the information, you will sentinel Establish and publish between - Subscription channel , To share info

 Insert picture description here

Third sentinel Connect master After the launch , Will get all the current master、slave、sentinel Of info, And add sentinel Release - Subscription channel

2. notice

sentinel Will take turns asking master and slave Information about , And then in sentinel My circle of friends posted , other sentinel Subscribe to
 Insert picture description here

3. Fail over

First sentinel1 Keep asking master, No response , Until the time in the sentry configuration file is exceeded, it is considered as this master Hang up , therefore sentinel1 hold master The status of is marked as sdown( Subjective offline ), Then I will send a message in my circle of friends saying master Hang up

 Insert picture description here

So other sentinel And keep asking master, See if there is any response . If more than half sentinel Later, I thought master Hang up , Then put master The status of is marked as odown( Objective offline )

master Once marked as odown,sentinels There will be a meeting , Choose one through a certain algorithm sentinel To go from slave Choose a new one from master
 Insert picture description here

Now? sentinel A new one will be selected according to the following principles master: online 、 Quick response 、 And the original master There is much information exchange 、offset Big ( Data synchronization is timely )、runid wait

 Insert picture description here
Select new master after ,sentinel I can do the following :

  • For the new master send out salve of no one, Disconnect NEW master And the original master The master-slave relationship of
  • To the other slave Send new master Of ip:port, Let others slave Redefining master
原网站

版权声明
本文为[BugMaker-shen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230430174511.html