Redis集群增删节点
部署环境
redis-3.0.0
VM虚拟机redhat6.5-x64:192.168.1.201、192.168.1.202、192.168.1.203、
192.168.1.204、192.168.1.205、192.168.1.206
Xshell4
部署方案
目前已经有:3主3从redis集群,还有两个已安装好redis节点207、208
现在需要增加 删除 207-master、208-slave
redis集群搭建参考:https://www.cnblogs.com/cac2020/p/9529900.html
操作步骤
准备工作:
一、介绍命令redis-trib.rb
1 create:创建一个集群环境host1:port1 ... hostN:portN(集群中的主从节点比例)
2 call:可以执行redis命令
3 add-node:将一个节点添加到集群里,第一个参数为新节点的ip:port,第二个参数为集群中任意一个已经存在的节点的ip:port
4 del-node:移除一个节点
5 reshard:重新分片
6 check:检查集群状态
二、操作
登录集群客户端201
/usr/local/redis/bin/redis-cli -c -h 192.168.1.201 -p 6379
操作一:新增207-master
/usr/local/redis-3.0.0/src/redis-trib.rb add-node 192.168.1.207:6379 192.168.1.201:6379
操作二:重新分配slot
/usr/local/redis-3.0.0/src/redis-trib.rb reshard 192.168.1.207:6379
步骤一:使用redis-trib命令,找到集群中的任意一个主节点(红色位置表现集群中的任意一个主节点),对其进行重新分片工作。
[root@bhz004 7001]# /usr/local/redis3.0/src/redis-trib.rb reshard 192.168.1.171:7001
输出如下:
>>> Performing Cluster Check (using node 192.168.1.171:7001)
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0 192.168.1.171:7006
slots: (0 slots) slave
replicates 83df08875c7707878756364039df0a4c8658f272
S: adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
slots: (0 slots) slave
replicates 8aac82b63d42a1989528cd3906579863a5774e77
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots: (0 slots) master
0 additional replica(s)
S: a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
slots: (0 slots) slave
replicates 614d0def75663f2620b6402a017014b57c912dad
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
slots:10923-16383 (5461 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)?
(提示二)
What is the receiving node ID? 382634a4025778c040b7213453fd42a709f79e28
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 200 slots.
Source nodes:
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
Destination node:
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots: (0 slots) master
0 additional replica(s)
Resharding plan:(分片执行计划日志)
Moving slot 5461 from 8aac82b63d42a1989528cd3906579863a5774e77
...
Moving slot 0 from 614d0def75663f2620b6402a017014b57c912dad
...
Moving slot 10923 from 83df08875c7707878756364039df0a4c8658f272
...
(提示三)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 65 from 192.168.1.171:7001 to 192.168.1.171:7007:
...
Moving slot 10923 from 192.168.1.171:7003 to 192.168.1.171:7007:
...
Moving slot 5527 from 192.168.1.171:7002 to 192.168.1.171:7007:
...
1提示一:是希望你需要多少个槽移动到新的节点上,可以自己设置,比如200个槽。
2提示二:是你需要把这200个slot槽移动到那个节点上去(需要指定节点id),并且下个 提示是输入all为从所有主节点(7001 7002 7003)中分别抽取响应的槽数(一共为200个槽到指定的新节点中!,并且会打印执行分片的计划。)
3提示三:输入yes确认开始执行分片任务
操作三:新增208-slave,作为207的从节点
/usr/local/redis-3.0.0/src/redis-trib.rb add-node 192.168.1.208:6379 192.168.1.201:6379
操作四:新增208-slave
/usr/local/redis/bin/redis-cli -c -h 192.168.1.208 -p 6379
cluster replicate 207节点id
操作五:删除节点208-slave
/usr/local/redis-3.0.0/src/redis-trib.rb del-node 192.168.1.208:6379 208节点id
操作六:删除节点207-master
删除主节点之前需要先将slot迁移到其他主节点上
/usr/local/redis-3.0.0/src/redis-trib.rb reshard 192.168.1.207:6379
/usr/local/redis-3.0.0/src/redis-trib.rb del-node 192.168.1.207:6379 207节点id
步骤一:删除7007(master)节点之前,我们需要先把其全部的数据(slot槽)移动到其他节点上去(目前只能把master的数据迁移到一个节点上,暂时做不了平均分配功能)。
[root@bhz004 7001]# /usr/local/redis3.0/src/redis-trib.rb reshard 192.168.1.171:7007
输出如下:
>>> Performing Cluster Check (using node 192.168.1.171:7007)
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots:0-65,5461-5527,10923-10988 (199 slots) master
0 additional replica(s)
S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0 192.168.1.171:7006
slots: (0 slots) slave
replicates 83df08875c7707878756364039df0a4c8658f272
S: a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
slots: (0 slots) slave
replicates 614d0def75663f2620b6402a017014b57c912dad
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:66-5460 (5395 slots) master
1 additional replica(s)
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5528-10922 (5395 slots) master
1 additional replica(s)
S: adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
slots: (0 slots) slave
replicates 8aac82b63d42a1989528cd3906579863a5774e77
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
slots:10989-16383 (5395 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)?
(注释:这里不会是正好200个槽)
What is the receiving node ID? 614d0def75663f2620b6402a017014b57c912dad
(注释:这里是需要把数据移动到哪?7001的主节点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:382634a4025778c040b7213453fd42a709f79e28
(注释:这里是需要数据源,也就是我们的7007节点id)
Source node #2:done
(注释:这里直接输入done 开始生成迁移计划)
Ready to move 199 slots.
Source nodes:
M: 382634a4025778c040b7213453fd42a709f79e28
192.168.1.171:7007
slots:0-65,5461-5527,10923-10988 (199 slots)
master
0 additional replica(s)
Destination
node:
M: 614d0def75663f2620b6402a017014b57c912dad
192.168.1.171:7001
slots:66-5460 (5395 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 0 from 382634a4025778c040b7213453fd42a709f79e28
...
Do you want to
proceed with the proposed reshard plan (yes/no)? Yes
(注释:这里输入yes开始迁移)
Moving slot 0 from 192.168.1.171:7007 to
192.168.1.171:7001:
...
到此为止我们已经成功的把7007主节点的数据迁移到7001上去了,我们可以看一下现在的集群状态如下图,你会发现7007下面已经没有任何数据(slot)槽了,证明迁移成功!
操作文档下载: