文章目录
- redis cluster如何增加和删除节点?
- 背景说明
- 添加节点
- 添加 master 节点
- 添加slave节点
- 删除节点
- 删除 master 节点
- 删除slave节点
redis cluster如何增加和删除节点?
背景说明
由于虚拟环境有限,目前所采用的集群方案是在 3 台机器去搭建 6 个 redis 实例的 redis cluster(每台两个)。
使用命令 redis-cli -h 10.0.0.4 -p 7001 cluster nodes 查看当前集群节点
添加节点
添加 master 节点
配置并启动即将添加的节点,具体操作参考简单搭建redis cluster集群。
第一步:使用命令 redis-cli --cluster add-node 10.0.0.7:7007 10.0.0.4:7001 添加节点;
前面的 ip:port 为将要添加的节点,后面的 ip:port 表示集群中的任意一个节点。
第二步:使用命令 redis-cli --cluster reshard 10.0.0.7:7007 分配 slots。
执行命令 redis-cli --cluster reshard 10.0.0.7:7007 时出现的询问:
How many slots do you want to move (from 1 to 16384)?:表示需要移动槽的数量。填写自己的需要即可。
What is the receiving node ID?:表示:那个 id 来接收它。即新的 7007 的 id。
Source node #1:从填写的那个节点 id 上移动散列插槽。如果需要平均节点插槽,那么每个主节点的 id 都写上或者写 all。
Do you want to proceed with the proposed reshard plan (yes/no)?:确定要移动这些槽的计划吗?输入 yes 或者 no。
[root@mq4 init.d]# redis-cli --cluster reshard 10.0.0.7:7007
>>> Performing Cluster Check (using node 10.0.0.7:7007)
M: f1f7185e3f0a1096ad16b598005875e1b2d306cc 10.0.0.7:7007
slots: (0 slots) master
1 additional replica(s)
S: 15df8829b8e756ce76ae1c2b3d879193a758348a 10.0.0.7:7008
slots: (0 slots) slave
replicates f1f7185e3f0a1096ad16b598005875e1b2d306cc
M: 58869cf6dee60706154a5a3461c4687922e6ea60 10.0.0.5:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 1a7a4d4168b0e3203f6c6c1e0a35351760f1bacf 10.0.0.4:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 32a1ec21e51934856a97efd8ce4bfc8ff90619d2 10.0.0.4:7002
slots: (0 slots) slave
replicates 5f8f0c5c6b71615cc837cb1efe2997a35de45623
M: 5f8f0c5c6b71615cc837cb1efe2997a35de45623 10.0.0.6:7005
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 6959f6d61974895474c800c4c46f704ad4857264 10.0.0.5:7004
slots: (0 slots) slave
replicates 1a7a4d4168b0e3203f6c6c1e0a35351760f1bacf
S: 0ea5f18383cfa1fb70d4af093cd1946fc0e2aff6 10.0.0.6:7006
slots: (0 slots) slave
replicates 58869cf6dee60706154a5a3461c4687922e6ea60
[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? f1f7185e3f0a1096ad16b598005875e1b2d306cc
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
Ready to move 4096 slots.
Source nodes:
M: 58869cf6dee60706154a5a3461c4687922e6ea60 10.0.0.5:7003
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 1a7a4d4168b0e3203f6c6c1e0a35351760f1bacf 10.0.0.4:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 5f8f0c5c6b71615cc837cb1efe2997a35de45623 10.0.0.6:7005
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
Destination node:
M: f1f7185e3f0a1096ad16b598005875e1b2d306cc 10.0.0.7:7007
slots: (0 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 5461 from 58869cf6dee60706154a5a3461c4687922e6ea60
Moving slot 5462 from 58869cf6dee60706154a5a3461c4687922e6ea60
Moving slot 5463 from 58869cf6dee60706154a5a3461c4687922e6ea60
Moving slot 5464 from 58869cf6dee60706154a5a3461c4687922e6ea60
Moving slot 5465 from 58869cf6dee60706154a5a3461c4687922e6ea60
Moving slot 5466 from 58869cf6dee60706154a5a3461c4687922e6ea60
Moving slot 5467 from 58869cf6dee60706154a5a3461c4687922e6ea60
Moving slot 5468 from 58869cf6dee60706154a5a3461c4687922e6ea60
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 5461 from 10.0.0.5:7003 to 10.0.0.7:7007:
Moving slot 5462 from 10.0.0.5:7003 to 10.0.0.7:7007:
Moving slot 5463 from 10.0.0.5:7003 to 10.0.0.7:7007:
Moving slot 5464 from 10.0.0.5:7003 to 10.0.0.7:7007:
Moving slot 5465 from 10.0.0.5:7003 to 10.0.0.7:7007:
Moving slot 5466 from 10.0.0.5:7003 to 10.0.0.7:7007:
Moving slot 5467 from 10.0.0.5:7003 to 10.0.0.7:7007:
Moving slot 5468 from 10.0.0.5:7003 to 10.0.0.7:7007:
......
查看集群节点
添加slave节点
相比于添加 master 节点,添加 slave 节点则不需要分配 slot,只需执行命令 redis-cli --cluster add-node 10.0.0.7:7008 10.0.0.4:7001 --cluster-s lave f1f7185e3f0a1096ad16b598005875e1b2d306cc 即可。
删除节点
删除 master 节点
因为 master 节点是分配了 slot 的,所以删除节点之前需要把 solt 移动到其他 master 节点上去,也就行执行 reshard 操作,否则数据会丢失。
然后执行命令 redis-cli --cluster del-node ip:prot 删除节点。
删除slave节点
相比于删除 master 节点,删除 slave 节点直接删除即可。