redis cluster增加和移除主从节点【转】

发布时间 2023-10-09 10:14:40作者: paul_hch

Redis-Cluster集群之Cluster节点增减

上篇我们了解了Redis的cluster集群的搭建,现在我们来说一下cluster集群的节点的增减

集群增加主节点

1.新建一个7006的一个节点,让其作为一个新的主节点加入,在/redis-cluster目录下,新建一个7006目录,配置相应的配置文件和数据目录,启动7006这个节点

[root@master /redis-cluster/7006]# ps -ef | grep redis
root 10280 1 0 Jan01 ? 00:00:47 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]
root 10284 1 0 Jan01 ? 00:00:48 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]
root 10288 1 0 Jan01 ? 00:00:47 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]
root 10292 1 0 Jan01 ? 00:00:48 /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]
root 10296 1 0 Jan01 ? 00:00:47 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]
root 10795 1 0 Jan01 ? 00:00:42 /usr/local/redis/bin/redis-server 127.0.0.1:7000 [cluster]
root 15728 1 0 10:07 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]


从上边我们可以看到7006的节点已经正常启动了

既然要将7006这个节点加入到集群中,那么我们就需要知道向集群中增加节点的命令

redis-trib.rb add-node是向集群加入节点的命令,新加入的节点就是127.0.0.1:7006,还需要指明加入到那个集群中,这里可以使用集群中的任何一个节点代表整个集群,来欢迎127.0.0.1:7006这个节点的加入

将7006节点加入到集群中

[root@master /redis-cluster/7006]# /usr/local/redis/bin/redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000
>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
S: 90f021de23019e7fe21b6b1bc27e3b620250cec6 127.0.0.1:7000
slots: (0 slots) slave
replicates ab44945684cfa74c277dc0f63c4491afd1fd2162
M: e297f582c94e12cbef91f6f3ae6548a61e6129da 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 127.0.0.1:7005
slots: (0 slots) slave
replicates e297f582c94e12cbef91f6f3ae6548a61e6129da
S: 958511d7c273152f2504c8d8c20ad175d8db074e 127.0.0.1:7004
slots: (0 slots) slave
replicates 8aa8e48d48f9bfe68ab9df58805f1891715bd60b
M: 8aa8e48d48f9bfe68ab9df58805f1891715bd60b 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: ab44945684cfa74c277dc0f63c4491afd1fd2162 127.0.0.1:7003
slots:0-5460 (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.
>>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster.
[OK] New node added correctly.

 

由执行命令系统返回的结果可以看出,7006节点成功加入到了集群当中

我们可以检查一下集群的状态

[root@master /redis-cluster/7006]# /usr/local/redis/bin/redis-trib.rb check 127.0.0.1:7006
>>> Performing Cluster Check (using node 127.0.0.1:7006)
M: c6056fdd214e92867bbafd2af8199988475fc82e 127.0.0.1:7006
slots: (0 slots) master #注意:这里分配的slot是0
0 additional replica(s)
M: 8aa8e48d48f9bfe68ab9df58805f1891715bd60b 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: e297f582c94e12cbef91f6f3ae6548a61e6129da 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 958511d7c273152f2504c8d8c20ad175d8db074e 127.0.0.1:7004
slots: (0 slots) slave
replicates 8aa8e48d48f9bfe68ab9df58805f1891715bd60b
S: fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 127.0.0.1:7005
slots: (0 slots) slave
replicates e297f582c94e12cbef91f6f3ae6548a61e6129da
S: 90f021de23019e7fe21b6b1bc27e3b620250cec6 127.0.0.1:7000
slots: (0 slots) slave
replicates ab44945684cfa74c277dc0f63c4491afd1fd2162
M: ab44945684cfa74c277dc0f63c4491afd1fd2162 127.0.0.1:7003
slots:0-5460 (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.

 

由上边的结果得知,7006这个节点已经成为了一个主节点了,但是7006这个节点分配的slot是0,也就是说7006这个节点虽然已经加入集群,但是还没有参与集群的负载

登录到集群的客户端也可以查询集群的节点状态

[root@master /redis-cluster/7006]# /usr/local/redis/bin/redis-cli -c -p 7006
127.0.0.1:7006> keys *
(empty list or set)
127.0.0.1:7006> cluster nodes
c6056fdd214e92867bbafd2af8199988475fc82e 127.0.0.1:7006 myself,master - 0 0 0 connected #7006已经成为一个主节点了
8aa8e48d48f9bfe68ab9df58805f1891715bd60b 127.0.0.1:7001 master - 0 1609600951030 2 connected 5461-10922
e297f582c94e12cbef91f6f3ae6548a61e6129da 127.0.0.1:7002 master - 0 1609600952052 3 connected 10923-16383
958511d7c273152f2504c8d8c20ad175d8db074e 127.0.0.1:7004 slave 8aa8e48d48f9bfe68ab9df58805f1891715bd60b 0 1609600951131 2 connected
fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 127.0.0.1:7005 slave e297f582c94e12cbef91f6f3ae6548a61e6129da 0 1609600952153 3 connected
90f021de23019e7fe21b6b1bc27e3b620250cec6 127.0.0.1:7000 slave ab44945684cfa74c277dc0f63c4491afd1fd2162 0 1609600952559 7 connected
ab44945684cfa74c277dc0f63c4491afd1fd2162 127.0.0.1:7003 master - 0 1609600952663 7 connected 0-5460
127.0.0.1:7006>

 

上边已经提到7006这个主节点分配的slot是0,这就需要我们手动将其他3个主节点分配的slot,向7006这个节点分配,那么.我们如何手动向7006这个节点分配slot呢?这里需要介绍一个命令

redis-trib.rb reshard 这个命令是用来迁移slot节点的,我们可以执行如下命令:

[root@master /redis-cluster/7006]# /usr/local/redis/bin/redis-trib.rb reshard 127.0.0.1:7005
>>> Performing Cluster Check (using node 127.0.0.1:7005)
S: fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 127.0.0.1:7005
slots: (0 slots) slave
replicates e297f582c94e12cbef91f6f3ae6548a61e6129da
S: 90f021de23019e7fe21b6b1bc27e3b620250cec6 127.0.0.1:7000
slots: (0 slots) slave
replicates ab44945684cfa74c277dc0f63c4491afd1fd2162
M: e297f582c94e12cbef91f6f3ae6548a61e6129da 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 8aa8e48d48f9bfe68ab9df58805f1891715bd60b 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: c6056fdd214e92867bbafd2af8199988475fc82e 127.0.0.1:7006
slots: (0 slots) master
0 additional replica(s)
S: 958511d7c273152f2504c8d8c20ad175d8db074e 127.0.0.1:7004
slots: (0 slots) slave
replicates 8aa8e48d48f9bfe68ab9df58805f1891715bd60b
M: ab44945684cfa74c277dc0f63c4491afd1fd2162 127.0.0.1:7003
slots:0-5460 (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)?

 

这个slot节点的迁移是交互式的,它问我们需要迁移多少slot,一般情况下,为了均衡,我们都是将slot平均分配,所以这里需要告诉系统,需要迁移4096个slots

How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID?

 

当我们输入4096回车后,又问,接受节点的ID是什么?通过上边我们可以知道7006这个节点的ID是c6056fdd214e92867bbafd2af8199988475fc82e

我们输入c6056fdd214e92867bbafd2af8199988475fc82e即可,输入后发现:

What is the receiving node ID? c6056fdd214e92867bbafd2af8199988475fc82e
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:

 

edis-trib 向你询问重新分片的源节点(source node), 也就是说, 要从哪个节点中取出 4096 个slot(哈希槽), 并将这些槽移动到7006节点上面。

如果我们不打算从特定的节点上取出指定数量的哈希槽, 那么可以向 redis-trib 输入 all , 这样的话, 集群中的所有主节点都会成为源节点, redis-trib 将从各个源节点中各取出一部分哈希槽, 凑够 4096 个, 然后移动到7006节点上:

Source node #1:all #输入all后回车

又会向你询问

Do you want to proceed with the proposed reshard plan (yes/no)? #你是否想继续执行建议的分片计划

 

这里输入yes后继续回车,回车后redis-trib就会执行重新分配slot的计划,分配完毕后我们查看

[root@master /redis-cluster/7006]# /usr/local/redis/bin/redis-trib.rb check 127.0.0.1:7006
>>> Performing Cluster Check (using node 127.0.0.1:7006)
M: c6056fdd214e92867bbafd2af8199988475fc82e 127.0.0.1:7006
slots:0-1364,5461-6826,10923-12287 (4096 slots) master #已经将其他三个主节点的部分slot分配到了7006上
0 additional replica(s)
M: 8aa8e48d48f9bfe68ab9df58805f1891715bd60b 127.0.0.1:7001
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: e297f582c94e12cbef91f6f3ae6548a61e6129da 127.0.0.1:7002
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: 958511d7c273152f2504c8d8c20ad175d8db074e 127.0.0.1:7004
slots: (0 slots) slave
replicates 8aa8e48d48f9bfe68ab9df58805f1891715bd60b
S: fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 127.0.0.1:7005
slots: (0 slots) slave
replicates e297f582c94e12cbef91f6f3ae6548a61e6129da
S: 90f021de23019e7fe21b6b1bc27e3b620250cec6 127.0.0.1:7000
slots: (0 slots) slave
replicates ab44945684cfa74c277dc0f63c4491afd1fd2162
M: ab44945684cfa74c277dc0f63c4491afd1fd2162 127.0.0.1:7003
slots:1365-5460 (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.

 

分配后7006的slot分配情况

M: c6056fdd214e92867bbafd2af8199988475fc82e 127.0.0.1:7006
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)

 

到现在为止,主节点已经增加完毕了

集群增加从节点


我们建立一个从节点7007,增加过程省略(和增加7006过程相同),将7007节点启动后,加入集群的从节点

redis-trib.rb add-node --slave这个命令是向集群增加从节点的命令,add-node是加入节点,–slave就是加入从节点,这样加入集群会产生一个问题,这个从节点会加在那个主节点身上?我们不确定,但是这里我们是给主节点7006加从节点的,所以,我们需要指定主节点,这是就需要参数**–master-id**了,指定主节点的ID号,问题就会迎刃而解了,执行加入集群的命令:

[root@master /redis-cluster/7007]# usr/local/redis/bin/redis-trib.rb add-node --slave --master-id127.0.0.1:7007 127.0.0.1:7006
>>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
S: 90f021de23019e7fe21b6b1bc27e3b620250cec6 127.0.0.1:7000
slots: (0 slots) slave
replicates ab44945684cfa74c277dc0f63c4491afd1fd2162
M: e297f582c94e12cbef91f6f3ae6548a61e6129da 127.0.0.1:7002
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 127.0.0.1:7005
slots: (0 slots) slave
replicates e297f582c94e12cbef91f6f3ae6548a61e6129da
S: 958511d7c273152f2504c8d8c20ad175d8db074e 127.0.0.1:7004
slots: (0 slots) slave
replicates 8aa8e48d48f9bfe68ab9df58805f1891715bd60b
M: 8aa8e48d48f9bfe68ab9df58805f1891715bd60b 127.0.0.1:7001
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: ab44945684cfa74c277dc0f63c4491afd1fd2162 127.0.0.1:7003
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: c6056fdd214e92867bbafd2af8199988475fc82e 127.0.0.1:7006
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster.
Waiting for the cluster to join....
>>> Configure node as replica of 127.0.0.1:7006.
[OK] New node added correctly.

 

从上边的信息我们可以看到7007已经加入到集群了

我们查看一下

[root@master /redis-cluster/7007]# /usr/local/redis/bin/redis-trib.rb check 127.0.0.1:7007
>>> Performing Cluster Check (using node 127.0.0.1:7007)
S: 7bd56cb6fd4bd37d6c66a12922d99336768d0f29 127.0.0.1:7007
slots: (0 slots) slave
replicates c6056fdd214e92867bbafd2af8199988475fc82e
M: c6056fdd214e92867bbafd2af8199988475fc82e 127.0.0.1:7006
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
M: e297f582c94e12cbef91f6f3ae6548a61e6129da 127.0.0.1:7002
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 127.0.0.1:7005
slots: (0 slots) slave
replicates e297f582c94e12cbef91f6f3ae6548a61e6129da
M: ab44945684cfa74c277dc0f63c4491afd1fd2162 127.0.0.1:7003
slots:1365-5460 (4096 slots) master
1 additional replica(s)
S: 90f021de23019e7fe21b6b1bc27e3b620250cec6 127.0.0.1:7000
slots: (0 slots) slave
replicates ab44945684cfa74c277dc0f63c4491afd1fd2162
S: 958511d7c273152f2504c8d8c20ad175d8db074e 127.0.0.1:7004
slots: (0 slots) slave
replicates 8aa8e48d48f9bfe68ab9df58805f1891715bd60b
M: 8aa8e48d48f9bfe68ab9df58805f1891715bd60b 127.0.0.1:7001
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.

 



我们通过查看可以看到7007节点已经如愿以偿的成为了7006的从节点了

集群移除一个主节点


集群中的节点又增加就有减少,那么如何移除一个节点呢?我们来操作一下

redis-trib.rb del-node这个命令就是移除集群中节点的命令,具体的命令格式是:

redis-trib.rb del-node 127.0.0.1:7002 node-id #127.0.0.1:7002和之前一样,代表的是这个集群,node-id是某个节点的id号

 

比如我们删除7002这个主节点

[root@master /redis-cluster]# redis-trib.rb del-node 127.0.0.1:7000 e297f582c94e12cbef91f6f3ae6548a61e6129da
>>> Removing node e297f582c94e12cbef91f6f3ae6548a61e6129da from cluster 127.0.0.1:7000
[ERR] Node 127.0.0.1:7002 is not empty! Reshard data away and try again.

 

提示我们报错了,它说7002这个主节点已经不是空节点了,需要从新分配数据再尝试,也就是说我们的slots需要重新分配,需要将7002的slots重新分配到其他的节点上,才能移除,那我们来重新分配一下,这个步骤和增加节点一样

[root@master /redis-cluster]# redis-trib.rb reshard 127.0.0.1:7001
......
How many slots do you want to move (from 1 to 16384)?

 

询问我们需要移动多少个哈希槽,经过查询可知,7002这个节点上有4096个slots,所以这里填写4096

How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID?

 

这里又询问我们,需要将7002节点的slots移动到那个ID上,我们就移动到7006这个节点上吧,注意:这里的接受节点必须是主节点,否则会提示让你重新输入

What is the receiving node ID? c6056fdd214e92867bbafd2af8199988475fc82e
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:

 

输入要接受slots的节点ID后,又询问从哪些节点上移动slots到7006上,这里我们写7002的ID,因为我们是要把7002上的slots移除

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: e297f582c94e12cbef91f6f3ae6548a61e6129da #7002的ID号
Source node #2:done #输入done,就会移除slot
............
Do you want to proceed with the proposed reshard plan (yes/no)? yes #这里输入yes
Moving slot 16383 from 127.0.0.1:7002 to 127.0.0.1:7006: #从这里就会看出7002的slots已经移动到了7006上

 

到这里为止slots的迁移就已经迁移完毕了,7002这个节点已经从集群中删除了,我们在查看一下

[root@master /redis-cluster]# redis-trib.rb check 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
S: 90f021de23019e7fe21b6b1bc27e3b620250cec6 127.0.0.1:7000
slots: (0 slots) slave
replicates ab44945684cfa74c277dc0f63c4491afd1fd2162
M: c6056fdd214e92867bbafd2af8199988475fc82e 127.0.0.1:7006
slots:0-1364,5461-6826,10923-16383 (8192 slots) master
2 additional replica(s)
M: e297f582c94e12cbef91f6f3ae6548a61e6129da 127.0.0.1:7002
slots: (0 slots) master
0 additional replica(s)
M: 8aa8e48d48f9bfe68ab9df58805f1891715bd60b 127.0.0.1:7001
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: ab44945684cfa74c277dc0f63c4491afd1fd2162 127.0.0.1:7003
slots:1365-5460 (4096 slots) master
1 additional replica(s)
S: fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 127.0.0.1:7005
slots: (0 slots) slave
replicates c6056fdd214e92867bbafd2af8199988475fc82e
S: 958511d7c273152f2504c8d8c20ad175d8db074e 127.0.0.1:7004
slots: (0 slots) slave
replicates 8aa8e48d48f9bfe68ab9df58805f1891715bd60b
S: 7bd56cb6fd4bd37d6c66a12922d99336768d0f29 127.0.0.1:7007
slots: (0 slots) slave
replicates c6056fdd214e92867bbafd2af8199988475fc82e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

通过查询7002节点上分配的slots已经全部移除,我们尝试连接一下7002

[root@master /redis-cluster]# redis-cli -c 127.0.0.1:7002
Could not connect to Redis at 127.0.0.1:6379: Connection refused

 

告诉我们已经连不上了…

集群移除一个从节点


移除从节点不需要考虑slots迁移的问题,这样的话,就简单多了,直接移除

比如我们这里移除7005这个节点

[root@master /redis-cluster]# redis-trib.rb del-node 127.0.0.1:7000 fec9e59d59a27456a4f69565ebd4f5ac1d7cb450
>>> Removing node fec9e59d59a27456a4f69565ebd4f5ac1d7cb450 from cluster 127.0.0.1:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

 

至此,Redis的Cluster集群中的增删节点,已经全部实验完成

转自
cluster redis 修改从节点 redis cluster增加节点
https://blog.51cto.com/u_16099226/6685998

还可以参考

从redis-trib.rb到集群加减节点实战-CSDN博客
https://blog.csdn.net/qq_40687433/article/details/131234885