天天看點

Redis Cluster入門Redis Cluster

Redis Cluster

Redis Cluster入門Redis Cluster

叢集

我們要使用叢集

  1. redis 并發量 10萬 / 每秒 ,但是有些業務需要 100萬的 QPS
  2. 資料量,我們普通機器 16~256g,而我們的業務需要500g

解決方案

  • 分布式 :加機器,友善以後需求擴容

Redis Cluster是Redis的分布式解決方案,在3.0版本正式推出,有效地解決了Redis分布式方面的需求。當遇到單機記憶體、并發、流量等瓶頸時,可以采用Cluster架構方案達到負載均衡的目的。

​ 在Redis Cluster,它們任何兩個節點之間都是互相連通的。用戶端可以與任何一個節點相連接配接,然後就可以通路叢集中的任何一個節點,對其進行存取和其它操作。

Redis Cluster提供的好處:

  • 将資料自動切分到多個節點的能力
  • 當叢集中的一部分節點失效或者無法進行通訊時,仍然可以繼續處理指令請求的能力,擁有自動故障轉移的能力。

    Redis Cluster 和 replication + sentinel 如何選擇:

如果資料量很少,主要是承載高并發高性能的場景,比如你的緩存一般就幾個G,單機就夠了。

  • Replication:一個master,多個slave,要幾個slave跟你的要求的讀吞吐量有關系,結合sentinel叢集,去保證redis主從架構的高可用就行了。
  • Redis Cluster:主要是針對海量資料+高并發+高可用的場景,海量資料,如果資料量很大,建議用Redis Cluster

資料分布

資料分區

Redis Cluster入門Redis Cluster

順序分布和哈希分布

  1. 順序分布
Redis Cluster入門Redis Cluster
  1. 哈希分步
    Redis Cluster入門Redis Cluster

哈希分布

  • 節點取餘分布 : ( hash (key) % nodes )
    • 使用特點的資料(包括redis的鍵或使用者ID),再根據節點數量N,使用公式:hash(key)%N計算出一個0~(N-1)值,來決定資料映射到哪一個節點上。即哈希值對節點總數取餘。
Redis Cluster入門Redis Cluster
  1. 用戶端分片 :哈希 + 取餘
  2. 節點伸縮 : 資料節點關系變化,導緻資料遷移
  3. 遷移數量和添加節點數量有關 :建議翻倍擴容
  • 一緻性哈希分區
    • 一緻性哈希分區(Distributed Hash Table)實作思路是為系統中每個節點配置設定一個token,範圍一般在0~232,這些token構成一個哈希環。資料讀寫執行節點查找操作時,先根據key計算hash值,然後順時針找到第一個大于等于該哈希值的token節點。
      Redis Cluster入門Redis Cluster

上圖就是一個一緻性hash的原了解析。

假設有n1~n4這四台機器,我們對每一台機器配置設定一個唯一token,每次有資料(黃色代表資料),一緻性雜湊演算法規則每次都順時針漂移資料,也就是圖中黃色的資料都指向n3。

這個時候我們需要增加一個節點n5,在n2和n3之間,資料還是會發生漂移(會偏移到大于等于的節點),但是這個時候你是否注意到,其實隻有n2~n3這部分的資料被漂移,其它的資料都是不會變的,這種方式相比節點取餘最大的好處在于加入和删除節點隻影響哈希環中相鄰的節點,對其它節點無影響。

  1. 用戶端分片: 哈希 + 順時針 (優化取餘)
  2. 節點伸縮 : 隻影響臨近節點,但是還是有資料遷移
  3. 翻倍伸縮 :保證最小遷移資料和負載均衡
  • 虛拟槽分區
    • 虛拟槽分區巧妙地使用了哈希空間,使用分散度良好的哈希函數把所有資料映射到一個固定範圍的整數集合中,整數定義為槽(slot)。這個範圍一般遠遠大于節點數,比如Redis Cluster槽範圍是0~16383(也就是說有16383個槽)。槽是叢集内資料管理和遷移的基本機關。采用大範圍槽的主要目的是為了友善資料拆分和叢集擴充。每個節點會負責一定數量的槽。
      Redis Cluster入門Redis Cluster
  1. 預設虛拟槽映射一個資料子集,一半節點數大
  2. 良好的哈希函數 : 例如CRC16
  3. 服務端管理節點、槽、資料:例如 Redis Cluster
  • 對比
分布方式 特點 典型産品
資料分散度高 , 鍵值分布業務無關 ,無法順序通路 ,支援批量操作 一緻性哈希Memcache ,Redis Cluster , 其他緩存産品
資料分散度易傾斜 , 鍵值業務相關 ,可順序通路 , 支援批量操作 BigTable ,HBase

搭建叢集

一、基本架構

  • 節點
    Redis Cluster入門Redis Cluster
  • meet
    Redis Cluster入門Redis Cluster
  • 指配槽
Redis Cluster入門Redis Cluster
  • Redis Cluster特性
    • 複制
    • 高可用
    • 分片

二、兩種安裝

  • 原生指令安裝
  1. 配置開啟節點 redis.conf
port ${port}  //端口
daemonize yes
dir "/opt/redis/redis/data/"
dbfilename "dump-${port}.rdb"  //rdb檔案
logfile "${port}.log"  //log 檔案
cluster-enabled yes
cluster-config-file nodes-${port}.conf  //給 cluster添加自己的配置檔案           

開啟節點

redis-server redis-7000.conf
redis-server redis-7001.conf
redis-server redis-7002.conf
redis-server redis-7003.conf
redis-server redis-7004.conf
redis-server redis-7005.conf
redis-server redis-7006.conf


           

cluster meet ip port // 彼此之間互相感覺

redis-cli -h 1270.0.1 -p 7000 cluster meet 127.0.0.1 7001
redis-cli -h 1270.0.1 -p 7000 cluster meet 127.0.0.1 7002
redis-cli -h 1270.0.1 -p 7000 cluster meet 127.0.0.1 7003
redis-cli -h 1270.0.1 -p 7000 cluster meet 127.0.0.1 7004
redis-cli -h 1270.0.1 -p 7000 cluster meet 127.0.0.1 7005
redis-cli -h 1270.0.1 -p 7000 cluster meet 127.0.0.1 7006
           

cluster addslots slot[slot .....]

redis-cli -h 127.0.0.1 -p 7000 cluster addslots {0...5461}
redis-cli -h 127.0.0.1 -p 7001 cluster addslots {5462...10922}
redis-cli -h 127.0.0.1 -p 7002 cluster addslots {10923...16383}           
  1. 主從

cluster replicate node-id

redis-cli -h 127.0.0.1 -p 7003 cluster replicate ${node-id-7000}

redis-cli -h 127.0.0.1 -p 7004 cluster replicate ${node-id-7001}

redis-cli -h 127.0.0.1 -p 7005 cluster replicate ${node-id-7002}           
  1. 具體安裝

建立檔案

Redis Cluster入門Redis Cluster

端口以及檔案依次後排 7001 ,7002

port 7000
daemonize yes
dir "/opt/software/redis-4.0.9/cluster-test/data"
logfile "/opt/software/redis-4.0.9/cluster-test/logs/7000.log"
#dbfilename不能配置為路徑
dbfilename "dump-7000.rdb"
cluster-enabled yes
cluster-config-file nodes-7000.conf
#是否需要每個節點都可用,叢集才算可用,關閉
cluster-require-full-coverage no           

一次通過配置檔案啟動叢集,我還在conf目錄下,是以這樣啟動

Redis Cluster入門Redis Cluster

有不懂的推薦Redis Cluster 安裝文章

https://blog.csdn.net/fst438060684/article/details/80712433
  • 官方工具安裝

Ruby 環境準備

  1. 下載下傳ruby
wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz           
  1. 安裝 ruby
tar -xvf ruby-2.3.1.tar.gz
./configure -prefix=/usr/local/ruby
make
make install
cd /usr/loacl/ruby
cp bin/ruby /usr/local/bin
cp bin/gem /usr/local/bin
           
  1. 安裝rubygem redis
wget http://rubygems.ory/downloads/redis-3.3.0.gem

gem install -| redis-3.3.0.gem

gem list --check redis gem           

啟動所有節點

redis-server redis-7000.conf
redis-server redis-7001.conf
redis-server redis-7002.conf
redis-server redis-7003.conf
           

redis-trib.rb支援的操作

複制代碼

# redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port
  info            host:port
  fix             host:port
                  --timeout <arg>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  del-node        host:port node_id
  set-timeout     host:port milliseconds
  call            host:port command arg arg .. arg
  import          host:port
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

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

支援的操作如下:

  1. create:建立叢集
  2. check:檢查叢集
  3. info:檢視叢集資訊
  4. fix:修複叢集
  5. reshard:線上遷移slot
  6. rebalance:平衡叢集節點slot數量
  7. add-node:添加新節點
  8. del-node:删除節點
  9. set-timeout:設定節點的逾時時間
  10. call:在叢集所有節點上執行指令
  11. import:将外部redis資料導入叢集

建立叢集

./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:7000
           

--replicas參數指定叢集中每個主節點配備幾個從節點,這裡設定為1。

[root@localhost redis]# ./src/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:7000
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7000 to 127.0.0.1:7003
M: f4ee0a501f9aaf11351787a46ffb4659d45b7bd7 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: 671a0524a616da8b2f50f3d11a74aaf563578e41 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: 18948dab5b07e3726afd1b6a42d5bf6e2f411ba1 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: 34e322ca50a2842e9f3664442cb11c897defba06 127.0.0.1:7004
   replicates f4ee0a501f9aaf11351787a46ffb4659d45b7bd7
S: 62a00566233fbff4467c4031345b1db13cf12b46 127.0.0.1:7005
   replicates 671a0524a616da8b2f50f3d11a74aaf563578e41
S: 2cb649ad3584370c960e2036fb01db834a546114 127.0.0.1:7000
   replicates 18948dab5b07e3726afd1b6a42d5bf6e2f411ba1
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 127.0.0.1:7001)
M: f4ee0a501f9aaf11351787a46ffb4659d45b7bd7 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 671a0524a616da8b2f50f3d11a74aaf563578e41 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 2cb649ad3584370c960e2036fb01db834a546114 127.0.0.1:7000
   slots: (0 slots) slave
   replicates 18948dab5b07e3726afd1b6a42d5bf6e2f411ba1
S: 34e322ca50a2842e9f3664442cb11c897defba06 127.0.0.1:7004
   slots: (0 slots) slave
   replicates f4ee0a501f9aaf11351787a46ffb4659d45b7bd7
M: 18948dab5b07e3726afd1b6a42d5bf6e2f411ba1 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 62a00566233fbff4467c4031345b1db13cf12b46 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 671a0524a616da8b2f50f3d11a74aaf563578e41
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.           

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

個人部落格:

http://blog.yanxiaolong.cn/

繼續閱讀