您好,登錄后才能下訂單哦!
怎么搭建Redis集群環(huán)境,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
Redis集群簡(jiǎn)介
Redis集群(Redis Cluster) 是Redis提供的分布式數(shù)據(jù)庫(kù)方案,通過(guò) 分片(sharding) 來(lái)進(jìn)行數(shù)據(jù)共享,并提供復(fù)制和故障轉(zhuǎn)移功能。相比于主從復(fù)制、哨兵模式,Redis集群實(shí)現(xiàn)了較為完善的高可用方案,解決了存儲(chǔ)能力受到單機(jī)限制,寫操作無(wú)法負(fù)載均衡的問(wèn)題。
本文是Redis集群學(xué)習(xí)的實(shí)踐總結(jié),詳細(xì)介紹逐步搭建Redis集群環(huán)境的過(guò)程,并完成集群伸縮的實(shí)踐。
1 Redis集群環(huán)境搭建
方便起見,這里集群環(huán)境的所有節(jié)點(diǎn)全部位于同一個(gè)服務(wù)器上,共6個(gè)節(jié)點(diǎn)以端口號(hào)區(qū)分,3個(gè)主節(jié)點(diǎn)+3個(gè)從節(jié)點(diǎn)。集群的簡(jiǎn)單架構(gòu)如圖:
本文基于最新的Redis 6.0+,直接從github下載最新的源碼編譯獲得常用工具 redis-server , redis-cli 。值得注意的是,從Redis 5.0以后的版本,集群管理軟件 redis-trib.rb 被集成到 redis-cli 客戶端工具中(詳細(xì)可參考cluster-tutorial)。
本節(jié)介紹集群環(huán)境搭建時(shí),并未借助 redis-trib.rb 快速管理,而是按照標(biāo)準(zhǔn)步驟一步步搭建,這也是為了熟悉集群管理的基本步驟。在集群伸縮實(shí)踐一節(jié)將借助 redis-trib.rb 完成集群重新分片工作。
集群的搭建可以分為四步:
鴻蒙官方戰(zhàn)略合作共建——HarmonyOS技術(shù)社區(qū)
啟動(dòng)節(jié)點(diǎn):將節(jié)點(diǎn)以集群方式啟動(dòng),此時(shí)節(jié)點(diǎn)是獨(dú)立的。
節(jié)點(diǎn)握手:將獨(dú)立的節(jié)點(diǎn)連成網(wǎng)絡(luò)。
槽指派:將16384個(gè)槽位分配給主節(jié)點(diǎn),以達(dá)到分片保存數(shù)據(jù)庫(kù)鍵值對(duì)的效果。
主從復(fù)制:為從節(jié)點(diǎn)指定主節(jié)點(diǎn)。
1.1 啟動(dòng)節(jié)點(diǎn)
每個(gè)節(jié)點(diǎn)初始狀態(tài)仍為 Master服務(wù)器,唯一不同的是:使用 Cluster 模式啟動(dòng)。需要對(duì)配置文件進(jìn)行修改,以端口號(hào)為6379的節(jié)點(diǎn)為例,主要修改如下幾項(xiàng):
# redis_6379_cluster.conf port 6379 cluster-enabled yes cluster-config-file "node-6379.conf" logfile "redis-server-6379.log" dbfilename "dump-6379.rdb" daemonize yes
其中 cluster-config-file 參數(shù)指定了集群配置文件的位置,每個(gè)節(jié)點(diǎn)在運(yùn)行過(guò)程中,會(huì)維護(hù)一份集群配置文件;每當(dāng)集群信息發(fā)生變化時(shí)(如增減節(jié)點(diǎn)),集群內(nèi)所有節(jié)點(diǎn)會(huì)將最新信息更新到該配置文件;當(dāng)節(jié)點(diǎn)重啟后,會(huì)重新讀取該配置文件,獲取集群信息,可以方便的重新加入到集群中。也就是說(shuō),當(dāng)Redis節(jié)點(diǎn)以集群模式啟動(dòng)時(shí),會(huì)首先尋找是否有集群配置文件,如果有則使用文件中的配置啟動(dòng),如果沒(méi)有,則初始化配置并將配置保存到文件中。集群配置文件由Redis節(jié)點(diǎn)維護(hù),不需要人工修改。
為6個(gè)節(jié)點(diǎn)修改好相應(yīng)的配置文件后,即可利用 redis-server redis_xxxx_cluster.conf 工具啟動(dòng)6個(gè)服務(wù)器(xxxx表示端口號(hào),對(duì)應(yīng)相應(yīng)的配置文件)。利用ps命令查看進(jìn)程:
$ ps -aux | grep redis ... 800 0.1 0.0 49584 2444 ? Ssl 20:42 0:00 redis-server 127.0.0.1:6379 [cluster] ... 805 0.1 0.0 49584 2440 ? Ssl 20:42 0:00 redis-server 127.0.0.1:6380 [cluster] ... 812 0.3 0.0 49584 2436 ? Ssl 20:42 0:00 redis-server 127.0.0.1:6381 [cluster] ... 817 0.1 0.0 49584 2432 ? Ssl 20:43 0:00 redis-server 127.0.0.1:6479 [cluster] ... 822 0.0 0.0 49584 2380 ? Ssl 20:43 0:00 redis-server 127.0.0.1:6480 [cluster] ... 827 0.5 0.0 49584 2380 ? Ssl 20:43 0:00 redis-server 127.0.0.1:6481 [cluster]
1.2 節(jié)點(diǎn)握手
1.1中將每個(gè)節(jié)點(diǎn)啟動(dòng)后,節(jié)點(diǎn)間是相互獨(dú)立的,他們都處于一個(gè)只包含自己的集群當(dāng)中,以端口號(hào)6379的服務(wù)器為例,利用 CLUSTER NODES 查看當(dāng)前集群包含的節(jié)點(diǎn)。
127.0.0.1:6379> CLUSTER NODES 37784b3605ad216fa93e976979c43def42bf763d :6379@16379 myself,master - 0 0 0 connected 449 4576 5798 7568 8455 12706
我們需要將各個(gè)獨(dú)立的節(jié)點(diǎn)連接起來(lái),構(gòu)成一個(gè)包含多個(gè)節(jié)點(diǎn)的集群,使用 CLUSTER MEET 命令。
$ redis-cli -p 6379 -c # -c 選項(xiàng)指定以Cluster模式運(yùn)行redis-cli 127.0.0.1:6379> CLUSTER MEET 127.0.0.1 6380 OK 127.0.0.1:6379> CLUSTER MEET 127.0.0.1 6381 OK 127.0.0.1:6379> CLUSTER MEET 127.0.0.1 6480 OK 127.0.0.1:6379> CLUSTER MEET 127.0.0.1 6381 OK 127.0.0.1:6379> CLUSTER MEET 127.0.0.1 6382 OK
再次查看此時(shí)集群中包含的節(jié)點(diǎn)情況:
127.0.0.1:6379> CLUSTER NODES c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380@16380 master - 0 1603632309283 4 connected 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379@16379 myself,master - 0 1603632308000 1 connected 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381@16381 master - 0 1603632310292 2 connected 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481@16481 master - 0 1603632309000 5 connected 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479@16479 master - 0 1603632308000 3 connected 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480@16480 master - 0 1603632311302 0 connected
可以發(fā)現(xiàn)此時(shí)6個(gè)節(jié)點(diǎn)均作為主節(jié)點(diǎn)加入到集群中, CLUSTER NODES 返回的結(jié)果各項(xiàng)含義如下:
<id> <ip:port@cport> <flags> <master> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot>
節(jié)點(diǎn)id: 由40個(gè)16進(jìn)制字符串組成,節(jié)點(diǎn)id只在集群初始化時(shí)創(chuàng)建一次,然后保存到集群配置文件(即前文提到的cluster-config-file)中,以后節(jié)點(diǎn)重新啟動(dòng)時(shí)會(huì)直接在集群配置文件中讀取。
port@cport: 前者為普通端口,用于為客戶端提供服務(wù);后者為集群端口,分配方法為:普通端口+10000,只用于節(jié)點(diǎn)間的通訊。
其余各項(xiàng)的詳細(xì)解釋可以參考官方文檔cluster nodes。
1.3 槽指派
Redis集群通過(guò)分片(sharding)的方式保存數(shù)據(jù)庫(kù)的鍵值對(duì),整個(gè)數(shù)據(jù)庫(kù)被分為16384個(gè)槽(slot),數(shù)據(jù)庫(kù)每個(gè)鍵都屬于這16384個(gè)槽的一個(gè),集群中的每個(gè)節(jié)點(diǎn)都可以處理0個(gè)或者最多16384個(gè)slot。
槽是數(shù)據(jù)管理和遷移的基本單位。當(dāng)數(shù)據(jù)庫(kù)中的16384個(gè)槽都分配了節(jié)點(diǎn)時(shí),集群處于上線狀態(tài)(ok);如果有任意一個(gè)槽沒(méi)有分配節(jié)點(diǎn),則集群處于下線狀態(tài)(fail)。
注意,只有主節(jié)點(diǎn)有處理槽的能力,如果將槽指派步驟放在主從復(fù)制之后,并且將槽位分配給從節(jié)點(diǎn),那么集群將無(wú)法正常工作(處于下線狀態(tài))。
利用 CLUSTER ADDSLOTS
redis-cli -p 6379 cluster addslots {0..5000} redis-cli -p 6380 cluster addslots {5001..10000} redis-cli -p 6381 cluster addslots {10001..16383}
槽指派后集群中節(jié)點(diǎn)情況如下:
127.0.0.1:6379> CLUSTER NODES c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380@16380 master - 0 1603632880310 4 connected 5001-10000 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379@16379 myself,master - 0 1603632879000 1 connected 0-5000 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381@16381 master - 0 1603632879000 2 connected 10001-16383 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481@16481 master - 0 1603632878000 5 connected 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479@16479 master - 0 1603632880000 3 connected 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480@16480 master - 0 1603632881317 0 connected 127.0.0.1:6379> CLUSTER INFO cluster_state:ok # 集群處于上線狀態(tài) cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:5 cluster_my_epoch:1 cluster_stats_messages_ping_sent:4763 cluster_stats_messages_pong_sent:4939 cluster_stats_messages_meet_sent:5 cluster_stats_messages_sent:9707 cluster_stats_messages_ping_received:4939 cluster_stats_messages_pong_received:4768 cluster_stats_messages_received:9707
1.4 主從復(fù)制
上述步驟后,集群節(jié)點(diǎn)均作為主節(jié)點(diǎn)存在,仍不能實(shí)現(xiàn)Redis的高可用,配置主從復(fù)制之后,才算真正實(shí)現(xiàn)了集群的高可用功能。
CLUSTER REPLICATE <node_id> 用來(lái)讓集群中接收命令的節(jié)點(diǎn)成為 node_id 所指定節(jié)點(diǎn)的從節(jié)點(diǎn),并開始對(duì)主節(jié)點(diǎn)進(jìn)行復(fù)制。
redis-cli -p 6479 cluster replicate 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 redis-cli -p 6480 cluster replicate c47598b25205cc88abe2e5094d5bfd9ea202335f redis-cli -p 6481 cluster replicate 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6379> CLUSTER NODES c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380@16380 master - 0 1603633105211 4 connected 5001-10000 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379@16379 myself,master - 0 1603633105000 1 connected 0-5000 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381@16381 master - 0 1603633105000 2 connected 10001-16383 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481@16481 slave 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 0 1603633107229 5 connected 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479@16479 slave 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 0 1603633106221 3 connected 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480@16480 slave c47598b25205cc88abe2e5094d5bfd9ea202335f 0 1603633104000 4 connected
順帶補(bǔ)充,上述步驟1.2,1.3,1.4可以利用 redis-trib.rb 工具整體實(shí)現(xiàn),在Redis 5.0之后直接利用 redis-cli 完成,參考命令如下:
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6479 127.0.0.1:6380 127.0.0.1:6480 127.0.0.1:6381 127.0.0.1:6481 --cluster-replicas 1
--cluster-replicas 1 指示給定的創(chuàng)建節(jié)點(diǎn)列表是以主節(jié)點(diǎn)+從節(jié)點(diǎn)對(duì)組成的。
1.5 在集群中執(zhí)行命令
集群此時(shí)處于上線狀態(tài),可以通過(guò)客戶端向集群中的節(jié)點(diǎn)發(fā)送命令。接收命令的節(jié)點(diǎn)會(huì)計(jì)算出命令要處理的鍵屬于哪個(gè)槽,并檢查這個(gè)槽是否指派給自己。
如果鍵所在的slot剛好指派給了當(dāng)前節(jié)點(diǎn),會(huì)直接執(zhí)行這個(gè)命令。
否則,節(jié)點(diǎn)向客戶端返回 MOVED 錯(cuò)誤,指引客戶端轉(zhuǎn)向 redirect 至正確的節(jié)點(diǎn),并再次發(fā)送此前的命令。
此處,我們利用 CLUSTER KEYSLOT 查看到鍵 name 所在槽號(hào)為5798(被分配在6380節(jié)點(diǎn)),當(dāng)對(duì)此鍵操作時(shí),會(huì)被重定向到相應(yīng)的節(jié)點(diǎn)。對(duì)鍵 fruits 的操作與此類似。
127.0.0.1:6379> CLUSTER KEYSLOT name (integer) 5798 127.0.0.1:6379> set name huey -> Redirected to slot [5798] located at 127.0.0.1:6380 OK 127.0.0.1:6380> 127.0.0.1:6379> get fruits -> Redirected to slot [14943] located at 127.0.0.1:6381 "apple" 127.0.0.1:6381>
值得注意的是,當(dāng)我們將命令通過(guò)客戶端發(fā)送給一個(gè)從節(jié)點(diǎn)時(shí),命令會(huì)被重定向至對(duì)應(yīng)的主節(jié)點(diǎn)。
127.0.0.1:6480> KEYS * 1) "name" 127.0.0.1:6480> get name -> Redirected to slot [5798] located at 127.0.0.1:6380 "huey"
1.6 集群故障轉(zhuǎn)移
集群中主節(jié)點(diǎn)下線時(shí),復(fù)制此主節(jié)點(diǎn)的所有的從節(jié)點(diǎn)將會(huì)選出一個(gè)節(jié)點(diǎn)作為新的主節(jié)點(diǎn),并完成故障轉(zhuǎn)移。和主從復(fù)制的配置相似,當(dāng)原先的從節(jié)點(diǎn)再次上線,它會(huì)被作為新主節(jié)點(diǎn)的的從節(jié)點(diǎn)存在于集群中。
下面模擬6379節(jié)點(diǎn)宕機(jī)的情況(將其SHUTDOWN),可以觀察到其從節(jié)點(diǎn)6479將作為新的主節(jié)點(diǎn)繼續(xù)工作。
462:S 26 Oct 14:08:12.750 * FAIL message received from c47598b25205cc88abe2e5094d5bfd9ea202335f about 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 462:S 26 Oct 14:08:12.751 # Cluster state changed: fail 462:S 26 Oct 14:08:12.829 # Start of election delayed for 595 milliseconds (rank #0, offset 9160). 462:S 26 Oct 14:08:13.434 # Starting a failover election for epoch 6. 462:S 26 Oct 14:08:13.446 # Failover election won: I'm the new master. 462:S 26 Oct 14:08:13.447 # configEpoch set to 6 after successful failover 462:M 26 Oct 14:08:13.447 # Setting secondary replication ID to d357886e00341b57bf17e46b6d9f8cf53b7fad21, valid up to offset: 9161. New replication ID is adbf41b16075ea22b17f145186c53c4499864d5b 462:M 26 Oct 14:08:13.447 * Discarding previously cached master state. 462:M 26 Oct 14:08:13.448 # Cluster state changed: ok
6379節(jié)點(diǎn)從宕機(jī)狀態(tài)恢復(fù)后,將作為6380節(jié)點(diǎn)的從節(jié)點(diǎn)存在。
127.0.0.1:6379> CLUSTER NODES 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381@16381 master - 0 1603692968000 2 connected 10001-16383 c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380@16380 master - 0 1603692968504 0 connected 5001-10000 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479@16479 master - 0 1603692967495 6 connected 0-5000 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379@16379 myself,slave 4c23b25bd4bcef7f4b77d8287e330ae72e738883 0 1603692964000 1 connected 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481@16481 slave 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 0 1603692967000 4 connected 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480@16480 slave c47598b25205cc88abe2e5094d5bfd9ea202335f 0 1603692967000 5 connected
前文提到 cluster-config-file 會(huì)記錄下集群節(jié)點(diǎn)的狀態(tài),打開節(jié)點(diǎn)6379的配置文件 nodes-6379.conf ,可以看到 CLUSTER NODES 所示信息均被保存在配置文件中:
51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381@16381 master - 0 1603694920206 2 connected 10001-16383 c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380@16380 master - 0 1603694916000 0 connected 5001-10000 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479@16479 master - 0 1603694920000 6 connected 0-5000 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379@16379 myself,slave 4c23b25bd4bcef7f4b77d8287e330ae72e738883 0 1603694918000 1 connected 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481@16481 slave 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 0 1603694919000 4 connected 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480@16480 slave c47598b25205cc88abe2e5094d5bfd9ea202335f 0 1603694919200 5 connected vars currentEpoch 6 lastVoteEpoch 0
2 集群伸縮實(shí)踐
集群伸縮的關(guān)鍵在于對(duì)集群的進(jìn)行重新分片,實(shí)現(xiàn)槽位在節(jié)點(diǎn)間的遷移。本節(jié)將以在集群中添加節(jié)點(diǎn)和刪除節(jié)點(diǎn)為例,對(duì)槽遷移進(jìn)行實(shí)踐。
借助于 redis-cli 中集成的 redis-trib.rb 工具進(jìn)行槽位的管理,工具的幫助菜單如下:
$ redis-cli --cluster help Cluster Manager Commands: create host1:port1 ... hostN:portN --cluster-replicas <arg> check host:port --cluster-search-multiple-owners info host:port fix host:port --cluster-search-multiple-owners --cluster-fix-with-unreachable-masters reshard host:port --cluster-from <arg> --cluster-to <arg> --cluster-slots <arg> --cluster-yes --cluster-timeout <arg> --cluster-pipeline <arg> --cluster-replace rebalance host:port --cluster-weight <node1node1=w1...nodeN=wN> --cluster-use-empty-masters --cluster-timeout <arg> --cluster-simulate --cluster-pipeline <arg> --cluster-threshold <arg> --cluster-replace add-node new_host:new_port existing_host:existing_port --cluster-slave --cluster-master-id <arg> del-node host:port node_id call host:port command arg arg .. arg set-timeout host:port milliseconds import host:port --cluster-from <arg> --cluster-copy --cluster-replace backup host:port backup_directory help For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
2.1 集群伸縮-添加節(jié)點(diǎn)
考慮在集群中添加兩個(gè)節(jié)點(diǎn),端口號(hào)為6382和6482,其中節(jié)點(diǎn)6482對(duì)6382進(jìn)行復(fù)制。
(1) 啟動(dòng)節(jié)點(diǎn):按照1.1中介紹的步驟,啟動(dòng)6382和6482節(jié)點(diǎn)。
(2) 節(jié)點(diǎn)握手:借助 redis-cli --cluster add-node 命令分別添加節(jié)點(diǎn)6382和6482。
redis-cli --cluster add-node 127.0.0.1:6382 127.0.0.1:6379 redis-cli --cluster add-node 127.0.0.1:6482 127.0.0.1:6379 $ redis-cli --cluster add-node 127.0.0.1:6382 127.0.0.1:6379 >>> Adding node 127.0.0.1:6382 to cluster 127.0.0.1:6379 >>> Performing Cluster Check (using node 127.0.0.1:6379) S: 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379 slots: (0 slots) slave replicates 4c23b25bd4bcef7f4b77d8287e330ae72e738883 M: 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381 slots:[10001-16383] (6383 slots) master 1 additional replica(s) M: c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380 slots:[5001-10000] (5000 slots) master 1 additional replica(s) M: 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479 slots:[0-5000] (5001 slots) master 1 additional replica(s) S: 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481 slots: (0 slots) slave replicates 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 S: 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480 slots: (0 slots) slave replicates c47598b25205cc88abe2e5094d5bfd9ea202335f [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:6382 to make it join the cluster. [OK] New node added correctly. ```shell 3) 重新分片:借助 redis-cli --cluster reshard 命令對(duì)集群重新分片,使得各節(jié)點(diǎn)槽位均衡(分別從節(jié)點(diǎn)6379/6380/6381中遷移一些slot到節(jié)點(diǎn)6382中)。需要指定: * 移動(dòng)的槽位數(shù):最終平均每個(gè)主節(jié)點(diǎn)有4096個(gè)slot,因此總共移動(dòng)4096 slots * 接收槽位的目標(biāo)節(jié)點(diǎn)ID:節(jié)點(diǎn)6382的ID * 移出槽位的源節(jié)點(diǎn)ID:節(jié)點(diǎn)6379/6380/6381的ID ```shell $ redis-cli --cluster reshard 127.0.0.1 6479 >>> Performing Cluster Check (using node 127.0.0.1:6479) M: 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479 slots:[0-5000] (5001 slots) master 1 additional replica(s) S: 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480 slots: (0 slots) slave replicates c47598b25205cc88abe2e5094d5bfd9ea202335f M: 706f399b248ed3a080cf1d4e43047a79331b714f 127.0.0.1:6482 slots: (0 slots) master M: af81109fc29f69f9184ce9512c46df476fe693a3 127.0.0.1:6382 slots: (0 slots) master M: 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381 slots:[10001-16383] (6383 slots) master 1 additional replica(s) S: 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481 slots: (0 slots) slave replicates 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 S: 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379 slots: (0 slots) slave replicates 4c23b25bd4bcef7f4b77d8287e330ae72e738883 M: c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380 slots:[5001-10000] (5000 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. How many slots do you want to move (from 1 to 16384)? 4096 What is the receiving node ID?
(4) 設(shè)置主從關(guān)系:
redis-cli -p 6482 cluster replicate af81109fc29f69f9184ce9512c46df476fe693a3 127.0.0.1:6482> CLUSTER NODES 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480@16480 slave c47598b25205cc88abe2e5094d5bfd9ea202335f 0 1603694930000 0 connected 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381@16381 master - 0 1603694931000 2 connected 11597-16383 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481@16481 slave 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 0 1603694932000 2 connected 706f399b248ed3a080cf1d4e43047a79331b714f 127.0.0.1:6482@16482 myself,slave af81109fc29f69f9184ce9512c46df476fe693a3 0 1603694932000 8 connected 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379@16379 slave 4c23b25bd4bcef7f4b77d8287e330ae72e738883 0 1603694932000 6 connected c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380@16380 master - 0 1603694933678 0 connected 6251-10000 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479@16479 master - 0 1603694932669 6 connected 1250-5000 af81109fc29f69f9184ce9512c46df476fe693a3 127.0.0.1:6382@16382 master - 0 1603694933000 9 connected 0-1249 5001-6250 10001-11596
2.2 集群伸縮-刪除節(jié)點(diǎn)
這里考慮將新添加的兩個(gè)節(jié)點(diǎn)6382和6482刪除,需要將節(jié)點(diǎn)6382上分配的槽位遷移到其他節(jié)點(diǎn)。
(1) 重新分片: 同樣借助 redis-cli --cluster reshard 命令,將6382節(jié)點(diǎn)上的槽位全部轉(zhuǎn)移到節(jié)點(diǎn)6479上。
$ redis-cli --cluster reshard 127.0.0.1 6382 >>> Performing Cluster Check (using node 127.0.0.1:6382) M: af81109fc29f69f9184ce9512c46df476fe693a3 127.0.0.1:6382 slots:[0-1249],[5001-6250],[10001-11596] (4096 slots) master 1 additional replica(s) M: 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381 slots:[11597-16383] (4787 slots) master 1 additional replica(s) S: 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379 slots: (0 slots) slave replicates 4c23b25bd4bcef7f4b77d8287e330ae72e738883 S: 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480 slots: (0 slots) slave replicates c47598b25205cc88abe2e5094d5bfd9ea202335f M: 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479 slots:[1250-5000] (3751 slots) master 1 additional replica(s) M: c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380 slots:[6251-10000] (3750 slots) master 1 additional replica(s) S: 706f399b248ed3a080cf1d4e43047a79331b714f 127.0.0.1:6482 slots: (0 slots) slave replicates af81109fc29f69f9184ce9512c46df476fe693a3 S: 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481 slots: (0 slots) slave replicates 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 [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 What is the receiving node ID? 4c23b25bd4bcef7f4b77d8287e330ae72e738883 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: af81109fc29f69f9184ce9512c46df476fe693a3 Source node #2: done 127.0.0.1:6379> CLUSTER NODES c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380@16380 master - 0 1603773540922 0 connected 6251-10000 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379@16379 myself,slave 4c23b25bd4bcef7f4b77d8287e330ae72e738883 0 1603773539000 1 connected 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479@16479 master - 0 1603773541000 10 connected 0-6250 10001-11596 706f399b248ed3a080cf1d4e43047a79331b714f 127.0.0.1:6482@16482 slave 4c23b25bd4bcef7f4b77d8287e330ae72e738883 0 1603773541000 10 connected 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480@16480 slave c47598b25205cc88abe2e5094d5bfd9ea202335f 0 1603773539000 5 connected 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481@16481 slave 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 0 1603773541931 4 connected af81109fc29f69f9184ce9512c46df476fe693a3 127.0.0.1:6382@16382 master - 0 1603773539000 9 connected 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381@16381 master - 0 1603773540000 2 connected 11597-16383
(2) 刪除節(jié)點(diǎn): 利用 redis-cli --cluster del-node 命令依次刪除從節(jié)點(diǎn)6482和主節(jié)點(diǎn)6382。
$ redis-cli --cluster del-node 127.0.0.1:6482 706f399b248ed3a080cf1d4e43047a79331b714f >>> Removing node 706f399b248ed3a080cf1d4e43047a79331b714f from cluster 127.0.0.1:6482 >>> Sending CLUSTER FORGET messages to the cluster... >>> Sending CLUSTER RESET SOFT to the deleted node. $ redis-cli --cluster del-node 127.0.0.1:6382 af81109fc29f69f9184ce9512c46df476fe693a3 >>> Removing node af81109fc29f69f9184ce9512c46df476fe693a3 from cluster 127.0.0.1:6382 >>> Sending CLUSTER FORGET messages to the cluster... >>> Sending CLUSTER RESET SOFT to the deleted node. 127.0.0.1:6379> CLUSTER NODES c47598b25205cc88abe2e5094d5bfd9ea202335f 127.0.0.1:6380@16380 master - 0 1603773679121 0 connected 6251-10000 87b7dfacde34b3cf57d5f46ab44fd6fffb2e4f52 127.0.0.1:6379@16379 myself,slave 4c23b25bd4bcef7f4b77d8287e330ae72e738883 0 1603773677000 1 connected 4c23b25bd4bcef7f4b77d8287e330ae72e738883 127.0.0.1:6479@16479 master - 0 1603773678000 10 connected 0-6250 10001-11596 32ed645a9c9d13ca68dba5a147937fb1d05922ee 127.0.0.1:6480@16480 slave c47598b25205cc88abe2e5094d5bfd9ea202335f 0 1603773680130 5 connected 9d587b75bdaed26ca582036ed706df8b2282b0aa 127.0.0.1:6481@16481 slave 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 0 1603773677099 4 connected 51081a64ddb3ccf5432c435a8cf20d45ab795dd8 127.0.0.1:6381@16381 master - 0 1603773678112 2 connected 11597-16383
關(guān)于怎么搭建Redis集群環(huán)境問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。