本文将上篇文章添加的节点从Redis集群里删除。
1、查看集群信息
cluster nodes
5aa077444a506afad1ff796616124a0b7298c125 172.16.254.126:7006@17006 master - 0 1614781558502 10 connected 13424-16383
a489464dfd1321cdbe2a0f464856218a38127e19 172.16.254.125:7005@17005 master - 0 1614781557238 9 connected 1500-2998 5461-11423
8205aa9f24882516bb8bbe0fb7d2c593271de152 172.16.254.127:7007@17007 slave 5aa077444a506afad1ff796616124a0b7298c125 0 1614781555914 10 connected
dae116bae245c1d9b94643c627243f0122dec6ad 172.16.254.124:7004@17004 master - 0 1614781555000 8 connected 0-1499 2999-5460
804ecd22d3de6dc2080a8de572a320407596b6fb 172.16.254.128:7008@17008 slave dae116bae245c1d9b94643c627243f0122dec6ad 0 1614781556000 8 connected
57bfd28d400e95395b10fb62e77a2b9f9a05dddb 172.16.254.121:7001@17001 master - 0 1614781558641 12 connected 11424-13423
0600bacf8e9be9572c780090cb242e1e1e0206a2 172.16.254.129:7009@17009 slave a489464dfd1321cdbe2a0f464856218a38127e19 0 1614781555570 9 connected
01049b46ff785dbc99d26f628893ab6cb24f86a1 172.16.254.122:7002@17002 myself,slave 57bfd28d400e95395b10fb62e77a2b9f9a05dddb 0 1614781552000 11 connected
2、删除从节点
redis-cli --cluster del-node 172.16.254.122:7002 01049b46ff785dbc99d26f628893ab6cb24f86a1
>>> Removing node 01049b46ff785dbc99d26f628893ab6cb24f86a1 from cluster 172.16.254.122:7002
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
3、迁移主节点数据
redis-cli --cluster reshard 172.16.254.121:7001
>>> Performing Cluster Check (using node 172.16.254.121:7001)
M: 57bfd28d400e95395b10fb62e77a2b9f9a05dddb 172.16.254.121:7001
slots:[11424-13423] (2000 slots) master
S: 8205aa9f24882516bb8bbe0fb7d2c593271de152 172.16.254.127:7007
slots: (0 slots) slave
replicates 5aa077444a506afad1ff796616124a0b7298c125
M: a489464dfd1321cdbe2a0f464856218a38127e19 172.16.254.125:7005
slots:[1500-2998],[5461-11423] (7462 slots) master
1 additional replica(s)
M: 5aa077444a506afad1ff796616124a0b7298c125 172.16.254.126:7006
slots:[13424-16383] (2960 slots) master
1 additional replica(s)
S: 0600bacf8e9be9572c780090cb242e1e1e0206a2 172.16.254.129:7009
slots: (0 slots) slave
replicates a489464dfd1321cdbe2a0f464856218a38127e19
S: 804ecd22d3de6dc2080a8de572a320407596b6fb 172.16.254.128:7008
slots: (0 slots) slave
replicates dae116bae245c1d9b94643c627243f0122dec6ad
M: dae116bae245c1d9b94643c627243f0122dec6ad 172.16.254.124:7004
slots:[0-1499],[2999-5460] (3962 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)? 4000
#要迁移的槽数,我们写4000
What is the receiving node ID? 5aa077444a506afad1ff796616124a0b7298c125
#接受数据的节点,我们指定一个节点172.16.254.126:7006的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: 57bfd28d400e95395b10fb62e77a2b9f9a05dddb
#要迁出数据的节点,既我们要删除的节点172.16.254.121的ID
查看集群信息:
redis-cli -c -h 172.16.254.124 -p 7004 cluster nodes
0600bacf8e9be9572c780090cb242e1e1e0206a2 172.16.254.129:7009@17009 slave a489464dfd1321cdbe2a0f464856218a38127e19 0 1614782942133 9 connected
a489464dfd1321cdbe2a0f464856218a38127e19 172.16.254.125:7005@17005 master - 0 1614782941000 9 connected 1500-2998 5461-11423
8205aa9f24882516bb8bbe0fb7d2c593271de152 172.16.254.127:7007@17007 slave 5aa077444a506afad1ff796616124a0b7298c125 0 1614782943282 13 connected
57bfd28d400e95395b10fb62e77a2b9f9a05dddb 172.16.254.121:7001@17001 master - 0 1614782940000 12 connected
dae116bae245c1d9b94643c627243f0122dec6ad 172.16.254.124:7004@17004 myself,master - 0 1614782938000 8 connected 0-1499 2999-5460
804ecd22d3de6dc2080a8de572a320407596b6fb 172.16.254.128:7008@17008 slave dae116bae245c1d9b94643c627243f0122dec6ad 0 1614782940629 8 connected
5aa077444a506afad1ff796616124a0b7298c125 172.16.254.126:7006@17006 master - 0 1614782939000 13 connected 11424-16383
可以看到172.16.254.121:7001已经没有了槽,如果有,要重复上一步迁移数据操作,将数据迁移完。
4、删除主节点
redis-cli --cluster del-node 172.16.254.121:7001 57bfd28d400e95395b10fb62e77a2b9f9a05dddb
>>> Removing node 57bfd28d400e95395b10fb62e77a2b9f9a05dddb from cluster 172.16.254.121:7001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
5、验证缩容
查看集群信息:
redis-cli -c -h 172.16.254.124 -p 7004 cluster nodes
0600bacf8e9be9572c780090cb242e1e1e0206a2 172.16.254.129:7009@17009 slave a489464dfd1321cdbe2a0f464856218a38127e19 0 1614783084000 9 connected
a489464dfd1321cdbe2a0f464856218a38127e19 172.16.254.125:7005@17005 master - 0 1614783086000 9 connected 1500-2998 5461-11423
8205aa9f24882516bb8bbe0fb7d2c593271de152 172.16.254.127:7007@17007 slave 5aa077444a506afad1ff796616124a0b7298c125 0 1614783087465 13 connected
dae116bae245c1d9b94643c627243f0122dec6ad 172.16.254.124:7004@17004 myself,master - 0 1614783086000 8 connected 0-1499 2999-5460
804ecd22d3de6dc2080a8de572a320407596b6fb 172.16.254.128:7008@17008 slave dae116bae245c1d9b94643c627243f0122dec6ad 0 1614783086411 8 connected
5aa077444a506afad1ff796616124a0b7298c125 172.16.254.126:7006@17006 master - 0 1614783084230 13 connected 11424-16383
可以看到从8节点的集群变成了6个节点,变成三主三从。