redis - cluster命令

一、redis cluster命令行

//集羣(cluster)  
CLUSTER INFO 打印集羣的信息  
CLUSTER NODES 列出集羣當前已知的所有節點(node),以及這些節點的相關信息。   
  
//節點(node)  
CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節點添加到集羣當中,讓它成爲集羣的一份子。  
CLUSTER FORGET <node_id> 從集羣中移除 node_id 指定的節點。  
CLUSTER REPLICATE <node_id> 將當前節點設置爲 node_id 指定的節點的從節點。  
CLUSTER SAVECONFIG 將節點的配置文件保存到硬盤裏面。   
  
//槽(slot)  
CLUSTER ADDSLOTS <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。  
CLUSTER DELSLOTS <slot> [slot ...] 移除一個或多個槽對當前節點的指派。  
CLUSTER FLUSHSLOTS 移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。  
CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麼先讓另一個節點刪除該槽>,然後再進行指派。  
CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。  
CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節點中導入槽 slot 到本節點。  
CLUSTER SETSLOT <slot> STABLE 取消對槽 slot 的導入(import)或者遷移(migrate)。   
  
//鍵 (key)  
CLUSTER KEYSLOT <key> 計算鍵 key 應該被放置在哪個槽上。  
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對數量。  
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個 slot 槽中的鍵。

二、添加節點

1、添加主節點

redis-trib.rb add-node 192.168.10.219:6378 192.168.10.219:6379 

註釋:

192.168.10.219:6378是新增的節點

192.168.10.219:6379集羣任一個舊節點

2、添加從節點

redis-trib.rb add-node --slave --master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.220:6385 192.168.10.219:6379

註釋:

--slave,表示添加的是從節點

--master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2,主節點的node id,在這裏是前面新添加的6378的node id

192.168.10.220:6385,新節點

192.168.10.219:6379集羣任一個舊節點

3、重新分配slot

  1. redis-trib.rb reshard 192.168.10.219:6378 //下面是主要過程  
  2.   
  3. How many slots do you want to move (from 1 to 16384)? 1000 //設置slot數1000  
  4. What is the receiving node ID? 03ccad2ba5dd1e062464bc7590400441fafb63f2 //新節點node id  
  5. Please enter all the source node IDs.  
  6.  Type 'all' to use all the nodes as source nodes for the hash slots.  
  7.  Type 'done' once you entered all the source nodes IDs.  
  8. Source node #1:all //表示全部節點重新洗牌  
  9. Do you want to proceed with the proposed reshard plan (yes/no)? yes //確認重新分 
新增加的主節點,是沒有slots的,主節點如果沒有slot的話,存取數據就都不會被選中。

4、查看一下,集羣情況

redis-trib.rb check 192.168.10.219:6379
三、改變從節點的master

//查看一下6378的從節點  
# redis-cli -p 6378 cluster nodes | grep slave | grep 03ccad2ba5dd1e062464bc7590400441fafb63f2  
 
//將6385加入到新的master  
# redis-cli -c -p 6385 -h 192.168.10.220  
192.168.10.220:6385> cluster replicate 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052  //新master的node id  
OK  
192.168.10.220:6385> quit  
 
//查看新master的slave  
# redis-cli -p 6379 cluster nodes | grep slave | grep 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052
四、刪除節點

1、刪除從節點

redis-trib.rb del-node 192.168.10.220:6385 '9c240333476469e8e2c8e80b089c48f389827265' 
2、刪除主節點

如果主節點有從節點,將從節點轉移到其他主節點

如果主節點有slot,去掉分配的slot,然後在刪除主節點


# redis-trib.rb reshard 192.168.10.219:6378 //取消分配的slot,下面是主要過程  
 
How many slots do you want to move (from 1 to 16384)? 1000 //被刪除master的所有slot數量  
What is the receiving node ID? 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 //接收6378節點slot的master  
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:03ccad2ba5dd1e062464bc7590400441fafb63f2 //被刪除master的node-id  
Source node #2:done   
 
Do you want to proceed with the proposed reshard plan (yes/no)? yes //取消slot後,reshard

新增master節點後,也進行了這一步操作,當時是分配,現在去掉。反着的。

redis-trib.rb del-node 192.168.10.219:6378 '03ccad2ba5dd1e062464bc7590400441fafb63f2' 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章