redis節點管理-新增主節點


原文:http://blog.sina.com.cn/s/blog_53b45c4d0102wg11.html

 

集群節點添加

 

   節點新增包括新增主節點、從節點兩種情況。以下分別做一下測試:

    1、新增主節點

 

       新增一個節點7007作為主節點修改配置文件

       

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# cp -r  redis01 redis07  
  2. [root@localhost redis-cluster]# cd redis07/  
  3. [root@localhost redis07]# sed -i "s/7001/7007/g" ./redis.conf   

 

 

      啟動7007redis服務

 

 

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis07]# ./redis-server redis.conf   
  2. [root@localhost redis07]# netstat -anp | grep 7007  
  3. tcp        0      0 127.0.0.1:17007         0.0.0.0:*               LISTEN      13441/./redis-serve   
  4. tcp        0      0 127.0.0.1:7007          0.0.0.0:*               LISTEN      13441/./redis-serve   
  5. [root@localhost redis07]#   


  上面可以看到,7007已經啟動,現在加入集群中。添加使用redis-trib.rb的add-node命令

 

 

 

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. ./redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7002  


    add-node是加入集群節點,127.0.0.1:7007為要加入的節點,127.0.0.1:7002 表示加入的集群的一個節點,用來辨識是哪個集群,理論上那個集群的節點都可以。

 

    執行以下add-node

      

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# ./redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7002  
  2. >>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7002  
  3. >>> Performing Cluster Check (using node 127.0.0.1:7002)  
  4. S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002  
  5.    slots: (0 slots) slave  
  6.    replicates a5db243087d8bd423b9285fa8513eddee9bb59a6  
  7. M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003  
  8.    slots:10923-16383 (5461 slots) master  
  9.    1 additional replica(s)  
  10. M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005  
  11.    slots:5461-10922 (5462 slots) master  
  12.    1 additional replica(s)  
  13. S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004  
  14.    slots: (0 slots) slave  
  15.    replicates dd19221c404fb2fc4da37229de56bab755c76f2b  
  16. S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006  
  17.    slots: (0 slots) slave  
  18.    replicates f9886c71e98a53270f7fda961e1c5f730382d48f  
  19. M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001  
  20.    slots:0-5460 (5461 slots) master  
  21.    1 additional replica(s)  
  22. [OK] All nodes agree about slots configuration.  
  23. >>> Check for open slots...  
  24. >>> Check slots coverage...  
  25. [OK] All 16384 slots covered.  
  26. >>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster.  
  27. [OK] New node added correctly.  
  28. [root@localhost redis-cluster]#   

 

 

     可以看到7007加入這個Cluster,並成為一個新的節點。

     可以check以下7007節點狀態

      

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7007  
  2. >>> Performing Cluster Check (using node 127.0.0.1:7007)  
  3. M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007  
  4.    slots: (0 slots) master  
  5.    0 additional replica(s)  
  6. S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006  
  7.    slots: (0 slots) slave  
  8.    replicates f9886c71e98a53270f7fda961e1c5f730382d48f  
  9. M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001  
  10.    slots:0-5460 (5461 slots) master  
  11.    1 additional replica(s)  
  12. M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003  
  13.    slots:10923-16383 (5461 slots) master  
  14.    1 additional replica(s)  
  15. S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002  
  16.    slots: (0 slots) slave  
  17.    replicates a5db243087d8bd423b9285fa8513eddee9bb59a6  
  18. M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005  
  19.    slots:5461-10922 (5462 slots) master  
  20.    1 additional replica(s)  
  21. S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004  
  22.    slots: (0 slots) slave  
  23.    replicates dd19221c404fb2fc4da37229de56bab755c76f2b  
  24. [OK] All nodes agree about slots configuration.  
  25. >>> Check for open slots...  
  26. >>> Check slots coverage...  
  27. [OK] All 16384 slots covered.  
  28. [root@localhost redis-cluster]#   

 

 

  M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007
   slots: (0 slots) master
   0 additional replica(s)

  上面信息可以看到有4個M節點,3個S節點,7007成為了M主節點,它沒有附屬的從節點,而且Cluster並未給7007分配哈希卡槽(0 slots)。

    

   可以從客戶端連接集群查看一下,集群節點的連接情況

 

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# cd redis07/  
  2. [root@localhost redis07]# ./redis-cli -c -p 7007  
  3. 127.0.0.1:7007> cluster nodes  
  4. 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006 slave f9886c71e98a53270f7fda961e1c5f730382d48f 0 1462955393326 3 connected  
  5. dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001 master - 0 1462955388247 1 connected 0-5460  
  6. ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007 myself,master - 0 0 0 connected  
  7. f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003 master - 0 1462955390270 3 connected 10923-16383  
  8. 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002 slave a5db243087d8bd423b9285fa8513eddee9bb59a6 0 1462955394334 7 connected  
  9. a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005 master - 0 1462955392309 7 connected 5461-10922  
  10. 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004 slave dd19221c404fb2fc4da37229de56bab755c76f2b 0 1462955389663 1 connected  
  11. 127.0.0.1:7007>   

 

 

     redis-cluster在新增節點時並未分配卡槽,需要我們手動對集群進行重新分片遷移數據,需要重新分片命令 reshard

    

              redis-trib.rb reshard 127.0.0.1:7005

 

     這個命令是用來遷移slot節點的,后面的127.0.0.1:7005是表示是哪個集群,端口填[7000-7007]都可以,執行結果如下:

    

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# ./redis-trib.rb reshard 127.0.0.1:7005  
  2. >>> Performing Cluster Check (using node 127.0.0.1:7005)  
  3. M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005  
  4.    slots:5461-10922 (5462 slots) master  
  5.    1 additional replica(s)  
  6. S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004  
  7.    slots: (0 slots) slave  
  8.    replicates dd19221c404fb2fc4da37229de56bab755c76f2b  
  9. M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003  
  10.    slots:10923-16383 (5461 slots) master  
  11.    1 additional replica(s)  
  12. S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002  
  13.    slots: (0 slots) slave  
  14.    replicates a5db243087d8bd423b9285fa8513eddee9bb59a6  
  15. M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007  
  16.    slots: (0 slots) master  
  17.    0 additional replica(s)  
  18. M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001  
  19.    slots:0-5460 (5461 slots) master  
  20.    1 additional replica(s)  
  21. S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006  
  22.    slots: (0 slots) slave  
  23.    replicates f9886c71e98a53270f7fda961e1c5f730382d48f  
  24. [OK] All nodes agree about slots configuration.  
  25. >>> Check for open slots...  
  26. >>> Check slots coverage...  
  27. [OK] All 16384 slots covered.  
  28. How many slots do you want to move (from 1 to 16384)?   

 

 

      它提示我們需要遷移多少slot到7007上,我們平分16384個哈希槽給4個節點:16384/4 = 4096,我們需要移動4096個槽點到7007上。

 

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [OK] All 16384 slots covered.  
  2. How many slots do you want to move (from 1 to 16384)? 4096  
  3. What is the receiving node ID?   

 

 

     需要輸入7007的節點id,ee3efb90e5ac0725f15238a64fc60a18a71205d7

 

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. Please enter all the source node IDs.  
  2.   Type 'all' to use all the nodes as source nodes for the hash slots.  
  3.   Type 'done' once you entered all the source nodes IDs.  
  4. Source node #1:  

 

 

      redis-trib 會向你詢問重新分片的源節點(source node),即,要從特點的哪個節點中取出 4096 個哈希槽,還是從全部節點提取4096個哈希槽, 並將這些槽移動到7007節點上面。

     如果我們不打算從特定的節點上取出指定數量的哈希槽,那么可以向redis-trib輸入 all,這樣的話, 集群中的所有主節點都會成為源節點,redis-trib從各個源節點中各取出一部分哈希槽,湊夠4096個,然后移動到7007節點上:

     

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. Source node #1:all  

 

 

    然后開始從別的主節點遷移哈希槽,並且確認。

 

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1.     Moving slot 1343 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  2.     Moving slot 1344 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  3.     Moving slot 1345 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  4.     Moving slot 1346 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  5.     Moving slot 1347 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  6.     Moving slot 1348 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  7.     Moving slot 1349 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  8.     Moving slot 1350 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  9.     Moving slot 1351 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  10.     Moving slot 1352 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  11.     Moving slot 1353 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  12.     Moving slot 1354 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  13.     Moving slot 1355 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  14.     Moving slot 1356 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  15.     Moving slot 1357 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  16.     Moving slot 1358 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  17.     Moving slot 1359 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  18.     Moving slot 1360 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  19.     Moving slot 1361 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  20.     Moving slot 1362 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  21.     Moving slot 1363 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  22.     Moving slot 1364 from dd19221c404fb2fc4da37229de56bab755c76f2b  
  23. Do you want to proceed with the proposed reshard plan (yes/no)? yes  


     確認之后,redis-trib就開始執行分片操作,將哈希槽一個一個從源主節點移動到7007目標主節點。

 

     重新分片結束后我們可以check以下節點的分配情況。

     

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# ./redis-trib.rb check 127.0.0.1:7001  
  2. >>> Performing Cluster Check (using node 127.0.0.1:7001)  
  3. M: dd19221c404fb2fc4da37229de56bab755c76f2b 127.0.0.1:7001  
  4.    slots:1365-5460 (4096 slots) master  
  5.    1 additional replica(s)  
  6. M: ee3efb90e5ac0725f15238a64fc60a18a71205d7 127.0.0.1:7007  
  7.    slots:0-1364,5461-6826,10923-12287 (4096 slots) master  
  8.    0 additional replica(s)  
  9. M: a5db243087d8bd423b9285fa8513eddee9bb59a6 127.0.0.1:7005  
  10.    slots:6827-10922 (4096 slots) master  
  11.    1 additional replica(s)  
  12. S: 8bb3ede48319b46d0015440a91ab277da9353c8b 127.0.0.1:7006  
  13.    slots: (0 slots) slave  
  14.    replicates f9886c71e98a53270f7fda961e1c5f730382d48f  
  15. M: f9886c71e98a53270f7fda961e1c5f730382d48f 127.0.0.1:7003  
  16.    slots:12288-16383 (4096 slots) master  
  17.    1 additional replica(s)  
  18. S: 1f07d76585bfab35f91ec711ac53ab4bc00f2d3a 127.0.0.1:7002  
  19.    slots: (0 slots) slave  
  20.    replicates a5db243087d8bd423b9285fa8513eddee9bb59a6  
  21. S: 50ce1ea59106b4c2c6bc502593a6a7a7dabf5041 127.0.0.1:7004  
  22.    slots: (0 slots) slave  
  23.    replicates dd19221c404fb2fc4da37229de56bab755c76f2b  
  24. [OK] All nodes agree about slots configuration.  
  25. >>> Check for open slots...  
  26. >>> Check slots coverage...  
  27. [OK] All 16384 slots covered.  
  28. [root@localhost redis-cluster]#   

 

 

   slots:0-1364,5461-6826,10923-12287 (4096 slots) master

   可以看到7007節點分片的哈希槽片不是連續的,間隔的移動。

   

[plain]  view plain  copy
 
  在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# cd redis07/  
  2. [root@localhost redis07]# ./redis-cli -c 7007  
  3. Could not connect to Redis at 127.0.0.1:6379: Connection refused  
  4. [root@localhost redis07]# ./redis-cli -c -p 7007  
  5. 127.0.0.1:7007> keys *  
  6. 1) "name"  
  7. 2) "age"  
  8. 127.0.0.1:7007>   
  9. 127.0.0.1:7007>   

 

 

    可以看到將7001的age[741]和name[5798]移動到7007節點上,

    主節點7007添加成功。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM