This article is mainly to tell you how to build a Redis cluster and the process needs to pay attention to the problem, very good, recommend to everyone, there is a need for small partners to refer to the next
Here, a 6-node Redis pseudo-cluster is built on a Linux virtual machine, and the idea is simple to open 6 Redis instances on a single virtual machine and each Redis instance has its own port. That would be equivalent to simulating 6 machines, and then building a Redis cluster with these 6 instances.
Prerequisite: Redis has been installed, directory for/usr/local/redis-4.0.1 if not, you can refer to the article Windows under the installation of Redis Linux under the installation of Redis
The Redis cluster is a ruby script, so the ruby environment is required to execute the script. The REDIS-TRIB.RB,REDIS-TRIB.RB, which corresponds to Redis's source src directory, is the official Redis-managed Redis cluster tool, which is packaged as a simple, convenient and practical tool based on the cluster commands provided by Redis. So
To install the Ruby environment:
1.yum install ruby
2.yum install rubygems
3.gem install redis
The CentOS default support for Ruby to 2.0.0,redis requires a minimum of 2.2.2. The workaround is to first install RVM and then upgrade the Ruby version to 2.3.3
1.sudo yum install curl
2. Installing RVM
curl -L get.rvm.io | bash -s stable
3.
source /usr/local/rvm/scripts/rvm
4. View a known version of Ruby 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. Uninstalling a known version
rvm remove 2.0.0
8. View version
ruby --version
9. Re-install Redis
gem install redis
Redis Cluster Setup
Create the Redis-cluster directory, create the redis-8001,redis-8002,redis-8003 node directory, and then copy the redis-conf to the node directory
Modify the node redis-conf file separately, because on a machine (192.16819.129), so each instance should have a different port, at the same time, each instance will obviously have its own place to store data, turn on the AOF mode, turn on the cluster configuration, turn on the background mode;
Turn on the Redis service to see if it can be started. OK, no problem.
Create startall.sh script (prompt permission denied description insufficient permissions, execute command chmod 777 startall.sh Modify permissions)
Start the startall.sh script
Create a stopall.sh script
Create a cluster
Next, we'll create the cluster through the Ruby script.
You can see that REDIS-TRIB.RB has the following features:
Create: Creating a Cluster
Check: Checking the cluster
Info: View cluster information
FIX: Fix cluster
Reshard: Online Migration Slots
Rebalance: Balancing the number of cluster node slots
Add-node: Adding a new node to the cluster
Del-node: Removing nodes from the cluster
Set-timeout: Setting the time-out for heartbeat connections between cluster nodes
Call: Execute commands on all nodes of the cluster
Import: Importing external Redis data into a cluster
There are two main classes of REDIS-TRIB.RB: ClusterNode
and RedisTrib
. The ClusterNode
information of each node is saved, RedisTrib
it is the implementation of each REDIS-TRIB.RB function
Note: Tips for a minimum of 3 master cluster nodes, previously said to be created 6, but the actual operation I only created 3 nodes, so we can conclude that we create a Redis cluster is a minimum of three master nodes, and should be an odd number, so, do not lazy, and then create three.
Special note: The key here is the optional replicas parameter,--replicas 2 meaning to allocate 2 each Slave,replicas for each master represents the need for several slave. Do not fill in this parameter can be created successfully, this is three master. About the replicas parameter, let me introduce you later.
First,--replicas 1 1 actually represents a ratio, which is the ratio of the number of primary nodes/nodes. So think about what nodes are the primary nodes when creating a cluster? Which nodes are from the node? The answer is to follow the order of Ip:port in the command, first 3 primary nodes, then 3 slave nodes.
Second, notice the concept of the slot in the diagram. Slot for a redis cluster, it is a place where data is stored, a slot. For each master, there is a range of slots, and slave does not. In a Redis cluster, master can read, write, and slave read-only. The writing of the data is actually distributed in the slot, which is the previous 1. The master-slave mode of x is different (Master/slave data storage is identical in master-slave mode) because the data storage of 3 master in a Redis cluster is not the same. This will be verified in the following essay.