【架构】Docker实现集群主从缩容【案例4/4】

实现集群主从缩容【4/4】

接上一节,在当前机器为4主4从的架构上,减缩容量为3主3从架构。即实现删除6387和6388.

示意图如下:

第一步:查看集群情况(第一次)

python 复制代码
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.
12.114.161.16:6381 (6e961a47...) -> 1 keys | 4096 slots | 1 slaves.
12.114.161.16:6383 (78ac7be1...) -> 1 keys | 4096 slots | 1 slaves.
12.114.161.16: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

python 复制代码
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.

第三步:查看集群情况(第二次)

python 复制代码
redis-cli --cluster check 127.0.0.1:6387
复制代码
略。。。

第四步:将该主机的槽位分给6381主机【--cluster reshard

python 复制代码
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

python 复制代码
redis-cli --cluster check 127.0.0.1:6387
复制代码
root@localhost:/data# redis-cli --cluster check 12.114.161.16:6381  
12.114.161.16:6381 (6e961a47...) -> 2 keys | 8192 slots | 1 slaves.      # 此处显示加上移除来的4096个槽位,总共有8192个槽位。转移成功!
12.114.161.16:6387 (bf73145e...) -> 0 keys | 0 slots | 0 slaves.         # 此次显示原来4096个槽位已经移除,显示为 0
12.114.161.16:6382 (f097fec9...) -> 1 keys | 4096 slots | 1 slaves.
12.114.161.16: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 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                                                 # 此次显示原来Master主机的 4096个槽位已经移除,显示为 0              
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.

根据上面的数据显示,槽位已经转移成功!

注意:如果想将4096个槽位平均分给另外三台master主机,那么需要重复该操作三次,分别输入不同的目标ID即可!

第六步:删除master主机(6387),删除参数【--cluster del-node

python 复制代码
redis-cli --cluster del-node 12.114.161.16:6387 bf73145e2eaaaa08084c9c2dad0d8c757faa48e1
复制代码
root@localhost:/data# redis-cli --cluster del-node 12.114.161.16:6381 bf73145e2eaaaa08084c9c2dad0d8c757faa48e1
>>> Removing node bf73145e2eaaaa08084c9c2dad0d8c757faa48e1 from cluster 12.114.161.16:6381
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

第七步:查看最终的集群情况(第四次)

python 复制代码
redis-cli --cluster check 127.0.0.1:6387
复制代码
root@localhost:/data# 
root@localhost:/data# 
root@localhost:/data# redis-cli --cluster check 12.114.161.16:6381                                            
12.114.161.16:6381 (6e961a47...) -> 2 keys | 8192 slots | 1 slaves.       # 该列表不在显示6387端口的master主机了
12.114.161.16:6382 (f097fec9...) -> 1 keys | 4096 slots | 1 slaves.
12.114.161.16: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 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
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.

至此,成功将原来的"4主4从架构",缩容成"3主3从架构"!

相关推荐
前行的小黑炭5 分钟前
设计模式:为什么使用模板设计模式(不相同的步骤进行抽取,使用不同的子类实现)减少重复代码,让代码更好维护。
android·java·kotlin
Java技术小馆11 分钟前
如何设计一个本地缓存
java·面试·架构
数据智能老司机1 小时前
CockroachDB权威指南——SQL调优
数据库·分布式·架构
数据智能老司机1 小时前
CockroachDB权威指南——应用设计与实现
数据库·分布式·架构
XuanXu1 小时前
Java AQS原理以及应用
java
数据智能老司机1 小时前
CockroachDB权威指南——CockroachDB 模式设计
数据库·分布式·架构
风象南4 小时前
SpringBoot中6种自定义starter开发方法
java·spring boot·后端
mghio13 小时前
Dubbo 中的集群容错
java·微服务·dubbo
uhakadotcom14 小时前
视频直播与视频点播:基础知识与应用场景
后端·面试·架构