Redis Cluster集群收縮主從節(jié)點
1.Cluster集群收縮概念
當項目壓力承載力過高時,需要增加節(jié)點來提高負載,當項目壓力不是很大時,也希望能夠?qū)⒓菏湛s下來,給其他項目使用,這就要用到集群收縮了
集群收縮操作和集群擴容是一樣的,只需要把方向反過來即可。
擴容的時候執(zhí)行一次命令就可以實現(xiàn)槽位遷移成功,而收縮的時候有幾個主節(jié)點就需要執(zhí)行多少次,比如除去要下線的節(jié)點,還有3個主節(jié)點,那么就需要執(zhí)行三次,填寫遷移出槽位的數(shù)量也需要除以3,每個節(jié)點也需要平均分配。
收縮的時候首先要填寫分出多少個槽位,然后填寫要分給誰,最后填寫從哪分出槽位,一般分多少個槽位,就需要看要下線的主機上有多少個槽位,然后除以集群主節(jié)點數(shù),使每一個主機點分到的槽位都是相同的,填寫要分配給誰的時候,第一次填寫第一個主節(jié)點的ID,第二次填寫第二個主節(jié)點的ID,最后填寫提供槽位的節(jié)點ID,就是下線節(jié)點的ID號。
集群收縮擴容槽位的時候不會影響數(shù)據(jù)的使用。
集群收縮的源端就是要下線的主節(jié)點,目標端就是在線的主節(jié)點(分配給誰的節(jié)點)。
咱們要清楚一點,只有主節(jié)點是有槽位的,因此呢需要將主節(jié)點的槽位分配給其他主節(jié)點,當槽位清空后,這個主機節(jié)點就可以下線了。
收縮集群前后對比圖
集群收縮操作步驟:
? 1.執(zhí)行reshard命令將需要下線的主節(jié)點進行槽位分散。
? 2.有幾個主節(jié)點就需要執(zhí)行幾次reshard命令,首先填寫要分出的槽位數(shù),然后填寫分給誰,最后填寫從哪里分。
? 3.當槽位分散完成后,要下線的主節(jié)點沒有任何數(shù)據(jù)時,將節(jié)點從集群中刪除。
集群信息
目前集群時四主四從共8個節(jié)點,我們需要將集群改為三主三從,收縮出兩個節(jié)點給其他程序使用。
2.將6390主節(jié)點從集群中收縮
2.1.計算需要分給每一個節(jié)點的槽位數(shù)
可以看到6390節(jié)點上有4096個槽位,刪除要下線的6390節(jié)點后,我們還有3個主節(jié)點,4096除3得到1365,分配槽位的時候給每個節(jié)點分配1365個槽位即可均勻。
2.2.分配1365個槽位給192.168.81.210的6380節(jié)點
我們需要將192.168.81.240的6390節(jié)點分出1365個槽位給192.168.81.210的6380節(jié)點。
只需要把What is the receiving node ID填寫成192.168.81.210的6380節(jié)點ID即可,指的是分配出來的槽位要給誰。
然后source node填寫192.168.81.240的6390節(jié)點的ID,這里指的是從哪個節(jié)點上分出1365個槽位,填寫ID后,回車后會提示還要從哪個節(jié)點上分配槽位,因為只有6390需要分出槽位,所以在這里填寫done,表示只有這個一個節(jié)點分出1365個槽位給其他節(jié)點。
[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é)點 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é)點分出槽位 Source node #2:done Do you want to proceed with the proposed reshard plan (yes/no)? yes #輸入yes繼續(xù)
下面是收縮節(jié)點的過程截圖。
數(shù)據(jù)遷移過程。
槽位分出遷移成功。
2.3.分配1365個槽位給192.168.81.220的6380節(jié)點
[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é)點 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é)點分出槽位 Source node #2:done Do you want to proceed with the proposed reshard plan (yes/no)? yes #輸入yes繼續(xù)
收縮過程截圖展示。
2.4.分配1365個槽位給192.168.81.230的6380節(jié)點
[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é)點 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é)點分出槽位 Source node #2:done Do you want to proceed with the proposed reshard plan (yes/no)? yes #輸入yes繼續(xù)
收縮過程截圖展示。
當最后一個節(jié)點遷移完數(shù)據(jù)后,6390主節(jié)點槽位數(shù)變?yōu)?。
2.5.查看當前集群槽位分配
槽位及數(shù)據(jù)已經(jīng)從6390即將下線的主機遷移完畢,可以看下當前集群三個主節(jié)點的槽位數(shù)。
可以非常清楚的看到,現(xiàn)在每個主節(jié)點的槽位數(shù)為5461。
如果覺得槽位重新分配后順序不太滿意,那么在執(zhí)行一下reshard,把其它節(jié)點的槽位都分給192.168.81.210的6380上,這樣一來,210的6380擁有的槽位就是0-16383,然后在將210的槽位一個節(jié)點分給5461個,分完之后,各節(jié)點的順序就一致了。
3.驗證數(shù)據(jù)遷移過程是否導致數(shù)據(jù)異常
多開幾個窗口,一個執(zhí)行數(shù)據(jù)槽位遷移,一個不斷創(chuàng)建key,一個查看key的創(chuàng)建進度,一個查看key的數(shù)據(jù)。
持續(xù)測試,發(fā)現(xiàn)沒有任何數(shù)據(jù)異常,全部顯示ok。
4.將下線的主節(jié)點從集群中刪除
4.1.刪除節(jié)點
使用redis-trib刪除一個節(jié)點,如果這個節(jié)點存在復制關(guān)系,有節(jié)點在復制當前節(jié)點或者當前節(jié)點復制別的節(jié)點的數(shù)據(jù),redis-trib會自動處理復制關(guān)系,然后將節(jié)點刪除,節(jié)點刪除后會把對應的進程也停止運行。
刪除節(jié)點之前必須確保該節(jié)點沒有任何槽位和數(shù)據(jù),否則會刪除失敗。
命令:./redis-trib.rb del-node 節(jié)點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.
4.2.調(diào)整主從交叉復制
刪掉192.168.81.240服務器上的兩個redis節(jié)點后,192.168.81.210服務器上的6380就沒有了復制關(guān)系,我們需要把192.168.81.230的6381節(jié)點復制192.168.81.210的6380節(jié)點。
[root@redis-1 ~]# redis-cli -h 192.168.81.230 -p 6381 192.168.81.230:6381> CLUSTER REPLICATE 80e256579658eb256c5b710a3f82c439665794ba OK
4.3.當節(jié)點存在數(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.
5.將下線主機清空集群信息
redis-trib雖然能夠?qū)⒐?jié)點在集群中刪除,但是無法將其的集群信息清空,如果集群信息還有保留,那么該接地那就無法加入其它集群。
在下線的redis節(jié)點上使用cluster reset刪除集群信息即可。
192.168.81.240:6390> CLUSTER reset
OK
到此這篇關(guān)于Redis Cluster集群收縮主從節(jié)點詳細教程的文章就介紹到這了,更多相關(guān)Redis Cluster集群收縮內(nèi)容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_44953658/article/details/121465277