Redis master-slave and sentinel mode

Master-slave mode

Although Redis reads and writes very fast, it also produces a situation where the read pressure is extremely high. In order to share the read pressure, although Redis reads and writes at a very fast speed, it also produces a situation where the read pressure is extremely high. In order to share the read pressure, the architecture can adopt a master-multi-slave or cascade structure. Redis master-slave replication can be divided into full synchronization and incremental synchronization according to whether it is full.

The purpose of master-slave replication

In order to copy the data in the primary database to the secondary database, to ensure the consistency of the primary database and the secondary database, so that the client reads from the primary and secondary databases indifferently, which can help the primary database to reduce the load pressure and solve the single point of failure problem.

Full synchronization

Redis full replication generally occurs in the initialization phase of the slave, and then the slave needs to copy all the data on the master. Specific steps are as follows:

The slave server connects to the master server and sends the SYNC command;
after receiving the SYNC name, the master server starts to execute the BGSAVE command to generate the RDB file and uses the buffer to record all write commands executed thereafter. After the
master server BGSAVE has executed, it sends the snapshot file to all slave servers , And continue to record the executed write commands during sending;
discard all old data after receiving the snapshot file from the server, and load the received snapshot; the
master server starts to send the write commands in the buffer to the slave server after the snapshot is sent;
The slave server finishes loading the snapshot, starts to receive command requests, and executes the write command from the buffer of the master server.

Incremental synchronization

Redis incremental replication refers to the process of synchronizing write operations from the master server to the slave server when the slave is initialized and starts to work normally.

The process of incremental replication is mainly that the master server sends the same write command to the slave server every time it executes a write command, and the slave server receives and executes the received write command.

Master-slave deployment

Insert picture description here
1. Turn off the firewall

systemctl stop firewalld  
setenforce 0

2. Upload the redis-5.0.4.tar.gz installation package
3. Unzip, configure and install

tar zxvf redis-5.0.4.tar.gz   
cd redis-5.0.4/
make
make DREFIX=/usr/local/redis install

4. Create a link

ln -s /usr/local/redis/bin/* /usr/local/bin/

5. Execute the script

cd redis-5.0.4/utils/
./install_server.sh

6. Modify the configuration file

vi /etc/redis/6379.conf 

(Both master and slave nodes need to be modified)

Number of lines content Description
69 0.0.0.0 Listening address
136 daemonize yes Start the daemon
171 logfile /var/log/redis_6379.log Modify the log file directory
263 dir / var / lib / redis / 6379 Modify working directory
699 appendonly yes Enable AOF persistence function

(slave node)
line 287

replicaof 20.0.0.11 6379

Insert picture description here
Reboot

/etc/init.d/redis_6379 stop
/etc/init.d/redis_6379 start

View log

tail -20 /var/log/redis_6379.log

Insert picture description here
Verify the slave node
Insert picture description here
on the master. Set a number on the master to
Insert picture description here
view it on the slave.
Insert picture description here

Sentinel mode

Principles of Sentinel Mode

The sentinel is a distributed system used to monitor each server in the master-slave structure. When a failure occurs, a new master is selected through a voting mechanism and all slaves are connected to the new master. Therefore, the number of the entire cluster running sentinels must not be less than 3 nodes.

The role of sentinel mode
① Monitoring
Continuously check whether the master and slave are operating normally.
Master survival detection, master and slave running status detection

② Notification (reminder)
When there is a problem with the monitored server, send a notification to other (sentinel room, client).

③ Automatic failover
Disconnect the master and slave, select one slave as the master, connect other slaves to the new master, and inform the client of the new server address

The sentinel is also a redis server, but it does not provide data services
. The startup of the sentinel depends on the master-slave mode, so the sentinel mode must be installed after the master-slave mode is installed. The sentinel mode needs to be deployed on all nodes. The sentinel mode will Monitor whether all redis working nodes are normal. When there is a problem with the master, because other nodes lose contact with the master node, they will vote. After more than half of the vote, it is considered that there is a problem with the master, and then the sentry room will be notified, and then selected from slaves One as the new master

Sentinel mode deployment

Deploy on a master-slave basis

Modify the sentinel.confconfiguration file

vi redis-5.0.4/sentinel.conf
Number of lines before fixing After modification Description
17 # protected-mode no protected-mode no Turn off protection mode
26 daemonize no daemonize yes Specify sentinel to start in the background
36 logfile “” logfile “/var/log/sentinel.log” Log file
65 dir / tmp dir / var / lib / redis / 6379 Data path

At least a few sentries in line 84 detect that the main server is faulty, and then failover will be performed.

sentinel monitor mymaster 20.0.0.11 6379 2 

Line 113 determines the time period when the server is down, the default is 30000 milliseconds (30 seconds)

sentinel down-after-milliseconds mymaster 3000

The maximum timeout for the fault section in line 146 is 180000 (180 seconds)

sentinel failover-timeout mymaster 100000

Copy the configuration file from above

scp redis-5.0.4/sentinel.conf [email protected]:/root/redis-5.0.4
scp redis-5.0.4/sentinel.conf [email protected]:/root/redis-5.0.4

Start master first, then slave

redis-sentinel redis-5.0.4/sentinel.conf &

View sentinel information

redis-cli -h 20.0.0.11 -p 26379 info Sentinel

Insert picture description here
View log files
Insert picture description here

Sentry mode test

ps -ef | grep redis

Insert picture description here
Kill the process ID of the redis-server on the master

Insert picture description here
View sentinel information

redis-cli -h 20.0.0.11 -p 26379 info Sentinel

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_50345511/article/details/111355151