Redis Cluster集羣

1、redis-cluster設計

Redis集羣搭建的方式有多種,例如使用zookeeper等,但從redis 3.0以後版本支持redis-cluster集羣,Redis-Cluster採用無中心結構,每一個節點保存數據和整個集羣狀態,每一個節點都和其餘全部節點鏈接。其redis-cluster架構圖以下:node

其結構特色:redis

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

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

 

   一、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節點上獲取數據 架構

    

     新增一個主節點:app

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

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


     一樣刪除一個節點也是相似,移動完成後就能夠刪除這個節點了。優化

 

 

     二、Redis Cluster主從模式

 

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

      上面那個例子裏, 集羣有ABC三個主節點, 若是這3個節點都沒有加入從節點,若是B掛掉了,咱們就沒法訪問整個集羣了。A和C的slot也沒法訪問。

     因此咱們在集羣創建的時候,必定要爲每一個主節點都添加了從節點, 好比像這樣, 集羣包含主節點A、B、C, 以及從節點A一、B一、C1, 那麼即便B掛掉系統也能夠繼續正確工做。

     B1節點替代了B節點,因此Redis集羣將會選擇B1節點做爲新的主節點,集羣將會繼續正確地提供服務。 當B從新開啓後,它就會變成B1的從節點。

    不過須要注意,若是節點B和B1同時掛了,Redis集羣就沒法繼續正確地提供服務了。

 

2、redis集羣的搭建

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

 

   

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

     一、安裝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

 

 

        複製六份,修改對應的端口號

        

 

     二、安裝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  

 

 

     三、啓動全部的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  

 

 

 

     四、使用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,也會報節點建立失敗。

        

3、redis集羣的測試

     一、測試存取值

         客戶端鏈接集羣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的特色,它是去中心化,每一個節點都是對等的,鏈接哪一個節點均可以獲取和設置數據。

 

 

4、集羣節點選舉

         如今模擬將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的從節點。

相關文章
相關標籤/搜索