redis cluster 添加/删除节点操作
Posted Hank.Yoon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis cluster 添加/删除节点操作相关的知识,希望对你有一定的参考价值。
RedisCluster 添加/删除节点
添加节点
新配置两个测试节点8008和9009
[root@192-13-10-77 ~]# /usr/local/redis-4.0.9/bin/redis-server /u02/redis/8008/conf/redis_8008.conf [root@192-13-10-77 ~]# /usr/local/redis-4.0.9/bin/redis-server /u02/redis/9009/conf/redis_9009.conf
1、添加主点
[root@192-13-10-77 ~]# redis-trib.rb add-node 192.13.10.77:8008 192.13.10.77:2002 >>> Adding node 192.13.10.77:8008 to cluster 192.13.10.77:2002 >>> Performing Cluster Check (using node 192.13.10.77:2002) M: d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.10.77:2002 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.10.77:4004 slots: (0 slots) slave replicates 1dba8274e36bde79a215a77d1f241ae6fc81c03e S: 46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.10.77:6006 slots: (0 slots) slave replicates ef76f232efb578249e8d0ec8fef8ec02b3524010 M: ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.10.77:3003 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 1dba8274e36bde79a215a77d1f241ae6fc81c03e 192.13.10.77:7007 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.10.77:5005 slots: (0 slots) slave replicates d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 192.13.10.77:8008 to make it join the cluster.
注释:
192.13.10.77:8008 新添加的节点
192.13.10.77:2002 集群任意一个节点
查看集群状态:
[root@192-13-10-77 ~]# redis-cli -h 192.13.10.77 -p 2002 -c -a "ysBhqkYHDifB" cluster nodes 06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.10.77:4004@14004 slave 1dba8274e36bde79a215a77d1f241ae6fc81c03e 0 1575526516000 9 connected d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.10.77:2002@12002 myself,master - 0 1575526514000 1 connected 0-5460 46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.10.77:6006@16006 slave ef76f232efb578249e8d0ec8fef8ec02b3524010 0 1575526519000 5 connected ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.10.77:3003@13003 master - 0 1575526518000 2 connected 5461-10922 1dba8274e36bde79a215a77d1f241ae6fc81c03e 192.13.10.77:7007@17007 master - 0 1575526519605 9 connected 10923-16383 b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.10.77:5005@15005 slave d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 0 1575526518605 4 connected 1600a0f3ae06f801a01be64dbda341d6a25c659a 192.13.10.77:8008@18008 master - 0 1575526517000 0 connected
添加从节点:
[root@192-13-10-77 ~]# redis-trib.rb add-node --slave --master-id 1600a0f3ae06f801a01be64dbda341d6a25c659a 192.13.10.77:9009 192.13.10.77:2002 >>> Adding node 192.13.10.77:9009 to cluster 192.13.10.77:2002 >>> Performing Cluster Check (using node 192.13.10.77:2002) M: d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.10.77:2002 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.10.77:4004 slots: (0 slots) slave replicates 1dba8274e36bde79a215a77d1f241ae6fc81c03e S: 46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.10.77:6006 slots: (0 slots) slave replicates ef76f232efb578249e8d0ec8fef8ec02b3524010 M: ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.10.77:3003 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 1dba8274e36bde79a215a77d1f241ae6fc81c03e 192.13.10.77:7007 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.10.77:5005 slots: (0 slots) slave replicates d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 M: 1600a0f3ae06f801a01be64dbda341d6a25c659a 192.13.10.77:8008 slots: (0 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 192.13.10.77:9009 to make it join the cluster. Waiting for the cluster to join. >>> Configure node as replica of 192.13.10.77:8008. [OK] New node added correctly.
注释:
--slave 表示添加的是从节点
--master-id 表示主节点的node-id,这里的node-id是前面刚添加的8008端口的node-id
192.13.10.77:9009 新添加的从节点
192.13.10.77:2002 集群中任意一个节点
重新分配slot
[root@192-13-10-77 ~]# redis-trib.rb reshard 192.168.10.219:6378 //下面是主要过程
How many slots do you want to move (from 1 to 16384)? 4096 //设置slot数4096
What is the receiving node ID? 1600a0f3ae06f801a01be64dbda341d6a25c659a //新节点node 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:all //表示全部节点重新洗牌
Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认重新分
新添加的主节点是没有slots的
1600a0f3ae06f801a01be64dbda341d6a25c659a 新添加的主节点,主节点如果没有slots的话,存取数据是不会被选中
可以把分配的过程理解成打扑克牌,all表示大家重新洗牌; 输入某个主节点的node-id,然后在输入done的话,就好比从某个节点抽牌
查看集群状态:
[root@192-13-10-77 ~]# redis-cli -h 192.13.10.77 -p 2002 -c cluster nodes 1600a0f3ae06f801a01be64dbda341d6a25c659a 192.13.10.77:8008@18008 master - 0 1575530380000 10 connected 0-2332 5461-8137 10923-13598 b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.10.77:5005@15005 slave d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 0 1575530379946 4 connected d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.10.77:2002@12002 myself,master - 0 1575530380000 1 connected 2333-5460 ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.10.77:3003@13003 master - 0 1575530378000 2 connected 8138-10922 4e1e8dcfe075e61ec92d0f922ec12bdf7e71ee2c 192.13.10.77:9009@19009 slave 1600a0f3ae06f801a01be64dbda341d6a25c659a 0 1575530380000 10 connected 1dba8274e36bde79a215a77d1f241ae6fc81c03e 192.13.10.77:7007@17007 master - 0 1575530379000 9 connected 13599-16383 06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.10.77:4004@14004 slave 1dba8274e36bde79a215a77d1f241ae6fc81c03e 0 1575530381950 9 connected 46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.10.77:6006@16006 slave ef76f232efb578249e8d0ec8fef8ec02b3524010 0 1575530380948 5 connected
检查redis状态
[root@192-13-10-77 conf]# redis-trib.rb check 192.13.10.77:2002 >>> Performing Cluster Check (using node 192.13.10.77:2002) M: e9883dd5b69227eb045ef85e0d95b5374c74b96a 192.13.10.77:2002 slots:0-4095 (4096 slots) master 1 additional replica(s) S: a73c6e9c3540ab1630ed18233f08182cba328219 192.13.10.77:9009 slots: (0 slots) slave replicates a86fa76d2279c1f5685500a8b60ea2a600fc19f2 M: b3966e1ed00ae4616077577f24eab2d5b13ace7a 192.13.10.77:5005 slots:12288-16383 (4096 slots) master 1 additional replica(s) S: 64538f2f5ec58bedb344e4b5f0ac540d9c09f506 192.13.10.77:8008 slots: (0 slots) slave replicates e9883dd5b69227eb045ef85e0d95b5374c74b96a M: 853a34de3d9c9347c5deac1127d3d3fe09788c2e 192.13.10.77:3003 slots:4096-8191 (4096 slots) master 1 additional replica(s) M: a86fa76d2279c1f5685500a8b60ea2a600fc19f2 192.13.10.77:4004 slots:8192-12287 (4096 slots) master 1 additional replica(s) S: 0268ecaf869839aac9cab7b9d96c7fbac7df04d6 192.13.10.77:6006 slots: (0 slots) slave replicates 853a34de3d9c9347c5deac1127d3d3fe09788c2e S: 7070789bbcb15b6845d0aa6c4e1bc99be1c1d6ce 192.13.10.77:7007 slots: (0 slots) slave replicates b3966e1ed00ae4616077577f24eab2d5b13ace7a [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
redis cluster 4.0.9版本若有密码,无法分配slots。 因此若有密码必须先删除密码
删除节点
删除从节点(若有密码无法操作)
[root@192-13-10-77 conf]# redis-trib.rb del-node 192.13.10.77:9009 a86fa76d2279c1f5685500a8b60ea2a600fc19f2 >>> Removing node a86fa76d2279c1f5685500a8b60ea2a600fc19f2 from cluster 192.13.10.77:9009 [ERR] Node 192.13.10.77:4004 is not empty! Reshard data away and try again.
查看4004主节点存在的slots数
[root@192-13-10-77 conf]# redis-trib.rb check 192.13.10.77:9009 >>> Performing Cluster Check (using node 192.13.10.77:9009) S: a73c6e9c3540ab1630ed18233f08182cba328219 192.13.10.77:9009 slots: (0 slots) slave replicates a86fa76d2279c1f5685500a8b60ea2a600fc19f2 S: 64538f2f5ec58bedb344e4b5f0ac540d9c09f506 192.13.10.77:8008 slots: (0 slots) slave replicates e9883dd5b69227eb045ef85e0d95b5374c74b96a M: b3966e1ed00ae4616077577f24eab2d5b13ace7a 192.13.10.77:5005 slots:12288-16383 (4096 slots) master 1 additional replica(s) S: 7070789bbcb15b6845d0aa6c4e1bc99be1c1d6ce 192.13.10.77:7007 slots: (0 slots) slave replicates b3966e1ed00ae4616077577f24eab2d5b13ace7a M: a86fa76d2279c1f5685500a8b60ea2a600fc19f2 192.13.10.77:4004 slots:8192-12287 (4096 slots) master 1 additional replica(s) M: e9883dd5b69227eb045ef85e0d95b5374c74b96a 192.13.10.77:2002 slots:0-4095 (4096 slots) master 1 additional replica(s) S: 0268ecaf869839aac9cab7b9d96c7fbac7df04d6 192.13.10.77:6006 slots: (0 slots) slave replicates 853a34de3d9c9347c5deac1127d3d3fe09788c2e M: 853a34de3d9c9347c5deac1127d3d3fe09788c2e 192.13.10.77:3003 slots:4096-8191 (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.
删除从节点9009
[root@192-13-10-77 conf]# redis-trib.rb del-node 192.13.10.77:9009 a73c6e9c3540ab1630ed18233f08182cba328219 >>> Removing node a73c6e9c3540ab1630ed18233f08182cba328219 from cluster 192.13.10.77:9009 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
删除主节点的slots数
[root@192-13-10-77 conf]# redis-trib.rb reshard 192.13.10.77:4004
>>> Performing Cluster Check (using node 192.13.10.77:4004)
M: a86fa76d2279c1f5685500a8b60ea2a600fc19f2 192.13.10.77:4004
slots:8192-12287 (4096 slots) master
1 additional replica(s)
S: 64538f2f5ec58bedb344e4b5f0ac540d9c09f506 192.13.10.77:8008
slots: (0 slots) slave
replicates e9883dd5b69227eb045ef85e0d95b5374c74b96a
S: a73c6e9c3540ab1630ed18233f08182cba328219 192.13.10.77:9009
slots: (0 slots) slave
replicates a86fa76d2279c1f5685500a8b60ea2a600fc19f2
M: b3966e1ed00ae4616077577f24eab2d5b13ace7a 192.13.10.77:5005
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: 0268ecaf869839aac9cab7b9d96c7fbac7df04d6 192.13.10.77:6006
slots: (0 slots) slave
replicates 853a34de3d9c9347c5deac1127d3d3fe09788c2e
M: e9883dd5b69227eb045ef85e0d95b5374c74b96a 192.13.10.77:2002
slots:0-4095 (4096 slots) master
1 additional replica(s)
M: 853a34de3d9c9347c5deac1127d3d3fe09788c2e 192.13.10.77:3003
slots:4096-8191 (4096 slots) master
1 additional replica(s)
S: 7070789bbcb15b6845d0aa6c4e1bc99be1c1d6ce 192.13.10.77:7007
slots: (0 slots) slave
replicates b3966e1ed00ae4616077577f24eab2d5b13ace7a
[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)? 4096 //删除的slots数
What is the receiving node ID? e9883dd5b69227eb045ef85e0d95b5374c74b96a // 接收的node-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:a86fa76d2279c1f5685500a8b60ea2a600fc19f2 //被删除master的node-id
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes //取消slot后,reshard
删除4004主节点
[root@192-13-10-77 conf]# redis-trib.rb del-node 192.13.10.77:4004 a86fa76d2279c1f5685500a8b60ea2a600fc19f2 >>> Removing node a86fa76d2279c1f5685500a8b60ea2a600fc19f2 from cluster 192.13.10.77:4004 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
查看集群状态
[root@192-13-10-77 conf]# redis-cli -h 192.13.10.77 -p 2002 -c cluster nodes b3966e1ed00ae4616077577f24eab2d5b13ace7a 192.13.10.77:5005@15005 master - 0 1575550836000 11 connected 12288-16383 64538f2f5ec58bedb344e4b5f0ac540d9c09f506 192.13.10.77:8008@18008 slave e9883dd5b69227eb045ef85e0d95b5374c74b96a 0 1575550836409 12 connected 853a34de3d9c9347c5deac1127d3d3fe09788c2e 192.13.10.77:3003@13003 master - 0 1575550835406 2 connected 4096-8191 e9883dd5b69227eb045ef85e0d95b5374c74b96a 192.13.10.77:2002@12002 myself,master - 0 1575550835000 12 connected 0-4095 8192-12287 0268ecaf869839aac9cab7b9d96c7fbac7df04d6 192.13.10.77:6006@16006 slave 853a34de3d9c9347c5deac1127d3d3fe09788c2e 0 1575550837410 5 connected 7070789bbcb15b6845d0aa6c4e1bc99be1c1d6ce 192.13.10.77:7007@17007 slave b3966e1ed00ae4616077577f24eab2d5b13ace7a 0 1575550836000 11 connected
以上是关于redis cluster 添加/删除节点操作的主要内容,如果未能解决你的问题,请参考以下文章
redis cluster模式key的模糊删除-java操作
redis cluster模式key的模糊删除-java操作