Taotao Mall series-Redis cluster construction (reproduced)

In this article, I will guide you how to build a Redis cluster. Let me first talk about why we need to build a Redis cluster. Redis stores data in memory, and our computers generally have small memory. This means that Redis is not suitable for storing big data. The Hbase or MogoDB of the Hadoop ecosystem is suitable for storing big data. Redis is more suitable for handling high concurrency. The storage capacity of one device is very limited, but the cooperation of multiple devices can increase the memory many times, which requires the use of clusters.

redis-cluster architecture diagram

Let's take a look at the redis-cluster architecture diagram, as shown in the figure below. It can be seen that the Redis cluster does not have a unified entry. The client can connect to any device in the cluster. Each device in the cluster interacts regularly to know whether the node is still working.
Write picture description here
According to my personal understanding, I will talk about some details of the cluster architecture:

  1. The blue circles in the above figure represent nodes. Each node uses the PING-PONG mechanism to communicate with each other, and the binary protocol is used internally to optimize the transmission speed and bandwidth.
  2. In the entire cluster, there is no agent layer, which means that no node in the cluster is the entry point. That is, if the client wants to connect to the cluster, it can connect to any node in the cluster. As long as you connect to one of them, then you can access any node in the cluster through this node.
  3. We now have so many nodes in the cluster, is the data stored on the nodes the same? If the stored data is the same, then this cluster is actually not very meaningful, at most it can be regarded as a reserve. All data in Redis is stored in the memory, and there is not much that can be stored in the memory. If the amount of data is large, then a server will not be able to store it at this time, even if you add more backup machines. Solve the problem. If you want to solve this storage capacity problem, you need to ensure that the data stored on each node is different. What if one of the nodes goes down? The data of these nodes in the entire cluster is incomplete. If any one of the nodes is down, then the cluster is down. If you want to keep it down, each node should have a backup machine. If the main node is down , Then the backup machine will come up. So how to judge that a node is down? ——The node hanging up is determined by voting by more than half of the nodes in the cluster (voting mechanism). If more than half of the nodes think that the node is down, then it will hang up. To be more detailed, Redis cluster has a fault-tolerant voting mechanism, as shown in the figure below. The light yellow node sends a ping command to the red node, and the red node does not respond. At this time, the light yellow node thinks that the node may be down, and it will vote for it, but it is only a question at this time, so the light yellow node Draw a "?", and then the light yellow node tells other nodes that the red node may be down. The second node tries to contact the red node and finds that the ping is different, so the second node also thinks this The red node hangs up, and the second node also casts one vote. Then the third node goes to contact the red node, but it can't be contacted, so it also casts one vote, so there are three votes. Redis's fault-tolerant voting mechanism is that more than half of the nodes in the cluster think that a node is down, then the node is deemed to be down. At this time, it depends on whether the red node has a backup node. If there is no backup node, the entire cluster will stop providing services to the outside world. If there is a backup node, the backup node will be righted and continue to provide services to the outside world.
    Write picture description here
  4. We want to store the data in different nodes. How do we determine which node my data is stored on? This time Redis cluster introduced another concept, that is, the slot number of this concept, the groove is fixed, a total of 16,384 slots. We first allocate this slot to different nodes, each node is allocated a certain number of slots, anyway, a total of 16384 slots. When we want to store a key in it, first calculate which slot the key should be in. After the calculation, we know which server this slot is allocated to. So, we find the corresponding server and save the key on that server.
    The Redis cluster has built-in 16384 hash slots. When a key-value needs to be placed in the Redis cluster, Redis first uses the crc16 algorithm to calculate a result on the key, and then calculates the remainder of the result to 16384, so that each key will correspond to a number In the hash slot between 0--16383, redis will map the hash to different nodes roughly equal to the number of nodes. Let's take the following figure as an example. We assign these 16384 hash slots to three servers. If the card slot on Server1 is 0-5000, the card slot on Server2 is 5001-10000, and the card slot on Server3 is 10001-16383. If we want to save the string "Hello", and "Hello" is 500 after crc16 algorithm and then the remainder of 16384, obviously it should be stored on Server1, if the string "Hello2" to be stored is crc16 algorithm and then 16384 The remainder is 11500, so obviously it should be stored on Server3. Similarly, "Hello3" and "Hello1" are stored on Server2 and Server1 respectively. In other words, each server actually stores different contents, which is why if a node fails, if there is no backup node, the entire cluster will fail because the data is incomplete. In addition, the number of card slots on each server can be determined according to the performance of the server. If the performance is good, more card slots can be allocated, so that more content can be stored on it.
    Write picture description here

Redis cluster construction

Next, let's build a Redis cluster. Because the fault tolerance mechanism of the cluster is that more than half of the nodes think that a node is down, they will confirm that it is down. Therefore, we should build an odd number of clusters (>=3). In addition, for high availability, each node needs to have a backup node, so we need to build a Redis cluster with at least 6 virtual machines. In this case, we need 6 virtual machines, and our local machine cannot run 6 virtual machines, but we still want to build a Redis cluster. What should we do? It's actually very simple. You can only build a pseudo-distributed cluster, which is not a real cluster. The specific approach is to use a virtual machine to run 6 Redis instances, each Redis instance needs to run on a different port, here I assume the 6 ports 7001-7006.

Create 6 Redis instances on a virtual machine

I believe that everyone knows how to create a Redis instance on a virtual machine. If readers do not already know it, they can learn from this article on the Taotao Mall series-Redis installation .
First, we create a redis-cluster directory under the /usr/local directory, as shown in the figure below.
Write picture description here
Then we copy the redis/bin directory to the redis-cluster/redis01 directory. If there is no redis01 in the redis-cluster directory, create the directory, as shown in the following figure.
Write picture description here
Then we enter the redis-cluster/redis01 directory and use the llcommand to view the directory.
Write picture description here
It can be found that there are some persistent files in the Redis instance, so we have to delete these persistent files, otherwise, if there is data in this node, the cluster will not be able to set up.
Write picture description here
Next, we modify the running port number of the redis01 instance through the redis.conf configuration file, set its port number to 7001, and remember to set cluster-enabled in the redis.conf configuration file to yes!
Write picture description here
After the configuration file of the redis01 instance is modified, we only need to copy 5 such Redis instances in the redis-cluster directory, as shown in the following figure.
Write picture description here
Now that we have 6 Redis instances, we need to run each Redis instance on a different port. The port number of the redis01 instance has been changed to 7001, and the next step is to modify the running port numbers of the other five Redis instances. The specific method is to set the port number of the redis02 instance to 7002, the port number of the redis03 instance to 7003, the port number of the redis04 instance to 7004, the port number of the redis05 instance to 7005, and the port number of the redis06 instance to 7006.

Start each Redis instance

After we create 6 Redis instances on a virtual machine, we need to start each Redis instance. If you want to start each Redis instance, you don't have to start each instance one by one, which is too troublesome. We can write a batch program to start them all at once. Use the vim start-all.shcommand in the redis-cluster directory to create a batch file, the content of the file is:

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 ..
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

After saving the file, use the llcommand to check the redis-cluster directory, you can find the start-all.sh file, as shown in the figure below.
Write picture description here
But this batch program cannot be run, so you chmod u+x start-all.shneed to change its permissions when using the ./start-all.shcommand , so that we can use the command to start each Redis instance.
Write picture description here
How do I know that each Redis instance is up? You can use ps aux|grep rediscommands to view the startup process of each Redis instance, as shown in the figure below.
Write picture description here

Install ruby ​​environment

We use ruby ​​scripts to build the cluster, so we need to install the ruby ​​operating environment. The specific method is to enter the yum install rubycommand to install the running environment of ruby when connected to the Internet , as shown in the following figure.
Write picture description here
After installing ruby, we also need to install rubygems (that is, ruby ​​third-party package management tool), use the command yum install rubygems, as shown in the figure below.
Write picture description here
After installing ruby ​​and rubygems, we need to install a package redis-3.0.0.gem needed by ruby ​​script to run. Below is the redis-3.0.0.gem package I downloaded.
Write picture description here
After you download it, you will upload it to the virtual machine. How do you know that we have uploaded it? Use the llcommand to check the directory under the current user's home directory (ie root) .
Write picture description here
Let's install this third-party package, enter the gem install redis-3.0.0.gemcommand and press Enter, as shown in the figure below.
Write picture description here

Use ruby ​​script to build cluster

All the preparations above are actually serving a script file (redis-trib.rb), which is located in the /root/redis-3.0.0/src directory, as shown in the figure below.
Write picture description here
To facilitate management, we copy this script file to the /usr/local/redis-cluster directory, then enter the directory, and use the llcommand to check whether there is a redis-trib.rb script file in the directory, as shown in the figure below.
Write picture description here
Let's officially start to build a cluster. We use the following commands to build a cluster.

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

In the command -replicas is to specify the number of nodes to be backed up by each node. We are now making one backup for each node, so enter 1.
Write picture description here
From the above figure, we can see that the slave master node is randomly composed, the master node is 192.168.25.128:7001, 192.168.25.128:7002, 192.168.25.128:7003 these three devices, 192.168.25.128:7004, 192.168.25.128:7005 , 192.168.25.128:7006 These three are standby nodes. Adding replica 192.168.25.128:7004 to 192.168.25.128:7001 means 192.168.25.128:7004 is used as the slave node of 192.168.25.128:7001. Similarly, 192.168.25.128:7005 is used as the slave node of 192.168.25.128:7002. 192.168.25.128:7006 serves as the slave node of 192.168.25.128:7003. In this way, our cluster is set up.

In this article, I will guide you how to build a Redis cluster. Let me first talk about why we need to build a Redis cluster. Redis stores data in memory, and our computers generally have small memory. This means that Redis is not suitable for storing big data. The Hbase or MogoDB of the Hadoop ecosystem is suitable for storing big data. Redis is more suitable for handling high concurrency. The storage capacity of one device is very limited, but the cooperation of multiple devices can increase the memory many times, which requires the use of clusters.

Guess you like

Origin blog.csdn.net/tonglei111/article/details/107163516