Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Building redis Cluster

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the method of redis cluster building, the introduction in the article is very detailed, has a certain reference value, interested friends must read it!

redis cluster building

Before introducing the formal content, let's first introduce how to build redis standalone version.

Download the redis package and extract the compressed file;

Go to the decompressed redis file directory (you can see Makefile at this point) and compile the redis source file;

Install the compiled redis source files into the/usr/local/redis directory. If there is no redis directory in the/local directory, a new redis directory will be automatically created.

Go to/usr/local/redis/bin directory, directly./ redis-server starts redis (in this case, the front end starts redis);

Change the redis startup mode to backend startup. The specific method is to copy the redis.conf file under the decompressed redis file to the/usr/local/redis/bin directory, and then modify the redis.conf file->daemonize: no to daemonize: yse;

In the/bin directory via./ redis-server redis.conf Start redis (in this case background start).

The installation of redis standalone version is complete.

For details, please refer to-> redis Getting Started

Please forgive me for my nagging, ok, and then let's go back to this topic---redis cluster building!

1. Introduction to Redis Cluster

Redis is an open source key value storage system that is favored by a large number of Internet companies. Before redis3.0, only single-case mode was supported, and clustering was supported only in 3.0 and later. I use redis3.0.0 here;

Redis cluster adopts P2P mode, which is completely decentralized, and there is no central node or proxy node;

Redis cluster has no unified entrance. When the client connects to the cluster, it can connect any node in the cluster. The nodes in the cluster communicate with each other (PING-PONG mechanism). Each node is a redis instance.

In order to achieve high availability of the cluster, that is, to determine whether the node is healthy (whether it can 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 this node is down (fail). This is the way to determine whether a node is down;

How do you determine if the cluster is dead? -> If any node in the cluster goes down, and that node has no slave nodes (backup nodes), then the cluster goes down. This is the way to determine if the cluster is down;

So why is it that if any node goes down (there are no slave nodes) the cluster goes down? -> Because the cluster has 16384 slots (hash slots) built in, 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 key-value in the Redis cluster, redis will first perform crc16 algorithm on this key, and then get a result. The remainder of 16384 corresponds to one of the slots [0-16383], which determines which node the key-value is stored in. Therefore, once a node hangs, the slot corresponding to the node cannot be used, which will cause the cluster to fail to work properly.

In summary, each Redis cluster can theoretically have up to 16384 nodes.

II. The environment needed for cluster building

2.1 Redis cluster needs at least 3 nodes, because voting fault tolerance mechanism requires more than half of the nodes to think that a node is hung, 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 the Redis cluster needs at least 6 servers. Because I don't have so many servers and I can't start so many virtual machines, what I set up here is a pseudo-distributed cluster, that is, a server runs 6 redis instances virtually, and the port number is modified to (7001-7006). Of course, the actual production environment Redis cluster is set up the same as here.

2.3 install Ruby

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

3.1 Create a new redis-cluster directory under usr/local directory to store cluster nodes

3.2 Copy all the files in the bin directory under the redis directory to the directory/usr/local/redis-cluster/redis01. Don't worry that there is no redis01 directory here. It will be created automatically. The commands are as follows (note the current path):

cp -r redis/bin/ redis-cluster/redis01

3.3 Delete the snapshot file dump.rdb in redis01 directory, and modify the redis.cnf file in this directory. Specifically, modify two places: one is to change the port number to 7001, and the other is to enable cluster creation mode. Open comments. As shown in the following figure:

Delete dump.rdb file

Change port number to 7001, default is 6379

Open the comment cluster-enabled yes

3.4 Copy 5 copies of the redis-cluster/redis01 file to the redis-cluster directory (redis02-redis06), create 6 redis instances, and simulate 6 nodes of the Redis cluster. Then change the port numbers in redis.conf to 7002-7006 in the remaining 5 files. As shown in the following figure:

Create redis02-06 directory

Modify redis.conf file port numbers to 7002-7006 respectively

3.5 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. The file content is as follows:

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

3.6 After creating the startup script file, you need to modify the permissions of the script so that it can be executed. The instructions are as follows:

chmod +x start-all.sh

3.7 Execute start-all.sh script to start 6 redis nodes

3.8 OK, so far the 6 redis nodes have been successfully started, and then the cluster is officially started. The above are the preparation conditions. Don't think that the picture looks too long so feel trouble, in fact, the above steps are also a matter of one sentence: create 6 redis instances (6 nodes) and start.

To build a cluster, you need to use a tool (script file) that is in the source code of the redis unzipped file. Because this tool is a ruby script file, so this tool needs to run ruby environment, which is equivalent to java language needs to run on jvm. So you need to install ruby, the instructions are as follows:

yum install ruby

Then you need to install the ruby-related package to the server, I use redis-3.0.0.gem here, you need to pay attention to is: redis version and ruby package version is best kept consistent.

Install Ruby packages to the server: download and install them first, as shown in Figure

The installation commands are as follows:

gem install redis-3.0.0.gem

3.9 The previous step has installed the environment and ruby package required for the ruby tool. Next, you need to copy the ruby script tool to the usr/local/redis-cluster directory. So where is this ruby scripting tool? As mentioned earlier, in the redis extract file source code, namely redis/src directory redis-trib.rb file.

3.10 Copy the ruby tool (redis-trib.rb) to the redis-cluster directory with the following instructions:

cp redis-trib.rb /usr/local/redis-cluster

Then use the script file to build the cluster, the instructions are as follows:

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

Note: everyone here should enter the corresponding ip address according to their own server ip!

There's a place where you need to manually type yes

So far, Redi cluster has been successfully built! Note that the last paragraph shows the slots assigned to each node. There are 6 nodes in total, 3 of which are slaves, so the 3 master nodes map 0-5460, 5461-10922, and 10933-16383solts respectively.

3.11 Finally, connect the cluster nodes. Connect any one of them:

redis01/redis-cli -p 7001 -c

Note: Be sure to add-c, otherwise there is no automatic jump between nodes! As can be seen in the following figure, the stored data (key-value) is evenly distributed to different nodes:

IV. Conclusion

Huff ~~~ Long sigh of relief…Finally built Redis cluster.

The whole process is actually quite simple, this article is mainly for entry-level friends, inserted a lot of pictures, so it seems lengthy, I hope you understand more, if not, hope to correct in time ~

Finally, add two basic redis cluster commands:

1. View current cluster information

cluster info

2. See how many nodes are in the cluster

Cluster nodes The above is "redis cluster building method" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report