redis cluster集群理解


Redis Cluster集群

一、redis-cluster設計

Redis集群搭建的方式有多種,例如使用zookeeper等,但從redis 3.0之后版本支持redis-cluster集群,Redis-Cluster采用無中心結構,每個節點保存數據和整個集群狀態,每個節點都和其他所有節點連接。其redis-cluster架構圖如下:

其結構特點:

     1、所有的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬。
     2、節點的fail是通過集群中超過半數的節點檢測失效時才生效。
     3、客戶端與redis節點直連,不需要中間proxy層.客戶端不需要連接集群所有節點,連接集群中任何一個可用節點即可。
     4、redis-cluster把所有的物理節點映射到[0-16383]slot上(不一定是平均分配),cluster 負責維護node<->slot<->value。

     5、Redis集群預分好16384個桶,當需要在 Redis 集群中放置一個 key-value 時,根據 CRC16(key) mod 16384的值,決定將一個key放到哪個桶中。

 

   1、redis cluster節點分配

 

   現在我們是三個主節點分別是:A, B, C 三個節點,它們可以是一台機器上的三個端口,也可以是三台不同的服務器。那么,采用哈希槽 (hash slot)的方式來分配16384個slot 的話,它們三個節點分別承擔的slot 區間是:


      節點A覆蓋0-5460;
      節點B覆蓋5461-10922;
      節點C覆蓋10923-16383.

    

     獲取數據:

      如果存入一個值,按照redis cluster哈希槽的算法: CRC16('key')384 = 6782。 那么就會把這個key 的存儲分配到 B 上了。同樣,當我連接(A,B,C)任何一個節點想獲取'key'這個key時,也會這樣的算法,然后內部跳轉到B節點上獲取數據 

    

     新增一個主節點:

      新增一個節點D,redis cluster的這種做法是從各個節點的前面各拿取一部分slot到D上,我會在接下來的實踐中實驗。大致就會變成這樣:
  

    節點A覆蓋1365-5460
    節點B覆蓋6827-10922
    節點C覆蓋12288-16383
    節點D覆蓋0-1364,5461-6826,10923-12287


     同樣刪除一個節點也是類似,移動完成后就可以刪除這個節點了。

 

 

     2、Redis Cluster主從模式

 

          redis cluster 為了保證數據的高可用性,加入了主從模式,一個主節點對應一個或多個從節點,主節點提供數據存取,從節點則是從主節點拉取數據備份,當這個主節點掛掉后,就會有這個從節點選取一個來充當主節點,從而保證集群不會掛掉。

      上面那個例子里, 集群有ABC三個主節點, 如果這3個節點都沒有加入從節點,如果B掛掉了,我們就無法訪問整個集群了。A和C的slot也無法訪問。

     所以我們在集群建立的時候,一定要為每個主節點都添加了從節點, 比如像這樣, 集群包含主節點A、B、C, 以及從節點A1、B1、C1, 那么即使B掛掉系統也可以繼續正確工作。

     B1節點替代了B節點,所以Redis集群將會選擇B1節點作為新的主節點,集群將會繼續正確地提供服務。 當B重新開啟后,它就會變成B1的從節點。

    不過需要注意,如果節點B和B1同時掛了,Redis集群就無法繼續正確地提供服務了。

 

二、redis集群的搭建

    集群中至少應該有奇數個節點,所以至少有三個節點,每個節點至少有一個備份節點,所以下面使用6節點(主節點、備份節點由redis-cluster集群確定)。

 

   

     下面使用redis-3.2.0安裝,下載地址   

     1、安裝redis節點指定端口

         解壓redis壓縮包,編譯安裝

         

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-3.2.0]# tar xzf redis-3.2.0.tar.gz  
  2. [root@localhost redis-3.2.0]# cd redis-3.2.0  
  3. [root@localhost redis-3.2.0]# make  
  4. [root@localhost redis01]# make install PREFIX=/usr/andy/redis-cluster  

 

 

        在redis-cluster下 修改bin文件夾為redis01,復制redis.conf配置文件

       

     配置redis的配置文件redis.conf

 

         daemonize yes #后台啟動

        port 7001 #修改端口號,從7001到7006

        cluster-enabled yes #開啟cluster,去掉注釋

        cluster-config-file nodes.conf

        cluster-node-timeout 15000

        appendonly yes

 

 

        復制六份,修改對應的端口號

        

 

     2、安裝redis-trib所需的 ruby腳本

          復制redis解壓文件src下的redis-trib.rb文件到redis-cluster目錄

          

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# cp /usr/andy/redis/redis-3.2.0/src/redis-trib.rb ./  

 

 

         安裝ruby環境:

          

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# yum install ruby  
  2. [root@localhost redis-cluster]# yum install rubygems  

 

 

        安裝redis-trib.rb運行依賴的ruby的包redis-3.2.2.gem,下載

        

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# gem install redis-3.2.2.gem  

 

 

     3、啟動所有的redis節點

       可以寫一個命令腳本start-all.sh

       

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. cd redis01  
  2. ./redis-server redis.conf  
  3. cd ..  
  4. cd redis02  
  5. ./redis-server redis.conf  
  6. cd ..  
  7. cd redis03  
  8. ./redis-server redis.conf  
  9. cd ..  
  10. cd redis04  
  11. ./redis-server redis.conf  
  12. cd ..  
  13. cd redis05  
  14. ./redis-server redis.conf  
  15. cd ..  
  16. cd redis06  
  17. ./redis-server redis.conf  
  18. cd ..  

 

 

     設置權限啟動

     

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# chmod 777 start-all.sh   
  2. [root@localhost redis-cluster]# ./start-all.sh   

 

       查看redis進程啟動狀態

 

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# ps -ef | grep redis  
  2.   
  3. root       4547      1  0 23:12 ?        00:00:00 ./redis-server 127.0.0.1:7001 [cluster]  
  4. root       4551      1  0 23:12 ?        00:00:00 ./redis-server 127.0.0.1:7002 [cluster]  
  5. root       4555      1  0 23:12 ?        00:00:00 ./redis-server 127.0.0.1:7003 [cluster]  
  6. root       4559      1  0 23:12 ?        00:00:00 ./redis-server 127.0.0.1:7004 [cluster]  
  7. root       4563      1  0 23:12 ?        00:00:00 ./redis-server 127.0.0.1:7005 [cluster]  
  8. root       4567      1  0 23:12 ?        00:00:00 ./redis-server 127.0.0.1:7006 [cluster]  
  9. root       4840   4421  0 23:26 pts/1    00:00:00 grep --color=auto redis  

   

  可以看到redis的6個節點已經啟動成功

   

    殺死全部的幾點:

     

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# pkill -9 redis  

 

 

 

     4、使用redis-trib.rb創建集群

      

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006  

 

 

       使用create命令 --replicas 1 參數表示為每個主節點創建一個從節點,其他參數是實例的地址集合。

      

       

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006  
  2. >>> Creating cluster  
  3. >>> Performing hash slots allocation on 6 nodes...  
  4. Using 3 masters:  
  5. 127.0.0.1:7001  
  6. 127.0.0.1:7002  
  7. 127.0.0.1:7003  
  8. Adding replica 127.0.0.1:7004 to 127.0.0.1:7001  
  9. Adding replica 127.0.0.1:7005 to 127.0.0.1:7002  
  10. Adding replica 127.0.0.1:7006 to 127.0.0.1:7003  
  11. M: dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001  
  12.    slots:0-5460 (5461 slots) master  
  13. M: e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002  
  14.    slots:5461-10922 (5462 slots) master  
  15. M: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003  
  16.    slots:10923-16383 (5461 slots) master  
  17. S: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004  
  18.    replicates dfd510594da614469a93a0a70767ec9145aefb1a  
  19. S: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005  
  20.    replicates e02eac35110bbf44c61ff90175e04d55cca097ff  
  21. S: 10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006  
  22.    replicates 4385809e6f4952ecb122dbfedbee29109d6bb234  
  23. Can I set the above configuration? (type 'yes' to accept): yes  
  24. >>> Nodes configuration updated  
  25. >>> Assign a different config epoch to each node  
  26. >>> Sending CLUSTER MEET messages to join the cluster  
  27. Waiting for the cluster to join......  
  28. >>> Performing Cluster Check (using node 127.0.0.1:7001)  
  29. M: dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001  
  30.    slots:0-5460 (5461 slots) master  
  31. M: e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002  
  32.    slots:5461-10922 (5462 slots) master  
  33. M: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003  
  34.    slots:10923-16383 (5461 slots) master  
  35. M: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004  
  36.    slots: (0 slots) master  
  37.    replicates dfd510594da614469a93a0a70767ec9145aefb1a  
  38. M: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005  
  39.    slots: (0 slots) master  
  40.    replicates e02eac35110bbf44c61ff90175e04d55cca097ff  
  41. M: 10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006  
  42.    slots: (0 slots) master  
  43.    replicates 4385809e6f4952ecb122dbfedbee29109d6bb234  
  44. [OK] All nodes agree about slots configuration.  
  45. >>> Check for open slots...  
  46. >>> Check slots coverage...  
  47. [OK] All 16384 slots covered.  

 

 

       上面顯示創建成功,有3個主節點,3個從節點,每個節點都是成功連接狀態。

 

       3個主節點[M]以及分配的哈希卡槽如下:

  M: dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001
       slots:0-5460 (5461 slots) master
  M: e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002
       slots:5461-10922 (5462 slots) master
  M: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003
       slots:10923-16383 (5461 slots) master

      

          3個從節點[S]以及附屬的主節點如下:

          S: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004
             replicates dfd510594da614469a93a0a70767ec9145aefb1a
          S: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005
             replicates e02eac35110bbf44c61ff90175e04d55cca097ff
          S: 10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006
             replicates 4385809e6f4952ecb122dbfedbee29109d6bb234

 

        以上集群安裝成功了,如果安裝未成功報如下錯誤
       >>> Creating cluster
       [ERR] Sorry, can't connect to node  ....

          需要安裝最新的ruby源碼,下載

           

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# tar -zxvf ruby-2.3.1.tar.gz   
  2. [root@localhost redis-cluster]# cd    
  3. [root@localhost redis-cluster]# ./configure --prefix=/usr/local/ruby-2.3.1    
  4. [root@localhost redis-cluster]# make && make install       
  5. [root@localhost redis-cluster]#gem install redis    

 

         還有一種情況是,在VMware做測試的時間(都在一台服務器時),ip應該使用127.0.0.1,如果使用局域網ip,也會報節點創建失敗。

        

三、redis集群的測試

     1、測試存取值

         客戶端連接集群redis-cli需要帶上 -c ,redis-cli -c -p 端口號

     

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis01]# ./redis-cli -c -p 7001  
  2. 127.0.0.1:7001> set name andy  
  3. -> Redirected to slot [5798] located at 127.0.0.1:7002  
  4. OK  
  5. 127.0.0.1:7002> get name  
  6. "andy"  
  7. 127.0.0.1:7002>   

 

 

        根據redis-cluster的key值分配,name應該分配到節點7002[5461-10922]上,上面顯示redis cluster自動從7001跳轉到了7002節點。

       我們可以測試一下7006從節點獲取name值

 

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis06]# ./redis-cli -c -p 7006  
  2. 127.0.0.1:7006> get name  
  3. -> Redirected to slot [5798] located at 127.0.0.1:7002  
  4. "andy"  
  5. 127.0.0.1:7002>   



 

        7006位7003的從節點,從上面也是自動跳轉至7002獲取值,這也是redis cluster的特點,它是去中心化,每個節點都是對等的,連接哪個節點都可以獲取和設置數據。

 

 

四、集群節點選舉

         現在模擬將7002節點掛掉,按照redis-cluster原理會選舉會將 7002的從節點7005選舉為主節點。

        

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# ps -ef | grep redis  
  2. root       7950      1  0 12:50 ?        00:00:28 ./redis-server 127.0.0.1:7001 [cluster]  
  3. root       7952      1  0 12:50 ?        00:00:29 ./redis-server 127.0.0.1:7002 [cluster]  
  4. root       7956      1  0 12:50 ?        00:00:29 ./redis-server 127.0.0.1:7003 [cluster]  
  5. root       7960      1  0 12:50 ?        00:00:29 ./redis-server 127.0.0.1:7004 [cluster]  
  6. root       7964      1  0 12:50 ?        00:00:29 ./redis-server 127.0.0.1:7005 [cluster]  
  7. root       7966      1  0 12:50 ?        00:00:29 ./redis-server 127.0.0.1:7006 [cluster]  
  8. root      11346  10581  0 14:57 pts/2    00:00:00 grep --color=auto redis  
  9. [root@localhost redis-cluster]# kill 7952  

 

 

        在查看集群中的7002節點

 

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


      可以看到集群連接不了7002節點,而7005有原來的S轉換為M節點,代替了原來的7002節點。我們可以獲取name值:

 

 

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis01]# ./redis-cli -c -p 7001  
  2. 127.0.0.1:7001> get name  
  3. -> Redirected to slot [5798] located at 127.0.0.1:7005  
  4. "andy"  
  5. 127.0.0.1:7005>   
  6. 127.0.0.1:7005>   


   從7001節點連入,自動跳轉到7005節點,並且獲取name值。

 

 

    現在我們將7002節點恢復,看是否會自動加入集群中以及充當的M還是S節點。

      

[plain]  view plain  copy   在CODE上查看代碼片 派生到我的代碼片
  1. [root@localhost redis-cluster]# cd redis02  
  2. [root@localhost redis02]# ./redis-server redis.conf   
  3. [root@localhost redis02]#   

 

 

     在check一下7002節點

 

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

 

 

    可以看到7002節點變成了a5db243087d8bd423b9285fa8513eddee9bb59a6 7005的從節點。


免責聲明!

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



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