Redis-Cluster cluster introduction and construction

One, cluster introduction

1.1 Introduction

1) Version requirements: Cluster is only supported starting from Redis3.0;
2) Master/backup requirements: The cluster requires nodes to support master/backup mode (that is, each master node has at least one slave node)

1.2, architecture scheme

1) Each circle represents a master node, and any two nodes can communicate;
2) The client can access any node for data access operations;
Insert picture description here

1.3, voting mechanism

1) The master nodes in Redis-cluster vote with each other through ping-pong to determine whether the node is available; (for example, if more than half of the master nodes do not respond to ping a master node, it will be considered down. Then it will continue. Ping its slave node. If it also goes down, the entire cluster will enter the fail unavailable state)
2) In what state will the cluster be unavailable?
a. Any cluster master node hangs up, and there is no slave or slave is unavailable, and enters the fail state;
b. More than half of the master nodes in the cluster hang up (regardless of whether the slave node is available)

1.4, storage mechanism

Principle
1. Redis-cluster will calculate the key of the operation according to the crc16 algorithm;
2. Take the remainder of the calculation result to 16384 to obtain a number between 0 and 16383;
3. Find the corresponding hash slot through the entire number value Node;
4. Then perform data access operations on the node;
Insert picture description here
for example:
perform the set key hello operation on Node1 (illustrated in the above figure)
1. crc16 algorithm: for example, get crc16:key = 26384
2. Take the remainder: 26384%16384 = 10000
3. Hash slot node: find the hash slot node Node2 corresponding to 10000.
4. Operation: actually execute the set key hello operation in Node2.
5. The principle is the same when get key hello.

Two, cluster construction

2.1. Preparation

1) Build the smallest redis-cluster cluster, that is, three pairs of master and slave (pseudo cluster is used here)
2) Install 6 redis instances in the virtual machine, and allocate ports 8001, 8002, 8003, 8004, 8005, 8006;
3) If To create a cluster using the used stand-alone version, you need to delete the dump.rdb and apeendonly.aof files;

2.2, installation

2.2.1, install ruby

[root@eureka1 ~]# yum install ruby
...
[root@eureka1 ~]# yum install rubygems

2.2.2, package preparation

Upload: rzCommand upload to the /tempdirectory

-rw-r--r--.  1 root root  1358081 117 2020 redis-3.0.0.tar.gz

Unzip: tar -zxvf redis-3.0.0.tar.gz

drwxrwxr-x.  6 root root     4096 41 2015 redis-3.0.0

2.2.3, find redis-trib.rb

This file can help us to create redis-cluster, written in ruby ​​language

[root@eureka1 temp]# cd redis-3.0.0/src/
[root@eureka1 src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 41 2015 redis-trib.rb

2.2.4, upload/install redis-3.0.0.gem

Upload to /tempcatalog

-rw-r--r--.  1 root root    57856 117 2020 redis-3.0.0.gem

installation

[root@eureka1 temp]# gem install redis-3.0.0.gem
Successfully installed redis-3.0.0
1 gem installed

2.2.5, stand-alone installation (as prepared for cluster installation)

Compile and enter the unzipped directory

[root@eureka1 redis-3.0.0]# cd /root/temp/redis-3.0.0
[root@eureka1 redis-3.0.0]# make

installation

[root@eureka1 redis-3.0.0]# make install PREFIX=/usr/local/redis3

Copy redis.conf

[root@eureka1 redis3]# cp /root/temp/redis-3.0.0/redis.conf /usr/local/redis3/bin/

Change daemonize no to yes to
start

[root@eureka1 bin]# ./redis-server redis.conf

stop

[root@eureka1 bin]# ./redis-cli shutdown

2.2.6, cluster mode installation (6 instances)

1) /usr/local/Create a redis-clusterfolder under the directory

[root@eureka1 temp]# cd /usr/local/
[root@eureka1 local]# mkdir redis-cluster

2) the /redis3directory /binare copied to redis-clusterthe next, and renameredis01

[root@eureka1 redis3]# cp -r bin ../redis-cluster/redis01
[root@eureka1 redis3]# 

3) Remove redis01中the dump..rdbthe apeendonly.aofdocument
4) modify the redis.confconfiguration file
Port: The default port 6379were changed to 8001
enable the cluster: release cluster-enabled yesconfiguration Note
5) into 6 replicate redis example, and ports are modified to 8002,8003,8004,8005,8006

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

6) Copy the redis-trib.rbruby script to redis-cluster

[root@eureka1 redis-3.0.0]# cd /root/temp/redis-3.0.0/src/
[root@eureka1 src]# cp redis-trib.rb /usr/local/redis-cluster/

7) Create a startup script startall.sh

[root@eureka1 redis-cluster]# vim startall.sh 
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 ..

8) Script authorization

[root@eureka1 redis-cluster]# chmod +x startall.sh 

9) Start script

[root@eureka1 redis-cluster]# ./startall.sh 
[root@eureka1 redis-cluster]# ps aux|grep redis
root       4108  0.1  0.6  40596  5688 ?        Ssl  13:33   0:00 ./redis-server *:8001 [cluster]
root       4110  0.1  0.4  40596  3660 ?        Ssl  13:33   0:00 ./redis-server *:8002 [cluster]
root       4114  0.0  0.4  40596  3660 ?        Ssl  13:33   0:00 ./redis-server *:8003 [cluster]
root       4118  0.0  0.4  40596  3700 ?        Ssl  13:33   0:00 ./redis-server *:8004 [cluster]
root       4120  0.0  0.4  40596  3664 ?        Ssl  13:33   0:00 ./redis-server *:8005 [cluster]
root       4126  0.0  0.4  40596  3612 ?        Ssl  13:33   0:00 ./redis-server *:8006 [cluster]
root       4165  0.0  0.1  12320   964 pts/0    S+   13:33   0:00 grep --color=auto redis

10) Creating a cluster
rbscript will automatically assign master and slave nodes, which 1means one master and one backup

[root@eureka1 redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.48.128:8001 192.168.48.128:8002 192.168.48.128:8003 192.168.48.128:8004 192.168.48.128:8005 192.168.48.128:8006

The following log the distribution details of the master node and the slave node, and the slot hash slot allocation interval of each master node;

>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.48.128:8001
192.168.48.128:8002
192.168.48.128:8003
Adding replica 192.168.48.128:8004 to 192.168.48.128:8001
Adding replica 192.168.48.128:8005 to 192.168.48.128:8002
Adding replica 192.168.48.128:8006 to 192.168.48.128:8003
M: e85faa81e1260828dbe58118753056796715d81d 192.168.48.128:8001
   slots:0-5460 (5461 slots) master
M: 86ce0943516ebbe30e5f3cebf40b7076f46212c7 192.168.48.128:8002
   slots:5461-10922 (5462 slots) master
M: b1736ef65c852458e27288162ff82e926fc16ace 192.168.48.128:8003
   slots:10923-16383 (5461 slots) master
S: f5c3b41dc41437ab514995c2f5952b53a6eb2a14 192.168.48.128:8004
   replicates e85faa81e1260828dbe58118753056796715d81d
S: 0a2a99d9940faa104d4b0b1137f98ec7277974d6 192.168.48.128:8005
   replicates 86ce0943516ebbe30e5f3cebf40b7076f46212c7
S: e1f4a09028d9aeeedc8b125e7ba3238dde6a21a9 192.168.48.128:8006
   replicates b1736ef65c852458e27288162ff82e926fc16ace
Can I set the above configuration? (type 'yes' to accept): yes

11) Through the nodes.conffile in the node , you can view the cluster information in the node

[root@eureka1 redis01]# cat /usr/local/redis-cluster/redis01/nodes.conf 
0a2a99d9940faa104d4b0b1137f98ec7277974d6 192.168.48.128:8005 slave 86ce0943516ebbe30e5f3cebf40b7076f46212c7 0 1599918231045 5 connected
e1f4a09028d9aeeedc8b125e7ba3238dde6a21a9 192.168.48.128:8006 slave b1736ef65c852458e27288162ff82e926fc16ace 0 1599918230327 6 connected
e85faa81e1260828dbe58118753056796715d81d 192.168.48.128:8001 myself,master - 0 0 1 connected 0-5460
b1736ef65c852458e27288162ff82e926fc16ace 192.168.48.128:8003 master - 0 1599918230021 3 connected 10923-16383
86ce0943516ebbe30e5f3cebf40b7076f46212c7 192.168.48.128:8002 master - 0 1599918227975 2 connected 5461-10922
f5c3b41dc41437ab514995c2f5952b53a6eb2a14 192.168.48.128:8004 slave e85faa81e1260828dbe58118753056796715d81d 0 1599918228998 4 connected
vars currentEpoch 6 lastVoteEpoch 0

12) Test cluster
Any node can be connected, but -cparameters need to be added , otherwise it will be regarded as a stand-alone mode and the operation will fail

[root@eureka1 redis-cluster]# ./redis01/redis-cli -h 192.168.48.128 -p 8001 -c
192.168.48.128:8001> set name haha
-> Redirected to slot [5798] located at 192.168.48.128:8002
OK
192.168.48.128:8002> get name
"haha"

13) Shut down the cluster and
create a shutdownall.shscript

[root@eureka1 redis-cluster]# vim shutdownall.sh
./redis01/redis-cli -h 192.168.48.128 -p 8001 shutdown
./redis01/redis-cli -h 192.168.48.128 -p 8002 shutdown
./redis01/redis-cli -h 192.168.48.128 -p 8003 shutdown
./redis01/redis-cli -h 192.168.48.128 -p 8004 shutdown
./redis01/redis-cli -h 192.168.48.128 -p 8005 shutdown
./redis01/redis-cli -h 192.168.48.128 -p 8006 shutdown
[root@eureka1 redis-cluster]# chmod +x shutdownall.sh 
[root@eureka1 redis-cluster]# ./shutdownall.sh 
[root@eureka1 redis-cluster]# ps aux|grep redis
root       5365  0.0  0.1  12320   968 pts/0    R+   14:02   0:00 grep --color=auto redis

Guess you like

Origin blog.csdn.net/shaixinxin/article/details/108544770