Redis 5.0 cluster 添加和移除节点

添加节点

Redis 5.0 cluster 添加和移除节点_第1张图片
redis.png

搭建Redis集群文档: https://www.cnblogs.com/esofar/p/10486621.html

如果所示,之前的集群是三主三从的Redis集群,现在要加入一主一从,主从复制。主节点端口是7004,从节点端口是8004。

  1. 启动7004主节点
$ cp -r 7001 7004
$ sed -i 's/7001/7004/g' 7004/redis.conf

$ redis-server 7004/redis.conf
18329:C 13 Nov 2019 20:18:29.350 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18329:C 13 Nov 2019 20:18:29.350 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=18329, just started
18329:C 13 Nov 2019 20:18:29.350 # Configuration loaded

$ ps -ef|grep redis
root      4047     1  0 Nov11 ?        00:03:16 redis-server 127.0.0.1:7001 [cluster]
root      4049     1  0 Nov11 ?        00:03:16 redis-server 127.0.0.1:7002 [cluster]
root      4051     1  0 Nov11 ?        00:03:19 redis-server 127.0.0.1:7003 [cluster]
root      4053     1  0 Nov11 ?        00:03:11 redis-server 127.0.0.1:8001 [cluster]
root      4055     1  0 Nov11 ?        00:03:13 redis-server 127.0.0.1:8002 [cluster]
root      4078     1  0 Nov11 ?        00:03:13 redis-server 127.0.0.1:8003 [cluster]
root     18330     1  0 20:18 ?        00:00:00 redis-server 127.0.0.1:7004 [cluster]
root     18338 17880  0 20:18 pts/3    00:00:00 grep --color=auto redis
  1. 执行cluster meet ip port命令
$ redis-cli -p 7001
127.0.0.1:7001> cluster nodes
1d2a67580a2eaede84ad3517e426a4c97ec6c159 127.0.0.1:7002@17002 master - 0 1573647995000 9 connected 1365 5462-6825 6827-10922
00c7caca52b3c5e796b409e838ef3cf9ab8e35b6 127.0.0.1:7001@17001 myself,master - 0 1573647992000 8 connected 0-1364 1366-5461
3976e02a25edf7fa43b67e2e10b0be6710bc3c93 127.0.0.1:8001@18001 slave 00c7caca52b3c5e796b409e838ef3cf9ab8e35b6 0 1573647994817 8 connected
b2c80079e80191ca47e9dc88481c137e4f45ceaf 127.0.0.1:8003@18003 slave ea043d48e69fef7ece7faf18bb79aa200fd7e7fe 0 1573647995819 10 connected
37b60fd0c31f84fffc453baa3d48f41a86875b15 127.0.0.1:8002@18002 slave 1d2a67580a2eaede84ad3517e426a4c97ec6c159 0 1573647993814 9 connected
ea043d48e69fef7ece7faf18bb79aa200fd7e7fe 127.0.0.1:7003@17003 master - 0 1573647994000 10 connected 6826 10923-16383

# 没有7004

127.0.0.1:7001> cluster meet 127.0.0.1 7004
OK

127.0.0.1:7001> cluster nodes
1d2a67580a2eaede84ad3517e426a4c97ec6c159 127.0.0.1:7002@17002 master - 0 1573648075989 9 connected 1365 5462-6825 6827-10922
00c7caca52b3c5e796b409e838ef3cf9ab8e35b6 127.0.0.1:7001@17001 myself,master - 0 1573648074000 8 connected 0-1364 1366-5461
3976e02a25edf7fa43b67e2e10b0be6710bc3c93 127.0.0.1:8001@18001 slave 00c7caca52b3c5e796b409e838ef3cf9ab8e35b6 0 1573648077993 8 connected
28274e614eb52c3367564383592f698c34807daa 127.0.0.1:7004@17004 master - 0 1573648077000 0 connected
b2c80079e80191ca47e9dc88481c137e4f45ceaf 127.0.0.1:8003@18003 slave ea043d48e69fef7ece7faf18bb79aa200fd7e7fe 0 1573648076992 10 connected
37b60fd0c31f84fffc453baa3d48f41a86875b15 127.0.0.1:8002@18002 slave 1d2a67580a2eaede84ad3517e426a4c97ec6c159 0 1573648076000 9 connected
ea043d48e69fef7ece7faf18bb79aa200fd7e7fe 127.0.0.1:7003@17003 master - 0 1573648075000 10 connected 6826 10923-16383
127.0.0.1:7001>

# 有7004了
  1. 重新分槽

此时新加入的节点7004并没有数据,并且也没有被分配slot也就是 目前7004是不可用的,现在要重新分配原集群的slot

$ redis-cli --cluster reshard 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 00c7caca52b3c5e796b409e838ef3cf9ab8e35b6 127.0.0.1:7001
   slots:[0-1364],[1366-5461] (5461 slots) master
   1 additional replica(s)
M: 1d2a67580a2eaede84ad3517e426a4c97ec6c159 127.0.0.1:7002
   slots:[1365],[5462-6825],[6827-10922] (5461 slots) master
   1 additional replica(s)
S: 3976e02a25edf7fa43b67e2e10b0be6710bc3c93 127.0.0.1:8001
   slots: (0 slots) slave
   replicates 00c7caca52b3c5e796b409e838ef3cf9ab8e35b6
S: b2c80079e80191ca47e9dc88481c137e4f45ceaf 127.0.0.1:8003
   slots: (0 slots) slave
   replicates ea043d48e69fef7ece7faf18bb79aa200fd7e7fe
S: 37b60fd0c31f84fffc453baa3d48f41a86875b15 127.0.0.1:8002
   slots: (0 slots) slave
   replicates 1d2a67580a2eaede84ad3517e426a4c97ec6c159
M: ea043d48e69fef7ece7faf18bb79aa200fd7e7fe 127.0.0.1:7003
   slots:[6826],[10923-16383] (5462 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)?

迁移多少个slots呢,16384/4=4096,那么就迁移4096个。

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

这里是选择接收方的node id,就是执行cluster nodes显示7004的字符串。

How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? 8cb1f2459e0f8a18147c375495c8de0e6e6ebe65
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

这里是选择slot的迁出方,我们这边选择的是all,也就是所有的节点,总共迁出4096个slot到7004.

中间会让你输入yes

....
Moving slot 12277 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12278 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12279 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12280 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12281 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12282 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12283 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12284 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12285 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12286 from 127.0.0.1:7003 to 127.0.0.1:7004:
Moving slot 12287 from 127.0.0.1:7003 to 127.0.0.1:7004:

一顿操作之后

$ redis-cli -p 7004
127.0.0.1:7004> role
1) "master"
2) (integer) 0
3) (empty list or set)
127.0.0.1:7004> cluster nodes
d35b37a277963f5f820ec0f89fd8e39407fe60d4 127.0.0.1:7002@17002 master - 0 1573648988000 2 connected 6827-10922
608d58a9d6400edb219c8311974209ebd7ea8b53 127.0.0.1:8002@18002 slave bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 0 1573648989239 1 connected
f67558884901a579f76cb024ce53feffab3f1a89 127.0.0.1:8003@18003 slave d35b37a277963f5f820ec0f89fd8e39407fe60d4 0 1573648986233 2 connected
b058b190620fb7a37cb4db87756f8c0a46ac5dc7 127.0.0.1:7003@17003 master - 0 1573648987000 3 connected 12288-16383
a202c46c84eb025e667a31d9b8706bb9391c98f1 127.0.0.1:8001@18001 slave b058b190620fb7a37cb4db87756f8c0a46ac5dc7 0 1573648987235 3 connected
8cb1f2459e0f8a18147c375495c8de0e6e6ebe65 127.0.0.1:7004@17004 myself,master - 0 1573648986000 7 connected 0-1364 5461-6826 10923-12287
bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 127.0.0.1:7001@17001 master - 0 1573648989000 1 connected 1365-5460

我们看到7004已经有slot了0-1364 5461-6826 10923-12287

  1. 启动8004主从复制
$ cp -r 8001 8004
$ sed -i 's/8001/8004/g' 8004/redis.conf
$ redis-server 8004/redis.conf
$ redis-cli --cluster add-node 127.0.0.1:8004 127.0.0.1:7004 --cluster-slave --cluster-master-id 8cb1f2459e0f8a18147c375495c8de0e6e6ebe65
>>> Adding node 127.0.0.1:8004 to cluster 127.0.0.1:7004
>>> Performing Cluster Check (using node 127.0.0.1:7004)
M: 8cb1f2459e0f8a18147c375495c8de0e6e6ebe65 127.0.0.1:7004
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: d35b37a277963f5f820ec0f89fd8e39407fe60d4 127.0.0.1:7002
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 608d58a9d6400edb219c8311974209ebd7ea8b53 127.0.0.1:8002
   slots: (0 slots) slave
   replicates bedbddc103e3abe62d20e43d5e70ad8de3b0d00a
S: f67558884901a579f76cb024ce53feffab3f1a89 127.0.0.1:8003
   slots: (0 slots) slave
   replicates d35b37a277963f5f820ec0f89fd8e39407fe60d4
M: b058b190620fb7a37cb4db87756f8c0a46ac5dc7 127.0.0.1:7003
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: a202c46c84eb025e667a31d9b8706bb9391c98f1 127.0.0.1:8001
   slots: (0 slots) slave
   replicates b058b190620fb7a37cb4db87756f8c0a46ac5dc7
M: bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 127.0.0.1:7001
   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.
>>> Send CLUSTER MEET to node 127.0.0.1:8004 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 127.0.0.1:7004.
[OK] New node added correctly.

输入yes之后主从复制就完成了。我们看一下节点没问题了。

$ redis-cli -p 7001
127.0.0.1:7001> cluster nodes
b058b190620fb7a37cb4db87756f8c0a46ac5dc7 127.0.0.1:7003@17003 master - 0 1573649288000 3 connected 12288-16383
a202c46c84eb025e667a31d9b8706bb9391c98f1 127.0.0.1:8001@18001 slave b058b190620fb7a37cb4db87756f8c0a46ac5dc7 0 1573649290000 4 connected
8cb1f2459e0f8a18147c375495c8de0e6e6ebe65 127.0.0.1:7004@17004 master - 0 1573649291656 7 connected 0-1364 5461-6826 10923-12287
d35b37a277963f5f820ec0f89fd8e39407fe60d4 127.0.0.1:7002@17002 master - 0 1573649290654 2 connected 6827-10922
bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 127.0.0.1:7001@17001 myself,master - 0 1573649288000 1 connected 1365-5460
f67558884901a579f76cb024ce53feffab3f1a89 127.0.0.1:8003@18003 slave d35b37a277963f5f820ec0f89fd8e39407fe60d4 0 1573649289000 6 connected
19f932d77a2621753eb4fd3b8a0c2bfb51d0bc87 127.0.0.1:8004@18004 slave 8cb1f2459e0f8a18147c375495c8de0e6e6ebe65 0 1573649288000 7 connected
608d58a9d6400edb219c8311974209ebd7ea8b53 127.0.0.1:8002@18002 slave bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 0 1573649289652 5 connected

移除节点

redis 集群多了之后,发现现在的有些节点有些多余没用,或者达不到用那么多节点的情况,那么就可以进行 节点的移除, 5.0 版本由于不用 ruby 进行集群操作了,所以方式跟5.0以前有很多不同的地方

我们由于刚刚添加了2个节点,现在把这2个节点删除,需要注意的是,假如你要下线节点7004,节点8004,请务必先下线从节点,并且把节点7004的slot的迁移到其他节点,如果先线下节点7004的话 会发产生故障切换,节点8004成主节点了。

  1. 移除从节点8004
$ redis-cli --cluster del-node 127.0.0.1:8004 19f932d77a2621753eb4fd3b8a0c2bfb51d0bc87
>>> Removing node 19f932d77a2621753eb4fd3b8a0c2bfb51d0bc87 from cluster 127.0.0.1:8004
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

查看一下,已经没有节点8004了

 redis-cli -p 7001 cluster nodes
b058b190620fb7a37cb4db87756f8c0a46ac5dc7 127.0.0.1:7003@17003 master - 0 1573649672000 3 connected 12288-16383
a202c46c84eb025e667a31d9b8706bb9391c98f1 127.0.0.1:8001@18001 slave b058b190620fb7a37cb4db87756f8c0a46ac5dc7 0 1573649671420 4 connected
8cb1f2459e0f8a18147c375495c8de0e6e6ebe65 127.0.0.1:7004@17004 master - 0 1573649671000 7 connected 0-1364 5461-6826 10923-12287
d35b37a277963f5f820ec0f89fd8e39407fe60d4 127.0.0.1:7002@17002 master - 0 1573649670417 2 connected 6827-10922
bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 127.0.0.1:7001@17001 myself,master - 0 1573649668000 1 connected 1365-5460
f67558884901a579f76cb024ce53feffab3f1a89 127.0.0.1:8003@18003 slave d35b37a277963f5f820ec0f89fd8e39407fe60d4 0 1573649672421 6 connected
608d58a9d6400edb219c8311974209ebd7ea8b53 127.0.0.1:8002@18002 slave bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 0 1573649669000 5 connected

接着删除7004,这个是一个主节点,删除起来比较麻烦一点,因为主节点分配了slot槽, 所以这里我们必须先把 7004的slot槽放到其他的可用节点中去,然后再进行移除节点操作才行,不然会出现数据丢失问题

由于节点7004有4096个slot,那么3个节点平均可以分配4096/3=1365.3,不能有小数,那么我们可以分别分配1365,1365,13667001,7002,7003节点

$ redis-cli --cluster reshard 127.0.0.1:7004
>>> Performing Cluster Check (using node 127.0.0.1:7004)
M: 8cb1f2459e0f8a18147c375495c8de0e6e6ebe65 127.0.0.1:7004
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: d35b37a277963f5f820ec0f89fd8e39407fe60d4 127.0.0.1:7002
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 608d58a9d6400edb219c8311974209ebd7ea8b53 127.0.0.1:8002
   slots: (0 slots) slave
   replicates bedbddc103e3abe62d20e43d5e70ad8de3b0d00a
S: f67558884901a579f76cb024ce53feffab3f1a89 127.0.0.1:8003
   slots: (0 slots) slave
   replicates d35b37a277963f5f820ec0f89fd8e39407fe60d4
M: b058b190620fb7a37cb4db87756f8c0a46ac5dc7 127.0.0.1:7003
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: a202c46c84eb025e667a31d9b8706bb9391c98f1 127.0.0.1:8001
   slots: (0 slots) slave
   replicates b058b190620fb7a37cb4db87756f8c0a46ac5dc7
M: bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 127.0.0.1:7001
   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.
How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node ID? bedbddc103e3abe62d20e43d5e70ad8de3b0d00a

这里输入1365 和 接收者7001node 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:

这里输入你想从那个节点移走数据,我们选7004node id

How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node ID? bedbddc103e3abe62d20e43d5e70ad8de3b0d00a
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: 8cb1f2459e0f8a18147c375495c8de0e6e6ebe65
Source node #2:

其实是让你还可以选择源节点,但是我这里只要把7004 节点分到其它地方就行,如果没有再把其它主节点分摊的需要,此处输入 done即可,否则输入其它节点的ID,最后输入 done

Do you want to proceed with the proposed reshard plan (yes/no)?

是否按照计划分配,输入yes

...
Moving slot 1358 from 127.0.0.1:7004 to 127.0.0.1:7001:
Moving slot 1359 from 127.0.0.1:7004 to 127.0.0.1:7001:
Moving slot 1360 from 127.0.0.1:7004 to 127.0.0.1:7001:
Moving slot 1361 from 127.0.0.1:7004 to 127.0.0.1:7001:
Moving slot 1362 from 127.0.0.1:7004 to 127.0.0.1:7001:
Moving slot 1363 from 127.0.0.1:7004 to 127.0.0.1:7001:
Moving slot 1364 from 127.0.0.1:7004 to 127.0.0.1:7001:

完成!!!

然后继续向另外2个节点分配slot
。。。。

我们最后查看

$ redis-cli -p 7001 cluster nodes
b058b190620fb7a37cb4db87756f8c0a46ac5dc7 127.0.0.1:7003@17003 master - 0 1573650334000 10 connected 6826 10923-16383
a202c46c84eb025e667a31d9b8706bb9391c98f1 127.0.0.1:8001@18001 slave b058b190620fb7a37cb4db87756f8c0a46ac5dc7 0 1573650332000 10 connected
8cb1f2459e0f8a18147c375495c8de0e6e6ebe65 127.0.0.1:7004@17004 master - 0 1573650335800 7 connected
d35b37a277963f5f820ec0f89fd8e39407fe60d4 127.0.0.1:7002@17002 master - 0 1573650334798 9 connected 5461-6825 6827-10922
bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 127.0.0.1:7001@17001 myself,master - 0 1573650334000 8 connected 0-5460
f67558884901a579f76cb024ce53feffab3f1a89 127.0.0.1:8003@18003 slave d35b37a277963f5f820ec0f89fd8e39407fe60d4 0 1573650333796 9 connected
608d58a9d6400edb219c8311974209ebd7ea8b53 127.0.0.1:8002@18002 slave bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 0 1573650332000 8 connected

7004已经没有slot了,那么就可以删除了节点7004了

$ redis-cli --cluster del-node 127.0.0.1:7004 8cb1f2459e0f8a18147c375495c8de0e6e6ebe65
>>> Removing node 8cb1f2459e0f8a18147c375495c8de0e6e6ebe65 from cluster 127.0.0.1:7004
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

查看,完美!

$ redis-cli -p 7001 cluster nodes
b058b190620fb7a37cb4db87756f8c0a46ac5dc7 127.0.0.1:7003@17003 master - 0 1573650413000 10 connected 6826 10923-16383
a202c46c84eb025e667a31d9b8706bb9391c98f1 127.0.0.1:8001@18001 slave b058b190620fb7a37cb4db87756f8c0a46ac5dc7 0 1573650411000 10 connected
d35b37a277963f5f820ec0f89fd8e39407fe60d4 127.0.0.1:7002@17002 master - 0 1573650413000 9 connected 5461-6825 6827-10922
bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 127.0.0.1:7001@17001 myself,master - 0 1573650411000 8 connected 0-5460
f67558884901a579f76cb024ce53feffab3f1a89 127.0.0.1:8003@18003 slave d35b37a277963f5f820ec0f89fd8e39407fe60d4 0 1573650413964 9 connected
608d58a9d6400edb219c8311974209ebd7ea8b53 127.0.0.1:8002@18002 slave bedbddc103e3abe62d20e43d5e70ad8de3b0d00a 0 1573650414966 8 connected

  • https://blog.csdn.net/woyixinyiyi/article/details/88290266
  • https://blog.csdn.net/a826193310/article/details/102579395

你可能感兴趣的:(Redis 5.0 cluster 添加和移除节点)