这儿的Redis集群指的是Redis Cluster,它是Redis在3.0版本正式推出的专用集群方案,有效地处理了Redis分布式方面的需求。当单机内存、并发、流量等遇到瓶颈的时分,能够选用这种Redis Cluster方案进行处理。
Redis Cluster选用虚拟槽(slot)进行数据分区,即运用分散度良好的哈希函数把一切键映射到一个固定范围的整数集合里,这儿的整数便是槽(slot)。Redis Cluster槽的范围是0~16383,计算公式:slot=CRC16(key) & 16383。
白嫖小贴士:CRC16是一种高质量的哈希算法,能够使每个槽所映射的键通常比较均匀。
当集群中有3个节点时,每个节点均匀大概负责5461个槽以及槽所映射的键值数据。这样一来,能够解耦数据与节点之间的联系,简化节点扩容和缩容的难度。节点自身保护槽的映射联系,不需要客户端或代理服务保护分区信息。
不过,Redis Cluster相对于单机还是存在一些限制的,比方:
扯了这么多Redis Cluster的分区规矩,下面咱们开端步入正题。
把Redis Cluster树立起来总共几步?答:三步!第一步把冰箱门翻开。第二步把大象关进去。第三步把冰箱门带上。不好意思,段子暴露年龄了。集群树立需要以下三个进程:
Redis Cluster由多个节点组成,节点数量至少有6个才能组成一个完整高可用的集群,其中有3个主节点和3个从节点,咱们就以此为例树立一个Redis Cluster。
预备节点
首先,为6个节点(同一台机器上的6380、6381、6382、6383、6384、6385端口)别离创立装备文件,以6380端口的节点为例:
# 节点端口 port 6380 #日志文件 logfile "log/redis-6380.log" # 开启集群形式 cluster-enabled yes # 集群装备文件 cluster-config-file "data/nodes-6380.conf"
坚持文件名为redis-6380.conf,其他节点的装备文件替换成各自的端口。预备好装备文件后启动一切节点,指令如下:
src/redis-server conf/redis-6380.conf &
src/redis-server conf/redis-6381.conf &
src/redis-server conf/redis-6382.conf &
src/redis-server conf/redis-6383.conf &
src/redis-server conf/redis-6384.conf &
src/redis-server conf/redis-6385.conf &
检测日志是否正确,以下是6380端口的节点的日志:
# oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=3031, just started # Configuration loaded * No cluster configuration found, I'm df1ac987f47dea35f1d0a83c3b405f0ef86892ab
* Running mode=cluster, port=6380.
6380端口的节点启动成功,第一次启动时如果没有集群装备文件,Redis会主动创立一个。6380端口的节点创立的集群装备文件如下:
df1ac987f47dea35f1d0a83c3b405f0ef86892ab :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
集群文件中记录的集群的状况,这儿最重要的是节点ID,它是一个40位的16进制字符串,用于唯一标识集群中的这个节点。同样,也能够经过cluster nodes指令检查集群节点状况。比方在6380端口的节点上履行指令:
127.0.0.1:6380> cluster nodes df1ac987f47dea35f1d0a83c3b405f0ef86892ab :6380@16380 myself,master - 0 0 0 connected
目前,咱们现已成功启动了6个节点,可是它们只能辨认自己的节点信息,相互之间并不知道。下面咱们经过节点握手让这6个节点相互之间树立联系从而组成一个集群。
节点握手
节点握手是一些运转在集群形式下的节点经过Gossip协议相互通讯,到达感知互相的进程。
白嫖小贴士:Gossip协议是基于流行病传达方式的节点或者进程之间信息交换的协议,在分布式系统中被广泛运用。
节点握手经过客户端履行cluster meet指令完成,它是一个异步指令,履行之后立刻返回,在Redis内部异步建议与目标节点的握手通讯,该指令的语法如下:
cluster meet 目标节点IP 目标节点端口
把6个节点加到一个集群中:
127.0.0.1:6380> cluster meet 127.0.0.1 6381 OK 127.0.0.1:6380> cluster meet 127.0.0.1 6382 OK 127.0.0.1:6380> cluster meet 127.0.0.1 6383 OK 127.0.0.1:6380> cluster meet 127.0.0.1 6384 OK 127.0.0.1:6380> cluster meet 127.0.0.1 6385 OK
只需要在集群中恣意节点上履行cluster meet指令加入新的节点,握手状况会经过音讯在集群中传达,其他节点也会主动发现新节点并与之建议握手流程。
咱们再履行一下cluster nodes指令,检查一下6个节点是否现已组成集群:
127.0.0.1:6380> cluster nodes 1e1f45677d7b9b0130d03193f0bcec34578ac47d 127.0.0.1:6385@16385 master - 0 1586617919021 5 connected
df1ac987f47dea35f1d0a83c3b405f0ef86892ab 127.0.0.1:6380@16380 myself,master - 0 1586617916000 2 connected 5846b66ebe4fb4a5dcfd035652cc471f7e412752 127.0.0.1:6381@16381 master - 0 1586617917005 1 connected
a435cf98c3444b0b110a224401e397a107c453ef 127.0.0.1:6384@16384 master - 0 1586617914988 4 connected 71e0e9e9a6f0c7c85dbe0d396846a9072625c5e8 127.0.0.1:6383@16383 master - 0 1586617918013 3 connected
e25590603c7a254cce43aa8437861c5c425d753d 127.0.0.1:6382@16382 master - 0 1586617916000 0 connected
能够看到,6个节点都在集群中了。不过,此刻由于还没有为集群中的节点分配槽,集群还处于下线状况,一切的数据读写都是被制止的。比方:
127.0.0.1:6380> set onemore study (error) CLUSTERDOWN Hash slot not served
接下来,咱们为集群中的节点分配槽。
分配槽
咱们把6380、6382、6384端口的节点作为主节点,负责处理槽和相关数据;6381、6383、6385端口的节点别离作为从节点,负责故障搬运。先把16384个槽均匀分配给6380、6382、6384端口的节点,为节点分配槽是经过cluster addslots指令完成:
# ./redis-cli -h 127.0.0.1 -p 6380 cluster addslots {0..5461} OK # ./redis-cli -h 127.0.0.1 -p 6382 cluster addslots {5462..10922} OK # ./redis-cli -h 127.0.0.1 -p 6384 cluster addslots {10923..16383} OK
咱们再履行一下cluster nodes指令,检查一下槽是否现已分配:
127.0.0.1:6380> cluster nodes 1e1f45677d7b9b0130d03193f0bcec34578ac47d 127.0.0.1:6385@16385 master - 0 1586619468000 5 connected
df1ac987f47dea35f1d0a83c3b405f0ef86892ab 127.0.0.1:6380@16380 myself,master - 0 1586619464000 2 connected 0-5461 5846b66ebe4fb4a5dcfd035652cc471f7e412752 127.0.0.1:6381@16381 master - 0 1586619467000 1 connected
a435cf98c3444b0b110a224401e397a107c453ef 127.0.0.1:6384@16384 master - 0 1586619467000 4 connected 10923-16383 71e0e9e9a6f0c7c85dbe0d396846a9072625c5e8 127.0.0.1:6383@16383 master - 0 1586619467348 3 connected
e25590603c7a254cce43aa8437861c5c425d753d 127.0.0.1:6382@16382 master - 0 1586619468355 0 connected 5462-10922
再运用cluster replicate指令把一个节点变成从节点.,这个指令有必要在从节点上运转,它的语法是:
cluster replicate 主节点ID
把6381、6383、6385端口的节点变成对应6380、6382、6384端口的节点的从节点:
# ./redis-cli -h 127.0.0.1 -p 6381 127.0.0.1:6381> cluster replicate df1ac987f47dea35f1d0a83c3b405f0ef86892ab
OK 127.0.0.1:6381> exit # ./redis-cli -h 127.0.0.1 -p 6383 127.0.0.1:6383> cluster replicate e25590603c7a254cce43aa8437861c5c425d753d
OK 127.0.0.1:6383> exit # ./redis-cli -h 127.0.0.1 -p 6385 127.0.0.1:6385> cluster replicate a435cf98c3444b0b110a224401e397a107c453ef
OK 127.0.0.1:6385> exit
咱们再履行一下cluster nodes指令,检查一下集群状况和主从联系:
127.0.0.1:6380> cluster nodes df1ac987f47dea35f1d0a83c3b405f0ef86892ab 127.0.0.1:6380@16380 myself,master - 0 1586620148000 2 connected 0-5461 5846b66ebe4fb4a5dcfd035652cc471f7e412752 127.0.0.1:6381@16381 slave df1ac987f47dea35f1d0a83c3b405f0ef86892ab 0 1586620150000 2 connected
e25590603c7a254cce43aa8437861c5c425d753d 127.0.0.1:6382@16382 master - 0 1586620151000 0 connected 5462-10922 71e0e9e9a6f0c7c85dbe0d396846a9072625c5e8 127.0.0.1:6383@16383 slave e25590603c7a254cce43aa8437861c5c425d753d 0 1586620152220 3 connected
a435cf98c3444b0b110a224401e397a107c453ef 127.0.0.1:6384@16384 master - 0 1586620150000 4 connected 10923-16383 1e1f45677d7b9b0130d03193f0bcec34578ac47d 127.0.0.1:6385@16385 slave a435cf98c3444b0b110a224401e397a107c453ef 0 1586620149000 5 connected
自此,RedisCluster现已手动树立完成。手动树立能够理解集群树立的流程和细节,不过大家也会发现手动树立有很多进程,当集群的节点比较多的时分,肯定会让人头大。所以Redis官方提供了redis-trib.rb东西,能够让咱们快速地树立集群。
redis-trib.rb是运用Ruby开发的Redis Cluster的管理东西,不需要额定下载,默认位于源码包的src目录下,但由于该东西是用Ruby开发的,所以需要预备相关的依靠环境。
环境预备
装置Ruby:
yum -y install zlib-devel
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz tar xvf ruby-2.5.1.tar.gz
cd ruby-2.5.1/
./configure -prefix=/usr/local/ruby
make
make install
cd /usr/local/ruby/ cp bin/ruby /usr/local/bin
cp bin/gem /usr/local/bin
装置rubygem redis依靠:
wget http://rubygems.org/downloads/redis-3.3.0.gem
gem install -l redis-3.3.0.gem
装置redis-trib.rb:
cp src/redis-trib.rb /usr/local/bin
履行redis-trib.rb指令确认一下环境是否预备正确:
# redis-trib.rb help Usage: redis-trib ...> create host1:port1 ... hostN:portN --replicas check host:port info host:port fix host:port --timeout reshard host:port --from ...此处省略一万个字...
树立集群
像前面的内容讲的,预备好节点装备并启动:
src/redis-server conf/redis-7380.conf &
src/redis-server conf/redis-7381.conf &
src/redis-server conf/redis-7382.conf &
src/redis-server conf/redis-7383.conf &
src/redis-server conf/redis-7384.conf &
src/redis-server conf/redis-7385.conf &
运用redis-trib.rb create指令完成节点握手和槽分配的工作,指令如下:
redis-trib.rb create --replicas 1 127.0.0.1:7380 127.0.0.1:7382 127.0.0.1:7384 127.0.0.1:7381 127.0.0.1:7383 127.0.0.1:7385
其中--replicas参数用来指定集群中每个主节点有几个从节点,这儿设置的是1。指令履行后,会首先给出主从节点的分配方案:
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7380 127.0.0.1:7382 127.0.0.1:7384 Adding replica 127.0.0.1:7383 to 127.0.0.1:7380 Adding replica 127.0.0.1:7385 to 127.0.0.1:7382 Adding replica 127.0.0.1:7381 to 127.0.0.1:7384 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: c25675d021c377c91f860986025e3779d89ede79 127.0.0.1:7380 slots:0-5460 (5461 slots) master M: 58980a81b49de31383802d7d21d6782881678922 127.0.0.1:7382 slots:5461-10922 (5462 slots) master M: 3f00a37d2c7a5ea40671c8f2934f66d059157a4a 127.0.0.1:7384 slots:10923-16383 (5461 slots) master S: 6f7dd93973a8332305831e6b7b5e2c54c15b3b51 127.0.0.1:7381 replicates 3f00a37d2c7a5ea40671c8f2934f66d059157a4a S: 03e01f82a935ed7f977af092e6a9cb71057df68a 127.0.0.1:7383 replicates c25675d021c377c91f860986025e3779d89ede79 S: 2cf3883e974a709b7070d6c4d7c528d9fa813358 127.0.0.1:7385 replicates 58980a81b49de31383802d7d21d6782881678922 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:7380) M: c25675d021c377c91f860986025e3779d89ede79 127.0.0.1:7380 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 58980a81b49de31383802d7d21d6782881678922 127.0.0.1:7382 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 2cf3883e974a709b7070d6c4d7c528d9fa813358 127.0.0.1:7385 slots: (0 slots) slave replicates 58980a81b49de31383802d7d21d6782881678922 S: 03e01f82a935ed7f977af092e6a9cb71057df68a 127.0.0.1:7383 slots: (0 slots) slave replicates c25675d021c377c91f860986025e3779d89ede79 S: 6f7dd93973a8332305831e6b7b5e2c54c15b3b51 127.0.0.1:7381 slots: (0 slots) slave replicates 3f00a37d2c7a5ea40671c8f2934f66d059157a4a M: 3f00a37d2c7a5ea40671c8f2934f66d059157a4a 127.0.0.1:7384 slots:10923-16383 (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage... [OK] All 16384 slots covered.
集群创立完成后,还能够运用redis-trib.rb check指令检查集群是否创立成功,具体指令如下:
# redis-trib.rb check 127.0.0.1:7380 >>> Performing Cluster Check (using node 127.0.0.1:7380) M: c25675d021c377c91f860986025e3779d89ede79 127.0.0.1:7380 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 58980a81b49de31383802d7d21d6782881678922 127.0.0.1:7382 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 2cf3883e974a709b7070d6c4d7c528d9fa813358 127.0.0.1:7385 slots: (0 slots) slave replicates 58980a81b49de31383802d7d21d6782881678922 S: 03e01f82a935ed7f977af092e6a9cb71057df68a 127.0.0.1:7383 slots: (0 slots) slave replicates c25675d021c377c91f860986025e3779d89ede79 S: 6f7dd93973a8332305831e6b7b5e2c54c15b3b51 127.0.0.1:7381 slots: (0 slots) slave replicates 3f00a37d2c7a5ea40671c8f2934f66d059157a4a M: 3f00a37d2c7a5ea40671c8f2934f66d059157a4a 127.0.0.1:7384 slots:10923-16383 (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage... [OK] All 16384 slots covered.
能够看到,一切的槽都已分配到节点上,大功告成!
本文转载于:https://blog.csdn.net/dafengit/article/details/106073709