redis cluster集群测试

redis3最大的变化之一就是cluster功能的正式发布,之前要搞redis集群,得借助一致性hash来本身搞sharding,如今方便多了,直接上cluster功能就好了,并且还支持节点动态添加、HA、节点增减后缓存从新分布(resharding)。html

下面是参考官方教程cluster-tutorial 在mac机上搭建cluster的过程:node

1、下载最新版redis 编译

目前最新版是3.0.7,下载地址:http://www.redis.io/downloadredis

编译很简单,一个make命令便可,不清楚的同窗,可参考我以前的笔记: redis 学习笔记(1)-编译、启动、中止算法

2、建6个目录

mkdir ~/app/redis-cluster/  #先建一个根目录
mkdir 7000 7001 7002 7003 7004 7005

注:与大多数分布式中间件同样,redis的cluster也是依赖选举算法来保证集群的高可用,因此相似ZK同样,通常是奇数个节点(能够容许N/2如下的节点失效),再考虑到每一个节点作Master-Slave互为备份,因此一个redis cluster集群最少也得6个节点。shell

而后把步骤1里编译好的redis,复制到这6个目录下。缓存

3、配置文件

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

把上面这段保存成redis-cluster.conf,放到每一个目录的redis目录中,注意修改port端口,即7000目录下的port为7000,7001目录下的port为7001...ruby

cluster-node-timeout 是集群中各节点相互通信时,容许"失联"的最大毫秒数,上面的配置为5秒,若是超过5秒某个节点没向其它节点汇报成功,认为该节点挂了。bash

4、依次启动各个redis

在每一个目录redis的src子目录下,输入:app

./redis-server ../redis-cluster.conf

这样7000~7005这6个节点就启动了。socket

5、安装redis的ruby模块

brew update
brew install ruby
sudo gem install redis #注:这个步骤建议翻^墙,否则你懂的

解释:虽然步骤4把6个redis server启动成功了,可是彼此之间是彻底独立的,须要借助其它工具将其加入cluster,而这个工具就是redis提供的一个名为redis-trib.rb的ruby脚本(我的估计redis的做者比较偏心ruby),mac自带了ruby2.0环境,可是没有redis模块,因此要安装这玩意儿,不然接下来的建立cluster将失败。

6、建立cluster

./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

仍然保持在某个目录的src子目录下,运行上面这段shell脚本,cluster就建立成功了,replicas 1的意思,就是每一个节点建立1个副本(即:slave),因此最终的结果,就是后面的127.0.0.1:7000~127.0.0.1:7005中,会有3个会指定成master,而其它3个会指定成slave。

注:利用redis-trib建立cluster的操做,只须要一次便可,假设系统关机,把全部6个节点全关闭后,下次重启后,即自动进入cluster模式,不用再次redis-trib.rb create。

此时,如何用ps查看redis进程,会看到每一个进程后附带了cluster的字样

点击看大图

若是想知道,哪些端口的节点是master,哪些端口的节点是slave,能够用下面的命令:

./redis-trib.rb check 127.0.0.1:7000

输出结果以下:

>>> Performing Cluster Check (using node 127.0.0.1:7000)
S: 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e 127.0.0.1:7000
   slots: (0 slots) slave
   replicates 38910c5baafea02c5303505acfd9bd331c608cfc
M: e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 88e16f91609c03277f2ee6ce5285932f58c221c1 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ec964a7c7cd53b986f54318a190c1426fc53a5fa
S: be7e9fd3b7d096b037306bc14e1017150fa59d7a 127.0.0.1:7004
   slots: (0 slots) slave
   replicates e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa
M: 38910c5baafea02c5303505acfd9bd331c608cfc 127.0.0.1:7003
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: ec964a7c7cd53b986f54318a190c1426fc53a5fa 127.0.0.1:7002
   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.

从上面的输出,能够看出7000、700四、7005是slave,而700一、700三、7002是master(若是你们人为作过一些failover的测试,好比把某个节点手动停掉,再恢复,输出的结果可能与上面不太同样),除了check参数,还有一个经常使用的参数info

./redis-trib.rb info 127.0.0.1:7000

输出结果以下:

127.0.0.1:7001 (e0e8dfdd...) -> 2 keys | 5462 slots | 1 slaves.
127.0.0.1:7003 (38910c5b...) -> 2 keys | 5461 slots | 1 slaves.
127.0.0.1:7002 (ec964a7c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.

它会把全部的master信息输出,包括这个master上有几个缓存key,有几个slave,全部master上的keys合计,以及平均每一个slot上有多少key,想了解更多redis-trib脚本的其它参数,能够用

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

上面已经屡次出现了slot这个词,略为解释一下:

点击看大图

如上图,redis-cluster把整个集群的存储空间划分为16384个slot(译为:插槽?),当6个节点分为3主3从时,至关于整个cluster中有3组HA的节点,3个master会平均分摊全部slot,每次向cluster中的key作操做时(好比:读取/写入缓存),redis会对key值作CRC32算法处理,获得一个数值,而后再对16384取模,经过余数判断该缓存项应该落在哪一个slot上,肯定了slot,也就肯定了保存在哪一个master节点上,当cluster扩容或删除节点时,只须要将slot从新分配便可(即:把部分slot从一些节点移动到其它节点)。

7、redis-cli客户端操做

./redis-cli -c -h localhost -p 7000

注意加参数-c,表示进入cluster模式,随便添加一个缓存试试:

localhost:7000> set user1 jimmy
-> Redirected to slot [8106] located at 127.0.0.1:7001
OK

注意第2行的输出,表示user1这个缓存经过计算后,落在8106这个slot上,最终定位在7001这个端口对应的节点上(解释:由于7000是slave,7001才是master,只有master才能写入),若是是在7001上重复上面的操做时,不会出现第2行(解释:7001是master,因此不存在redirect的过程)

➜  src ./redis-cli -c -h localhost -p 7001
localhost:7001> set user1 yang
OK
localhost:7001>

8、FailOver测试

先用redis-trib.rb 查看下当前的主、从状况

➜  src ./redis-trib.rb check localhost:7000
>>> Performing Cluster Check (using node localhost:7000)
S: 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e localhost:7000
   slots: (0 slots) slave
   replicates 38910c5baafea02c5303505acfd9bd331c608cfc
M: ec964a7c7cd53b986f54318a190c1426fc53a5fa 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: be7e9fd3b7d096b037306bc14e1017150fa59d7a 127.0.0.1:7004
   slots: (0 slots) slave
   replicates e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa
S: 88e16f91609c03277f2ee6ce5285932f58c221c1 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ec964a7c7cd53b986f54318a190c1426fc53a5fa
M: 38910c5baafea02c5303505acfd9bd331c608cfc 127.0.0.1:7003
   slots:0-5460 (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.

从输出上看7000是7003(38910c5baafea02c5303505acfd9bd331c608cfc)的slave,如今咱们人工把7003的redis进程给kill掉,而后观察7000的终端输出:

872:S 21 Mar 10:55:55.663 * Connecting to MASTER 127.0.0.1:7003
3872:S 21 Mar 10:55:55.663 * MASTER <-> SLAVE sync started
3872:S 21 Mar 10:55:55.663 # Error condition on socket for SYNC: Connection refused
3872:S 21 Mar 10:55:55.771 * Marking node 38910c5baafea02c5303505acfd9bd331c608cfc as failing (quorum reached).
3872:S 21 Mar 10:55:55.771 # Cluster state changed: fail
3872:S 21 Mar 10:55:55.869 # Start of election delayed for 954 milliseconds (rank #0, offset 183).
3872:S 21 Mar 10:55:56.703 * Connecting to MASTER 127.0.0.1:7003
3872:S 21 Mar 10:55:56.703 * MASTER <-> SLAVE sync started
3872:S 21 Mar 10:55:56.703 # Error condition on socket for SYNC: Connection refused
3872:S 21 Mar 10:55:56.909 # Starting a failover election for epoch 10.
3872:S 21 Mar 10:55:56.911 # Failover election won: I'm the new master.
3872:S 21 Mar 10:55:56.911 # configEpoch set to 10 after successful failover
3872:M 21 Mar 10:55:56.911 * Discarding previously cached master state.
3872:M 21 Mar 10:55:56.911 # Cluster state changed: ok

注意5,6,11这几行,第5行代表因为7003宕机,cluster状态已经切换到fail状态,第6行表示发起选举,第11行表示7000端口对应的节点当选为new master。

9、cluster 扩容

业务规模变大后,集群扩容是迟早的事情,下面演示如何再添加2个节点,先把7000复制二份,变成7006,7007,而后进入7006/7007目录redis的src子目录下

rm nodes.conf dump.rdb appendonly.aof

因为7000咱们刚才启动过,里面有已经有一些数据了,因此要把数据文件,日志文件,以及cluster的nodes.conf文件删除,变成一个空的redis独立节点,不然没法加入cluster。

而后修改redis-cluster.conf

port 7000
cluster-enabled yes
cluster-config-file "nodes.conf"
cluster-node-timeout 10000
appendonly yes
# Generated by CONFIG REWRITE
dir "/Users/yjmyzz/app/redis-cluster/7000/redis-3.0.7/src"

要修改的地方有二处,1是第一行的端口,改为与7006/7007匹配的端口,2是最后2行,这是7000运行后,自动添加的,把最后二行删除。

作完这些后,启动7006,7007这二个redis节点,此时这2个新节点与cluster没有任何关系,能够用下面的命令将7006作为master添加到cluster中。

./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000

注:第1个参数为新节点的"IP:端口",第2个参数为集群中的任一有效的节点。

顺利的话,输出以下:

>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: be7e9fd3b7d096b037306bc14e1017150fa59d7a 127.0.0.1:7004
   slots: (0 slots) slave
   replicates e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa
M: ec964a7c7cd53b986f54318a190c1426fc53a5fa 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 88e16f91609c03277f2ee6ce5285932f58c221c1 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ec964a7c7cd53b986f54318a190c1426fc53a5fa
S: 38910c5baafea02c5303505acfd9bd331c608cfc 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster.
[OK] New node added correctly.

能够再用check确认下状态:

➜  src ./redis-trib.rb check 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: be7e9fd3b7d096b037306bc14e1017150fa59d7a 127.0.0.1:7004
   slots: (0 slots) slave
   replicates e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa
M: 226d1af3c95bf0798ea9fed86373b89347f889da 127.0.0.1:7006
   slots: (0 slots) master
   0 additional replica(s)
M: ec964a7c7cd53b986f54318a190c1426fc53a5fa 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 88e16f91609c03277f2ee6ce5285932f58c221c1 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ec964a7c7cd53b986f54318a190c1426fc53a5fa
S: 38910c5baafea02c5303505acfd9bd331c608cfc 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

12-14行说明7006已是cluster的新master了,继续,用下面的命令把7007当成slave加入:

./redis-trib.rb add-node --slave --master-id 226d1af3c95bf0798ea9fed86373b89347f889da 127.0.0.1:7007 127.0.0.1:7000

这里多出了二个参数:--slave 表示准备将新节点当成slave加入,--master-id xxxxx 则是指定要当谁的slave,后面的xxx部分,即为前面check的输出结果中,7006的ID,完事以后,能够再次确认状态:

➜  src ./redis-trib.rb check 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 792bcccf35845c4922dd33d7f9827420ebb89bc9 127.0.0.1:7007
   slots: (0 slots) slave
   replicates 226d1af3c95bf0798ea9fed86373b89347f889da
M: e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: be7e9fd3b7d096b037306bc14e1017150fa59d7a 127.0.0.1:7004
   slots: (0 slots) slave
   replicates e0e8dfddd4e9d855090d6efd18e55ea9c0e1f7aa
M: 226d1af3c95bf0798ea9fed86373b89347f889da 127.0.0.1:7006
   slots: (0 slots) master
   1 additional replica(s)
M: ec964a7c7cd53b986f54318a190c1426fc53a5fa 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 88e16f91609c03277f2ee6ce5285932f58c221c1 127.0.0.1:7005
   slots: (0 slots) slave
   replicates ec964a7c7cd53b986f54318a190c1426fc53a5fa
S: 38910c5baafea02c5303505acfd9bd331c608cfc 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

观察6-8行、15-17行,说明7007已是7006的slave。

10、reshard 从新划分slot

增长新的节点以后,问题就来了,16384个slot已经被其它3组节点分完了,新节点没有slot,没办法存放缓存,因此须要将slot从新分布。

➜  src ./redis-trib.rb info 127.0.0.1:7000
127.0.0.1:7000 (0b7e0d53...) -> 4 keys | 5461 slots | 1 slaves.
127.0.0.1:7001 (e0e8dfdd...) -> 4 keys | 5462 slots | 1 slaves.
127.0.0.1:7006 (226d1af3...) -> 0 keys | 0 slots | 1 slaves. #7006上彻底没有slot
127.0.0.1:7002 (ec964a7c...) -> 9 keys | 5461 slots | 1 slaves.
[OK] 17 keys in 4 masters.
0.00 keys per slot on average.

用下面的命令能够从新分配slot

./redis-trib.rb reshard 127.0.0.1:7000

reshard后面的IP:port,只要是在cluster中的有效节点便可。

➜  src ./redis-trib.rb reshard 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e 127.0.0.1:7000
   slots:1792-4095 (2304 slots) master
   0 additional replica(s)
   ...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1000 #这里输入要移动多少slot
What is the receiving node ID? 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e #这里输入目标节点的id
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:all #将全部node都当成源节点
    ...
    Moving slot 4309 from ec964a7c7cd53b986f54318a190c1426fc53a5fa
    Moving slot 4310 from ec964a7c7cd53b986f54318a190c1426fc53a5fa
    Moving slot 4311 from ec964a7c7cd53b986f54318a190c1426fc53a5fa
    Moving slot 4312 from ec964a7c7cd53b986f54318a190c1426fc53a5fa
    Moving slot 4313 from ec964a7c7cd53b986f54318a190c1426fc53a5fa
Do you want to proceed with the proposed reshard plan (yes/no)? yes #确认执行

注:第一个交互询问,填写多少slot移动时,要好好想一想,若是填成16384,则将全部slot都移动到一个固定节点上,会致使更加不均衡!建议每次移动500~1000,这样对线上的影响比较小。

另外在填写source node时,除了all以外,还能够直接填写源节点的id,即:

[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 300
What is the receiving node ID? 0b7e0d5337e87ac7b59bba4c1248e5c9e8d1905e
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:226d1af3c95bf0798ea9fed86373b89347f889da #这里填写源节点的id
Source node #2:done #这里输入done表示,再也不继续添加源节点了

reshard能够屡次操做,直到达到指望的分布为止(注:我的以为redis的reshard这里有点麻烦,要移动多少slot须要人工计算,若是能提供一个参数之类,让16384个slot自动平均分配就行了),调整完成后,能够再看看分布状况:

➜  src ./redis-trib.rb info 127.0.0.1:7000
127.0.0.1:7000 (0b7e0d53...) -> 4 keys | 4072 slots | 0 slaves.
127.0.0.1:7001 (e0e8dfdd...) -> 5 keys | 4099 slots | 0 slaves.
127.0.0.1:7006 (226d1af3...) -> 5 keys | 4132 slots | 4 slaves.
127.0.0.1:7002 (ec964a7c...) -> 3 keys | 4081 slots | 0 slaves.
[OK] 17 keys in 4 masters.
0.00 keys per slot on average.

11、删除节点del-node

既然有扩容,就会有反向需求,某些节点再也不须要时,能够用del-node删除,好比刚才我一阵乱倒腾后,发现7006已经有4个slave了,而其它master一个slave都没有,这明显不合理。

删除节点命令:

./redis-trib.rb del-node 127.0.0.1:7006 88e16f91609c03277f2ee6ce5285932f58c221c1

del-node后面的ip:port只要是cluster中有效节点便可,最后一个参数为目标节点的id,注意:只有slave节点和空的master节点能够删除,若是master非空,先用reshard把上面的slot移动到其它node后再删除,若是有一组master-slave节点,将master上全部slot移到其它节点,而后将master删除,剩下的slave会另寻他主,变成其它master的slave。

另外:删除节点的含义,不只仅是从cluster中将这个节点移除,还会直接将目标节点的redis服务中止。

参考文章:

http://www.redis.io/topics/cluster-tutorial

相关文章
相关标签/搜索