关于redis-cluster:redis重新分片

10次阅读

共计 2448 个字符,预计需要花费 7 分钟才能阅读完成。

为什么会呈现从新分片
呈现上面状况后 redis 集群会从新进行分片:

  1. 有新的节点退出
  2. 有节点须要下线
  3. 某些节点数据分布不平均须要从新调整

如何从新分片
从新分片须要人工应用 redis.trib 工具,流程如下:

  1. 执行命令./redis-trib.rb reshard ip:port
    执行了这个命令之后,控制台会显示集群里所有节点信息,包含 ip,port,每个节点负责的 slots,每个节点的 master/slave 角色,及每个节点的 ID。相似:
$ ./redis-trib.rb reshard 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 9991306f0e50640a5684f1958fd754b38fa034c9 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 393c6df5eb4b4cec323f0e4ca961c8b256e3460a 127.0.0.1:7002
slots:10922-16383 (5462 slots) master
S: 3375be2ccc321932e8853234ffa87ee9fde973ff 127.0.0.1:7005
slots: (0 slots) slave
M: e68e52cee0550f558b03b342f2f0354d2b8a083b 127.0.0.1:7001
slots:5461-10921 (5461 slots) master
S: 48b728dbcedff6bf056231eb44990b7d1c35c3e0 127.0.0.1:7003
slots: (0 slots) slave
S: 345ede084ac784a5c030a0387f8aaa9edfc59af3 127.0.0.1:7004
slots: (0 slots) slave
[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)? 1000

同时还会询问咱们须要迁徙多少个 slots,示例中咱们迁徙 1000.

  1. 指定迁徙的指标节点
    这里咱们须要输出的是指标节点的 ID 而非指标节点的 ip+port
$ ./redis-trib.rb reshard 127.0.0.1:7000
...
What is the receiving node ID? 9991306f0e50640a5684f1958fd754b38fa034c9

咱们抉择了 9991306f0e50640a5684f1958fd754b38fa034c9 这个节点

  1. 指定迁徙的源节点
    也就是咱们心愿从哪些节点将 1000 个 slots 迁徙到指标节点下来。如果咱们不指定特定节点而是输出 all,这样之前集群中的所有主节点都会成为源节点,redis-trib 会从各个源节点各取出一部分 slots 凑够 1000 个,而后迁往指标节点:
$ ./redis-trib.rb reshard 127.0.0.1:7000
...
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:all

输出 all 之后,redis-trib 会列出哪些 slot 由哪些节点迁徙出,要是没问题须要咱们进行确认:

$ ./redis-trib.rb reshard 127.0.0.1:7000
...
Moving slot 11421 from 393c6df5eb4b4cec323f0e4ca961c8b256e3460a
Moving slot 11422 from 393c6df5eb4b4cec323f0e4ca961c8b256e3460a
Moving slot 5461 from e68e52cee0550f558b03b342f2f0354d2b8a083b
Moving slot 5469 from e68e52cee0550f558b03b342f2f0354d2b8a083b
...
Moving slot 5959 from e68e52cee0550f558b03b342f2f0354d2b8a083b
Do you want to proceed with the proposed reshard plan (yes/no)? yes

输出 yes 确认后,迁徙工作正式进行。
迁徙实现之后,redis-trib 会向集群中所有节点发送 cluster setslot <slot> node <target_id> 命令,这样,集群中的所有节点会更新最新的 slots 与节点的对应关系。

ASK 谬误及主动转向
在迁徙过程中 redis 集群会持续响应用户申请,但如果波及的 key 刚好属于正在迁徙的 slot 时就会呈现 ask 谬误,当然这个 ask 谬误不会返回给用户,集群会依据响应的 ask 信息里的 ip,port 进行主动转向。

总结:

  1. 节点呈现变动后须要从新分片
  2. 从新分片应用 redis-trib 工具,指定迁徙指标节点,须要迁徙多少 slots,迁徙的源节点
  3. 迁徙过程中也会解决用户申请

参考文章:redis-cluster 从新分片

正文完
 0