Redis Cluster
集群
我們要使用集群
- redis 并發(fā)量 10萬 / 每秒 ,但是有些業(yè)務(wù)需要 100萬的 QPS
- 數(shù)據(jù)量,我們普通機(jī)器 16~256g,而我們的業(yè)務(wù)需要500g
解決方案
- 分布式 :加機(jī)器,方便以后需求擴(kuò)容
Redis Cluster
Redis Cluster是Redis的分布式解決方案,在3.0版本正式推出,有效地解決了Redis分布式方面的需求。當(dāng)遇到單機(jī)內(nèi)存、并發(fā)、流量等瓶頸時,可以采用Cluster架構(gòu)方案達(dá)到負(fù)載均衡的目的。
? 在Redis Cluster,它們?nèi)魏蝺蓚€節(jié)點之間都是相互連通的??蛻舳丝梢耘c任何一個節(jié)點相連接,然后就可以訪問集群中的任何一個節(jié)點,對其進(jìn)行存取和其它操作。
Redis Cluster提供的好處:
- 將數(shù)據(jù)自動切分到多個節(jié)點的能力
- 當(dāng)集群中的一部分節(jié)點失效或者無法進(jìn)行通訊時,仍然可以繼續(xù)處理命令請求的能力,擁有自動故障轉(zhuǎn)移的能力。
Redis Cluster 和 replication + sentinel 如何選擇:
如果數(shù)據(jù)量很少,主要是承載高并發(fā)高性能的場景,比如你的緩存一般就幾個G,單機(jī)就夠了。
- Replication:一個master,多個slave,要幾個slave跟你的要求的讀吞吐量有關(guān)系,結(jié)合sentinel集群,去保證redis主從架構(gòu)的高可用就行了。
- Redis Cluster:主要是針對海量數(shù)據(jù)+高并發(fā)+高可用的場景,海量數(shù)據(jù),如果數(shù)據(jù)量很大,建議用Redis Cluster
數(shù)據(jù)分布
數(shù)據(jù)分區(qū)
順序分布和哈希分布
- 順序分布
-
哈希分步
哈希分布
-
節(jié)點取余分布 : ( hash (key) % nodes )
- 使用特點的數(shù)據(jù)(包括redis的鍵或用戶ID),再根據(jù)節(jié)點數(shù)量N,使用公式:hash(key)%N計算出一個0~(N-1)值,來決定數(shù)據(jù)映射到哪一個節(jié)點上。即哈希值對節(jié)點總數(shù)取余。
- 客戶端分片 :哈希 + 取余
- 節(jié)點伸縮 : 數(shù)據(jù)節(jié)點關(guān)系變化,導(dǎo)致數(shù)據(jù)遷移
- 遷移數(shù)量和添加節(jié)點數(shù)量有關(guān) :建議翻倍擴(kuò)容
- 一致性哈希分區(qū)
-
一致性哈希分區(qū)(Distributed Hash Table)實現(xiàn)思路是為系統(tǒng)中每個節(jié)點分配一個token,范圍一般在0~232,這些token構(gòu)成一個哈希環(huán)。數(shù)據(jù)讀寫執(zhí)行節(jié)點查找操作時,先根據(jù)key計算hash值,然后順時針找到第一個大于等于該哈希值的token節(jié)點。
在這里插入圖片描述
-
上圖就是一個一致性hash的原理解析。
假設(shè)有n1~n4這四臺機(jī)器,我們對每一臺機(jī)器分配一個唯一token,每次有數(shù)據(jù)(黃色代表數(shù)據(jù)),一致性哈希算法規(guī)則每次都順時針漂移數(shù)據(jù),也就是圖中黃色的數(shù)據(jù)都指向n3。
這個時候我們需要增加一個節(jié)點n5,在n2和n3之間,數(shù)據(jù)還是會發(fā)生漂移(會偏移到大于等于的節(jié)點),但是這個時候你是否注意到,其實只有n2~n3這部分的數(shù)據(jù)被漂移,其它的數(shù)據(jù)都是不會變的,這種方式相比節(jié)點取余最大的好處在于加入和刪除節(jié)點只影響哈希環(huán)中相鄰的節(jié)點,對其它節(jié)點無影響。
- 客戶端分片: 哈希 + 順時針 (優(yōu)化取余)
- 節(jié)點伸縮 : 只影響臨近節(jié)點,但是還是有數(shù)據(jù)遷移
- 翻倍伸縮 :保證最小遷移數(shù)據(jù)和負(fù)載均衡
-
虛擬槽分區(qū)
-
虛擬槽分區(qū)巧妙地使用了哈希空間,使用分散度良好的哈希函數(shù)把所有數(shù)據(jù)映射到一個固定范圍的整數(shù)集合中,整數(shù)定義為槽(slot)。這個范圍一般遠(yuǎn)遠(yuǎn)大于節(jié)點數(shù),比如Redis Cluster槽范圍是0~16383(也就是說有16383個槽)。槽是集群內(nèi)數(shù)據(jù)管理和遷移的基本單位。采用大范圍槽的主要目的是為了方便數(shù)據(jù)拆分和集群擴(kuò)展。每個節(jié)點會負(fù)責(zé)一定數(shù)量的槽。
在這里插入圖片描述
-
- 預(yù)設(shè)虛擬槽映射一個數(shù)據(jù)子集,一半節(jié)點數(shù)大
- 良好的哈希函數(shù) : 例如CRC16
- 服務(wù)端管理節(jié)點、槽、數(shù)據(jù):例如 Redis Cluster
- 對比
| 分布方式 | 特點 | 典型產(chǎn)品 |
|---|---|---|
| 哈希分布 | 數(shù)據(jù)分散度高 , 鍵值分布業(yè)務(wù)無關(guān) ,無法順序訪問 ,支持批量操作 | 一致性哈希Memcache ,Redis Cluster , 其他緩存產(chǎn)品 |
| 順序分布 | 數(shù)據(jù)分散度易傾斜 , 鍵值業(yè)務(wù)相關(guān) ,可順序訪問 , 支持批量操作 | BigTable ,HBase |
搭建集群
一、基本架構(gòu)
-
節(jié)點
在這里插入圖片描述 -
meet
指配槽
- Redis Cluster特性
- 復(fù)制
- 高可用
- 分片
二、兩種安裝
- 原生命令安裝
- 配置開啟節(jié)點 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添加自己的配置文件
開啟節(jié)點
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
- meet
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}
- 主從
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}
- 具體安裝
創(chuàng)建文件
[圖片上傳失敗...(image-30eb9a-1603206111514)]
端口以及文件依次后排 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
#是否需要每個節(jié)點都可用,集群才算可用,關(guān)閉
cluster-require-full-coverage no
一次通過配置文件啟動集群,我還在conf目錄下,所以這樣啟動
有不懂的推薦Redis Cluster 安裝文章 https://blog.csdn.net/fst438060684/article/details/80712433
- 官方工具安裝
Ruby 環(huán)境準(zhǔn)備
- 下載ruby
wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
- 安裝 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
- 安裝rubygem redis
wget http://rubygems.ory/downloads/redis-3.3.0.gem
gem install -| redis-3.3.0.gem
gem list --check redis gem
啟動所有節(jié)點
redis-server redis-7000.conf
redis-server redis-7001.conf
redis-server redis-7002.conf
redis-server redis-7003.conf
redis-trib.rb支持的操作
復(fù)制代碼
# 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.
支持的操作如下:
create:創(chuàng)建集群
check:檢查集群
info:查看集群信息
fix:修復(fù)集群
reshard:在線遷移slot
rebalance:平衡集群節(jié)點slot數(shù)量
add-node:添加新節(jié)點
del-node:刪除節(jié)點
set-timeout:設(shè)置節(jié)點的超時時間
call:在集群所有節(jié)點上執(zhí)行命令
import:將外部redis數(shù)據(jù)導(dǎo)入集群
創(chuàng)建集群
./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參數(shù)指定集群中每個主節(jié)點配備幾個從節(jié)點,這里設(shè)置為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.
上面顯示創(chuàng)建成功,有3個主節(jié)點,3個從節(jié)點,每個節(jié)點都是成功連接狀態(tài)。