Principle:
-
Redis cluster uses a consistent hash slot to allocate a certain hash slot to each primary node in the cluster, and hashes the written data to a primary node for storage.
-
The cluster uses formula (CRC16 key) & 16384 to calculate the key key data slot.
-
16,384 slots are evenly distributed on each node.
-
Each primary node in a cluster maintains a portion of slots where data is stored. Each primary node has at least one secondary node for high availability.
Node communication mode:
-
Enable a port The port number +10000 is used for exchanging information between nodes in clusters.
-
By default, each node selects 5 random nodes to send ping messages 10 times per second to transmit its own information and known cluster information. After receiving the ping message, pong message is returned to reply. Finally, through this random message exchange, each node will finally obtain all information.
-
When a master node fails, all nodes will notice that the master node has failed, and the slave node, acting as the master node, will take over the work of the master node and tell all other nodes that he is the master. The other surviving nodes will then update the information tables they maintain and the slave node will take over. If they all fail, the cluster will report an error. When operated from a node, the consistency hash calculation will be stored in one of the master nodes, and the slave node will synchronize the master data.
-
Redis Cluster is decentralized, each node in the cluster is equal, each node stores its own data and the status of the whole cluster. Each node is connected to all other nodes, and these connections remain active.
-
When building a cluster, there will be a master node for each shard, corresponding to a slave node. Slaveof function is implemented. At the same time, when the primary node is down, automatic failover of Sentinel is implemented
Redis Distributed Cluster (Deployment) :
Port number: 7000-7005
This distributed shard cluster can be installed on a LInux system. You only need to install multiple instances as the cluster configuration.
Install Ruby environment support:
yum -y install ruby rubygemsCopy the code
Yum installs version 2.0.0, but gem requires version 2.2.2 or higher, so compile
Download and install the Ruby environment:
Wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.1.tar.gztar xf ruby - 2.6.1. Tar. Gz &&cdRuby - 2.6.1 /. / configure -- prefix = / usr /local/rubymake && make install && echo $?echo 'export PATH=$PATH:/usr/local/ruby/bin' >> /etc/profilesource /etc/profileCopy the code
Modify gem tool
# see gem tool source address gem sources - l # added a ali cloud gem tool source gem sources - a http://mirrors.aliyun.com/rubygems/# delete default foreign source gem gem tools sources --remove https://rubygems.org/# Download the latest cluster plug-in gem install redis -v 4.1.0Copy the code
Preparations for cluster nodes:
mkdir /data/700{0.. 5}Copy the code
Example of configuring port 7000:
vim /data/7000/redis.confport 7000daemonize yespidfile /data/7000/redis.pidloglevel noticelogfile "/data/7000/redis.log"dbfilename dump.rdbdir /data/7000protected-mode nocluster-enabled yescluster-config-file nodes.confcluster-node-timeout 5000appendonly yesCopy the code
Copy other port instances:
Conf /data/7000/redis.conf /data/7001/cp /data/7000/redis.conf /data/7002/cp /data/7000/redis.conf /data/7003/cp /data/7000/redis.conf /data/7003/cp /data/7000/redis. Conf /data/7004/cp /data/7000/redis. /data/7001/redis.confsed -i 's#7000#7002#g' /data/7002/redis.confsed -i 's#7000#7003#g' /data/7003/redis.confsed -i 's#7000#7004#g' /data/7004/redis.confsed -i 's#7000#7005#g' /data/7005/redis.confCopy the code
Start all instances:
redis-server /data/7000/redis.confredis-server /data/7001/redis.confredis-server /data/7002/redis.confredis-server /data/7003/redis.confredis-server /data/7004/redis.confredis-server /data/7005/redis.confCopy the code
Create command soft link:
(This command is out of date, now use redis-cli command)
ln -s /usr/local/ redis - 5.0.2 / SRC/redis - trib. Rb/usr/sbin /Copy the code
View the process:
ps -ef |grep redis-serverCopy the code
Add all instance nodes to cluster management:
Redis -cli --cluster create --cluster-replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005Copy the code
The distributed master-slave rule is that the first three instance nodes are the master, and the next three are the slave nodes. If Replicas is 2, it adds three more instance nodes
View the status of the primary node:
redis-cli -p 7000 cluster nodes|grep masterCopy the code
View the status of the secondary node:
redis-cli -p 7000 cluster nodes|grep slaveCopy the code
Redis- Distributed Clustering (Administration)
Preparations for adding cluster nodes:
mkdir /data/700{6.. 7} Copy other port instances:Cp /data/7000/redis.conf /data/7006/cp /data/7000/redis.conf /data/7007/ /data/7006/redis.confsed -i 's#7000#7007#g' /data/7007/redis.confCopy the code
Start a new node instance:
redis-server /data/7006/redis.confredis-server /data/7007/redis.confCopy the code
View the process:
ps -ef |grep redis-serverCopy the code
Add the primary node :(the 7000 instance is the management node)
Redis -cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000Copy the code
View the status of the primary node:
redis-cli -p 7000 cluster nodes|grep masterCopy the code
Transfer slot (resharding) :
#' Operation cluster management node is reassigned, IDredis-cli --cluster reshard 127.0.0.1:7000How many slots Do you want to move (from 1 to 16384)? Calculate the total size of the shard divided by the master node manually. # 2129 d28f0a86fc89571e49a59a0739812cff7953 choose to receive data fragmentation of node ID, (this is the 7006 instances of the new node ID) Source node # 1: Do you want to proceed with the proposed reshard plan (yes/no)? Yes # Confirm the modification of the above actionCopy the code
Re-view the master node status :(you can see the re-sharding of the cluster data)
redis-cli -p 7000 cluster nodes|grep masterCopy the code
Adding slave nodes:
Add 7007 to primary 7006 Redis-cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7000 --cluster-slave --cluster-master-id specifies the management node where the primary 7006 instance resides 2129d28f0a86fc89571e49a59a0739812cff7953Copy the code
View the status of the secondary node:
redis-cli -p 7000 cluster nodes|grep slaveCopy the code
Preparations for deleting cluster nodes:
Move the shard of the node to be deleted:
IDredis-cli --cluster reshard 127.0.0.1:7000 / cluster reshard 127.0.0.1:7000 / cluster reshard How many slots do you want to move (from 1 to 16384)? What is the receiving node ID? E64f9074a3733fff7baa9a4848190e56831d5447 # choose to receive data fragmentation of node ID, (this is the 7006 instances of the new node ID) Source node # 1: D28f0a86fc89571e49a59a0739812cff7953 # 2129 to choose from which Source the master node to shard to the new master node (this is to delete the master node ID number) of the Source node # 2: Do you want to proceed with the proposed reshard plan (yes/no)? Yes # Confirm the modification of the above actionCopy the code
Re-view the master node status :(you can see the re-sharding of the cluster data)
redis-cli -p 7000 cluster nodes|grep masterCopy the code
Continue moving the data slice:
IDredis-cli --cluster reshard 127.0.0.1:7000 / cluster reshard 127.0.0.1:7000 / cluster reshard How many slots do you want to move (from 1 to 16384)? 1366 # What is the receiving node ID? F6c1aaea3a8c56e0c7dee8ad7ae17e26dd04244c # choose to receive data fragmentation of node ID, (this is the 7006 instances of the new node ID) Source node # 1: D28f0a86fc89571e49a59a0739812cff7953 # 2129 to choose from which Source the master node to shard to the new master node (this is to delete the master node ID number) of the Source node # 2: Do you want to proceed with the proposed reshard plan (yes/no)? Yes # Confirm the modification of the above actionCopy the code
Re-view the master node status :(you can see the re-sharding of the cluster data)
redis-cli -p 7000 cluster nodes|grep masterCopy the code
Last move of data slice:
IDredis-cli --cluster reshard 127.0.0.1:7000 / cluster reshard 127.0.0.1:7000 / cluster reshard How many slots do you want to move (from 1 to 16384)? What is the receiving node ID? 5a0df4ea0af5f35c1248e45e88d44c3f2e10169fSource node #1: 2129d28f0a86fc89571e49a59a0739812cff7953Source node #2: doneCopy the code
Re-view the master node status :(you can see the re-sharding of the cluster data)
redis-cli -p 7000 cluster nodes|grep masterCopy the code
Delete the master node of the data slice:
Redis -cli --cluster del-node 127.0.0.1:7006 2129 d28f0a86fc89571e49a59a0739812cff7953 # delete no main library redis - 7007 instances of cli - cluster del -node 127.0.0.1:7007 821bcf78c5e4c0b08aa7a5d514214b657ebec4abCopy the code
Other configuration management:
Redis -cli -p 6382 -a redhat info memoryset the maximum memory size to 100MB redis-cli -p 6382 -a redhat config set maxmemory 102400000Copy the code
The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.