接上一节,在当前机器为4主4从的架构上,减缩容量为3主3从架构。即实现删除6387和6388.
示意图如下:
第一步:查看集群情况(第一次)
redis-cli --cluster check 127.0.0.1:6387
root@localhost:/data# redis-cli --cluster check 127.0.0.1:6387
127.0.0.1:6387 (bf73145e...) -> 1 keys | 4096 slots | 1 slaves.
122.114.161.116:6381 (6e961a47...) -> 1 keys | 4096 slots | 1 slaves.
122.114.161.116:6383 (78ac7be1...) -> 1 keys | 4096 slots | 1 slaves.
122.114.161.116:6382 (f097fec9...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 4 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:6387)
M: bf73145e2eaaaa08084c9c2dad0d8c757faa48e1 127.0.0.1:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
S: 4ed46e0368698cd9d5af2ee84631c878b8ebc4d0 12.114.161.16:6386
slots: (0 slots) slave
replicates 6e961a4765b555189708bebb69badf7dfad25cd5
S: 664e456ae7d5cf27ea6583ba2f437dac11d4e767 12.114.161.16:6388
slots: (0 slots) slave
replicates bf73145e2eaaaa08084c9c2dad0d8c757faa48e1
S: 9bc2417a9cd6545ef2445eb4aa0610d586acd73b 12.114.161.16:6385
slots: (0 slots) slave
replicates 78ac7be13522bd4ffd6fcf900c6c149c6938ecc2
M: 6e961a4765b555189708bebb69badf7dfad25cd5 12.114.161.16:6381
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: 0e34147ce2544cd90f5cce78d5493ae9e8625dfe 12.114.161.16:6384
slots: (0 slots) slave
replicates f097fec937f54d147d316c1c62e26cb67c9fd059
M: 78ac7be13522bd4ffd6fcf900c6c149c6938ecc2 12.114.161.16:6383
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: f097fec937f54d147d316c1c62e26cb67c9fd059 12.114.161.16:6382
slots:[6827-10922] (4096 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.
第二步:删除从主机(6388),删除参数【--cluster del-node
】
redis-cli --cluster del-node 12.114.161.16:6388 664e456ae7d5cf27ea6583ba2f437dac11d4e767
root@localhost:/data# redis-cli --cluster del-node 12.114.161.16:6388 664e456ae7d5cf27ea6583ba2f437dac11d4e767
>>> Removing node 664e456ae7d5cf27ea6583ba2f437dac11d4e767 from cluster 12.114.161.16:6388
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
第三步:查看集群情况(第二次)
redis-cli --cluster check 127.0.0.1:6387
略。。。
第四步:将该主机的槽位分给6381主机【--cluster reshard
】
redis-cli --cluster reshard 12.114.161.16:6381
root@localhost:/data# redis-cli --cluster reshard 12.114.161.16:6381
>>> Performing Cluster Check (using node 12.114.161.16:6381)
M: 6e961a4765b555189708bebb69badf7dfad25cd5 12.114.161.16:6381
slots:[0-6826],[10923-12287] (8192 slots) master
1 additional replica(s)
S: 4ed46e0368698cd9d5af2ee84631c878b8ebc4d0 12.114.161.16:6386
slots: (0 slots) slave
replicates 6e961a4765b555189708bebb69badf7dfad25cd5
M: bf73145e2eaaaa08084c9c2dad0d8c757faa48e1 12.114.161.16:6387
slots: (0 slots) master
S: 9bc2417a9cd6545ef2445eb4aa0610d586acd73b 12.114.161.16:6385
slots: (0 slots) slave
replicates 78ac7be13522bd4ffd6fcf900c6c149c6938ecc2
S: 0e34147ce2544cd90f5cce78d5493ae9e8625dfe 12.114.161.16:6384
slots: (0 slots) slave
replicates f097fec937f54d147d316c1c62e26cb67c9fd059
M: f097fec937f54d147d316c1c62e26cb67c9fd059 12.114.161.16:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
M: 78ac7be13522bd4ffd6fcf900c6c149c6938ecc2 12.114.161.16:6383
slots:[12288-16383] (4096 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.
How many slots do you want to move (from 1 to 16384)? 4096 # 移除多少槽位数?
What is the receiving node ID? 6e961a4765b555189708bebb69badf7dfad25cd5 # 分给哪一台master 主机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: bf73145e2eaaaa08084c9c2dad0d8c757faa48e1 # 从哪一台主机移除的槽位 主机ID
Source node #2: done # 输入done即可 ,后面提示 直接输入‘yes’即可
...
....
第五步:查看集群情况(第三次),确定需要移除主机的槽位数为0
redis-cli --cluster check 127.0.0.1:6387
root@localhost:/data# redis-cli --cluster check 122.114.161.116:6381
122.114.161.116:6381 (6e961a47...) -> 2 keys | 8192 slots | 1 slaves. # 此处显示加上移除来的4096个槽位,总共有8192个槽位。转移成功!
122.114.161.116:6387 (bf73145e...) -> 0 keys | 0 slots | 0 slaves. # 此次显示原来4096个槽位已经移除,显示为 0
122.114.161.116:6382 (f097fec9...) -> 1 keys | 4096 slots | 1 slaves.
122.114.161.116:6383 (78ac7be1...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 4 keys in 4 masters. # 显示总共4台master主机
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 122.114.161.116:6381)
M: 6e961a4765b555189708bebb69badf7dfad25cd5 122.114.161.116:6381
slots:[0-6826],[10923-12287] (8192 slots) master
1 additional replica(s)
S: 4ed46e0368698cd9d5af2ee84631c878b8ebc4d0 122.114.161.116:6386
slots: (0 slots) slave
replicates 6e961a4765b555189708bebb69badf7dfad25cd5
M: bf73145e2eaaaa08084c9c2dad0d8c757faa48e1 122.114.161.116:6387
slots: (0 slots) master # 此次显示原来Master主机的 4096个槽位已经移除,显示为 0
S: 9bc2417a9cd6545ef2445eb4aa0610d586acd73b 122.114.161.116:6385
slots: (0 slots) slave
replicates 78ac7be13522bd4ffd6fcf900c6c149c6938ecc2
S: 0e34147ce2544cd90f5cce78d5493ae9e8625dfe 122.114.161.116:6384
slots: (0 slots) slave
replicates f097fec937f54d147d316c1c62e26cb67c9fd059
M: f097fec937f54d147d316c1c62e26cb67c9fd059 122.114.161.116:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
M: 78ac7be13522bd4ffd6fcf900c6c149c6938ecc2 122.114.161.116:6383
slots:[12288-16383] (4096 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.
根据上面的数据显示,槽位已经转移成功!
【注意:如果想将4096个槽位平均分给另外三台master主机,那么需要重复该操作三次,分别输入不同的目标ID即可!
】
第六步:删除master主机(6387),删除参数【--cluster del-node
】
redis-cli --cluster del-node 12.114.161.16:6387 bf73145e2eaaaa08084c9c2dad0d8c757faa48e1
root@localhost:/data# redis-cli --cluster del-node 122.114.161.116:6381 bf73145e2eaaaa08084c9c2dad0d8c757faa48e1
>>> Removing node bf73145e2eaaaa08084c9c2dad0d8c757faa48e1 from cluster 122.114.161.116:6381
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
第七步:查看最终的集群情况(第四次)
redis-cli --cluster check 127.0.0.1:6387
root@localhost:/data#
root@localhost:/data#
root@localhost:/data# redis-cli --cluster check 122.114.161.116:6381
122.114.161.116:6381 (6e961a47...) -> 2 keys | 8192 slots | 1 slaves. # 该列表不在显示6387端口的master主机了
122.114.161.116:6382 (f097fec9...) -> 1 keys | 4096 slots | 1 slaves.
122.114.161.116:6383 (78ac7be1...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 4 keys in 3 masters. # 此次仅仅剩下3台Master主机
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 122.114.161.116:6381)
M: 6e961a4765b555189708bebb69badf7dfad25cd5 122.114.161.116:6381
slots:[0-6826],[10923-12287] (8192 slots) master
1 additional replica(s)
S: 4ed46e0368698cd9d5af2ee84631c878b8ebc4d0 122.114.161.116:6386
slots: (0 slots) slave
replicates 6e961a4765b555189708bebb69badf7dfad25cd5
S: 9bc2417a9cd6545ef2445eb4aa0610d586acd73b 122.114.161.116:6385
slots: (0 slots) slave
replicates 78ac7be13522bd4ffd6fcf900c6c149c6938ecc2
S: 0e34147ce2544cd90f5cce78d5493ae9e8625dfe 122.114.161.116:6384
slots: (0 slots) slave
replicates f097fec937f54d147d316c1c62e26cb67c9fd059
M: f097fec937f54d147d316c1c62e26cb67c9fd059 122.114.161.116:6382
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
M: 78ac7be13522bd4ffd6fcf900c6c149c6938ecc2 122.114.161.116:6383
slots:[12288-16383] (4096 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.
至此,成功将原来的“4主4从架构”,缩容成“3主3从架构”!