Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.


In this article, we will introduce Redis Cluster deployment solution, and its dynamic capacity expansion and reduction process.

Redis Cluster is a distributed server Cluster composed of multiple master and slave node groups. It has replication, high availability, and sharding features. It does not require Sentinel Sentinel to perform node removal and failover, has better performance and high availability than Sentinel mode, and has very simple cluster configuration.

During the configuration process, you need to set each node to cluster mode. This cluster mode has no central node and can be horizontally scaled. According to official documents, it can be linearly scaled up to 1000 nodes.

Native structures,

First of all, we set up 3 master and 3 slave Redis clusters in a native way, and configure one slave node for each of the 3 master nodes, totaling 6 Redis nodes.

1, Modify the configuration file, first modify the general configuration:

Bind 0.0.0.0 port 7000 pidfile /var/run/redis_7000.pid logfile "redis7000.log" dbfilename dump7000. RDB dir /home/hydra/files/redis/cluster/redis7000 masterauth 123456 requirepass 123456 appendonly yes appendfilename "appendonly7000.aof"Copy the code

Then modify the core configuration:

Cluster-enabled yes cluster-config-file nodes-7000.conf # This config will save the cluster configuration cluster-node-timeout 15000 # Cluster-replica-validity-factor 10 cluster-require-full-coverage No # Important configurationCopy the code

Log in to the redis cli and run the cluster Nodes command to check the cluster status. Now only one node exists in the cluster:

The other five Redis instances perform the same operation and remain independent after completion.

2. Cluster node association

Use meet instruction to associate nodes under the cluster and execute it on node 7000:

Cluster meet 127.0.0.1 7001Copy the code

Then nodes 7000 and 7001 can communicate with each other. Run cluster Nodes to see that the nodes are associated. If one more node is associated, all three nodes are associated with each other. To continue:

Cluster meet 127.0.0.1 7002 cluster meet 127.0.0.1 7003 cluster meet 127.0.0.1 7004 cluster meet 127.0.0.1 7005Copy the code

So the six Redis instances are all related:

However, as you can see, the current default is all master nodes, no slave nodes, still cannot write.

3, Assign slot (s)

Write a value to a node. Write a value to a node. Write a value to a node.

There are 16384 built-in hash slots in the Redis cluster. When a piece of data needs to be placed in the Redis cluster, Redis first uses crC16 algorithm to calculate a result for key, and then mod the result to 16384. In this way, each key will correspond to a hash slot numbered between 0 and 16383, and Redis will map hash slots to different nodes roughly equally based on the number of nodes. The mapping process is as follows:

The current configuration architecture consists of three active and three slave slots, and an average of 16,384 slots are allocated:

  • Node 7000:0-5461
  • Node 7001 assigned: 5462-10922
  • Assigned to node 7002:10923-16383

Slot allocation instructions:

Cluster addslots slot #slot indicates the slot subscriptCopy the code

Then on the redis7000 instance you need to execute:

cluster addslots 0
...
cluster addslots 5461
Copy the code

If you want all key values to be executed properly, you need to manually execute 16384 times. Therefore, create a script for batch execution:

start=$1 end=$2 port=$3 for slot in `seq ${start} ${end}` do echo "slot:${slot}" / home/hydra/files/redis/redis - 5.0.4 127.0.0.1 / SRC/redis - cli - h - p ${port} - a 123456 cluster addslots ${slot} the doneCopy the code

Execute shell script:

ssh addslots.sh 0 5461 7000
sh addslots.sh 5462 10922 7001
sh addslots.sh 10923 16383 7002
Copy the code

View cluster status:

Note that in the Redis Cluster, only master hosts have slots. Slave hosts do not need to assign slots. Currently, we use the equal allocation method. In the actual environment, if there is a difference in server performance, more slots can be allocated to servers with good performance.

4. Assign master and slave

Redis cluster primary/secondary assignment command:

cluster replicate node-id
Copy the code

To use 7003 as the slave machine of 7000, log in to 7003 first and run:

cluster replicate  9d73de74af827cd5025dcd3910d8c2919d9aa24b
Copy the code

The next 40 digits are the node-IDS of redis7000. Continue to assign the reDIS7004 host to ReDIS7001 and the reIDS7005 host to ReDIS7002. View cluster status:

The cluster information is saved to the redis700x/ Node-700x. conf configuration file.

Run the client in cluster mode, add the startup parameter -c in redis-cli:

. /redis-5.0.4/ SRC /redis-cli -h 172.16.67.134 -p 7000 -a 123456-cCopy the code

Data will be written to different hosts in different slots:

At this point, the Redis Cluster of three master and three slave architectures is set up.

Quickly build

In addition to manually building a Redis Cluster using commands, you can also use the built-in instructions provided by Redis for quick one-click building. First of all, the same as manual construction, first modify the configuration file, the configuration is exactly the same as above, after the modification is complete, start.

After startup, use the cluster create command to build:

. /redis-5.0.4/ SRC /redis-cli --cluster create 127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8006 --cluster-replicas 1 -a 123456Copy the code

–cluster-replicas 1 represents 1 master and 1 slave architecture. If the assignment is 1, 2 and slave, it should be written as:

--cluster-replicas 2
Copy the code

Type yes to confirm:

After building the cluster, check the cluster status:

This includes meet, slot assignment, and master/slave assignment.

The cluster expansion

1. Prepare a new node

Create two new reDIS7006, redis7007, copy the previous configuration file, and start the instance. In this case, these two nodes are isolated:

2. Add the new primary node to the cluster using redis cli. The syntax is as follows:

--cluster add-node <newNode ip:port> <oldNode ip:port>
Copy the code

Perform:

. /redis-5.0.4/ SRC /redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000 -a 123456Copy the code

After the command is executed, the new node is added to the cluster:

By default, master nodes are added and no slots are allocated:

Add a secondary node and specify its primary node.

--cluster add-node <newIp:port> <oldIp:port> --cluster-slave --cluster-master-id masterID
Copy the code

Perform:

. /redis-5.0.4/ SRC /redis-cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7000 --cluster-slave --cluster-master-id edc8ff41aef320beb5081c5b50bf32485a7ffb9e -a 123456Copy the code

Redis7007 is specified as the secondary node of ReDIS7006:

View cluster status:

4. Migrate slots and data.

/redis-cli --cluster reshard <ip:port>
Copy the code

Perform:

. /redis-5.0.4/ SRC /redis-cli --cluster reshard 172.16.67.134:7000-a 123456Copy the code

During migration, the following questions are asked:

  • How many slots do you want to move (from 1 to 16384)? Given a hint for how many slots to allocate, we’re evenly allocated 4 instances, so type 4096
  • What is the receiving node ID? Receiver node ID: enter 7006node-id
  • "All/done"From what, if you chooseall, all nodes are evenly distributed, or manually enternode-idDistribution,doneThe end of the

View cluster status:

After the allocation is complete, you can look at the slot 7006 and see that each of the three primary nodes has allocated some slots to it:

  • 0-1365.
  • 5462-6826.
  • 10923-12287.

Cluster shrinkage capacity

In cluster scaling, you need to delete slots first and then nodes. If you delete a master node directly, its slave nodes become the master node.

1, offline migration slot, syntax is as follows:

Redis-cli --cluster reshard --cluster-from NODE ID to be migrated --cluster-to receiver slot node ID --cluster-slots Number of migrated slots. IP addresses of existing nodes existCopy the code

Migrate 7006 0-1365 back to 7000

. / redis - 5.0.4 / SRC/redis - cli - cluster reshard - cluster - from edc8ff41aef320beb5081c5b50bf32485a7ffb9e - cluster - the to 9 d73de74af827cd5025dcd3910d8c2919d9aa24b - cluster - slots 127.0.0.1 1366 7000-12345 aCopy the code

Then migrate the other slots to reDIS7001 and ReDIS7002 nodes in the same way. After the command is executed, all slots on Redis7006 are migrated back to the three hosts:

Delete a node.

Redis -cli --cluster del-node Existing node ID: indicates the ID of a node to be deleted from a portCopy the code

Delete 7006 first:

. / redis - 5.0.4 / SRC/redis - cli - cluster del -node 127.0.0.1:7000 edc8ff41aef320beb5081c5b50bf32485a7ffb9e - 123456 aCopy the code

After the command is executed, the node is shut down and the process of Redis7006 is killed. Take a look at the cluster status:

In this case, ReDIS7007 will be assigned to another primary node as the secondary node. This is because ReDIS7006 has no slots and data, so there is no failover. Redis7007 will be upgraded to the primary node. So let’s look at the failover process again, after killing the redis7000 process with the kill command:

Redis7000 becomes a fail state, and its slave redis7003 inherits its slots and data.

The last

If you think it is helpful, you can like it and forward it. Thank you very much

Public number agriculture ginseng, add a friend, do a thumbs-up friend ah