Redis叢集環境搭建實踐
0 Redis叢集簡介
Redis叢集(Redis Cluster)是Redis提供的分散式資料庫方案,通過分片(sharding)來進行資料共享,並提供複製和故障轉移功能。相比於主從複製、哨兵模式,Redis叢集實現了較為完善的高可用方案,解決了儲存能力受到單機限制,寫操作無法負載均衡的問題。
本文是Redis叢集學習的實踐總結,詳細介紹逐步搭建Redis叢集環境的過程,並完成叢集伸縮的實踐。
1 Redis叢集環境搭建
方便起見,這裡叢集環境的所有節點全部位於同一個伺服器上,共6個節點以埠號區分,3個主節點+3個從節點。叢集的簡單架構如圖:
本文基於最新的Redis 6.0+,直接從github下載最新的原始碼編譯獲得常用工具 redis-server , redis-cli 。值得注意的是,從Redis 5.0以後的版本,叢集管理軟體 redis-trib.rb 被整合到 redis-cli 客戶端工具中(詳細可參考cluster-tutorial)。
本節介紹叢集環境搭建時,並未藉助 redis-trib.rb 快速管理,而是按照標準步驟一步步搭建,這也是為了熟悉叢集管理的基本步驟。在叢集伸縮實踐一節將藉助 redis-trib.rb 完成叢集重新分片工作。
叢集的搭建可以分為四步:
- 啟動節點:將節點以叢集方式啟動,此時節點是獨立的。
- 節點握手:將獨立的節點連成網路。
- 槽指派:將16384個槽位分配給主節點,以達到分片儲存資料庫鍵值對的效果。
- 主從複製:為從節點指定主節點。
1.1 啟動節點
每個節點初始狀態仍為 Master伺服器,唯一不同的是:使用 Cluster 模式啟動。需要對配置檔案進行修改,以埠號為6379的節點為例,主要修改如下幾項:
# 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 引數指定了叢集配置檔案的位置,每個節點在執行過程中,會維護一份叢集配置檔案;每當叢集資訊發生變化時(如增減節點),叢集內所有節點會將最新資訊更新到該配置檔案;當節點重啟後,會重新讀取該配置檔案,獲取叢集資訊,可以方便的重新加入到叢集中。 也就是說,當Redis節點以叢集模式啟動時,會首先尋找是否有叢集配置檔案,如果有則使用檔案中的配置啟動,如果沒有,則初始化配置並將配置儲存到檔案中。叢集配置檔案由Redis節點維護,不需要人工修改。
為6個節點修改好相應的配置檔案後,即可利用 redis-server redis_xxxx_cluster.conf 工具啟動6個伺服器(xxxx表示埠號,對應相應的配置檔案)。利用ps
$ 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 節點握手
1.1中將每個節點啟動後,節點間是相互獨立的,他們都處於一個只包含自己的叢集當中,以埠號6379的伺服器為例,利用 CLUSTER NODES 檢視當前叢集包含的節點。
127.0.0.1:6379> CLUSTER NODES 37784b3605ad216fa93e976979c43def42bf763d :6379@16379 myself,master - 0 0 0 connected 449 4576 5798 7568 8455 12706
我們需要將各個獨立的節點連線起來,構成一個包含多個節點的叢集,使用 CLUSTER MEET <ip> <port> 命令。
$ redis-cli -p 6379 -c # -c 選項指定以Cluster模式執行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
再次檢視此時叢集中包含的節點情況:
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
可以發現此時6個節點均作為主節點加入到叢集中, CLUSTER NODES 返回的結果各項含義如下:
<id> <ip:port@cport> <flags> <master> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot>
- 節點id: 由40個16進位制字串組成,節點id只在叢集初始化時建立一次,然後儲存到叢集配置檔案(即前文提到的cluster-config-file)中,以後節點重新啟動時會直接在叢集配置檔案中讀取。
- port@cport: 前者為普通埠,用於為客戶端提供服務;後者為叢集埠,分配方法為:普通埠+10000,只用於節點間的通訊。
其餘各項的詳細解釋可以參考官方文件cluster nodes。
1.3 槽指派
Redis叢集通過分片(sharding)的方式儲存資料庫的鍵值對,整個資料庫被分為16384個槽(slot),資料庫每個鍵都屬於這16384個槽的一個,叢集中的每個節點都可以處理0個或者最多16384個slot。
槽是資料管理和遷移的基本單位。當資料庫中的16384個槽都分配了節點時,叢集處於上線狀態(ok);如果有任意一個槽沒有分配節點,則叢集處於下線狀態(fail)。
注意,只有主節點有處理槽的能力,如果將槽指派步驟放在主從複製之後,並且將槽位分配給從節點,那麼叢集將無法正常工作(處於下線狀態)。
利用 CLUSTER ADDSLOTS <slot> [slot ...] 命令進行槽指派:
redis-cli -p 6379 cluster addslots {0..5000} redis-cli -p 6380 cluster addslots {5001..10000} redis-cli -p 6381 cluster addslots {10001..16383}
槽指派後集群中節點情況如下:
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 # 叢集處於上線狀態 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 主從複製
上述步驟後,叢集節點均作為主節點存在,仍不能實現Redis的高可用,配置主從複製之後,才算真正實現了叢集的高可用功能。
CLUSTER REPLICATE <node_id> 用來讓叢集中接收命令的節點成為 node_id 所指定節點的從節點,並開始對主節點進行復制。
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
順帶補充,上述步驟1.2,1.3,1.4可以利用 redis-trib.rb 工具整體實現,在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 指示給定的建立節點列表是以主節點+從節點對組成的。
1.5 在叢集中執行命令
叢集此時處於上線狀態,可以通過客戶端向叢集中的節點發送命令。接收命令的節點會計算出命令要處理的鍵屬於哪個槽,並檢查這個槽是否指派給自己。
- 如果鍵所在的slot剛好指派給了當前節點,會直接執行這個命令。
- 否則,節點向客戶端返回 MOVED 錯誤,指引客戶端轉向 redirect 至正確的節點,並再次傳送此前的命令。
此處,我們利用 CLUSTER KEYSLOT 檢視到鍵 name 所在槽號為5798(被分配在6380節點),當對此鍵操作時,會被重定向到相應的節點。對鍵 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>
值得注意的是,當我們將命令通過客戶端傳送給一個從節點時,命令會被重定向至對應的主節點。
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 叢集故障轉移
叢集中主節點下線時,複製此主節點的所有的從節點將會選出一個節點作為新的主節點,並完成故障轉移。和主從複製的配置相似,當原先的從節點再次上線,它會被作為新主節點的的從節點存在於叢集中。
下面模擬6379節點宕機的情況(將其SHUTDOWN),可以觀察到其從節點6479將作為新的主節點繼續工作。
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節點從宕機狀態恢復後,將作為6380節點的從節點存在。
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 會記錄下叢集節點的狀態,開啟節點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 叢集伸縮實踐
叢集伸縮的關鍵在於對叢集的進行重新分片,實現槽位在節點間的遷移。本節將以在叢集中新增節點和刪除節點為例,對槽遷移進行實踐。
藉助於 redis-cli 中整合的 redis-trib.rb 工具進行槽位的管理,工具的幫助選單如下:
$ 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 <node1=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 叢集伸縮-新增節點
考慮在叢集中新增兩個節點,埠號為6382和6482,其中節點6482對6382進行復制。
(1) 啟動節點:按照1.1中介紹的步驟,啟動6382和6482節點。
(2) 節點握手:藉助 redis-cli --cluster add-node 命令分別新增節點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.
(3) 重新分片:藉助 redis-cli --cluster reshard 命令對叢集重新分片,使得各節點槽位均衡(分別從節點6379/6380/6381中遷移一些slot到節點6382中)。需要指定:
- 移動的槽位數:最終平均每個主節點有4096個slot,因此總共移動4096 slots
- 接收槽位的目標節點ID:節點6382的ID
- 移出槽位的源節點ID:節點6379/6380/6381的ID
$ 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) 設定主從關係:
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 叢集伸縮-刪除節點
這裡考慮將新新增的兩個節點6382和6482刪除,需要將節點6382上分配的槽位遷移到其他節點。
(1) 重新分片:同樣藉助 redis-cli --cluster reshard 命令,將6382節點上的槽位全部轉移到節點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) 刪除節點:利用 redis-cli --cluster del-node 命令依次刪除從節點6482和主節點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
3 總結
Redis叢集環境的搭建主要包括啟動節點、節點握手、槽指派和主從複製等四個步驟,叢集伸縮同樣涉及這幾個方面。藉助 redis-cli --cluster 命令來管理叢集環境,不僅能增加簡便性,還能降低操作失誤的風險。
參考資料
[1] Redis設計與實現,黃健巨集著
[2] Redis cluster tutorial: (https://redis.io/topics/cluster-tutorial)
[3] 深入學習Redis(5):叢集 (https://www.cnblogs.com/kismetv/p/9853040.h