1. 程式人生 > >Redis 5.0 redis-cli --cluster help說明

Redis 5.0 redis-cli --cluster help說明

背景:

      Redis Cluster 在5.0之後取消了ruby指令碼 redis-trib.rb的支援(手動命令列新增叢集的方式不變),集合到redis-cli裡,避免了再安裝ruby的相關環境。直接使用redis-clit的引數--cluster 來取代。為方便自己後面查詢就說明下如何使用該命令進行Cluster的建立和管理,關於Cluster的相關說明可以檢視官網或則Redis Cluster部署、管理和測試。

環境:

系統版本:Ubuntu 14.04
Redis版本:5.0.5
機器IP:192.168.163.132

說明:redis-cli --cluster help

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 #修復槽的重複分配問題
  reshard        host:port                     #指定叢集的任意一節點進行遷移slot,重新分slots
                 --cluster-from <arg>          #需要從哪些源節點上遷移slot,可從多個源節點完成遷移,以逗號隔開,傳遞的是節點的node id,還可以直接傳遞--from all,這樣源節點就是叢集的所有節點,不傳遞該引數的話,則會在遷移過程中提示使用者輸入
                 --cluster-to <arg>            #slot需要遷移的目的節點的node id,目的節點只能填寫一個,不傳遞該引數的話,則會在遷移過程中提示使用者輸入
                 --cluster-slots <arg>         #需要遷移的slot數量,不傳遞該引數的話,則會在遷移過程中提示使用者輸入。
                 --cluster-yes                 #指定遷移時的確認輸入
                 --cluster-timeout <arg>       #設定migrate命令的超時時間
                 --cluster-pipeline <arg>      #定義cluster getkeysinslot命令一次取出的key數量,不傳的話使用預設值為10
                 --cluster-replace             #是否直接replace到目標節點
  rebalance      host:port                                      #指定叢集的任意一節點進行平衡叢集節點slot數量 
                 --cluster-weight <node1=w1...nodeN=wN>         #指定叢集節點的權重
                 --cluster-use-empty-masters                    #設定可以讓沒有分配slot的主節點參與,預設不允許
                 --cluster-timeout <arg>                        #設定migrate命令的超時時間
                 --cluster-simulate                             #模擬rebalance操作,不會真正執行遷移操作
                 --cluster-pipeline <arg>                       #定義cluster getkeysinslot命令一次取出的key數量,預設值為10
                 --cluster-threshold <arg>                      #遷移的slot閾值超過threshold,執行rebalance操作
                 --cluster-replace                              #是否直接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                         #設定cluster-node-timeout
  import         host:port                                      #將外部redis資料匯入叢集
                 --cluster-from <arg>                           #將指定例項的資料匯入到叢集
                 --cluster-copy                                 #migrate時指定copy
                 --cluster-replace                              #migrate時指定replace
  help           

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

注意:Redis Cluster最低要求是3個主節點

① 建立叢集主節點

redis-cli --cluster create 192.168.163.132:6379 192.168.163.132:6380 192.168.163.132:6381
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 slots) master
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes #slot分配
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 192.168.163.132:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 slots) master
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
View Code

② 建立叢集主從節點

/redis-cli --cluster create 192.168.163.132:6379 192.168.163.132:6380 192.168.163.132:6381 192.168.163.132:6382 192.168.163.132:6383 192.168.163.132:6384 --cluster-replicas 1

說明:--cluster-replicas 引數為數字,1表示每個主節點需要1個從節點。

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.163.132:6383 to 192.168.163.132:6379
Adding replica 192.168.163.132:6384 to 192.168.163.132:6380
Adding replica 192.168.163.132:6382 to 192.168.163.132:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 slots) master
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382
   replicates 56005b9413cbf225783906307a2631109e753f8f
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383
   replicates 117457eab5071954faab5e81c3170600d5192270
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 192.168.163.132:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382
   slots: (0 slots) slave
   replicates 56005b9413cbf225783906307a2631109e753f8f
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383
   slots: (0 slots) slave
   replicates 117457eab5071954faab5e81c3170600d5192270
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384
   slots: (0 slots) slave
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
View Code

通過該方式建立的帶有從節點的機器不能夠自己手動指定主節點,所以如果需要指定的話,需要自己手動指定,先使用①建立好主節點後,再通過③來處理。

③ 新增叢集主節點

redis-cli --cluster add-node 192.168.163.132:6382 192.168.163.132:6379 

說明:為一個指定叢集新增節點,需要先連到該叢集的任意一個節點IP(192.168.163.132:6379),再把新節點加入。該2個引數的順序有要求:新加入的節點放前

>>> Adding node 192.168.163.132:6382 to cluster 192.168.163.132:6379
>>> Performing Cluster Check (using node 192.168.163.132:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 slots) master
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
[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.168.163.132:6382 to make it join the cluster.
[OK] New node added correctly.
View Code

④ 新增叢集從節點

redis-cli --cluster add-node 192.168.163.132:6382 192.168.163.132:6379 --cluster-slave --cluster-master-id 117457eab5071954faab5e81c3170600d5192270

說明:把6382節點加入到6379節點的叢集中,並且當做node_id為 117457eab5071954faab5e81c3170600d5192270 的從節點。如果不指定 --cluster-master-id 會隨機分配到任意一個主節點。

>>> Adding node 192.168.163.132:6382 to cluster 192.168.163.132:6379
>>> Performing Cluster Check (using node 192.168.163.132:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 slots) master
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
[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.168.163.132:6382 to make it join the cluster.
Waiting for the cluster to join
..
>>> Configure node as replica of 192.168.163.132:6379.
View Code

⑤ 刪除節點

redis-cli --cluster del-node 192.168.163.132:6384 f6a6957421b80409106cb36be3c7ba41f3b603ff

說明:指定IP、埠和node_id 來刪除一個節點,從節點可以直接刪除,主節點不能直接刪除,刪除之後,該節點會被shutdown。

刪除從節點:
redis-cli --cluster del-node 192.168.163.132:6384 f6a6957421b80409106cb36be3c7ba41f3b603ff
>>> Removing node f6a6957421b80409106cb36be3c7ba41f3b603ff from cluster 192.168.163.132:6384
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

刪除主節點:
redis-cli --cluster del-node 192.168.163.132:6380 815da8448f5d5a304df0353ca10d8f9b77016b28
>>> Removing node 815da8448f5d5a304df0353ca10d8f9b77016b28 from cluster 192.168.163.132:6380
[ERR] Node 192.168.163.132:6380 is not empty! Reshard data away and try again.
View Code

注意:當被刪除掉的節點重新起來之後不能自動加入叢集,但其和主的複製還是正常的,也可以通過該節點看到叢集資訊(通過其他正常節點已經看不到該被del-node節點的資訊)。

如果想要再次加入叢集,則需要先在該節點執行cluster reset,再用add-node進行新增,進行增量同步複製。

到此,目前整個叢集的狀態如下:

192.168.163.132:6379> cluster nodes
815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380@16380 master - 0 1569748297177 2 connected 5461-10922
0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383@16383 slave 56005b9413cbf225783906307a2631109e753f8f 0 1569748295000 4 connected
3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382@16382 slave 815da8448f5d5a304df0353ca10d8f9b77016b28 0 1569748295000 5 connected
117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379@16379 myself,master - 0 1569748297000 1 connected 0-5460
56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381@16381 master - 0 1569748295000 3 connected 10923-16383
f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384@16384 slave 117457eab5071954faab5e81c3170600d5192270 0 1569748298185 6 connected

⑥ 檢查叢集

redis-cli --cluster check 192.168.163.132:6384 --cluster-search-multiple-owners

說明:任意連線一個叢集節點,進行叢集狀態檢查

redis-cli --cluster check 192.168.163.132:6384 --cluster-search-multiple-owners
192.168.163.132:6380 (815da844...) -> 0 keys | 5462 slots | 1 slaves.
192.168.163.132:6381 (56005b94...) -> 0 keys | 5461 slots | 1 slaves.
192.168.163.132:6379 (117457ea...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 2 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.163.132:6384)
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384
   slots: (0 slots) slave
   replicates 117457eab5071954faab5e81c3170600d5192270
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383
   slots: (0 slots) slave
   replicates 56005b9413cbf225783906307a2631109e753f8f
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382
   slots: (0 slots) slave
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 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.
>>> Check for multiple slot owners...
View Code

⑦ 叢集資訊檢視

redis-cli --cluster info 192.168.163.132:6384

說明:檢查key、slots、從節點個數的分配情況

/redis-cli --cluster info 192.168.163.132:6384
192.168.163.132:6380 (815da844...) -> 0 keys | 5462 slots | 1 slaves.
192.168.163.132:6381 (56005b94...) -> 0 keys | 5461 slots | 1 slaves.
192.168.163.132:6379 (117457ea...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 2 keys in 3 masters.
0.00 keys per slot on average.

⑧ 修復叢集

redis-cli --cluster fix 192.168.163.132:6384 --cluster-search-multiple-owners

說明:修復叢集和槽的重複分配問題

redis-cli --cluster fix 192.168.163.132:6384 --cluster-search-multiple-owners
192.168.163.132:6380 (815da844...) -> 0 keys | 5462 slots | 1 slaves.
192.168.163.132:6381 (56005b94...) -> 0 keys | 5461 slots | 1 slaves.
192.168.163.132:6379 (117457ea...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 2 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.163.132:6384)
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384
   slots: (0 slots) slave
   replicates 117457eab5071954faab5e81c3170600d5192270
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383
   slots: (0 slots) slave
   replicates 56005b9413cbf225783906307a2631109e753f8f
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382
   slots: (0 slots) slave
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 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.
>>> Check for multiple slot owners...
View Code

⑨ 設定叢集的超時時間 

redis-cli --cluster set-timeout 192.168.163.132:6382 10000

說明:連線到叢集的任意一節點來設定叢集的超時時間引數cluster-node-timeout

redis-cli --cluster set-timeout 192.168.163.132:6382 10000
>>> Reconfiguring node timeout in every cluster node...
*** New timeout set for 192.168.163.132:6382
*** New timeout set for 192.168.163.132:6384
*** New timeout set for 192.168.163.132:6383
*** New timeout set for 192.168.163.132:6379
*** New timeout set for 192.168.163.132:6381
*** New timeout set for 192.168.163.132:6380
>>> New node timeout set. 6 OK, 0 ERR.

⑩ 叢集中執行相關命令

redis-cli --cluster call 192.168.163.132:6381 config set requirepass cc
redis-cli -a cc --cluster call 192.168.163.132:6381 config set masterauth cc
redis-cli -a cc --cluster call 192.168.163.132:6381 config rewrite

說明:連線到叢集的任意一節點來對整個叢集的所有節點進行設定。

redis-cli --cluster call 192.168.163.132:6381 config set cluster-node-timeout 12000
>>> Calling config set cluster-node-timeout 12000
192.168.163.132:6381: OK
192.168.163.132:6383: OK
192.168.163.132:6379: OK
192.168.163.132:6384: OK
192.168.163.132:6382: OK
192.168.163.132:6380: OK
...
...

到此,相關叢集的基本操作已經介紹完,現在說明叢集遷移的相關操作。

遷移相關

① 線上遷移slot :線上把叢集的一些slot從叢集原來slot節點遷移到新的節點,即可以完成叢集的線上橫向擴容和縮容。有2種方式進行遷移

一是根據提示來進行操作:

直接連線到叢集的任意一節點
redis-cli -a cc --cluster reshard 192.168.163.132:6379

資訊如下:

redis-cli -a cc --cluster reshard 192.168.163.132:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.163.132:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383
   slots: (0 slots) slave
   replicates 56005b9413cbf225783906307a2631109e753f8f
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382
   slots: (0 slots) slave
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384
   slots: (0 slots) slave
   replicates 117457eab5071954faab5e81c3170600d5192270
[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)? 1
What is the receiving node ID? 815da8448f5d5a304df0353ca10d8f9b77016b28
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: 117457eab5071954faab5e81c3170600d5192270
Source node #2: done

Ready to move 1 slots.
  Source nodes:
    M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 0 from 117457eab5071954faab5e81c3170600d5192270
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 192.168.163.132:6379 to 192.168.163.132:6380: 
View Code

二是根據引數進行操作:

redis-cli -a cc --cluster reshard 192.168.163.132:6379 --cluster-from 117457eab5071954faab5e81c3170600d5192270 --cluster-to 815da8448f5d5a304df0353ca10d8f9b77016b28 --cluster-slots 10 --cluster-yes --cluster-timeout 5000 --cluster-pipeline 10 --cluster-replace

說明:連線到叢集的任意一節點來對指定節點指定數量的slot進行遷移到指定的節點。 

>>> Performing Cluster Check (using node 192.168.163.132:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[11-5460] (5450 slots) master
   1 additional replica(s)
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[0-10],[5461-10922] (5473 slots) master
   1 additional replica(s)
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383
   slots: (0 slots) slave
   replicates 56005b9413cbf225783906307a2631109e753f8f
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382
   slots: (0 slots) slave
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384
   slots: (0 slots) slave
   replicates 117457eab5071954faab5e81c3170600d5192270
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Ready to move 10 slots.
  Source nodes:
    M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
       slots:[11-5460] (5450 slots) master
       1 additional replica(s)
  Destination node:
    M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
       slots:[0-10],[5461-10922] (5473 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 11 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 12 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 13 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 14 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 15 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 16 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 17 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 18 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 19 from 117457eab5071954faab5e81c3170600d5192270
    Moving slot 20 from 117457eab5071954faab5e81c3170600d5192270
Moving slot 11 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 12 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 13 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 14 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 15 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 16 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 17 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 18 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 19 from 192.168.163.132:6379 to 192.168.163.132:6380: 
Moving slot 20 from 192.168.163.132:6379 to 192.168.163.132:6380: 
View Code

② 平衡(rebalance)slot :

1)平衡叢集中各個節點的slot數量

redis-cli -a cc --cluster rebalance 192.168.163.132:6379
>>> Performing Cluster Check (using node 192.168.163.132:6379)
[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.00
Moving 522 slots from 192.168.163.132:6380 to 192.168.163.132:6379
##########################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 500 slots from 192.168.163.132:6380 to 192.168.163.132:6381
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
View Code

 2)根據叢集中各個節點設定的權重等平衡slot數量(不執行,只模擬)

redis-cli -a cc --cluster rebalance --cluster-weight 117457eab5071954faab5e81c3170600d5192270=5 815da8448f5d5a304df0353ca10d8f9b77016b28=4 56005b9413cbf225783906307a2631109e753f8f=3 --cluster-simulate 192.168.163.132:6379

③ 匯入叢集

redis-cli --cluster import 192.168.163.132:6379 --cluster-from 192.168.163.132:9021 --cluster-replace

說明:外部Redis例項(9021)匯入到叢集中的任意一節點。

>>> Importing data from 192.168.163.132:9021 to cluster 192.168.163.132:6379
>>> Performing Cluster Check (using node 192.168.163.132:6379)
M: 117457eab5071954faab5e81c3170600d5192270 192.168.163.132:6379
   slots:[1366-5961],[11423-12287] (5461 slots) master
   1 additional replica(s)
M: 815da8448f5d5a304df0353ca10d8f9b77016b28 192.168.163.132:6380
   slots:[1365],[5962-11422] (5462 slots) master
   1 additional replica(s)
S: 0c21b6cee354594a23f4d5abf0d01b48bdc96d55 192.168.163.132:6383
   slots: (0 slots) slave
   replicates 56005b9413cbf225783906307a2631109e753f8f
S: 3a1d04983ab6c4ae853f9602dd922d4ebadc4dbf 192.168.163.132:6382
   slots: (0 slots) slave
   replicates 815da8448f5d5a304df0353ca10d8f9b77016b28
M: 56005b9413cbf225783906307a2631109e753f8f 192.168.163.132:6381
   slots:[0-1364],[12288-16383] (5461 slots) master
   1 additional replica(s)
S: f6a6957421b80409106cb36be3c7ba41f3b603ff 192.168.163.132:6384
   slots: (0 slots) slave
   replicates 117457eab5071954faab5e81c3170600d5192270
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** Importing 97847 keys from DB 0
Migrating 9223372011174675807 to 192.168.163.132:6381: OK
Migrating 9223372033047675807 to 192.168.163.132:6381: OK
...
...
View Code

注意:測試下來發現引數--cluster-replace沒有用,如果叢集中已經包含了某個key,在匯入的時候會失敗,不會覆蓋,只有清空叢集key才能匯入。

*** Importing 97847 keys from DB 0
Migrating 9223372011174675807 to 192.168.163.132:6381: Source 192.168.163.132:9021 replied with error:
ERR Target instance replied with error: BUSYKEY Target key name already exists

並且發現如果叢集設定了密碼,也會匯入失敗,需要設定叢集密碼為空才能進行匯入(call)。通過monitor(9021)的時候發現,在migrate的時候需要密碼進行auth認證。 

總結:

      Redis Cluster 通過redis-cli --cluster來建立和管理叢集的方式和 redis-trib.rb指令碼是絕大部分都是一樣的,所以對於比較熟悉 redis-trib.rb 指令碼的,使用--cluster也非常順手。

  

&n