Redis distributed cluster construction (detailed)

First of all, let's talk about the redis stand-alone build process

1. Download the redis installation package and unzip it. I downloaded redis-5.0.7.tar.gz here;

2. Enter the decompressed redis file directory (you can see the Makefile at this time), and install the redis source file by compiling;

  make PREFIX=/usr/local/redis install At this point, the compilation is complete.

3. Then a bin folder appears under the directory,

4. Enter the /opt/redis/bin directory, and directly start redis by ./redis-server (the front end starts redis at this time);
5. Change the redis startup mode to back-end startup, the specific method: under the decompressed redis file Copy the redis.conf file to the /opt/redis/bin directory, then modify the redis.conf file -> daemonize: no to daemonize: yes;
5. Start redis in the directory through ./redis-server redis.conf (Start in the background at this time).
  In summary, the redis stand-alone version is installed and started.


Enter the topic, build a redis cluster

1. Introduction to Redis Cluster

1.1 Redis is an open source key value storage system, which is favored by the majority of Internet companies. Before redis3.0 version, only singleton mode was supported, and clusters were only supported in version 3.0 and later;
1.2 redis cluster adopts P2P mode, which is completely decentralized, and there is no central node or proxy node;
1.3 redis cluster has no unified entrance Yes, when the client connects to the cluster, it can connect to any node in the cluster. The nodes within the cluster communicate with each other (PING-PONG mechanism), and each node is a redis instance;
1.4 In order to achieve The high availability of the cluster is to determine whether the node is healthy (can it be used normally), redis-cluster has such a voting fault tolerance mechanism: if more than half of the nodes in the cluster vote that a node is down, then the node is down (fail ). This is the method to judge whether the node is down;
1.5 So how to judge whether the cluster is down? -> If any node in the cluster is down and the node has no slave node (backup node), then the cluster is down. This is the way to judge whether the cluster is down;
1.6 So why does any node hang up (there is no slave node) and the cluster hangs up? -> Because the cluster has built-in 16384 slots (hash slots), and all physical nodes are mapped to these 16384[0-16383] slots, or these slots are equally distributed to each node. When you need to store a data (key-value) in the Redis cluster, redis will first perform the crc16 algorithm on the key, and then get a result. Then calculate the remainder of 16384 with this result. This remainder will correspond to one of the slots [0-16383], and then determine which node the key-value is stored in. Therefore, once a node is down, the slot corresponding to the node cannot be used, which will cause the cluster to fail to work normally.
1.7 In summary, each Redis cluster can theoretically have up to 16,384 nodes.

Second, the environment required for cluster construction

2.1 A Redis cluster requires at least 3 nodes, because the voting fault tolerance mechanism requires that more than half of the nodes think that a node is down before the node is down, so 2 nodes cannot form a cluster.
2.2 To ensure the high availability of the cluster, each node needs to have a slave node, that is, a backup node, so a Redis cluster requires at least 6 servers. Because I don’t have so many servers, and I can’t start so many virtual machines, the pseudo-distributed cluster is built here, that is, one server runs six redis instances virtually, and the port number is changed to (9001-9006). Of course, the actual production environment The Redis cluster setup is the same as here.
2.3 Install ruby.

3. The specific steps of cluster construction are as follows (note that the firewall should be turned off)

3.1 Create a new redis-cluster directory under the /opt directory to store cluster nodes;

3.2 Copy all files in the bin directory under the redis directory to the /opt/redis-cluster/redis01 directory;

3.3 Modify the redis.conf file in this directory. There are three specific modifications: one is to modify bind's own IP, the other is to modify the port number to 9001 (the default is 6379), and the third is to open the cluster creation mode and open the comment. As shown in the figure below:

3.4 Copy 5 copies of the redis-cluster/redis01 file to the redis-cluster directory (redis02-redis06), create 6 redis instances, and simulate the 6 nodes of the Redis cluster. Then modify the port numbers in redis.conf under the remaining 5 files to 9002-9006. Respectively as shown below:
Create redis02-06 directory

3.5 Modify the port number of the redis.conf file of redis02-redis06 respectively

3.6 Then start all redis nodes. Since it is too troublesome to start one by one, create a script file to start redis nodes in batches. The command is start-all.sh , and the content of the file is as follows:

3.7 Execute start-all.sh to start all redis and view the process

3.8 So far, the 6 redis nodes have been successfully started, and then the cluster will be officially started. The above are all prerequisites.

To build a cluster, you need to use a tool (script file), which is in the source code of the redis decompression file. Because this tool is a ruby ​​script file, the operation of this tool requires ruby's operating environment, which is equivalent to the operation of java language on jvm. So you need to install ruby, the instructions are as follows:

3.9 Download redis-3.5.5.gem and install it.

3.10 Then build a redis cluster, and manually yes in the middle. as follows:

redis-cli --cluster create 192.168.26.11:9001 192.168.26.11:9002 192.168.26.11:9003 192.168.26.11:9004 192.168.26.11:9005 192.168.26.11:9006 --cluster-replicas 1

    So far, the Redi cluster has been successfully built! Everyone pays attention to the last paragraph, which shows the slots (hash slots) allocated by each node. Here there are a total of 6 nodes, of which 3 are slave nodes, so the 3 master nodes map 0-5460, 5461-10922, 10933-16383solts.

3.11 Finally, test the cluster connection node.

Finally, the building is completed! ! ! There are shortcomings, please advise! ! ! ! ! !

Guess you like

Origin blog.csdn.net/qq_41587243/article/details/103878605