为什么会呈现从新分片
呈现上面状况后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:7000Connecting to node 127.0.0.1:7000: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7005: OKConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7003: OKConnecting to node 127.0.0.1:7004: OK>>> Performing Cluster Check (using node 127.0.0.1:7000)M: 9991306f0e50640a5684f1958fd754b38fa034c9 127.0.0.1:7000slots:0-5460 (5461 slots) masterM: 393c6df5eb4b4cec323f0e4ca961c8b256e3460a 127.0.0.1:7002slots:10922-16383 (5462 slots) masterS: 3375be2ccc321932e8853234ffa87ee9fde973ff 127.0.0.1:7005slots: (0 slots) slaveM: e68e52cee0550f558b03b342f2f0354d2b8a083b 127.0.0.1:7001slots:5461-10921 (5461 slots) masterS: 48b728dbcedff6bf056231eb44990b7d1c35c3e0 127.0.0.1:7003slots: (0 slots) slaveS: 345ede084ac784a5c030a0387f8aaa9edfc59af3 127.0.0.1:7004slots: (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 393c6df5eb4b4cec323f0e4ca961c8b256e3460aMoving slot 11422 from 393c6df5eb4b4cec323f0e4ca961c8b256e3460aMoving slot 5461 from e68e52cee0550f558b03b342f2f0354d2b8a083bMoving slot 5469 from e68e52cee0550f558b03b342f2f0354d2b8a083b...Moving slot 5959 from e68e52cee0550f558b03b342f2f0354d2b8a083bDo 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从新分片