原文:http://blog.sina.com.cn/s/blog_53b45c4d0102wg13.html
节点的移除
和节点添加一样,移除节点也有移除主节点,从节点。
1、移除主节点
移除节点使用redis-trib的del-node命令,
- redis-trib del-node 127.0.0.1:7002 ${node-id}
127.0.0.1:7002位集群节点,node-id为要删除的主节点。 和添加节点不同,移除节点node-id是必需的,测试删除7001主节点:
- [root@localhost redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7001 dd19221c404fb2fc4da37229de56bab755c76f2b
- >>> Removing node dd19221c404fb2fc4da37229de56bab755c76f2b from cluster 127.0.0.1:7002
- [ERR] Node 127.0.0.1:7001 is not empty! Reshard data away and try again.
- [root@localhost redis-cluster]#
redis cluster提示7001已经有数据了,不能够被删除,需要将他的数据转移出去,也就是和新增主节点一样需重新分片。
- [root@localhost redis-cluster]# ./redis-trib.rb reshard 127.0.0.1:7002
执行以后会提示我们移除的大小,因为7001占用了4096个槽点
- >>> 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
提示移动的node id,填写7009的node id。(这里好像有误,应该填移动到的node id)
- How many slots do you want to move (from 1 to 16384)? 4096
- What is the receiving node ID? (接收的nodeid)
需要移动到全部主节点上还是单个主节点(这里好像有误,需要移动的的节点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:
将4096个槽点移动到7009上,填写7001的node id :dd19221c404fb2fc4da37229de56bab755c76f2b
- Source node #1:dd19221c404fb2fc4da37229de56bab755c76f2b
- Source node #2:done
- Do you want to proceed with the proposed reshard plan (yes/no)? yes
确认之后会一个一个将7001的卡槽移到到7009上。
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7009
- >>> Performing Cluster Check (using node 127.0.0.1:7009)
- M: 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009
- slots:0-6826,10923-12287 (8192 slots) master
- 3 additional replica(s)
- S: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001
- slots: (0 slots) master
- 0 additional replica(s)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- 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.
- [root@localhost redis-cluster]#
可以看到7001有0个卡槽,而7009有8192个卡槽。
在执行移除操作
- [root@localhost redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7002 dd19221c404fb2fc4da37229de56bab755c76f2b
- >>> Removing node dd19221c404fb2fc4da37229de56bab755c76f2b from cluster 127.0.0.1:7002
- >>> Sending CLUSTER FORGET messages to the cluster...
- >>> SHUTDOWN the node.
- [root@localhost redis-cluster]#
已经删除了7001节点。
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7001
- [ERR] Sorry, can't connect to node 127.0.0.1:7001
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7009
- >>> Performing Cluster Check (using node 127.0.0.1:7009)
- M: 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009
- slots:0-6826,10923-12287 (8192 slots) master
- 3 additional replica(s)
- S: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 127.0.0.1:7008
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- 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.
- [root@localhost redis-cluster]#
可以看到7001已经连接不了;而7001的从节点7004自动分配到了7009主节点中,7009现在3个从节点。
2、移除从节点
比如删除7009的7008节点:
- [root@localhost redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7009 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5
- >>> Removing node 2ab1b061c36f30ae35604e9a171ae3afdc3c87e5 from cluster 127.0.0.1:7009
- >>> Sending CLUSTER FORGET messages to the cluster...
- >>> SHUTDOWN the node.
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7008
- [ERR] Sorry, can't connect to node 127.0.0.1:7008
- [root@localhost redis-cluster]#
删除从节点比较方便,现在redis-cluster中有3个主节点,4个从节点,如下:
- [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7009
- >>> Performing Cluster Check (using node 127.0.0.1:7009)
- M: 1f51443ede952b98724fea2a12f61fe710ab6cb1 127.0.0.1:7009
- slots:0-6826,10923-12287 (8192 slots) master
- 2 additional replica(s)
- S: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004
- slots: (0 slots) slave
- replicates 1f51443ede952b98724fea2a12f61fe710ab6cb1
- M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003
- slots:12288-16383 (4096 slots) master
- 1 additional replica(s)
- S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002
- slots: (0 slots) slave
- replicates a5db243087d8bd423b9285fa8513eddee9bb59a6
- S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006
- slots: (0 slots) slave
- replicates f9886c71e98a53270f7fda961e1c5f730382d48f
- M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005
- 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.
- [root@localhost redis-cluster]#