Redis cluster building tutorial and troubleshooting, redis cluster building tutorial
Here, the idea of building a redis pseudo cluster with six nodes on a Linux virtual machine is simple. Six redis instances are enabled on one virtual machine, and each redis instance has its own port. In this case, it is equivalent to simulating six machines, and then you can build a redis cluster with these six instances.
Premise: redis has been installed, directory for/usr/local/redis-4.0.1 if not, you can refer to the article windows install redis Linux install redis
Redis clusters use ruby scripts. to execute this script, you need a ruby environment .. Corresponding redis source code src directory under the redis-trib.rb, redis-trib.rb is redis officially launched the redis cluster management tool, is based on redis cluster command package into simple, convenient and practical operation tools. So
Install the ruby environment:
1.yum install ruby
2.yum install rubygems
3.gem install redis
By default, Centos supports ruby to 2.0.0, and redis requires a minimum of 2.2.2. The solution is to first install rvm and then upgrade ruby to 2.3.3.
1.sudo yum install curl
2. Install rvm
curl -L get.rvm.io | bash -s stable
3.
source /usr/local/rvm/scripts/rvm
4. view known ruby versions in the rvm Library
rvm list known
5. Install a ruby version
rvm install 2.3.3
6. Use a ruby version
rvm use 2.3.3
7. Uninstall a known version
rvm remove 2.0.0
8. view the version
ruby --version
9. Install redis again
gem install redis
Redis cluster Construction
Create redis-cluster directory, then create redis-8001, redis-8002, redis-8003 node directory, and then copy redis-conf to the node directory respectively
Modify the redis-conf file under the node separately. Because it is on a machine (192.16819.129), each instance should have a different port. At the same time, each instance obviously has its own data storage location; enable AOF mode; Enable cluster configuration; Enable background mode;
Enable the redis service to see if it can be started. OK.
Create the startall. sh script (the prompt "permission denied" indicates that the permission is insufficient. Run the command chmod 777 startall. sh to modify the permission)
Start the startall. sh script
Create a stopall. sh script
Create a cluster
Next, we will use the Ruby script to create a cluster.
You can see that the redis-trib.rb has the following features:
- Create: create a cluster
- Check: check the Cluster
- Info: view Cluster Information
- Fix: fix the Cluster
- Reshard: online migration slot
- Rebalance: balance the number of cluster node Slots
- Add-node: add a new node to the Cluster
- Del-node: delete a node from a cluster
- Set-timeout: set the timeout time for heartbeat connections between cluster nodes.
- Call: execute commands on all nodes in the cluster.
- Import: import external redis data to the Cluster
Redis-trib.rb mainly has two types:ClusterNode
AndRedisTrib
.ClusterNode
The information of each node is saved,RedisTrib
It is the realization of each function of redis-trib.rb
Note: The system prompts at least three master cluster nodes. In the previous example, we created six, but I only created three nodes. Therefore, we can see that we have created at least three master nodes for a redis cluster, it should be an odd number, so. Do not be lazy. Create three more.
Note: The key here is the optional replicas parameter. -- replicas 2 indicates that two slave instances are allocated for each master, and replicas indicates that several slave instances are required. If you do not enter this parameter, you can create three masters. Let's talk about the replicas parameter later.
First, -- replicas 1 actually represents a ratio, that is, the ratio of the number of master nodes/The number of slave nodes. Think about which nodes are the Master nodes when creating a cluster? Which nodes are slave nodes? The answer is to follow the order of IP: PORT in the command, first three master nodes and then three slave nodes.
Note the concept of slot in the figure. For Redis clusters, slot is a place where data is stored and a slot. For each Master, a slot range exists, while the Slave does not. In Redis clusters, the Master can still read and write data, while the Slave is read-only. Data Writing is actually distributed in the slot, which is different from the previous one. the Master/Slave Mode of X is different (Master/Slave Data Storage is completely consistent in Master/Slave Mode), because the data storage of the three Master nodes in the Redis cluster is not the same. This will be verified later.