redis cluster節點/新增刪除操作

haoge0205發表於2020-07-14

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章