Redis6 builds a highly available multi-master and multi-slave cluster

Environmental preparation

First of all, we need 6 redis, so why 6 redis? This is because we want to deploy a multi-master and multi-slaver cluster, with three masters and three slaves each, as shown in the figure:
Insert image description here

Of course, this time I will install one and then clone the virtual machine to achieve this. My six redis IP addresses are as follows:

192.168.1.171 192.168.1.172 192.168.1.173
192.168.1.174 192.168.1.175 192.168.1.176

If there is any IP address involved in the article, please change it to your own.

Build a redis6 cluster

Install redis6

I have written an article before about installing redis6, please install redis first:https://blog.csdn.net/m0_51510236/article/details/132661137 , of course, because we need six, I installed one here and cloned it directly:
Insert image description here

Because there are many servers, 1c1g per server is enough. The production environment decides how high the configuration should be based on its own server configuration.

Modify configuration file

In this step, all 6 servers need to be modified. A total of three places need to be modified. Don’t miss them. Connect to the Liutian server and turn on sending keyboard input to all sessions:
Insert image description here

Modify cluster-enabled

Modify redis.conf cluster-enabled configuration at approximately line 1387, uncomment and ensure the value is yes. This configuration ensures that the cluster is enabled:
Insert image description here

Modify cluster-config-file

Modify redis.conf About line 1395, set a cluster configuration file name:
Insert image description here

Modify cluster-node-timeout

Modify redis.conf at about line 1401, set the timeout for cluster node shutdown:
Insert image description here

The three configuration items of the configuration file have been modified.

Start the cluster

Six servers are started at the same time. Execute this line of command respectively (execute according to the path installed in myprevious article. If the file location Different commands need to be modified):

cd /opt/server/redis-6.2.13/
./bin/redis-server ./conf/redis.conf

You can see that the cluster started successfully in cluster mode:
Insert image description here

Next we need to associate these six servers together. Just execute this command on any redis. You need to execute this command to create a cluster. Pay attention to the modification later. IP address:

./bin/redis-cli --cluster create --cluster-replicas 1 \
192.168.1.171:6379 \
192.168.1.172:6379 \
192.168.1.173:6379 \
192.168.1.174:6379 \
192.168.1.175:6379 \
192.168.1.176:6379

This line means to create a cluster, each master has a shard (with a slave). After execution, such an interface will appear. Enter yes directly here:
Insert image description here

Then you can see a prompt like this, which also means that the build is successful:
Insert image description here

Later, when you use redis, redis will automatically calculate the slot value of the cache key through a certain algorithm (it will also be calculated when querying, different keys are queried in different masters, and the three masters communicate with each other), and set to the specified In the master, how do we connect to this redis cluster next? You can enter this command on any machine:

./bin/redis-cli -c
# 或者带上端口号
./bin/redis-cli -c -p 6379

As shown in the figure, successfully entered the redis cluster:
Insert image description here

We enter this command to view the cluster node status:

CLUSTER NODES

You can see three masters and three slaves:
Insert image description here

When we set the value and query the value, we will calculate the slot and jump to the specified server:
Insert image description here

At this point, the redis high-availability cluster is completed and get out of class is over.

Guess you like

Origin blog.csdn.net/m0_51510236/article/details/132684529