Redis3 对集群进行重新分片

时间:2022-05-06
本文章向大家介绍Redis3 对集群进行重新分片,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

重新分片就是在集群各个节点间移动slot,这个操作是向集群中添加节点和删除节点的基础

步骤

(1)执行 redis-trib 命令,用 reshard 参数指明要重新分片,还需要指定集群中一个节点的地址和端口,这样就会自动找到集群中的其他节点

(2)设定要移动的slot数量

(3)指定接收这些slot的节点ID

(4)指定从哪个节点ID移动这些slot

示例

# redis-trib.rb reshard 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
......
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: e0f4fd7576a019b812bd6090d02cec5f975c2bbe 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
......
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)?

这里询问需要移动多少个slot,输入 100

What is the receiving node ID?

这里询问要把slot移动到哪个节点,我的测试环境里有3个master,这里使用第二个作为目标节点,输入第二个节点的ID 9b569ab95eb442550299dab16aef6a5c5db295cf

节点ID就是上面显示出来的:

M: e0f4fd7576a019b812bd6090d02cec5f975c2bbe 127.0.0.1:7000

其中 e0f4fd7576a019b812bd6090d02cec5f975c2bbe 就是节点ID

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:

这里询问从哪个节点中取出slot进行移动,如果想从所有节点中获取,就输入all,否则就输入节点ID,回车后还会询问下一个节点ID,如果源节点的ID都输入完成了,就输入 done

这里输入 all 进行测试

接下来显示的信息是重新分片的计划

Resharding plan:
    Moving slot 0 from e0f4fd7576a019b812bd6090d02cec5f975c2bbe
    ......
Do you want to proceed with the proposed reshard plan (yes/no)?

输入 yes 后,正式开始执行重新分片操作,显示移动过程,显示完成后,重新分片操作彻底完成

Moving slot 0 from 127.0.0.1:7000 to 127.0.0.1:7001:
Moving slot 1 from 127.0.0.1:7000 to 127.0.0.1:7001:

......

检查

redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
......
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: e0f4fd7576a019b812bd6090d02cec5f975c2bbe 127.0.0.1:7000
   slots:50-5460 (5411 slots) master
   1 additional replica(s)
M: 9b569ab95eb442550299dab16aef6a5c5db295cf 127.0.0.1:7001
   slots:0-49,5461-10972 (5562 slots) master
   1 additional replica(s)
M: b0734e888058eab62527384e5d280ebbe57bf348 127.0.0.1:7002
   slots:10973-16383 (5411 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.

可以看到检查正常,节点2的slot数量比1和3多了一点