Detailed explanation of redis5 cluster construction and usage under Linux (Centos7)

Detailed explanation of redis5 cluster construction and usage under Linux (Centos7)

1. Brief description

In October 2018, Redis released a stable version 5.0, which introduced various new features. One of them was to abandon the Ruby clustering method and switch to the redis-cli method written in C language, which greatly reduced the complexity of cluster construction. Updates to the cluster can be found in the Redis5 version notes, as follows:

The cluster manager was ported from Ruby (redis-trib.rb) to C code inside redis-cli. check `redis-cli --cluster help ` for more info.

You can check the Redis official website to see how to build a cluster. The connection is as follows

https://redis.io/topics/cluster-tutorial

There should be at least three nodes in the cluster, each with a backup node. Requires 6 servers.

If conditions are limited, you can build a pseudo-distributed system. The following steps are to build a Redis cluster with 6 nodes on a Linux server.

2. Steps to create a cluster

2.1. Create a directory

Create a new directory: mkdir /usr/local/redis-cluster

2.2. Download the source code and decompress and compile

​wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar xzf redis-5.0.0.tar.gz
cd redis-5.0.0
make
make install PREFIX=/usr/local/redis

3. Create 6 Redis configuration files

The six configuration files cannot be in the same directory. Here we define them as follows:

/root/software/redis/redis-cluster-conf/7001/redis.conf
/root/software/redis/redis-cluster-conf/7002/redis.conf
/root/software/redis/redis-cluster-conf/7003/redis.conf
/root/software/redis/redis-cluster-conf/7004/redis.conf
/root/software/redis/redis-cluster-conf/7005/redis.conf
/root/software/redis/redis-cluster-conf/7006/redis.conf

Some operation commands are for reference only:

cp redis.conf /usr/local/redis/bin
cd /usr/local/redis/
cp -r bin ../redis-cluster/redis01
cd /usr/local/redis-cluster/redis01
rm dump.rdb #Delete snapshot vim redis.conf

The content of the configuration file is:

port 7001 #port cluster-enabled yes #Enable cluster mode cluster-config-file nodes.conf
cluster-node-timeout 5000 #Timeout appendonly yes
daemonize yes #background run protected-mode no #non-protected mode pidfile /var/run/redis_7001.pid
bind 172.20.10.7 #Change 127.0.0.1 to the local IP address. You can use ifconfig to view the IP address.

The port and pidfile need to be adjusted according to the different folders.

Create the remaining 5 instances:

[root@master redis-cluster]# cp -r redis01/ redis02
[root@master redis-cluster]# cp -r redis01/ redis03
[root@master redis-cluster]# cp -r redis01/ redis04
[root@master redis-cluster]# cp -r redis01/ redis05
[root@master redis-cluster]# cp -r redis01/ redis06

Modify the port and pidfile under redis.conf of redis02 ~ redis06 respectively

4. Start the node

Enter the redis01, redis02, ...redis06 directories respectively and execute: ./redis-server ./redis.conf

Create a batch file to start six Redis at the same time

vim startall.sh

Add the following content:

cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..

Then execute chmod u+x start-all.sh to turn start-all.sh into an executable file

Start in the current directory: ./startall.sh

View: ps aux|grep redis

5. Start the cluster

Because the cluster we use is built with version 5.0.0 of Redis, we only need to copy the redis-cli file in the compiled redis directory to the redis-cluster directory. (Redis version 5.0 and later are started directly using C language)

/usr/local/redis-cluster/redis-cli --cluster create 172.20.10.7:7001 172.20.10.7:7002 172.20.10.7:7003 172.20.10.7:7004 172.20.10.7:7005 172.20.10.7:7006 --cluster-replicas 1

After startup, you can see the success message as follows:

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.20.10.7:7004 to 172.20.10.7:7001
Adding replica 172.20.10.7:7005 to 172.20.10.7:7002
Adding replica 172.20.10.7:7006 to 172.20.10.7:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001
 slots:[0-5460] (5461 slots) master
M: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002
 slots:[5461-10922] (5462 slots) master
M: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003
 slots:[10923-16383] (5461 slots) master
S: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004
 replicates a4128b5e581c3722acd9b093c5f29f5056f680b0
S: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005
 replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26
S: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006
 replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413
Can I set the above configuration? (type 'yes' to accept): yes

Enter yes and press Enter

>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
......
>>> Performing Cluster Check (using node 172.20.10.7:7001)
M: a4128b5e581c3722acd9b093c5f29f5056f680b0 172.20.10.7:7001
 slots:[0-5460] (5461 slots) master
 1 additional replica(s)
M: d6fed6f21269b8469a3076ac5fb168bd20f70c26 172.20.10.7:7002
 slots:[5461-10922] (5462 slots) master
 1 additional replica(s)
S: 45cc35740ac67f7988bb75325871ba12d08a76e4 172.20.10.7:7004
 slots: (0 slots) slave
 replicates a4128b5e581c3722acd9b093c5f29f5056f680b0
M: 51a0f62dacead745ce5351cdbe0bdbae553ce413 172.20.10.7:7003
 slots:[10923-16383] (5461 slots) master
 1 additional replica(s)
S: 668054fe16cdf8741152cae863f5c636ed18b803 172.20.10.7:7005
 slots: (0 slots) slave
 replicates d6fed6f21269b8469a3076ac5fb168bd20f70c26
S: ae39b7db285703f8c08412d6b04998c60a634295 172.20.10.7:7006
 slots: (0 slots) slave
 replicates 51a0f62dacead745ce5351cdbe0bdbae553ce413
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

At this point, the Reids5 cluster is built.

6. Cluster Operation

6.1. Shut down the cluster

Method 1:

Redis5 provides tools to shut down the cluster, which are in the following directory:

/root/redis-5.0.0/utils/create-cluster

Open this file and modify the port to our own, as shown below:

Port PROT is set to 7000, NODES is set to 6, and the tool will automatically add 1 to generate six nodes 7001-7006 for operation.

Look down and modify the path and add the IP address. If you do not add it, the default local 127.0.0.1

After modification, execute the following command to shut down the cluster:

/root/redis-5.0.0/utils/create-cluster/create-cluster stop

Method 2:

Write a script file in the create-cluster directory: vim shutdown.sh
The content is as follows:

/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7001 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7002 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7003 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7004 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7005 shutdown
/usr/local/redis-cluster/redis-cli -c -h 172.20.10.7 -p 7006 shutdown

Then execute chmod u+x shutdown.sh to turn shutdown.sh into an executable file

Start in the current directory: ./shutdown.sh

View: ps aux|grep redis

Official: /usr/local/redis-cluster/redis-cli -a xxx -c -h 192.168.5.100 -p 8001

Tips: -a access server password, -c indicates cluster mode, -h specifies IP address, -p specifies port number

6.2. Restart the cluster

/root/redis-5.0.0/utils/create-cluster/create-cluster start

6.3. Start the cluster using script files

vim startall.sh add the following content: (remember to change your own IP address)

/usr/local/redis-cluster/redis-cli --cluster create 172.20.10.7:7001 172.20.10.7:7002 172.20.10.7:7003 172.20.10.7:7004 172.20.10.7:7005 172.20.10.7:7006 --cluster-replicas

Start: ./startall.sh

7. Test cluster

Execute in the redis-cluster directory

redis01/redis-cli -h 192.168.25.153 -p 7002 -c

Where -c means connecting to redis in cluster mode , -h specifies the IP address, and -p specifies the port number

cluster nodes Query cluster node information

cluster info query cluster status information

Summarize

The above is the detailed explanation of the construction and use of redis5 cluster under Linux (Centos7) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Detailed explanation of three ways to build a Redis cluster
  • Method of building redis cluster based on docker
  • Use Docker to build a Redis master-slave replication cluster
  • Redis automatic installation and cluster implementation process
  • Example of building a redis-sentinel cluster based on docker
  • Redis cluster construction process (very detailed, suitable for novices)

<<:  Tutorial on migrating mysql from phpstudy to Linux

>>:  Explore JavaScript prototype data sharing and method sharing implementation

Recommend

Vue realizes the function of book shopping cart

This article example shares the specific code of ...

MySQL cursor detailed introduction

Table of contents 1. What is a cursor? 2. How to ...

Detailed explanation of vue.js dynamic components

:is dynamic component Use v-bind:is="compone...

An example of elegantly writing status labels in Vue background

Table of contents Preface optimization Extract va...

Keep-alive multi-level routing cache problem in Vue

Table of contents 1. Problem Description 2. Cause...

HTML uses marquee to achieve text scrolling left and right

Copy code The code is as follows: <BODY> //...

js to achieve simple product screening function

This article example shares the specific code of ...

Detailed explanation of CSS line-height and height

Recently, when I was working on CSS interfaces, I...

CSS--overflow:hidden in project examples

Here are some examples of how I use this property ...

How to create Apache image using Dockerfile

Table of contents 1. Docker Image 2. Create an in...

Issues and precautions about setting maxPostSize for Tomcat

1. Why set maxPostSize? The tomcat container has ...

How to access the local machine (host machine) in Docker

Question How to access the local database in Dock...

HTML uncommon tags optgroup, sub, sup and bdo example code

Optgroup is used in the select tag to make the dro...