Redis cluster configuration (manual switching of main Redis, Sentinel automatic switching of main Redis)

1. Disadvantages of stand-alone nodes

  • single point of failure
  • Limited capacity
  • Connection pressure

2. Cluster model

illustrate:

ACF

X: full, mirroring, horizontal expansion

Y: Business, function, vertical expansion according to business

Z: Priority, logical split, vertical clusters are divided again according to certain rules

3. Cluster evolution

4. Cluster construction

Use the install_server command to create two Redis instances with ports 6380 and 6381 respectively.

In order to facilitate reading the log, set the Redis instance log to be printed directly on the console and make the following configuration

Copy all configuration files to a temporary directory:

2. Modify their respective configuration files respectively:

  1. Change the configuration to no to indicate input to the console.
  2. /var/log/redis_6379.log Display the log on the console, do not record it in the log file.
  3. no AOF is closed.

3. Synchronization command

Versions before 5.0 use SALVEOF, and versions after 5.0 use the new command: REPLICAOF .

Main Redis log information

Information from Redis log

Test master-slave synchronization

6379 Main Redis sets a key value

6380 Check the keys status from Redis, and write operations are not allowed by default from Redis (can be modified to enable write operations in the configuration file)

Scenario 1: The slave machine (6381) suddenly hangs up. After the hangup, the master machine continues to write data. Will the data be synchronized after the slave machine is turned on?

Slave machine 6381 is down, and then 6379 master Redis continues to write data.

Data can be obtained normally from machine 6380

 

From machine 6381 use the command

redis-server /redis/test/6381.conf  --REPLICAOF 127.0.0.1 6379

The client reconnected to 6381 and found that the data would be synchronized normally.

Scenario 2: Master Redis hangs, use manual switching between master and slave

Stop 6379 from service

Cong Redis will prompt that the information cannot be connected, but the data can be queried normally, but the data cannot be written.

artificial rescue

Redis does not follow previous 6379

Command: REPLICAOF no one

After REPLICAOF no one is set, this slave Redis instance becomes the master instance.

The Redis instance follows this new master machine

Use the command: REPLICAOF 127.0.0.1 6380

Test whether the following is successful

It was found that 6380 new primary Redis data had been synchronized normally.

Scenario 2: The main Redis is down, use sentinel mode to automatically switch to the main Redis

Set three sentinel configuration files:

26379.conf

port  26379 sentinel monitor mymonitor 127.0.0.1 6379 2 //2 数字代表 2票通过

26380.conf

port  26380 sentinel monitor mymonitor 127.0.0.1 6379 2

26381.conf

port  26381 sentinel monitor mymonitor 127.0.0.1 6379 2

I found that redis-sentinel is actually the same command used by redis-server, but the parameters are different, so the sentinel command is still redis-server.

Start three Sentinels respectively

redis-server 26379.conf --sentinel 

redis-server 26380.conf --sentinel 

redis-server 26381.conf --sentinel

Start Sentinel success interface

If you manually stop the 6379 service at this time, will Sentinel switch between master and slave normally? After waiting for a few seconds, we found that Sentinel and the previous two had log output from the Redis instance console:

 

The 6380 console log shows: 6381 requested access to itself and accepted it, indicating that 6380 is the main Redis

The console log of machine 6381 shows: connected to machine 6380 and followed 6380.

Test whether 6381 follows 6380 normally:

 

 

It was found that 6381 has followed the 6380 machine normally, and 6380 automatically switched to the main Redis successfully.

Open the Sentinel configuration file you just started editing again and find that the file content has been appended with new content by the program.

Question: How does a sentinel know about other sentinels?

Answer: Redis’s publish and subscribe function

 

 

Guess you like

Origin blog.csdn.net/u010960161/article/details/119879813