ITPub博客

首页 > 数据库 > NoSQL > redis cluster节点/添加删除操作

redis cluster节点/添加删除操作

原创 NoSQL 作者:haoge0205 时间:2020-07-14 11:32:33 0 删除 编辑

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、添加主点(redis cluster 扩容迁移槽和数据不影响其他节点读写)

[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.13.10.77:2002   //下面是主要过程 

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   //确认重新分

 

或者 输入"源节点ID",最后用 done 结束

输入源节点ID,这里分别输入2002,3003,4004三个节点ID,最后用done结束

Source node #1: cfb28ef1deee4e0fa78da86abe5d24566744411e 

Source node #2: 8e41673d59c9568aa9d29fb174ce733345b3e8f1 

Source node #3: 40b8d09d44294d2e23c7c768efc8fcd153446746 

Source node #4: done

  

新添加的主节点是没有slots的

1600a0f3ae06f801a01be64dbda341d6a25c659a 新添加的主节点,主节点如果没有slots的话,存取数据是不会被选中

可以把分配的过程理解成打pukepai,all表示大家重新洗牌; 输入某个主节点的node-id,然后在输入done的话,就好比从某个节点抽牌

 

迁移之后用redis-trib.rb rebalance命令检查节点之间槽的均衡性

[root@192-13-10-77 conf]# redis-trib.rb rebalance 127.0.0.1:2002

>>> Performing Cluster Check (using node 127.0.0.1:2002)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Rebalancing across 3 nodes. Total weight = 3

Moving 1366 slots from 127.0.0.1:2002 to 192.13.10.77:5005

######################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################

Moving 1365 slots from 127.0.0.1:2002 to 192.13.10.77:3003

#####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################

[root@192-13-10-77 conf]# redis-trib.rb rebalance 127.0.0.1:2002

>>> Performing Cluster Check (using node 127.0.0.1:2002)

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

*** No rebalancing needed! All nodes are within the 2.0% threshold.(槽数据量差异在2%以内,集群节点数据相对均衡,无需在调整)


 

 查看集群状态:

[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主节点(redis cluster 最好使用redis-trib.rb del-node删除节点,尽量不要用cluster forget)

[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


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28939273/viewspace-2704328/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-11-28

  • 博文量
    300
  • 访问量
    1784193