This article introduces the Redis Cluster, briefly introduces the implementation of the Cluster, mainly concrete practice part: Cluster start, failover, add nodes, remove nodes

Basic concepts in Redis clustering

Redis cluster mode provides sharding of data and ensures availability on each shard. Each node in the Redis cluster needs to open two TCP connections, one for the client to provide services, such as 6379, and the other port on the first port +10000, which is 16379. For the cluster bus, nodes use the cluster bus for fault detection, configuration updates, failover authorization, etc. Clients should not attempt to connect to the cluster bus interface. These two ports must be enabled for the cluster to work properly. (The offset of both ports is always 10000)

Data sharding in a cluster

Instead of using consistent Hash, the Redis Cluster uses a different approach: Hash slot.

The Redis cluster has 16,384 Hash slots. CRC 16 and CRC 16384 are used to calculate Hash slots. Each node in the Redis cluster is responsible for part of the hash slots, for example, three nodes, including node A (0-5500), node B (5501-11000), and node C (11001-16383).

To add or delete A node, you only need to move some slots on the node to the new node. For example, to add A new node D, you only need to move some slots on A,B, and C to D. For example, if you want to delete node A, you only need to move slot A to slot B and slot C.

A master-slave model in a cluster

To ensure the availability of each node in the cluster, a cluster uses a master-slave model. For example, in the three-master, three-slave architecture,A,B,C,A1,B1,C1,A and A1 are replicated in active/standby mode. If NODE A fails,A1 becomes the active node in the cluster to ensure the availability of the cluster. If A1 also fails, the cluster becomes unavailable.

Cluster Consistency Assurance

Redis Cluster cannot guarantee strong data consistency. The asynchronous replication of Redis is the first reason that data consistency cannot be guaranteed: when the client writes data to A,A replies to the client, and A diffuses data to secondary nodes A1,A2, and A3, faults may occur during the diffusion process, resulting in data loss. You can set replication diffusion to synchronous and write all the data back to the client, but this results in poor performance.

When network partitioning occurs, clients are isolated from A few instances, and data loss may also occur. A,A1,B,B1,C,C1,Client, As a result, client and C are isolated. C1 is elected as the new primary node in the cluster. After the network is restored, data in C will be lost. After the node times out, the primary node is considered to have failed and can be replaced by one of the replicas. Similarly, after a node timeout has elapsed and the master node is unable to sense most other masters, it goes into an error state and stops accepting writes.

Redis cluster consistency is basically a trade-off between performance and Y consistency.


Redis cluster configuration

cluster-enabled <yes/no>
cluster-config-file <filename>
cluster-node-timeout <milliseconds>
cluster-slave-validity-factor <factor>
cluster-migration-barrier <count>
cluster-require-full-coverage <yes/no>
Copy the code
  • cluster-enabled <yes/no>By default, the cluster mode is disabled
  • cluster-config-file <filename>After the cluster is started, the Redis cluster will write the configuration of each change to the file, which will be used during the restart. This file is generally not considered modified.
  • cluster-node-timeout <milliseconds>, the timeout period of the node. If the node cannot be accessed after this time, the slave node will fail over.
  • cluster-slave-validity-factor <factor>, if set to zero, the slave will always try to failover the master regardless of whether the link between the master and slave remains disconnected for any length of time. If the value is positive, the maximum disconnection time is calculated as the node timeout value multiplied by the factor provided by this option. If the node is a slave node, no failover attempt is made if the primary link is disconnected for longer than the specified time. For example, if the node timeout is set to 5 seconds and the validity factor is set to 10, a slave device disconnected from the master device for more than 50 seconds will not attempt to failover its master device. Note that any value other than zero may make the Redis cluster unavailable after the primary site fails if no slave site is able to failover it. In this case, the cluster will only return when the original primary server rejoins the cluster.
  • cluster-migration-barrier <count>, the master will keep the minimum number of slave servers connected so that another slave can migrate to a master server that is no longer overridden by any slave servers.
  • cluster-require-full-coverage <yes/no>, : If set to yes, the cluster will stop accepting writes by default if any node does not cover a certain percentage of key space. If this option is set to no, the cluster will serve queries even if only requests with a critical subset can be processed.

Redis Cluster using

The Redis version I’m using here is 5.0.3, and the commands may vary from version to version.

First, prepare 6 Redis nodes on one server. Copy 6 copies of Redis. Conf.

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
Copy the code

6 nodes port number is: 7000700 1700 2700 3700 4700

Start 6 nodes respectively:

redis-server redis-1.conf
redis-server redis-2.conf
redis-server redis-3.conf
redis-server redis-4.conf
redis-server redis-5.conf
redis-server redis-6.conf
Copy the code

Six nodes are now enabled

[root@iZnom30el3gvhxZ redis]# ps -ef | grep redis
root     16270     1  0 15:15 ?        00:00:09 redis-server *:7000 [cluster]
root     16277     1  0 15:15 ?        00:00:09 redis-server *:7001 [cluster]
root     16534     1  0 15:20 ?        00:00:08 redis-server *:7003 [cluster]
root     16541     1  0 15:20 ?        00:00:08 redis-server *:7004 [cluster]
root     16548     1  0 15:20 ?        00:00:08 redis-server *:7005 [cluster]
root     17158     1  0 15:33 ?        00:00:08 redis-server *:7002 [cluster]
root     22290 13798  0 17:17 pts/0    00:00:00 grep --color=auto redis
Copy the code

Enabling the Cluster Mode

Use the following command to create a Redis Cluster:

Redis -cli --cluster create 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:7005 --cluster-replicas 1 # The node has a password redis-cli --cluster create 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:7005 --cluster-replicas 1 -a <requirepass>Copy the code

–cluster-replicas 1 indicates that each node has a slave node

Note: If you want to set a password for the instance, it is better to set the same password for all nodes and configure masterauth. Otherwise, an error may occur during failover. You can also run the config set masterauth ABC config set requirepass ABC config rewrite command to set the password.

[root@iZnom30el3gvhxZ cluster]# redis-cli --cluster create 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:7005 --cluster-replicas 1 -a ABC Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 127.0.0.1:7003 to 127.0.0.1:7000 Adding Replica 127.0.0.1:7004 to 127.0.0.1:7001 Adding Replica 127.0.0.1:7005 to 127.0.0.1:7000 127.0.0.1:7002 >>> Trying to optimize Slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as Their master M: 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 slots: [0-5460] (5461 slots) master M: 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1:7001 slots: [5461-10922] (5462 slots) master M: 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 slots: [10923-16383] (5461 slots) master S: 3 a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 replicates 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd S: 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 3 replicates dc5d7b70542e56f85b0ce74190b4d37e540cdc0 S: 08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea replicates Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join ... >>> Performing Cluster Check (using node 127.0.0.1:7000) M: 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 slots: [0-5460] (5461 slots) master 1 additional up (s) M: 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 slots: [10923-16383] (5461 slots) master 1 additional up (s) S: 08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 slots: (0 slots) slave replicates 4c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea S: 3 a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 slots: (0 slots) slave replicates 07bc00f5b816a612e96ad2fcdc25b4decdc498bd S: 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 slots: (0 slots) slave replicates 3dc5d7b70542e56f85b0ce74190b4d37e540cdc0 M: 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1:7001 slots: [5461-10922] (5462 slots) master 1 additional up (s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.Copy the code

After entering the command, it will confirm the information to you, enter yes, and finally print [OK] All 16384 slots covered. The cluster is successfully created.

Create-cluster is also available in Redis 5.0 and can be used for startup, shutdown, etc.

Enter a node using redis-cli:

[root@iZnom30el3gvhxZ cluster]# redis-cli -c -p 7000 -a abc
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7000>
Copy the code

-c indicates that the cluster mode is entered. If you add this parameter to redis, you will need to save a key in redis.

127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
127.0.0.1:7002>
Copy the code

As you can see, Foo calculates hash Slot 12182, which in the third node is stored to 7002

Let’s see what happens if we don’t add -c:

[root@iZnom30el3gvhxZ cluster]# redis-cli -p 7000 -a abc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:7000> get foo (error) Version 12182 127.0.0.1:7002, 7000:127.0.0.1 >Copy the code

MOVED 12182 127.0.0.1:7002: > 12182 127.0.0.1:7002: > redis > cli: > redis > CLI: > redis > CLI: > 7002

On the Redis-CLI, you can view the cluster status using cluster info:

127.0.0.1:7000> CLUSTER INFO Cluster_state: OK Cluster_SLOts_assigned :16384 Cluster_SLOts_OK :16384 Cluster_SLOts_pfail :0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:1001 cluster_stats_messages_pong_sent:987 cluster_stats_messages_sent:1988 cluster_stats_messages_ping_received:982 cluster_stats_messages_pong_received:1001 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:1988Copy the code

Cluster_state :ok indicates that the cluster is normal

Test failover

Cluster Nodes View nodes in a cluster:

127.0.0.1:7000 > CLUSTER NODES 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 @ 17000 myself, master - 0 1557916585000 1 connected 0-5460-3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1: master 7002 @ 17002-0. 1557916585000 3 connected 10923-16383-08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 @ 17005 slave 4c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 0 1557916584562 6 connected 3a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 @ 17003 slave 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 0 1557916585565 4 connected 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 @ 17004 slave 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 0 The 1557916585966 5 connected 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1:7001 @ 17001 master - 0 1557916585000 2 connected 5461-10922Copy the code

Shut down one of the primary nodes to simulate a failure scenario. Shut down 7000 here:

[root@iZnom30el3gvhxZ ~]# redis-cli -a abc -p 7000 shutdown Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. [root@iZnom30el3gvhxZ ~]# redis-cli -c -p 7001 -a abc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:7001> CLUSTER NODES 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 @ 17002 master - 0 1557972888691 3 connected. 10923-16383 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 @ 17004 slave 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 0 The 1557972889594 5 connected 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1:7001 @ 17001 myself, master 1557972888000-0 2 connected to 5461-10922-4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 @ 17000 master, fail - 1557972867020 1557972866000 1 disconnected 08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 @ 17005 master - 0, 1557972888188 Connected 0-5460-3 a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 @ 17003 slave 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 0 1557972889191 4 connected 127.0.0.1:7001 >Copy the code

As you can see, the current status of port 7000 fails, and the original 7005 node becomes the primary node.

Restart node 7000 again:

[root@iZnom30el3gvhxZ ~]# cd /usr/local/redis/cluster/ [root@iZnom30el3gvhxZ cluster]# redis-server cluster1/redis.conf [root@iZnom30el3gvhxZ cluster]# redis-cli -c -p 7000 -a abc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:7000> CLUSTER NODES 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 @ 17004 slave 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 0 1557973018558 5 connected 3 a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 @ 17003 slave 07bc00f5b816a612e96ad2fcdc25b4decdc498bd 0 1557973017957 4 connected 07bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1:7001 @ 17001 master - 0 1557973018000 2 connected four c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea. 5461-10922 127.0.0.1:7000 @ 17000 myself, slave 08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 0 1557973018000 1 connected 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 @ 17002 master - 0 1557973017556 3 connected. 10923-16383 08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 @ 17005 master - 0 1557973016553 7 connected 0-5460 127.0.0.1:7000 >Copy the code

Node 7000 becomes a secondary node after being restarted.

Manual failover

Redis Cluster provides the function of manually executing FAILOVER. Sometimes, we need to maintain or upgrade a certain point in the Cluster. The specific command is Cluster FAILOVER, which needs to be executed on the slave node that wants to be promoted as the master node

For example, if node 7000 is the secondary node of node 7005, and you want to perform offline maintenance on node 7005, you need to promote node 7000 to the primary node and maintain the availability of external servers. Run the following command on the client of node 7000:

[root@iZnom30el3gvhxZ cluster]# redis-cli -c -p 7000 -a abc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:7000> CLUSTER FAILOVER OK 127.0.0.1:7000 > CLUSTER NODES 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 @ 17004 slave 3dc5d7b70542e56f85b0ce74190b4d37e540cdc0 0 1557973168583 5 connected 3a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 @ 17003 slave 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 0 1557973167380 4 connected 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1:7001 @ 17001 master - 0 1557973168884 2 connected. 5461-10922 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 @ 17000 myself, master - 0 8 connected a scale of 0-5460 to 1557973168000 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 @ 17002 master - 0 1557973168583 3 connected. 10923-16383 08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 @ 17005 slave 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 0 1557973169385 8 connected 127.0.0.1:7000 >Copy the code

Node 7005 has become a secondary node and can be taken offline.

Manual failover client switching occurs by ensuring that the new master replicates exactly the data of the failed old master, avoiding data loss.

Add a node

First, a new redis instance needs to be prepared as the node to be added, as shown below,7006 is the newly started node

[root@iZnom30el3gvhxZ cluster]# ps -ef | grep redis
root      8522     1  0 10:16 ?        00:00:25 redis-server *:7000 [cluster]
root     18060     1  0 13:28 ?        00:00:00 redis-server *:7006 [cluster]
root     18066  7831  0 13:28 pts/0    00:00:00 grep --color=auto redis
root     26183     1  0 May15 ?        00:01:34 redis-server *:7001 [cluster]
root     26190     1  0 May15 ?        00:01:35 redis-server *:7002 [cluster]
root     26202     1  0 May15 ?        00:01:26 redis-server *:7003 [cluster]
root     26208     1  0 May15 ?        00:01:26 redis-server *:7004 [cluster]
root     26215     1  0 May15 ?        00:01:27 redis-server *:7005 [cluster]
Copy the code

Run the redis-cli –cluster add-node 127.0.0.1:7006 127.0.0.1:7000 command to add a node. The first parameter of the add-node is the IP address of the newly added node, and the second parameter is the IP address of any node in the cluster

[root@iZnom30el3gvhxZ cluster]# redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000 -a wk123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Adding node 127.0.0.1:7006 To cluster 127.0.0.1:7000 >>> Performing Cluster Check (using node 127.0.0.1:7000) M: 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 slots: [8461-16383] (7923 slots) master 1 additional up (s) S: 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 slots: (0 slots) slave replicates 3dc5d7b70542e56f85b0ce74190b4d37e540cdc0 S: 3 a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 slots: (0 slots) slave replicates 4c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea M: 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1:7001 slots: [0-5460] (5461 slots) master 1 additional up (s) M: 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 slots: [5461-8460] (3000 slots) master 1 additional up (s) s: 08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 slots: (0 slots) slave replicates 07bc00f5b816a612e96ad2fcdc25b4decdc498bd [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... >>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the CLUSTER. [OK] New node Added correctly. 127.0.0.1:7006 > CLUSTER NODES 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 @ 17004 slave 3dc5d7b70542e56f85b0ce74190b4d37e540cdc0 0 1557985157416 12 connected 08aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 @ 17005 slave 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 0 1557985159418 11 connected 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 @ 17000 master - 0 1557985158417 10 connected. 8461-16383 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1: master 7001 @ 17001-0. 1557985158000 11 connected 0-5460 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 @ 17002 master - 0 1557985158517 12 connected. 5461-8460 3 a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 @ 17003 slave 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 0 1557985158000 10 connected 3425 f765692f0b8b9c4931c038fae8d86fe6e383 127.0.0.1:7006 @ 17006 myself, master 1557985158000-0  0 connectedCopy the code

After the redis-cli –cluster add-node 127.0.0.1:7006 127.0.0.1:7000 -a ABC command is executed, the command output is [OK] New node added correctly Redis-cli, check node, already added, only 7006 has no Hash slot assigned, now we need to assign some hash slot on other nodes to 7006 (using reshard) Command), which shows how to divide 4000 nodes from 7000 nodes to 7006 nodes:

[root@iZnom30el3gvhxZ cluster]# redis-cli --cluster reshard 127.0.0.1:7000 -a wk123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using the node 127.0.0.1:7000) M: 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 slots: [8461-16383] (7923 slots) master 1 additional up (s) S: 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 slots: (0 slots) slave replicates 3dc5d7b70542e56f85b0ce74190b4d37e540cdc0 S: 3 a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 slots: (0 slots) slave replicates 4c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea M: 3425 f765692f0b8b9c4931c038fae8d86fe6e383 127.0.0.1:7006 slots: (0 slots) master M: 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1:7001 slots: [0-5460] (5461 slots) master 1 additional up (s) M: 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 slots: [5461-8460] (3000 slots) master 1 additional up (s) s: 08 aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 slots: (0 slots) slave replicates 07bc00f5b816a612e96ad2fcdc25b4decdc498bd [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)?Copy the code

So what I want you to do is put in the number of slots that you want to assign, so let’s say 4000,

How many slots do you want to move (from 1 to 16384)? 4000
What is the receiving node ID?
Copy the code

There needs to be input to receive these slot node, input the destination node ID of typing, 3425 f765692f0b8b9c4931c038fae8d86fe6e383 input here,

How many slots do you want to move (from 1 to 16384)? 4000 What is the receiving node ID? 3425f765692f0b8b9c4931c038fae8d86fe6e383 Please enter all the source node IDs. Type 'all' to use all the nodes as source  nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:Copy the code

Enter the id of the source node. If it is all nodes, enter all. If it is one or more nodes, enter done one by one.

How many slots do you want to move (from 1 to 16384)? 4000 What is the receiving node ID? 3425f765692f0b8b9c4931c038fae8d86fe6e383 Please enter all the source node IDs. Type 'all' to use all the nodes as source  nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1: 4c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea Source node #2: doneCopy the code

I will confirm it to you again and enter yes to wait for the assignment to be completed.

Enter redis-CLI to view the node:

127.0.0.1:7006 > CLUSTER NODES 1793 ee70d256fc3ab7ab582b20a9da0c8884e683 127.0.0.1:7004 @ 17004 slave 3dc5d7b70542e56f85b0ce74190b4d37e540cdc0 0 1557985824187 12 connected 08aa7f83a0e69d441f523a5b2758cf42d7b1fe6d 127.0.0.1:7005 @ 17005 slave 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 0 1557985823000 11 connected 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 127.0.0.1:7000 @ 17000 master - 0 1557985823085 10 connected. 12461-16383 07 bc00f5b816a612e96ad2fcdc25b4decdc498bd 127.0.0.1: master 7001 @ 17001-0. 1557985823185 11 connected 0-5460 3 dc5d7b70542e56f85b0ce74190b4d37e540cdc0 127.0.0.1:7002 @ 17002 master - 0 1557985824586 12 connected. 5461-8460 3 a5b137a1997d72cdd61fb4cf58de6530a78fbca 127.0.0.1:7003 @ 17003 slave 4 c0b7a5b52bf3c4cb61c88f3af7fc73b8db00dea 0 1557985823000 10 connected 3425 f765692f0b8b9c4931c038fae8d86fe6e383 127.0.0.1:7006 @ 17006 myself, master 1557985824000-0  13 connected 8461-12460Copy the code

7006 already has slot allocations

Add a new node as a slave node

Adding a new node is relatively simple and will not be demonstrated here, but the main commands are posted below:

Redis -cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7000 --cluster-slave Add the -node 127.0.0.1:127.0.0.1 7007:7000 - cluster - slave - cluster - master - 3425 f765692f0b8b9c4931c038fae8d86fe6e383 idCopy the code

or

3425 f765692f0b8b9c4931c038fae8d86fe6e383 redis 127.0.0.1:7007 > cluster replicateCopy the code

Remove node

Run the following command to delete a node: redis-cli –cluster del-node 127.0.0.1:7000

del-node The first parameter is the IP address of any node in the cluster and the second parameter is the ID of the node to be removed

Note that before deleting a master node, you must allocate slots in the master node. Otherwise, the master node cannot be deleted

[root @ iZnom30el3gvhxZ cluster] # redis - cli - cluster del -node 127.0.0.1:3425 f765692f0b8b9c4931c038fae8d86fe6e383-7000 a abc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node 3425 f765692f0b8b9c4931c038fae8d86fe6e383 from cluster 127.0.0.1:7000 (ERR) Node 127.0.0.1:7006 is not empty! Reshard data away and try again.Copy the code

Select * from slot 7006; select * from slot 7006;

[root @ iZnom30el3gvhxZ cluster] # redis - cli - cluster del -node 127.0.0.1:3425 f765692f0b8b9c4931c038fae8d86fe6e383-7000 a wk123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node 3425 f765692f0b8b9c4931c038fae8d86fe6e383 from cluster 127.0.0.1:7000 > > > Sending cluster FORGET messages to the cluster... >>> SHUTDOWN the node. [root@iZnom30el3gvhxZ cluster]# ps -ef | grep redis root 8522 1 0 10:16 ? 00:00:32 redis-server *:7000 [cluster] root 19854 7831 0 14:04 pts/0 00:00:00 grep --color=auto redis root 26183 1 0 May15 ? 00:01:40 redis-server *:7001 [cluster] root 26190 1 0 May15 ? 00:01:40 redis-server *:7002 [cluster] root 26202 1 0 May15 ? 00:01:29 redis-server *:7003 [cluster] root 26208 1 0 May15 ? 00:01:28 redis-server *:7004 [cluster] root 26215 1 0 May15 ? 00:01:29 redis-server *:7005 [cluster]Copy the code

Node 7006 has been removed and taken offline.


For more details please refer to:

Redis Cluster official document

Redis Cluster advanced official documentation