集群管理工具redis-cli

在介绍完 Redis 集群的搭建方法以及基本功能的使用方法之后,我们接下来要学习的就是如何管理 Redis 集群了。Redis 官方为管理集群提供了两种工具,一种是 redis-cli 客户端附带的集群管理程序,而另一种则是 Redis 内置的集群管理命令。

本节中我们将对 redis-cli 客户端附带的集群管理程序做详细的介绍。这个程序可以通过在执行 redis-cli 的时候给定 cluster 选项来启动,输入 help 子命令可以看到该程序支持的各个子命令及其格式:

$ redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
info host:port
fix host:port
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help
For check, fix, reshard, del-node, settimeout you can specify the host and port of any working node in the cluster.

接下来将对上述列出的各个子命令做详细介绍。

创建集群

cluster 选项的 create 子命令允许用户根据已有的节点创建出一个集群。用户只需要在命令中依次给出各个节点的 IP 地址和端口号,命令就会将它们聚合到同一个集群中,并根据节点的数量将槽平均地指派给它们负责:

create <ip1>:<port1> ... <ipN>:<portN>

举个例子,如果用户想要创建出一个包含 3 个节点的集群,可以执行以下命令: redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003

redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003

通过以下可选项,用户还可以在创建集群的同时,决定要为每个主节点配备多少个从节点:

--cluster-replicas <num>

作为例子,在以下这个 create 命令的运行实例中,我们将向它提供 6 个节点,并将 --cluster-replicas 的值设置为 1,以此来创建一个包含 3 个主节点和 3 个从节点的集群:

$ redis-cli --
cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 1
27.0.0.1:30006 --cluster-replicas 1
>>> 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:30004 to 127.0.0.1:30001
Adding replica 127.0.0.1:30005 to 127.0.0.1:30002
Adding replica 127.0.0.1:30006 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
Can I set the above configuration? (type 'yes' to accept):

create 命令首先会检查给定的所有节点,然后根据节点的数量以及我们指定的从节点数量划归出一个集群配置并将其打印出来。如果我们觉得这个集群配置没问题,就可以在命令行中输入 yes 来接受它,这样命令就会根据该配置构建出集群:

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:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
slots: (0 slots) slave
replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
slots: (0 slots) slave
replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
slots: (0 slots) slave
replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看集群信息

拥有集群之后,用户就可以通过 cluster 选项的 info 子命令查看集群的相关信息。为了找到指定的节点,用户需要向命令提供集群中任意一个节点的地址作为参数:

info <ip>:<port>

命令返回的信息包括:

  • 主节点的地址以及运行 ID,它们存储的键数量以及负责的槽数量,以及它们拥有的从节点数量。

  • 集群包含的数据库键数量以及主节点数量,以及每个槽平均存储的键数量。

以下是一个 info 子命令执行的示例:

$ redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (4979f858...) -> 1 keys | 5461 slots | 1 slaves.
127.0.0.1:30002 (4ff303d9...) -> 4 keys | 5462 slots | 1 slaves.
127.0.0.1:30003 (07e23080...) -> 3 keys | 5461 slots | 1 slaves.
[OK] 8 keys in 3 masters.
0.00 keys per slot on average.

从命令返回的结果可以看到,节点 30001 所在的集群包含了 3 个主节点,以主节点 127.0.0.1:30001 为例:

  • 它的运行 ID 前缀为 4979f858。

  • 它被指派了 5461 个槽,但是目前只存储了 1 个数据库键。

  • 这个节点拥有 1 个从节点。

命令结果的最后两行记录了集群的总体存储情况:整个集群总共拥有 3 个主节点,这些主节点一共存储了 8 个键,平均每个槽只存储了 0 个键。

检查集群

通过 cluster 选项的 check 子命令,用户可以检查集群的配置是否正确,以及全部 16384 个槽是否已经全部指派给了主节点。与 info 子命令一样,check 子命令也接受集群其中一个节点的地址作为参数:

check <ip>:<port>

对于一个正常运行的集群,对其执行 check 子命令将得到一切正常的结果:

$ redis-cli --cluster check 127.0.0.1:30001
127.0.0.1:30001 (4979f858...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30002 (4ff303d9...) -> 0 keys | 5462 slots | 1 slaves.
127.0.0.1:30003 (07e23080...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
slots: (0 slots) slave
replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
slots: (0 slots) slave
replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
slots: (0 slots) slave
replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

现在,如果我们撤销节点 30001 对槽 5460 的指派,然后再次执行检查,那么命令将报告集群中的 16384 个槽并未完全指派:

$ redis-cli --cluster check 127.0.0.1:30001
127.0.0.1:30001 (4979f858...) -> 1 keys | 5460 slots | 1 slaves.
127.0.0.1:30002 (4ff303d9...) -> 4 keys | 5462 slots | 1 slaves.
127.0.0.1:30003 (07e23080...) -> 3 keys | 5461 slots | 1 slaves.
[OK] 8 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5459] (5460 slots) master
1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
slots: (0 slots) slave
replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
slots: (0 slots) slave
replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
slots: (0 slots) slave
replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[ERR] Nodes don't agree about configuration!
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.

修复槽错误

当集群在重分片、负载均衡或者槽迁移的过程中出现错误时,执行 cluster 选项的 fix 子命令,可以让操作涉及的槽重新回到正常状态:

fix <ip>:<port>

fix 命令会检查各个节点中处于 “导入中” 和 “迁移中” 状态的槽,并根据情况,将槽迁移至更合理的一方。

举个例子,假设我们现在正在将节点 30001 的 5460 槽迁移至节点 30002,并且为了模拟迁移中断导致出错的情况,我们在迁移完成之后并没有清理相应节点的 “导入中” 和 “迁移中” 状态。现在,如果我们执行 fix 命令,那么它将发现并修复这一问题:

$ redis-cli --cluster fix 127.0.0.1:30001
127.0.0.1:30001 (4979f858...) -> 1 keys | 5461 slots | 1 slaves.
127.0.0.1:30002 (4ff303d9...) -> 4 keys | 5462 slots | 1 slaves.
127.0.0.1:30003 (07e23080...) -> 3 keys | 5461 slots | 1 slaves.
[OK] 8 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
slots: (0 slots) slave
replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
slots: (0 slots) slave
replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
slots: (0 slots) slave
replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[ERR] Nodes don't agree about configuration!
>>> Check for open slots...
[WARNING] Node 127.0.0.1:30001 has slots in migrating state 5460.
[WARNING] Node 127.0.0.1:30002 has slots in importing state 5460.
[WARNING] The following slots are open: 5460.
>>> Fixing open slot 5460
Set as migrating in: :30001
Set as importing in: 127.0.0.1:30002
Moving slot 5460 from 127.0.0.1:30001 to 127.0.0.1:30002:
>>> Check slots coverage...
[OK] All 16384 slots covered.

如果 fix 命令在检查集群之后没有发现任何异常,那么它将不做其他动作,直接退出。

重分片

通过 cluster 选项的 reshard 子命令,用户可以将指定数量的槽从原节点迁移至目标节点,被迁移的槽将交由后者负责,并且槽中已有的数据也会陆续从原节点转移至目标节点:

reshard <ip>:<port>
    --cluster-from <id> # 源节点的ID
    --cluster-to <id> # 目标节点的ID
    --cluster-slots <num> # 需要迁移的槽数量
    --cluster-yes # 直接确认
    --cluster-timeout <time> # 迁移的最大时限
    --cluster-pipeline <yes/no> # 是否使用流水线

假设现在节点 30001 正在负责从槽 0 到槽 5460 的 5461 个槽,而我们又想将其中的 10 个槽迁移至节点 30007,那么可以执行以下命令:

$ redis-cli --cluster reshard 127.0.0.1:30001 --clusterfrom 4979f8583676c46039672fb7319e917e4b303707 --clusterto 1cd87d132101893b7aa2b81cf333b2f7be9e1b75 --cluster-slots 10
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
slots: (0 slots) slave
replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
slots: (0 slots) slave
replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
M: 1cd87d132101893b7aa2b81cf333b2f7be9e1b75 127.0.0.1:30007
slots: (0 slots) master
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
slots: (0 slots) slave
replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Ready to move 10 slots.
Source nodes:
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
Destination node:
M: 1cd87d132101893b7aa2b81cf333b2f7be9e1b75 127.0.0.1:30007
slots: (0 slots) master
Resharding plan:
Moving slot 0 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 1 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 2 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 3 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 4 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 5 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 6 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 7 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 8 from 4979f8583676c46039672fb7319e917e4b303707
Moving slot 9 from 4979f8583676c46039672fb7319e917e4b303707
Do you want to proceed with the proposed reshard plan (yes/no)?

可以看到,reshard 命令会先用 check 子命令检查一次集群,确保集群和槽都处于正常状态,然后再给出一个重分片计划,并询问我们的意见。在输入 yes 并按下 Enter 键之后,命令就会实施预定好的重分片计划:

Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 1 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 2 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 3 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 4 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 5 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 6 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 7 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 8 from 127.0.0.1:30001 to 127.0.0.1:30007:
Moving slot 9 from 127.0.0.1:30001 to 127.0.0.1:30007:

通过执行 info 子命令,我们可以确认,之前指定的 10 个槽已经被迁移并指派给了节点 30007 负责:

$ redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (4979f858...) -> 1 keys | 5451 slots | 1 slaves.
127.0.0.1:30007 (1cd87d13...) -> 0 keys | 10 slots | 0 slaves.
127.0.0.1:30002 (4ff303d9...) -> 4 keys | 5462 slots | 1 slaves.
127.0.0.1:30003 (07e23080...) -> 3 keys | 5461 slots | 1 slaves.

负载均衡

cluster 选项的 rebalance 子命令允许用户在有需要时重新分配各个节点负责的槽数量,从而使得各个节点的负载压力趋于平衡:

rebalance <ip>:<port>

举个例子,假设我们现在的集群有 30001、30002 和 30003 这 3 个主节点,它们分别被分配了 2000、11384 和 3000 个槽:

$ redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (61d1f17b...) -> 0 keys | 2000 slots | 1 slaves.
127.0.0.1:30002 (101fbae9...) -> 0 keys | 11384 slots | 1 slaves.
127.0.0.1:30003 (31822e0a...) -> 0 keys | 3000 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

因为节点 30002 负责的槽数量明显比其他两个节点要多,所以它的负载将比其他两个节点要重。为了解决这个问题,我们需要执行以下这个命令,对 3 个节点的槽数量进行平衡:

$ redis-cli --cluster rebalance 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 3 nodes. Total weight = 3.00
Moving 3462 slots from 127.0.0.1:30002 to 127.0.0.1:30001
#####...#####
Moving 2461 slots from 127.0.0.1:30002 to 127.0.0.1:30003
#####...#####

在 rebalance 命令执行之后,3 个节点的槽数量将趋于平均:

$ redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (61d1f17b...) -> 0 keys | 5462 slots | 1 slaves.
127.0.0.1:30002 (101fbae9...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30003 (31822e0a...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

rebalance 命令提供了很多可选项,它们可以让用户更精确地控制负载均衡操作的具体行为。比如,通过以下可选项,用户可以为不同的节点设置不同的权重,而权重较大的节点将被指派更多槽。这样一来,用户就可以通过这个选项,让性能更强的节点负担更多负载:

--cluster-weight <node_id1>=<weight1> <node_id2>=<weight2> ...

在没有显式地指定权重的情况下,每个节点的默认权重为 1.0。将一个节点的权重设置为0将导致它被撤销所有槽指派,成为一个空节点。

如果用户在执行负载均衡操作时,想要为尚未被指派槽的空节点也分配相应的槽,那么可以使用以下可选项:

--cluster-use-empty-masters

rebalance 命令在执行时会根据各个节点目前负责的槽数量以及用户给定的权重计算出每个节点应该负责的槽数量(期望槽数量),如果这个槽数量与节点目前负责的槽数量之间的比率超过了指定的阈值,那么就会触发槽的重分配操作。触发重分配操作的阈值默认为 2.0,也就是期望槽数量与实际槽数量之间不能相差超过两倍,用户也可以通过以下可选项来指定自己想要的阈值:

--cluster-threshold <value>

除了上述可选项之外,用户还可以通过以下可选项来设置负载均衡操作是否使用流水线:

--cluster-pipeline <yes/no>

或者通过以下可选项设置负载均衡操作的最大可执行时限:

--cluster-timeout <time>

最后,rebalance 命令在执行负载均衡操作的时候,通常会一个接一个地对节点的槽数量进行调整,但如果用户想要同时对多个节点实施调整,那么只需要给定以下可选项即可:

--cluster-simulate

如果 rebalance 命令在执行时发现集群并没有平衡的必要,那么它将不执行任何其他操作,直接退出:

$ redis-cli --cluster rebalance 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.

添加节点

cluster 选项的 add-node 子命令允许用户将给定的新节点添加到已有的集群当中,用户只需要依次给定新节点的地址以及集群中某个节点的地址即可:

add-node <new_host>:<port> <existing_host>:<port>

在默认情况下,add-node 命令添加的新节点将作为主节点存在。如果用户想要添加的新节点为从节点,那么可以在执行命令的同时,通过给定以下两个可选项来将新节点设置为从节点:

--cluster-slave
--cluster-master-id <id>

其中可选项 cluster-master-id 的 id 参数用于设置从节点将要复制的主节点。

作为例子,以下代码演示了如何将节点 30007 添加到节点 30001 所在的集群当中:

$ redis-cli --cluster add-node 127.0.0.1:30007 127.0.0.1:30001
>>> Adding node 127.0.0.1:30007 to cluster 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
slots: (0 slots) slave
replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
slots: (0 slots) slave
replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
slots: (0 slots) slave
replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:30007 to make it join the cluster.
[OK] New node added correctly.

从命令打印的最后一行结果来看,节点 30007 已经被成功地添加到了集群中。

移除节点

当用户不再需要集群中的某个节点时,可以通过 cluster 选项的 delnode 子命令来移除该节点:

del-node <ip>:<port> <node_id>

其中命令的 ip 和 port 参数用于指定集群中的某个节点作为入口,而 node_id 则用于指定用户想要移除的节点的 ID。

作为例子,以下代码演示了如何从节点 30001 所在的集群中,移除 ID 为 e1971eef02709cf4698a6fcb09935a910982ab3b 的节点 30007:

e1971eef02709cf4698a6fcb09935a910982ab3b的节点30007:
$ redis-cli --cluster del-node 127.0.0.1:30001 e1971eef02709cf4698a6fcb09935a910892ab3b
>>> Removing node e1971eef02709cf4698a6fcb09935a910982ab3b from cluster 127.0.0.1:30001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

执行命令

通过 cluster 选项的 call 子命令,用户可以在整个集群的所有节点上执行给定的命令:

call host:port command arg arg .. arg

比如,通过执行以下命令,我们可以在集群的所有节点上执行 PING 命令并获得相应的反馈:

$ redis-cli --cluster call 127.0.0.1:30001 PING
>>> Calling PING
127.0.0.1:30001: PONG
127.0.0.1:30004: PONG
127.0.0.1:30005: PONG
127.0.0.1:30006: PONG
127.0.0.1:30002: PONG
127.0.0.1:30003: PONG

又比如,通过执行以下命令,我们可以快速地查看各个节点存储的键值对数量:

$ redis-cli --cluster call 127.0.0.1:30001 DBSIZE
>>> Calling DBSIZE
127.0.0.1:30001: (integer) 0
127.0.0.1:30004: (integer) 0
127.0.0.1:30005: (integer) 3
127.0.0.1:30006: (integer) 0
127.0.0.1:30002: (integer) 3
127.0.0.1:30003: (integer) 0

设置超时时间

通过 cluster 选项的 set-timeout 子命令,用户可以为集群的所有节点重新设置 cluster-node-timeout 选项的值:

set-timeout <host>:<port> <milliseconds>

作为例子,以下代码演示了如何将集群内所有节点的 cluster-node-timeout 选项的值设置为 50000:

$ redis-cli --cluster set-timeout 127.0.0.1:7000 50000
>>> Reconfiguring node timeout in every cluster node...
*** New timeout set for 127.0.0.1:7000
*** New timeout set for 127.0.0.1:7001
*** New timeout set for 127.0.0.1:7005
*** New timeout set for 127.0.0.1:7004
*** New timeout set for 127.0.0.1:7003
*** New timeout set for 127.0.0.1:7002
>>> New node timeout set. 6 OK, 0 ERR.

导入数据

用户可以通过 cluster 选项的 import 子命令,将给定单机 Redis 服务器的数据导入集群中:

import <node-host>:<port> # 集群入口节点的IP地址和端口号
    --cluster-from <server-host>:<port> # 单机服务器的IP地址和端口号
    --cluster-copy # 使用复制导入
    --cluster-replace # 覆盖同名键

在默认情况下,import 命令在向集群导入数据的同时,还会删除单机服务器中的源数据。如果用户想要保留单机服务器中的数据,那么可以在执行命令的同时给定 --cluster-copy 选项。

此外,在导入数据的过程中,如果命令发现将要导入的键在集群数据库中已经存在(同名键冲突),那么命令在默认情况下将中断导入操作。如果用户想要使用导入的键去覆盖集群中已有的同名键,那么可以在执行命令的同时给定 --cluster-replace 选项。

作为例子,以下代码展示了一个 import 子命令的执行示例:

$ redis-cli --cluster import 127.0.0.1:30001 --cluster-from 127.0.0.1:6379 --cluster-copy --
cluster-replace
>>> Importing data from 127.0.0.1:6379 to cluster 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 4979f8583676c46039672fb7319e917e4b303707 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 4788fd4d92387fc5d38a2cd12f0c0d80fc0f6609 127.0.0.1:30004
slots: (0 slots) slave
replicates 4979f8583676c46039672fb7319e917e4b303707
S: b45a7f4355ea733a3177b89654c10f9c31092e92 127.0.0.1:30005
slots: (0 slots) slave
replicates 4ff303d96f5c7436ce8ce2fa6e306272e82cd454
S: 7c56ffba63e3758bc4c2e9b6a55caf294bb21650 127.0.0.1:30006
slots: (0 slots) slave
replicates 07e230805903e4e1657743a2e4d8811a59e2f32f
M: 4ff303d96f5c7436ce8ce2fa6e306272e82cd454 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 07e230805903e4e1657743a2e4d8811a59e2f32f 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** Importing 3 keys from DB 0
Migrating alphabets to 127.0.0.1:30002: OK
Migrating number to 127.0.0.1:30002: OK
Migrating msg to 127.0.0.1:30002: OK

在这个例子中,程序将从单机服务器 127.0.0.1:6379 向节点 127.0.0.1:30001 所在的集群导入数据。在这个过程中,集群中的同名键会被覆盖,而单机服务器原有的数据库则会被保留。