1 introduction

Before Redis 3.0, sentinel mechanism was used to monitor the status between nodes. Redis Cluster is a distributed solution of Redis, which was officially launched in version 3.0, effectively solving the needs of Redis in the distributed aspect. Cluster architecture can be used to achieve load balancing when encountering bottlenecks such as single memory, concurrency and flow

2 Redis Cluster Describes the Cluster

The Redis Cluster Cluster mode usually has the features of high availability, scalability, distribution, and fault tolerance. Redis Cluster uses ** virtual slot partitions. ** What is a virtual slot partition

2.1 Virtual Slot Partitioning

Virtual slot partitioning makes clever use of hash space, using well-distributed hash functions to map all data into a fixed range of integers defined as slots. Generally, the range is much larger than the number of nodes. For example, the range of Redis Cluster slots is 0 to 16,383. A slot is the basic unit for data management and migration in a cluster. The main purpose of adopting a wide range of slots is to facilitate data splitting and cluster expansion. Each node is responsible for a certain number of slots, as shown in the figure:

The current cluster has five nodes, and each node is responsible for an average of about 3276 slots. Due to the high quality hash algorithm, the data mapped by each slot is usually fairly uniform, and the data is evenly divided into 5 nodes for data partitioning. Redis Cluster uses virtual slot partitions.

  • Node 1: contains hash slots 0 to 3276.
  • Node 2: Contains hash slots 3277 through 6553.
  • Node 3: contains hash slots 6554 through 9830.
  • Node 4: Contains hash slots 9831 through 13107.
  • Node 5: Contains hash slots 13108 to 16383.

This structure makes it easy to add or remove nodes. If a node 6 is added, some slots from nodes 1 to 5 are allocated to node 6. If you want to remove node 1, move the slots in node 1 to nodes 2 to 5, and remove node 1 from the cluster.

Since moving a hash slot from one node to another does not stop service, adding or deleting or changing the number of hash slots on a node does not make the cluster unavailable.

2.2 Redis data partitioning

Redis Cluster uses virtual slot partitioning. All keys are mapped into slots 0 to 16383 according to the hash function. Each node is responsible for maintaining a part of slots and the key value data mapped to the slots, as shown in the figure:

3 Redis cluster construction

3.1 Environment Preparation

3.1.1 install GCC

Redis installation requires the GCC environment

\1. Check whether GCC is installed

Run the GCC -v command

If the GCC version information is displayed, GCC is installed. Otherwise, install GCC

\ 2. Install the GCC

Create a directory /usr/local/gccsrc

Upload the following RPM files to the /usr/local/gccsrc directory

CPP – 4.8.5-11. El7. X86_64. RPM

GCC – 4.8.5-11. El7. X86_64. RPM

Glibc – devel – 2.17-157. El7. X86_64. RPM

Glibc – headers – 2.17-157. El7. X86_64. RPM

The kernel – the headers – 3.10.0-514. El7. X86_64. RPM

Libmpc 1.0.1-3. El7. X86_64. RPM

MPFR 3.1.1-4. El7. X86_64. RPM

\3. Install in sequence

The RPM – the ivh MPFR 3.1.1-4. El7. X86_64. RPM

The RPM – the ivh libmpc 1.0.1-3. El7. X86_64. RPM

The RPM – the ivh kernel headers – 3.10.0-514. El7. X86_64. RPM

The RPM – the ivh glibc – headers – 2.17-157. El7. X86_64. RPM

The RPM – the ivh glibc – devel – 2.17-157. El7. X86_64. RPM

The RPM – the ivh CPP – 4.8.5-11. El7. X86_64. RPM

The RPM – the ivh GCC – 4.8.5-11. El7. X86_64. RPM

\4. Verify GCC -V after successful installation

3.1.2 installation zlib

Because Redis cluster environment needs to use ruby interface, need to use Redis gem package, and install gem package needs zlib environment

1. Upload zlib-1.2.11.tar.gz to /usr/local

2. Decompress tar -xzvf zlib-1.2.11.tar.gz

3, CD zlib – 1.2.11

4,. / configure — prefix = / usr/local/zlib

GCC check for GCC check for GCC check for GCC

5, make

6, make install

3.1.3 install openssl

Because Redis cluster environment needs to use Ruby interface, need to use Redis gem package, and install gem package need to have OpenSSL environment

1. Upload openSSL-1.0.1h.tar. gz to /usr/local/

2. Decompress tar -xzvf openSSL-1.0.1h.tar.gz

3, CD openssl – h / 1.0.1

/ config-fpic –prefix=/usr/local/openssl enable-shared

5,. / config – t

6, make && make install

7, which openSSL // Check the path of system OpenSSL

Rm -rf /usr/bin/openssl // /usr/bin/openssl is the path to which openssl of an earlier version is displayed

9, cp/usr/local/openssl/bin/openssl/usr/bin/openssl / / copy the new version of the installation path to the system path

10. Run openSSL version

Output Version Information

3.1.4 install ruby

Ruby is required to install a Redis cluster environment

1. Upload ruby-2.1.7.tar. gz to /usr/local

2, decompress tar -xzvf ruby-2.1.7.tar. gz

3, CD ruby – 2.5.7

/configure — prefix=/usr/local/ruby //-prefix =/usr/local/ruby //-prefix

5, make && make install

The successful installation is shown

6. Configure environment variables

vi /etc/profile

Add environment variables as shown in the figure

7. Run the command to make the configuration file take effect

source /etc/profile

8. Run the following command to check whether the Ruby installation is successful: If the installation is successful, the Ruby version information is displayed

ruby -v

9. Go to the /usr/local/ruby-2.1.7/ex/zlib/directory

CD/usr/local/ruby – 2.5.7 / ext/zlib /

Note: /usr/local/ruby-2.5.7 this directory is used to decompress the ruby installation package.

Ruby extconf.rb –with-zlib-include=/usr/local/ zlib-include / –with-zlib-lib=/usr/local/ zlib-lib

(Note :/usr/local/zlib is the zlib installation directory and a Makefile will be generated after the command is executed.)

11. Open the Makefile and find the following line (third from last) to change the path

Zlib. o: $(top_srcdir)/include/ruby.h: zlib.o:.. /.. /include/ruby.h

12, make && make install

13. Go to the /usr/local/ruby-2.1.7/ex/openssl/directory

CD/usr/local/ruby – 2.5.7 / ext/openssl /

14, execute command:

ruby extconf.rb –with-openssl-include=/usr/local/openssl/include/ –with-openssl-lib=/usr/local/openssl/lib

(Note :/usr/local/openssl is the openSSL installation directory and a Makefile will be generated after the command is executed.)

15, open the Makefile in topdir = / usr/local/ruby/include/ruby – XXX behind a new line top_srcdir = / usr/local/ruby – 2.5.7

(Note that /usr/local/ruby-2.5.7 is the directory where ruby decompresses.)

16, make && make install

3.1.5 installed redis – 4.1.3. Gem

The redis-4.1.3.gem provides an interface to Ruby that is required to install a cluster environment

1. Upload redis-4.1.3.gem to /usr/local/

2. Run the gem install redis-4.1.3.gem command

The successful installation is shown

3.2 Installing single-server Redis

1. Create /usr/local/redis and upload redis-4.0.14.tar.gz to /usr/local/redis

2、cd /usr/local/redis

3, decompress tar -xzvf redis-4.0.14.tar.gz

Redis – 4.0.14/4, CD

5, make && make install

See the following information, The Redis installation is successful

Go to the installation directory CD /usr/local/redis/redis-4.0.14

6. Modify the redis.conf configuration file

Bind 127.0.0.1 to bind 0.0.0.0

Daemonize no daemonize yes

Protected -mode yes change to protected-mode no

Logfile “” logfile “” redis log store file path”

7, start redis command

Go to the redis installation directory /usr/local/redis/redis-4.0.14 and run the following command to start Redis

redis-server redis.conf

Run the following command to connect to Redis

Redis -cli -p 6379 -h 192.169.1.86

(Note: 6379 is the default redis port 192.169.1.86 is the IP address of the server where Redis resides.)

Run the set 1 1 command to save the value to Redis to test

3.3 Deploying a Cluster Environment

The cluster mode is equivalent to dividing data slots. Each node is divided into a data slice. In this case, when a node goes down and the node is not backed up, the shard is no longer available. Therefore, it is recommended that each node in the cluster be equipped with a slave node for cold backup.

The cluster deployment mode recommended on the official website is three-active/three-slave deployment. This section uses the three-active/three-slave deployment as an example.

Node information to be configured

The name of the node The port number It is the Lord from Owning Primary node
Redis7001 7001 The main
Redis7002 7002 The main
Redis7003 7003 The main
Redis7004 7004 from Redis7004
Redis7005 7005 from Redis7001
Redis7006 7006 from Redis7002

1. Access the /usr/local/redis/ directory

cd /usr/local/redis

2. Run mkdir redis700{1,2,3,4,5,6} to create folders redis7001, redis7002, redis7003, redis7004, redis7005, and redis7006

Copy redis-cli and redis-server from /usr/local/bin/ to redis7001… Redis7006 directory

1. Create the redis.conf file in the redis7001 directory

vim redis.conf

The file content

# port number (6 corresponding port numbers)

port 7001

appendonly yes

# start cluster

cluster-enabled yes

#yes Enables the daemon

daemonize yes

Associate the cluster configuration file

cluster-config-file “nodes.conf”

# set timeout

cluster-node-timeout 5000

# log message

logfile “redis7001.log”

# specify the access address

The bind 0.0.0.0

tcp-keepalive 300

Conf file is created in the redis7002 directory

The content of the file is

port 7002

appendonly yes

cluster-enabled yes

daemonize yes

cluster-config-file “nodes.conf”

cluster-node-timeout 5000

logfile “redis7002.log”

The bind 0.0.0.0

tcp-keepalive 300

Conf files are created in redis7003 to redis7006

Each directory (redis7001 through redis7006) should have these three files

Run the command redis-server redis.conf to start each service

/usr/local/redis/ sh script to start redis instance

vim startall.sh

cd redis7001

./redis-server redis.conf

cd ..

cd redis7002

./redis-server redis.conf

cd ..

cd redis7003

./redis-server redis.conf

cd ..

cd redis7004

./redis-server redis.conf

cd ..

cd redis7005

./redis-server redis.conf

cd ..

cd redis7006

./redis-server redis.conf

Grant executable permission to the startall.sh script

chmod 777 startall.sh

Run the startall.sh script

./startall.sh

Perform ps – ef | grep redis situation view redis running situation, start-up success as shown

Create a cluster and add these nodes to the cluster. First go to the directory where redis-trib.rb is located

Go to the /usr/local/redis/redis-4.0.14/src/ directory

CD/usr/local/redis/redis – 4.0.14 / SRC /

Run the following command:

./redis-trib.rb create –replicas 1 192.169.1.86:7001 192.169.1.86:7002 192.169.1.86:7003 192.169.1.86:7004 192.169.1.86:7005 192.169.1.86:7006

[Note :192.169.1.86 is the IP address of the server where Redis resides. Note: this IP address cannot be written as 127.0.0.1, otherwise only the local machine can connect to it.]

Enter yes and press Enter. Redis-trib. rb starts node handshake and slot allocation. As shown in the figure, the node is successfully added to the cluster

Note :M indicates the primary node, S indicates the secondary node.

Perform cluster check to check the number of hash slots and slot coverage occupied by each Redis node. Among 16,384 slots, Redis7001, Redis7002, and Redis7003 occupy 5461, 5462, and 5461 slots, respectively.

Cluster Connection Test

Connection command :redis-cli -p Port of one node -h IP -c of one node

[Note: There must be -c. -C indicates that the connection is in cluster mode. Without -c, the connection is a single point.]

And save a value to Redis for test :set a A, as shown in the following figure, it is successful

3.3 Adding passwords for each node in a Cluster Environment

There are two ways to add a password:

U Method 1: Modify the redis.conf configuration file in all Redis clusters

Masterauth 123456 // Set the master password so that Salve can connect to Master RequirePass 123456 // set the Redis passwordCopy the code

U Method 2: Enter the real-time configuration in each Redis cluster

/redis-cli -c -p 7001 // Set masterauth 123456 config set requirepass 123456 config rewriteCopy the code

Note: The password for each node must be the same or Redirected will fail. The first method is recommended.

To shutdown all redis services, run the redis-cli -p 7001 shutdown command to shutdown redis instances one by one. After the preceding steps, the corresponding node information file (nodes.conf) and data information file (appendone.aof, dump.rdb) are generated. Delete all three files from the redis7001 to redis7006 folders, then modify redis.conf to add the password as shown in the figure

Then go to the client.rb file (use find / -name “client.rb”) and change the password, as shown in the figure below

The script is then used to start all cluster nodes.

Run the./redis-trib.rb create –replicas 1 192.169.1.21:7001 192.169.1.21:7002 192.169.1.21:7003 192.169.1.21:7004 192.169.1.21:7005 192.169.1.21:7006 Create clusters.

Test cluster

Use redis-cli -c -p 7001 to set a set name song, you will be prompted for authentication by typing Auth “123456”, then set name song will redirected 7002, also requiring password Redis -cli -c -p 7001 -a 123456 connection (-a is equivalent to input password authentication), so you can directly jump to. As shown in figure

After the password is added, the script to close the redis instance should be modified accordingly. After the parameter of -a 123456 is added, the modified content is as follows: