What is the receiving node>
Please enter all the source node> Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes> Source node #1:90cb860b7f4ff516304c577bc1e514dc95ecd09b
Source node #2:3f121a67fab0d74f0d31b69326259e687902e1b3
Source node #3:fa2acce219d088e2b33756dac2e85ca92936a8dd
Source node #4:done
Ready to move 4096 slots.
Source nodes:
M: 90cb860b7f4ff516304c577bc1e514dc95ecd09b 127.0.0.1:6879
slots:0-4095,4097-5460 (5460 slots) master
1 additional replica(s)
M: 3f121a67fab0d74f0d31b69326259e687902e1b3 127.0.0.1:6880
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: fa2acce219d088e2b33756dac2e85ca92936a8dd 127.0.0.1:6881
slots:10923-16383 (5461 slots) master
1 additional replica(s)
Destination node:
M: 99ea0df1d9683affb1271a5092fc8b15b378adba 127.0.0.1:6885
slots:4096 (1 slots) master
0 additional replica(s)
Resharding plan:
Moving slot 5461 from 3f121a67fab0d74f0d31b69326259e687902e1b3
...
Moving slot 11090 from fa2acce219d088e2b33756dac2e85ca92936a8dd
...
Moving slot 1364 from 90cb860b7f4ff516304c577bc1e514dc95ecd09b
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 5461 from 127.0.0.1:6880 to 127.0.0.1:6885: ..
...
Moving slot 12177 from 127.0.0.1:6881 to 127.0.0.1:6885: ....
...
Moving slot 1364 from 127.0.0.1:6879 to 127.0.0.1:6885: .....
查看节点和槽新的映射关系。
127.0.0.1:6879> cluster nodes
99ea0df1d9683affb1271a5092fc8b15b378adba 127.0.0.1:6885 master - 0 1532852546583 9 connected 0-1364 4096 5461-6826 10923-12287
558b0fb8d44933e694b46c15d05e595ce5ae4fab 127.0.0.1:6886 master - 0 1532852550630 8 connected
fa2acce219d088e2b33756dac2e85ca92936a8dd 127.0.0.1:6881 master - 0 1532852548097 3 connected 12288-16383
3f121a67fab0d74f0d31b69326259e687902e1b3 127.0.0.1:6880 master - 0 1532852549619 2 connected 6827-10922
...
90cb860b7f4ff516304c577bc1e514dc95ecd09b 127.0.0.1:6879 myself,master - 0 0 1 connected 1365-4095 4097-5460
迁移后使用redis-trib.rb rebalance命令检查节点间槽的均衡性。
$ redis-trib.rb rebalance 127.0.0.1:6879
...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.0% threshold.
(3)添加从节点
把节点6886作为6885的从节点,保证整个集群的高可用。
127.0.0.1:6886> cluster replicate 99ea0df1d9683affb1271a5092fc8b15b378adba
查看节点6886状态已成为6885的从节点,至此扩容完成。
127.0.0.1:6886> cluster nodes
99ea0df1d9683affb1271a5092fc8b15b378adba 127.0.0.1:6885 master - 0 1532852938340 9 connected 0-1364 4096 5461-6826 10923-12287
558b0fb8d44933e694b46c15d05e595ce5ae4fab 127.0.0.1:6886 myself,slave 99ea0df1d9683affb1271a5092fc8b15b378adba 0 0 8 connected
...
收缩集群
收缩集群意味着从现有集群中安全下线部分节点。首先要确定下线节点是否有负责的槽,若有,需把槽迁移到其它节点,保证节点下线后整个集群槽和节点映射的完整性。当下线节点不再负责槽或本身是从节点时,就可以通知集群内其它节点忘记下线节点,当所有节点忘记该节点后可以正常关闭。下面按照2个过程进行。
(1)下线迁移槽
下线节点要把自己负责的槽迁移到其它节点,原理和节点扩容槽迁移过程一致。如把6881和6884节点下线,6881是主节点,负责槽(12288-16383),6884是它的从节点。下线6881节点之前,要把它负责的槽迁移到6879,6880和6885这3个节点。由于每次执行reshard命令只能有一个目标节点,因此要执行3次reshard命令,分别迁移1365,1365和1366个槽。
$ redis-trib.rb reshard 127.0.0.1:6879
...
How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node>
Please enter all the source node> Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes> Source node #1:fa2acce219d088e2b33756dac2e85ca92936a8dd
Source node #2:done
Ready to move 1365 slots.
Source nodes:
M: fa2acce219d088e2b33756dac2e85ca92936a8dd 127.0.0.1:6881
slots:12288-16383 (4096 slots) master
1 additional replica(s)
Destination node:
M: 90cb860b7f4ff516304c577bc1e514dc95ecd09b 127.0.0.1:6879
slots:1365-4095,4097-5460 (4095 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 12288 from fa2acce219d088e2b33756dac2e85ca92936a8dd
Moving slot 12289 from fa2acce219d088e2b33756dac2e85ca92936a8dd
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
Moving slot 13651 from 127.0.0.1:6881 to 127.0.0.1:6879: ..
Moving slot 13652 from 127.0.0.1:6881 to 127.0.0.1:6879: ...
槽迁移完成后,6879节点接管了6881节点的1365个槽12288-13652。
继续把1365个,和1366个槽迁移到6880节点,和6885节点。
$ redis-trib.rb reshard 127.0.0.1:6879
...
How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node>
Please enter all the source node> Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes> Source node #1:fa2acce219d088e2b33756dac2e85ca92936a8dd
Source node #2:done
...
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
$ redis-trib.rb reshard 127.0.0.1:6879
...
How many slots do you want to move (from 1 to 16384)? 1366
What is the receiving node>
Please enter all the source node> Type 'all' to use all the nodes as source nodes for the hash slots.