溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

發(fā)布時間:2022-04-22 10:12:43 來源:億速云 閱讀:357 作者:zzz 欄目:關(guān)系型數(shù)據(jù)庫

這篇“Redis Cluster集群收縮主從節(jié)點(diǎn)的方法”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Redis Cluster集群收縮主從節(jié)點(diǎn)的方法”文章吧。

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

1.Cluster集群收縮概念

當(dāng)項目壓力承載力過高時,需要增加節(jié)點(diǎn)來提高負(fù)載,當(dāng)項目壓力不是很大時,也希望能夠?qū)⒓菏湛s下來,給其他項目使用,這就要用到集群收縮了

集群收縮操作和集群擴(kuò)容是一樣的,只需要把方向反過來即可。

擴(kuò)容的時候執(zhí)行一次命令就可以實(shí)現(xiàn)槽位遷移成功,而收縮的時候有幾個主節(jié)點(diǎn)就需要執(zhí)行多少次,比如除去要下線的節(jié)點(diǎn),還有3個主節(jié)點(diǎn),那么就需要執(zhí)行三次,填寫遷移出槽位的數(shù)量也需要除以3,每個節(jié)點(diǎn)也需要平均分配。

收縮的時候首先要填寫分出多少個槽位,然后填寫要分給誰,最后填寫從哪分出槽位,一般分多少個槽位,就需要看要下線的主機(jī)上有多少個槽位,然后除以集群主節(jié)點(diǎn)數(shù),使每一個主機(jī)點(diǎn)分到的槽位都是相同的,填寫要分配給誰的時候,第一次填寫第一個主節(jié)點(diǎn)的ID,第二次填寫第二個主節(jié)點(diǎn)的ID,最后填寫提供槽位的節(jié)點(diǎn)ID,就是下線節(jié)點(diǎn)的ID號。

集群收縮擴(kuò)容槽位的時候不會影響數(shù)據(jù)的使用。

集群收縮的源端就是要下線的主節(jié)點(diǎn),目標(biāo)端就是在線的主節(jié)點(diǎn)(分配給誰的節(jié)點(diǎn))。
咱們要清楚一點(diǎn),只有主節(jié)點(diǎn)是有槽位的,因此呢需要將主節(jié)點(diǎn)的槽位分配給其他主節(jié)點(diǎn),當(dāng)槽位清空后,這個主機(jī)節(jié)點(diǎn)就可以下線了。

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法
收縮集群前后對比圖
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

集群收縮操作步驟:

1.執(zhí)行reshard命令將需要下線的主節(jié)點(diǎn)進(jìn)行槽位分散。

2.有幾個主節(jié)點(diǎn)就需要執(zhí)行幾次reshard命令,首先填寫要分出的槽位數(shù),然后填寫分給誰,最后填寫從哪里分。

3.當(dāng)槽位分散完成后,要下線的主節(jié)點(diǎn)沒有任何數(shù)據(jù)時,將節(jié)點(diǎn)從集群中刪除。

集群信息

目前集群時四主四從共8個節(jié)點(diǎn),我們需要將集群改為三主三從,收縮出兩個節(jié)點(diǎn)給其他程序使用。
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

2.將6390主節(jié)點(diǎn)從集群中收縮

2.1.計算需要分給每一個節(jié)點(diǎn)的槽位數(shù)

可以看到6390節(jié)點(diǎn)上有4096個槽位,刪除要下線的6390節(jié)點(diǎn)后,我們還有3個主節(jié)點(diǎn),4096除3得到1365,分配槽位的時候給每個節(jié)點(diǎn)分配1365個槽位即可均勻。
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

2.2.分配1365個槽位給192.168.81.210的6380節(jié)點(diǎn)

我們需要將192.168.81.240的6390節(jié)點(diǎn)分出1365個槽位給192.168.81.210的6380節(jié)點(diǎn)。

只需要把What is the receiving node ID填寫成192.168.81.210的6380節(jié)點(diǎn)ID即可,指的是分配出來的槽位要給誰。

然后source node填寫192.168.81.240的6390節(jié)點(diǎn)的ID,這里指的是從哪個節(jié)點(diǎn)上分出1365個槽位,填寫ID后,回車后會提示還要從哪個節(jié)點(diǎn)上分配槽位,因?yàn)橹挥?390需要分出槽位,所以在這里填寫done,表示只有這個一個節(jié)點(diǎn)分出1365個槽位給其他節(jié)點(diǎn)。

[root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb reshard 192.168.81.210:6380
How many slots do you want to move (from 1 to 16384)? 1365				#分配出多少個槽位

What is the receiving node ID? 80e256579658eb256c5b710a3f82c439665794ba				#將槽位分給那個節(jié)點(diǎn)

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:6bee155f136f40e28e1f60c8ddec3b158cd8f8e8					#從哪個節(jié)點(diǎn)分出槽位
Source node #2:done

Do you want to proceed with the proposed reshard plan (yes/no)? yes			#輸入yes繼續(xù)

下面是收縮節(jié)點(diǎn)的過程截圖。
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法
數(shù)據(jù)遷移過程。
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法
槽位分出遷移成功。
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

2.3.分配1365個槽位給192.168.81.220的6380節(jié)點(diǎn)

[root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb reshard 192.168.81.210:6380
How many slots do you want to move (from 1 to 16384)? 1365				#分配出多少個槽位

What is the receiving node ID? 10dc7f3f9a753140a8494adbbe5a13d0026451a1				#將槽位分給那個節(jié)點(diǎn)

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:6bee155f136f40e28e1f60c8ddec3b158cd8f8e8					#從哪個節(jié)點(diǎn)分出槽位
Source node #2:done

Do you want to proceed with the proposed reshard plan (yes/no)? yes			#輸入yes繼續(xù)

收縮過程截圖展示。

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

2.4.分配1365個槽位給192.168.81.230的6380節(jié)點(diǎn)

[root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb reshard 192.168.81.210:6380
How many slots do you want to move (from 1 to 16384)? 1366				#分配出多少個槽位

What is the receiving node ID? a4381138fdc142f18881b7b6ca8ae5b0d02a3228				#將槽位分給那個節(jié)點(diǎn)

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:6bee155f136f40e28e1f60c8ddec3b158cd8f8e8					#從哪個節(jié)點(diǎn)分出槽位
Source node #2:done

Do you want to proceed with the proposed reshard plan (yes/no)? yes			#輸入yes繼續(xù)

收縮過程截圖展示。

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

當(dāng)最后一個節(jié)點(diǎn)遷移完數(shù)據(jù)后,6390主節(jié)點(diǎn)槽位數(shù)變?yōu)?。

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

2.5.查看當(dāng)前集群槽位分配

槽位及數(shù)據(jù)已經(jīng)從6390即將下線的主機(jī)遷移完畢,可以看下當(dāng)前集群三個主節(jié)點(diǎn)的槽位數(shù)。

可以非常清楚的看到,現(xiàn)在每個主節(jié)點(diǎn)的槽位數(shù)為5461。

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

如果覺得槽位重新分配后順序不太滿意,那么在執(zhí)行一下reshard,把其它節(jié)點(diǎn)的槽位都分給192.168.81.210的6380上,這樣一來,210的6380擁有的槽位就是0-16383,然后在將210的槽位一個節(jié)點(diǎn)分給5461個,分完之后,各節(jié)點(diǎn)的順序就一致了。
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

3.驗(yàn)證數(shù)據(jù)遷移過程是否導(dǎo)致數(shù)據(jù)異常

多開幾個窗口,一個執(zhí)行數(shù)據(jù)槽位遷移,一個不斷創(chuàng)建key,一個查看key的創(chuàng)建進(jìn)度,一個查看key的數(shù)據(jù)。
持續(xù)測試,發(fā)現(xiàn)沒有任何數(shù)據(jù)異常,全部顯示ok。
Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

4.將下線的主節(jié)點(diǎn)從集群中刪除

4.1.刪除節(jié)點(diǎn)

使用redis-trib刪除一個節(jié)點(diǎn),如果這個節(jié)點(diǎn)存在復(fù)制關(guān)系,有節(jié)點(diǎn)在復(fù)制當(dāng)前節(jié)點(diǎn)或者當(dāng)前節(jié)點(diǎn)復(fù)制別的節(jié)點(diǎn)的數(shù)據(jù),redis-trib會自動處理復(fù)制關(guān)系,然后將節(jié)點(diǎn)刪除,節(jié)點(diǎn)刪除后會把對應(yīng)的進(jìn)程也停止運(yùn)行。

刪除節(jié)點(diǎn)之前必須確保該節(jié)點(diǎn)沒有任何槽位和數(shù)據(jù),否則會刪除失敗。

命令:./redis-trib.rb del-node 節(jié)點(diǎn)IP:端口 ID

[root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb del-node 192.168.81.240:6390 6bee155f136f40e28e1f60c8ddec3b158cd8f8e8
>>> Removing node 6bee155f136f40e28e1f60c8ddec3b158cd8f8e8 from cluster 192.168.81.240:6390
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

[root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb del-node 192.168.81.240:6391 f6b9320dfbc929ad5a31cdb149360b0fd8de2e60
>>> Removing node f6b9320dfbc929ad5a31cdb149360b0fd8de2e60 from cluster 192.168.81.240:6391
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

4.2.調(diào)整主從交叉復(fù)制

刪掉192.168.81.240服務(wù)器上的兩個redis節(jié)點(diǎn)后,192.168.81.210服務(wù)器上的6380就沒有了復(fù)制關(guān)系,我們需要把192.168.81.230的6381節(jié)點(diǎn)復(fù)制192.168.81.210的6380節(jié)點(diǎn)。

[root@redis-1 ~]# redis-cli -h 192.168.81.230 -p 6381
192.168.81.230:6381> CLUSTER REPLICATE 80e256579658eb256c5b710a3f82c439665794ba
OK

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

4.3.當(dāng)節(jié)點(diǎn)存在數(shù)據(jù)無法刪除

[root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb del-node 192.168.81.220:6380 10dc7f3f9a753140a8494adbbe5a13d0026451a1
>>> Removing node 10dc7f3f9a753140a8494adbbe5a13d0026451a1 from cluster 192.168.81.220:6380
[ERR] Node 192.168.81.220:6380 is not empty! Reshard data away and try again.

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

5.將下線主機(jī)清空集群信息

redis-trib雖然能夠?qū)⒐?jié)點(diǎn)在集群中刪除,但是無法將其的集群信息清空,如果集群信息還有保留,那么該接地那就無法加入其它集群。

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

在下線的redis節(jié)點(diǎn)上使用cluster reset刪除集群信息即可。

192.168.81.240:6390> CLUSTER reset
OK

Redis Cluster集群收縮主從節(jié)點(diǎn)的方法

以上就是關(guān)于“Redis Cluster集群收縮主從節(jié)點(diǎn)的方法”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI