How to deploy Redis Sentinel and Cluster

Table of contents

1. Redis database

2. Redis Sentinel Mode

3. Deploy Redis Sentinel

The first step is to turn off the firewall and security mechanism

The second step is to modify the Redis configuration file

The third step is to open the Master node

Step 4 View Sentinel Information

4. How to deploy Redis cluster 

The first step is to create directories for different port nodes.

Step 2 Create script file

Step 3: Authorize and execute the script file

Step 4 Modify the configuration file

Step 5: Start the Redis node

Step 6 Check whether all ports are started successfully

Step 7: Start the cluster

Step 8: Test cluster login

Step 9 Check the node hash slot range

Step 10 Check Name key slot number


1. Redis database

●Master-slave replication: Master-slave replication is the basis of high-availability Redis. Both Sentinel and Cluster achieve high availability based on master-slave replication. Master-slave replication mainly implements multi-machine backup of data, as well as load balancing and simple fault recovery for read operations. Disadvantages: Failure recovery cannot be automated; write operations cannot be load balanced; storage capacity is limited by a single machine.
●Sentinel: Based on master-slave replication, Sentinel implements automated fault recovery. Defects: Write operations cannot be load balanced; storage capacity is limited by a single machine; Sentinel cannot perform automatic failover of slave nodes. In a read-write separation scenario, slave node failure will cause the read service to be unavailable, and additional monitoring of the slave nodes is required. , switching operation.
●Cluster: Through clustering, Redis solves the problem that write operations cannot be load-balanced and storage capacity is limited by a single machine, achieving a relatively complete high-availability solution.

2. Redis Sentinel Mode

The core function of Sentinel: Based on master-slave replication, Sentinel introduces automatic failover of the master node.

#Sentinel mode principle:
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 the voting mechanism and all Slaves are connected to the new Master. . Therefore, the number of the entire cluster running Sentinel must not be less than 3 nodes.

The role of sentry mode:

●Monitoring: Sentinel will constantly check whether the master node and slave nodes are operating normally.

●Automatic failover: When the master node fails to work properly, Sentinel will start an automatic failover operation. It will upgrade one of the slave nodes of the failed master node to the new master node, and let other slave nodes change to replicate the new master node. .

●Notification (reminder): Sentinel can send the results of failover to the client.


The sentinel structure consists of two parts, sentinel nodes and data nodes:

●Sentinel node: The sentinel system consists of one or more sentinel nodes. Sentinel nodes are special redis nodes that do not store data.
●Data node: Both the master node and the slave node are data nodes.


The startup of Sentinel depends on the master-slave mode, so the master-slave mode must be installed before starting the Sentinel mode. The Sentinel mode needs to be deployed on all nodes. The Sentinel mode will monitor whether all Redis working nodes are normal. When the Master appears When there is a problem, because other nodes have lost contact with the master node, they will vote. If more than half of the votes are cast, it will be considered that there is indeed a problem with the Master. Then the sentinel room will be notified, and one of the Slaves will be selected as the new Master.

It is important to note that objective offline is a concept that only exists for the master node; if the slave node and sentinel node fail, after being subjectively offline by the sentinel, there will be no subsequent objective offline and failover operations.

3. Deploy Redis Sentinel

The first step is to turn off the firewall and security mechanism

Instruction: systemctl stop firewalld

systemctl disable firewalld

setenforce 0

The second step is to modify the Redis configuration file

Command: vim /opt/redis-5.0.7/sentinel.conf

The third step is to open the Master node

Command: cd /opt/redis-5.0.7

redis-sentinel sentinel.conf

Step 4 View Sentinel Information

Command: redis-cli -p 26379 info sentinel

4. How to deploy Redis cluster 

The first step is to create directories for different port nodes.

Instruction: cd /etc/redis/
mkdir -p redis-cluster/redis600{1..6}

Step 2 Create script file

Footstep content:

for i in {1..6}
do
cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis600$i
cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis600$i
done

Step 3: Authorize and execute the script file

Command: chmod 777 jb.sh

./jb.sh

Step 4 Modify the configuration file

Command: vim /etc/redis/redis-cluster/redis6001/redis.conf

vim /etc/redis/redis-cluster/redis6002/redis.conf

vim /etc/redis/redis-cluster/redis6003/redis.conf

vim /etc/redis/redis-cluster/redis6004/redis.conf

vim /etc/redis/redis-cluster/redis6005/redis.conf

vim /etc/redis/redis-cluster/redis6006/redis.conf

Step 5: Start the Redis node

Instruction: cd /etc/redis/redis-cluster/redis6001

redis-server redis.conf

cd /etc/redis/redis-cluster/redis6002

redis-server redis.conf

cd /etc/redis/redis-cluster/redis6003

redis-server redis.conf

cd /etc/redis/redis-cluster/redis6004

redis-server redis.conf

cd /etc/redis/redis-cluster/redis6005

redis-server redis.conf

cd /etc/redis/redis-cluster/redis6006

redis-server redis.conf

Step 6 Check whether all ports are started successfully

Command: ps -elf | grep redis

Step 7: Start the cluster

command: redis-cli --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.01:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1

Step 8: Test cluster login

Command: redis-cli -p [port number] -c

Step 9 Check the node hash slot range

Command: cluster slots

Step 10 Check Name key slot number

Command: cluster keyslot name

Guess you like

Origin blog.csdn.net/Liu_Fang_Hong/article/details/132014840