Redis Cluster 3.0搭建与使用

来源:互联网 发布:三星网络电视看不了 编辑:程序博客网 时间:2024/06/04 10:45

Redis Cluster 3.0搭建与使用

标签: redis cluster集群基础教程
 220人阅读 评论(0) 收藏 举报
 分类:
 

目录(?)[+]

Redis Cluster终于出了Stable,这让人很是激动,等Stable很久了,所以还是先玩玩。


Redis Cluster基本概念

Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation)。

Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令需要在多个 Redis 节点之间移动数据, 并且在高负载的情况下, 这些命令将降低 Redis 集群的性能, 并导致不可预测的行为。

Redis 集群通过分区(partition)来提供一定程度的可用性(availability): 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。

优点

Redis 集群提供了以下两个好处:

  • 将数据自动切分(split)到多个节点的能力。
  • 当集群中的一部分节点失效或者无法进行通讯时, 仍然可以继续处理命令请求的能力。

实现原理

Redis 集群使用数据分片(sharding)而非一致性哈希(consistency hashing)来实现: 一个 Redis 集群包含 16384 个哈希槽(hash slot), 数据库中的每个键都属于这 16384 个哈希槽的其中一个, 集群使用公式 CRC16(key) 16384 来计算键 key 属于哪个槽, 其中 CRC16(key) 语句用于计算键 key 的 CRC16 校验和 。

集群中的每个节点负责处理一部分哈希槽。 举个例子, 一个集群可以有三个哈希槽, 其中:

  • 节点 A 负责处理 0 号至 5500 号哈希槽。
  • 节点 B 负责处理 5501 号至 11000 号哈希槽。
  • 节点 C 负责处理 11001 号至 16384 号哈希槽。

这种将哈希槽分布到不同节点的做法使得用户可以很容易地向集群中添加或者删除节点。 比如说:

  • 如果用户将新节点 D 添加到集群中, 那么集群只需要将节点 A 、B 、 C 中的某些槽移动到节点 D 就可以了。
  • 与此类似, 如果用户要从集群中移除节点 A , 那么集群只需要将节点 A 中的所有哈希槽移动到节点 B 和节点 C , 然后再移除空白(不包含任何哈希槽)的节点 A 就可以了。

因为将一个哈希槽从一个节点移动到另一个节点不会造成节点阻塞, 所以无论是添加新节点还是移除已存在节点, 又或者改变某个节点包含的哈希槽数量, 都不会造成集群下线。

为了使得集群在一部分节点下线或者无法与集群的大多数(majority)节点进行通讯的情况下, 仍然可以正常运作, Redis 集群对节点使用了主从复制功能: 集群中的每个节点都有 1 个至 N 个复制品(replica), 其中一个复制品为主节点(master), 而其余的 N-1 个复制品为从节点(slave)。

在之前列举的节点 A 、B 、C 的例子中, 如果节点 B 下线了, 那么集群将无法正常运行, 因为集群找不到节点来处理 5501 号至 11000号的哈希槽。

另一方面, 假如在创建集群的时候(或者至少在节点 B 下线之前), 我们为主节点 B 添加了从节点 B1 , 那么当主节点 B 下线的时候, 集群就会将 B1 设置为新的主节点, 并让它代替下线的主节点 B , 继续处理 5501 号至 11000 号的哈希槽, 这样集群就不会因为主节点 B 的下线而无法正常运作了。

不过如果节点 B 和 B1 都下线的话, Redis 集群还是会停止运作。


Redis-cluster 架构

架构图如下:

 

架构细节:

(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.

(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.

(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可

(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster负责维护node<->slot<->value

 

Redis Cluster搭建使用


要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下(为了简单演示都在同一台机器上面)

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


1. 下载最新版redis

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. wget http://download.redis.io/releases/redis-3.0.0.tar.gz  

2. 解压,安装

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. tar xf redis-3.0.0.tar.gz                         
  2. cd redis-3.0.0  
  3. make && make install  

3.创建存放多个实例的目录

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mkdir /data/cluster -p  
  2. cd /data/cluster  
  3. mkdir 7000 7001 7002 7003 7004 7005  

4.修改配置文件

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. cp redis-3.0.0/redis.conf /data/cluster/7000/  

修改配置文件中下面选项

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. port 7000  
  2.   
  3. daemonize yes  
  4.   
  5. cluster-enabled yes  
  6.   
  7. cluster-config-file nodes.conf  
  8.   
  9. cluster-node-timeout 5000  
  10.   
  11. appendonly yes  

文件中的 cluster-enabled 选项用于开实例的集群模式, 而 cluster-conf-file 选项则设定了保存节点配置文件的路径, 默认值为nodes.conf 。其他参数相信童鞋们都知道。节点配置文件无须人为修改, 它由 Redis 集群在启动时创建, 并在有需要时自动进行更新。

修改完成后,把修改完成的redis.conf复制到7001-7005目录下,并且端口修改成和文件夹对应。

5.分别启动6个redis实例

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. cd /data/cluster/7000  
  2. redis-server redis.conf  
  3. cd /data/cluster/7001  
  4. redis-server redis.conf  
  5. cd /data/cluster/7002  
  6. redis-server redis.conf  
  7. cd /data/cluster/7003  
  8. redis-server redis.conf  
  9. cd /data/cluster/7004  
  10. redis-server redis.conf  
  11. cd /data/cluster/7005  
  12. redis-server redis.conf  

查看进程否存在。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-server 7005]# ps -ef | grep redis  
  2. root      4168     1  0 11:49 ?        00:00:00 redis-server *:7000 [cluster]  
  3. root      4176     1  0 11:49 ?        00:00:00 redis-server *:7001 [cluster]  
  4. root      4186     1  0 11:50 ?        00:00:00 redis-server *:7002 [cluster]  
  5. root      4194     1  0 11:50 ?        00:00:00 redis-server *:7003 [cluster]  
  6. root      4202     1  0 11:50 ?        00:00:00 redis-server *:7004 [cluster]  
  7. root      4210     1  0 11:50 ?        00:00:00 redis-server *:7005 [cluster]  
  8. root      4219  4075  0 11:50 pts/2    00:00:00 grep redis  

6.执行命令创建集群

首先安装依赖,否则创建集群失败。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. yum install ruby rubygems -y  

安装gem-redis

下载地址:https://rubygems.org/gems/redis/versions/3.0.0

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. gem install redis -v 3.0.0   

复制集群管理程序到/usr/local/bin

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. cp redis-3.0.0/src/redis-trib.rb /usr/local/bin/redis-trib   

创建集群:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. redis-trib 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  

命令的意义如下:

  • 给定 redis-trib.rb 程序的命令是 create , 这表示我们希望创建一个新的集群。
  • 选项 --replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
  • 之后跟着的其他参数则是实例的地址列表, 我们希望程序使用这些地址所指示的实例来创建新集群。

简单来说, 以上命令的意思就是让 redis-trib 程序创建一个包含三个主节点和三个从节点的集群。

接着, redis-trib 会打印出一份预想中的配置给你看, 如果你觉得没问题的话, 就可以输入 yes , redis-trib 就会将这份配置应用到集群当中:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. >>> Creating cluster  
  2. Connecting to node 127.0.0.1:7000: OK  
  3. Connecting to node 127.0.0.1:7001: OK  
  4. Connecting to node 127.0.0.1:7002: OK  
  5. Connecting to node 127.0.0.1:7003: OK  
  6. Connecting to node 127.0.0.1:7004: OK  
  7. Connecting to node 127.0.0.1:7005: OK  
  8. >>> Performing hash slots allocation on 6 nodes...  
  9. Using 3 masters:  
  10. 127.0.0.1:7000  
  11. 127.0.0.1:7001  
  12. 127.0.0.1:7002  
  13. Adding replica 127.0.0.1:7003 to 127.0.0.1:7000  
  14. Adding replica 127.0.0.1:7004 to 127.0.0.1:7001  
  15. Adding replica 127.0.0.1:7005 to 127.0.0.1:7002  
  16. M: 2774f156af482b4f76a5c0bda8ec561a8a1719c2 127.0.0.1:7000  
  17.    slots:0-5460 (5461 slots) master  
  18. M: 2d03b862083ee1b1785dba5db2987739cf3a80eb 127.0.0.1:7001  
  19.    slots:5461-10922 (5462 slots) master  
  20. M: 0456869a2c2359c3e06e065a09de86df2e3135ac 127.0.0.1:7002  
  21.    slots:10923-16383 (5461 slots) master  
  22. S: 37b251500385929d5c54a005809377681b95ca90 127.0.0.1:7003  
  23.    replicates 2774f156af482b4f76a5c0bda8ec561a8a1719c2  
  24. S: e2e2e692c40fc34f700762d1fe3a8df94816a062 127.0.0.1:7004  
  25.    replicates 2d03b862083ee1b1785dba5db2987739cf3a80eb  
  26. S: 9923235f8f2b2587407350b1d8b887a7a59de8db 127.0.0.1:7005  
  27.    replicates 0456869a2c2359c3e06e065a09de86df2e3135ac  
  28. Can I set the above configuration? (type 'yes' to accept):  

输入 yes 并按下回车确认之后, 集群就会将配置应用到各个节点, 并连接起(join)各个节点 —— 也即是, 让各个节点开始互相通讯:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Can I set the above configuration? (type 'yes' to accept): yes  
  2. >>> Nodes configuration updated  
  3. >>> Assign a different config epoch to each node  
  4. >>> Sending CLUSTER MEET messages to join the cluster  
  5. Waiting for the cluster to join......  
  6. >>> Performing Cluster Check (using node 127.0.0.1:7000)  
  7. M: 2774f156af482b4f76a5c0bda8ec561a8a1719c2 127.0.0.1:7000  
  8.    slots:0-5460 (5461 slots) master  
  9. M: 2d03b862083ee1b1785dba5db2987739cf3a80eb 127.0.0.1:7001  
  10.    slots:5461-10922 (5462 slots) master  
  11. M: 0456869a2c2359c3e06e065a09de86df2e3135ac 127.0.0.1:7002  
  12.    slots:10923-16383 (5461 slots) master  
  13. M: 37b251500385929d5c54a005809377681b95ca90 127.0.0.1:7003  
  14.    slots: (0 slots) master  
  15.    replicates 2774f156af482b4f76a5c0bda8ec561a8a1719c2  
  16. M: e2e2e692c40fc34f700762d1fe3a8df94816a062 127.0.0.1:7004  
  17.    slots: (0 slots) master  
  18.    replicates 2d03b862083ee1b1785dba5db2987739cf3a80eb  
  19. M: 9923235f8f2b2587407350b1d8b887a7a59de8db 127.0.0.1:7005  
  20.    slots: (0 slots) master  
  21.    replicates 0456869a2c2359c3e06e065a09de86df2e3135ac  
  22. [OK] All nodes agree about slots configuration.  
  23. >>> Check for open slots...  
  24. >>> Check slots coverage...  
  25. [OK] All 16384 slots covered.  

一切正常输出以下信息:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [OK] All nodes agree about slots configuration.  
  2. >>> Check for open slots...  
  3. >>> Check slots coverage...  
  4. [OK] All 16384 slots covered.  


Redis Cluster客户端

Redis 集群现阶段的一个问题是客户端实现很少。 以下是一些我知道的实现:

  • redis-rb-cluster 是我(@antirez)编写的 Ruby 实现, 用于作为其他实现的参考。 该实现是对 redis-rb 的一个简单包装, 高效地实现了与集群进行通讯所需的最少语义(semantic)。
  • redis-py-cluster 看上去是 redis-rb-cluster 的一个 Python 版本, 这个项目有一段时间没有更新了(最后一次提交是在六个月之前), 不过可以将这个项目用作学习集群的起点。
  • 流行的 Predis 曾经对早期的 Redis 集群有过一定的支持, 但我不确定它对集群的支持是否完整, 也不清楚它是否和最新版本的 Redis 集群兼容 (因为新版的 Redis 集群将槽的数量从 4k 改为 16k 了)。
  • Redis unstable 分支中的 redis-cli 程序实现了非常基本的集群支持, 可以使用命令 redis-cli -c 来启动。


Redis Cluster测试

集群比较简单的办法就是使用 redis-rb-cluster 或者 redis-cli , 接下来我们将使用 redis-cli 为例来进行演示:


redis-cli基本测试

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-server ~]# redis-cli -c -p 7001  
  2. 127.0.0.1:7001> set name yayun  
  3. OK  
  4. 127.0.0.1:7001> get name  
  5. "yayun"  
  6. 127.0.0.1:7001>   

我们可以看看还有哪些命令可以用:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-server ~]# redis-trib help  
  2. Usage: redis-trib <command> <options> <arguments ...>  
  3.   
  4.   set-timeout     host:port milliseconds  
  5.   add-node        new_host:new_port existing_host:existing_port  
  6.                   --master-id <arg>  
  7.                   --slave  
  8.   fix             host:port  
  9.   help            (show this help)  
  10.   del-node        host:port node_id  
  11.   import          host:port  
  12.                   --from <arg>  
  13.   check           host:port  
  14.   call            host:port command arg arg .. arg  
  15.   create          host1:port1 ... hostN:portN  
  16.                   --replicas <arg>  
  17.   reshard         host:port  
  18.                   --yes  
  19.                   --to <arg>  
  20.                   --from <arg>  
  21.                   --slots <arg>  
  22.   
  23. For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.  
  24. [root@redis-server ~]#   

可以看见有add-node,不用想了,肯定是添加节点。那么del-node就是删除节点。还有check肯定就是检查状态了。

状态检查 cluster nodes

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-server ~]#  redis-cli -p 7000 cluster nodes   
  2. 2d03b862083ee1b1785dba5db2987739cf3a80eb 127.0.0.1:7001 master - 0 1428293673322 2 connected 5461-10922  
  3. 37b251500385929d5c54a005809377681b95ca90 127.0.0.1:7003 slave 2774f156af482b4f76a5c0bda8ec561a8a1719c2 0 1428293672305 4 connected  
  4. e2e2e692c40fc34f700762d1fe3a8df94816a062 127.0.0.1:7004 slave 2d03b862083ee1b1785dba5db2987739cf3a80eb 0 1428293674340 5 connected  
  5. 0456869a2c2359c3e06e065a09de86df2e3135ac 127.0.0.1:7002 master - 0 1428293670262 3 connected 10923-16383  
  6. 2774f156af482b4f76a5c0bda8ec561a8a1719c2 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460  
  7. 9923235f8f2b2587407350b1d8b887a7a59de8db 127.0.0.1:7005 slave 0456869a2c2359c3e06e065a09de86df2e3135ac 0 1428293675362 6 connected  
  8. [root@redis-server ~]#   

检查集群,我们通过check cluster的一个节点,就知道整个集群的状况,可以看出来哪些节点是主,哪些是从。由上可以看到7000-7002是master,7003-7005是slave。


删除节点 del node

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-cluster-yang 7001]# redis-trib del-node 127.0.0.1:7001 '518a8abc4a64578a51323492a8c79c29a1354f4f'  
  2. >>> Removing node 518a8abc4a64578a51323492a8c79c29a1354f4f from cluster 127.0.0.1:7001  
  3. Connecting to node 127.0.0.1:7001: OK  
  4. Connecting to node 127.0.0.1:7004: OK  
  5. Connecting to node 127.0.0.1:7000: OK  
  6. Connecting to node 127.0.0.1:7005: OK  
  7. Connecting to node 127.0.0.1:7002: OK  
  8. Connecting to node 127.0.0.1:7003: OK  
  9. >>> Sending CLUSTER FORGET messages to the cluster...  
  10. >>> SHUTDOWN the node.  
  11. [root@redis-cluster-yang 7001]# redis-trib check 127.0.0.1:7000  
  12. Connecting to node 127.0.0.1:7000: OK  
  13. Connecting to node 127.0.0.1:7005: OK  
  14. Connecting to node 127.0.0.1:7003: OK  
  15. Connecting to node 127.0.0.1:7004: OK  
  16. Connecting to node 127.0.0.1:7002: OK  
  17. >>> Performing Cluster Check (using node 127.0.0.1:7000)  
  18. M: 9c12c07dfbf10e56fea928ff7125f095b28a99d7 127.0.0.1:7000  
  19.    slots:0-5460 (5461 slots) master  
  20.    1 additional replica(s)  
  21. S: 85dde9c952d3a6ffa7fbfe733734bc3a9113091c 127.0.0.1:7005  
  22.    slots: (0 slots) slave  
  23.    replicates d666a74d5bfce0d0cdeaab26fec4a1bed8beae62  
  24. S: cda1b9a0e4242188f3296ed8d3ff0dbe5388dcb7 127.0.0.1:7003  
  25.    slots: (0 slots) slave  
  26.    replicates 9c12c07dfbf10e56fea928ff7125f095b28a99d7  
  27. M: b59cfdc50d347695d5fa22ac0a2f10a4c1b398a7 127.0.0.1:7004  
  28.    slots:5461-10922 (5462 slots) master  
  29.    0 additional replica(s)  
  30. M: d666a74d5bfce0d0cdeaab26fec4a1bed8beae62 127.0.0.1:7002  
  31.    slots:10923-16383 (5461 slots) master  
  32.    1 additional replica(s)  
  33. [OK] All nodes agree about slots configuration.  
  34. >>> Check for open slots...  
  35. >>> Check slots coverage...  
  36. [OK] All 16384 slots covered.  

添加节点add-node

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-cluster-yang 7001]# redis-trib check 127.0.0.1:7004  
  2. Connecting to node 127.0.0.1:7004: OK  
  3. Connecting to node 127.0.0.1:7000: OK  
  4. Connecting to node 127.0.0.1:7002: OK  
  5. Connecting to node 127.0.0.1:7005: OK  
  6. Connecting to node 127.0.0.1:7003: OK  
  7. >>> Performing Cluster Check (using node 127.0.0.1:7004)  
  8. M: b59cfdc50d347695d5fa22ac0a2f10a4c1b398a7 127.0.0.1:7004  
  9.    slots:5461-10922 (5462 slots) master  
  10.    0 additional replica(s)  
  11. M: 9c12c07dfbf10e56fea928ff7125f095b28a99d7 127.0.0.1:7000  
  12.    slots:0-5460 (5461 slots) master  
  13.    1 additional replica(s)  
  14. M: d666a74d5bfce0d0cdeaab26fec4a1bed8beae62 127.0.0.1:7002  
  15.    slots:10923-16383 (5461 slots) master  
  16.    1 additional replica(s)  
  17. S: 85dde9c952d3a6ffa7fbfe733734bc3a9113091c 127.0.0.1:7005  
  18.    slots: (0 slots) slave  
  19.    replicates d666a74d5bfce0d0cdeaab26fec4a1bed8beae62  
  20. S: cda1b9a0e4242188f3296ed8d3ff0dbe5388dcb7 127.0.0.1:7003  
  21.    slots: (0 slots) slave  
  22.    replicates 9c12c07dfbf10e56fea928ff7125f095b28a99d7  
  23. [OK] All nodes agree about slots configuration.  
  24. >>> Check for open slots...  
  25. >>> Check slots coverage...  
  26. [OK] All 16384 slots covered.  
  27. [root@redis-cluster-yang 7001]# redis-trib add-node 127.0.0.1:7001 127.0.0.1:7004  
  28. >>> Adding node 127.0.0.1:7001 to cluster 127.0.0.1:7004  
  29. Connecting to node 127.0.0.1:7004: OK  
  30. Connecting to node 127.0.0.1:7000: OK  
  31. Connecting to node 127.0.0.1:7002: OK  
  32. Connecting to node 127.0.0.1:7005: OK  
  33. Connecting to node 127.0.0.1:7003: OK  
  34. >>> Performing Cluster Check (using node 127.0.0.1:7004)  
  35. M: b59cfdc50d347695d5fa22ac0a2f10a4c1b398a7 127.0.0.1:7004  
  36.    slots:5461-10922 (5462 slots) master  
  37.    0 additional replica(s)  
  38. M: 9c12c07dfbf10e56fea928ff7125f095b28a99d7 127.0.0.1:7000  
  39.    slots:0-5460 (5461 slots) master  
  40.    1 additional replica(s)  
  41. M: d666a74d5bfce0d0cdeaab26fec4a1bed8beae62 127.0.0.1:7002  
  42.    slots:10923-16383 (5461 slots) master  
  43.    1 additional replica(s)  
  44. S: 85dde9c952d3a6ffa7fbfe733734bc3a9113091c 127.0.0.1:7005  
  45.    slots: (0 slots) slave  
  46.    replicates d666a74d5bfce0d0cdeaab26fec4a1bed8beae62  
  47. S: cda1b9a0e4242188f3296ed8d3ff0dbe5388dcb7 127.0.0.1:7003  
  48.    slots: (0 slots) slave  
  49.    replicates 9c12c07dfbf10e56fea928ff7125f095b28a99d7  
  50. [OK] All nodes agree about slots configuration.  
  51. >>> Check for open slots...  
  52. >>> Check slots coverage...  
  53. [OK] All 16384 slots covered.  
  54. Connecting to node 127.0.0.1:7001: OK  
  55. [ERR] Node 127.0.0.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.  

最常见的错误是Node 127.0.0.1:7001 is not empty。解决办法亦很简单。需要删除redis.conf里面cluster-config-file所指定的文件。然后通过redis-cli -p port进去,使用flushall命令删掉所有的数据……   这两件事情都做OK了,重新启动redis,再执行添加节点操作就不会提示 not empty了。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-cluster-yang 7001]# redis-trib add-node 127.0.0.1:7001 127.0.0.1:7004  
  2. >>> Adding node 127.0.0.1:7001 to cluster 127.0.0.1:7004  
  3. Connecting to node 127.0.0.1:7004: OK  
  4. Connecting to node 127.0.0.1:7000: OK  
  5. Connecting to node 127.0.0.1:7002: OK  
  6. Connecting to node 127.0.0.1:7005: OK  
  7. Connecting to node 127.0.0.1:7003: OK  
  8. >>> Performing Cluster Check (using node 127.0.0.1:7004)  
  9. M: b59cfdc50d347695d5fa22ac0a2f10a4c1b398a7 127.0.0.1:7004  
  10.    slots:5461-10922 (5462 slots) master  
  11.    0 additional replica(s)  
  12. M: 9c12c07dfbf10e56fea928ff7125f095b28a99d7 127.0.0.1:7000  
  13.    slots:0-5460 (5461 slots) master  
  14.    1 additional replica(s)  
  15. M: d666a74d5bfce0d0cdeaab26fec4a1bed8beae62 127.0.0.1:7002  
  16.    slots:10923-16383 (5461 slots) master  
  17.    1 additional replica(s)  
  18. S: 85dde9c952d3a6ffa7fbfe733734bc3a9113091c 127.0.0.1:7005  
  19.    slots: (0 slots) slave  
  20.    replicates d666a74d5bfce0d0cdeaab26fec4a1bed8beae62  
  21. S: cda1b9a0e4242188f3296ed8d3ff0dbe5388dcb7 127.0.0.1:7003  
  22.    slots: (0 slots) slave  
  23.    replicates 9c12c07dfbf10e56fea928ff7125f095b28a99d7  
  24. [OK] All nodes agree about slots configuration.  
  25. >>> Check for open slots...  
  26. >>> Check slots coverage...  
  27. [OK] All 16384 slots covered.  
  28. Connecting to node 127.0.0.1:7001: OK  
  29. >>> Send CLUSTER MEET to node 127.0.0.1:7001 to make it join the cluster.  
  30. [OK] New node added correctly.  

默认add-node是添加主master节点,确认一下新添加的节点为master角色了。哈可以通过使用redis-trib.rb add-node –slave –master-id master-id 给指定的redis master节点添加从节点。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-cluster-yang 7001]# redis-trib check 127.0.0.1:7000  
  2. Connecting to node 127.0.0.1:7000: OK  
  3. Connecting to node 127.0.0.1:7005: OK  
  4. Connecting to node 127.0.0.1:7003: OK  
  5. Connecting to node 127.0.0.1:7004: OK  
  6. Connecting to node 127.0.0.1:7001: OK  
  7. Connecting to node 127.0.0.1:7002: OK  
  8. >>> Performing Cluster Check (using node 127.0.0.1:7000)  
  9. M: 9c12c07dfbf10e56fea928ff7125f095b28a99d7 127.0.0.1:7000  
  10.    slots:0-5460 (5461 slots) master  
  11.    1 additional replica(s)  
  12. S: 85dde9c952d3a6ffa7fbfe733734bc3a9113091c 127.0.0.1:7005  
  13.    slots: (0 slots) slave  
  14.    replicates d666a74d5bfce0d0cdeaab26fec4a1bed8beae62  
  15. S: cda1b9a0e4242188f3296ed8d3ff0dbe5388dcb7 127.0.0.1:7003  
  16.    slots: (0 slots) slave  
  17.    replicates 9c12c07dfbf10e56fea928ff7125f095b28a99d7  
  18. M: b59cfdc50d347695d5fa22ac0a2f10a4c1b398a7 127.0.0.1:7004  
  19.    slots:5461-10922 (5462 slots) master  
  20.    0 additional replica(s)  
  21. M: 96f7aeaa57bf974be8eac4df54deeb96b66ff689 127.0.0.1:7001  
  22.    slots: (0 slots) master  
  23.    0 additional replica(s)  
  24. M: d666a74d5bfce0d0cdeaab26fec4a1bed8beae62 127.0.0.1:7002  
  25.    slots:10923-16383 (5461 slots) master  
  26.    1 additional replica(s)  
  27. [OK] All nodes agree about slots configuration.  
  28. >>> Check for open slots...  
  29. >>> Check slots coverage...  
  30. [OK] All 16384 slots covered.  

对集群进行重新分片reshard

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. redis-trib reshard 127.0.0.1:7000  
你只需要指定集群中其中一个节点的地址,redis-trib 就会自动找到集群中的其他节点。
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. How many slots do you want to move (from 1 to 16384)? 4000  

除了移动的哈希槽数量之外,redis-trib 还需要知道重新分片的目标(target node),也即是,负责接收这哈希槽的节点。指定目标需要使用节点的 ID ,而不是 IP 地址和端口

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Please enter all the source node IDs.  
  2. Type 'all' to use all the nodes as source nodes for the hash slots.  
  3. Type 'done' once you entered all the source nodes IDs.  
  4. Source node #1:all  

输入 all 并按下回车之后,redis-trib 将打印出哈希槽的移动计划,如果你觉得没问题的话,就可以输入yes 并再次按下回车:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. $ ./redis-trib.rb reshard 127.0.0.1:7000  
  2. ...  
  3. Moving slot 11421 from 393c6df5eb4b4cec323f0e4ca961c8b256e3460a  
  4. Moving slot 11422 from 393c6df5eb4b4cec323f0e4ca961c8b256e3460a  
  5. Moving slot 5461 from e68e52cee0550f558b03b342f2f0354d2b8a083b  
  6. Moving slot 5469 from e68e52cee0550f558b03b342f2f0354d2b8a083b  
  7. ...  
  8. Moving slot 5959 from e68e52cee0550f558b03b342f2f0354d2b8a083b  
  9. Do you want to proceed with the proposed reshard plan (yes/no)? yes  
确认分片后集群是否正常

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-cluster-yang 7001]# redis-trib check 127.0.0.1:7000  
  2. Connecting to node 127.0.0.1:7000: OK  
  3. Connecting to node 127.0.0.1:7005: OK  
  4. Connecting to node 127.0.0.1:7003: OK  
  5. Connecting to node 127.0.0.1:7004: OK  
  6. Connecting to node 127.0.0.1:7001: OK  
  7. Connecting to node 127.0.0.1:7002: OK  
  8. >>> Performing Cluster Check (using node 127.0.0.1:7000)  
  9. M: 9c12c07dfbf10e56fea928ff7125f095b28a99d7 127.0.0.1:7000  
  10.    slots:1333-5460 (4128 slots) master  
  11.    1 additional replica(s)  
  12. S: 85dde9c952d3a6ffa7fbfe733734bc3a9113091c 127.0.0.1:7005  
  13.    slots: (0 slots) slave  
  14.    replicates d666a74d5bfce0d0cdeaab26fec4a1bed8beae62  
  15. S: cda1b9a0e4242188f3296ed8d3ff0dbe5388dcb7 127.0.0.1:7003  
  16.    slots: (0 slots) slave  
  17.    replicates 9c12c07dfbf10e56fea928ff7125f095b28a99d7  
  18. M: b59cfdc50d347695d5fa22ac0a2f10a4c1b398a7 127.0.0.1:7004  
  19.    slots:6795-10922 (4128 slots) master  
  20.    0 additional replica(s)  
  21. M: 96f7aeaa57bf974be8eac4df54deeb96b66ff689 127.0.0.1:7001  
  22.    slots:0-1332,5461-6794,10923-12255 (4000 slots) master  
  23.    0 additional replica(s)  
  24. M: d666a74d5bfce0d0cdeaab26fec4a1bed8beae62 127.0.0.1:7002  
  25.    slots:12256-16383 (4128 slots) master  
  26.    1 additional replica(s)  
  27. [OK] All nodes agree about slots configuration.  
  28. >>> Check for open slots...  
  29. >>> Check slots coverage...  
  30. [OK] All 16384 slots covered.  


故障转移测试

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 127.0.0.1:7001> KEYS *  
  2. 1) "name"  
  3. 127.0.0.1:7001> get name  
  4. "yayun"  
  5. 127.0.0.1:7001>   

可以看见7001是正常的,并且获取到了key,value,现在kill掉7000实例,再进行查询。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-server ~]# ps -ef | grep 7000  
  2. root      4168     1  0 11:49 ?        00:00:03 redis-server *:7000 [cluster]  
  3. root      4385  4361  0 12:39 pts/3    00:00:00 grep 7000  
  4. [root@redis-server ~]# kill 4168  
  5. [root@redis-server ~]# ps -ef | grep 7000  
  6. root      4387  4361  0 12:39 pts/3    00:00:00 grep 7000  
  7. [root@redis-server ~]# redis-cli -c -p 7001  
  8. 127.0.0.1:7001> get name  
  9. "yayun"  
  10. 127.0.0.1:7001>  

可以正常获取到value,现在看看状态。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@redis-server ~]# redis-cli -c -p 7001 cluster nodes  
  2. 2d03b862083ee1b1785dba5db2987739cf3a80eb 127.0.0.1:7001 myself,master - 0 0 2 connected 5461-10922  
  3. 0456869a2c2359c3e06e065a09de86df2e3135ac 127.0.0.1:7002 master - 0 1428295271619 3 connected 10923-16383  
  4. 37b251500385929d5c54a005809377681b95ca90 127.0.0.1:7003 master - 0 1428295270603 7 connected 0-5460  
  5. e2e2e692c40fc34f700762d1fe3a8df94816a062 127.0.0.1:7004 slave 2d03b862083ee1b1785dba5db2987739cf3a80eb 0 1428295272642 5 connected  
  6. 2774f156af482b4f76a5c0bda8ec561a8a1719c2 127.0.0.1:7000 master,fail - 1428295159553 1428295157205 1 disconnected  
  7. 9923235f8f2b2587407350b1d8b887a7a59de8db 127.0.0.1:7005 slave 0456869a2c2359c3e06e065a09de86df2e3135ac 0 1428295269587 6 connected  
  8. [root@redis-server ~]#   

原来的7000端口实例已经显示fail,原来的7003是slave,现在自动提升为master。

关于更多的在线添加节点,删除节点,以及对集群进行重新分片请参考官方文档。


总结

redis-cluster是个好东西,只是stable才出来不久,肯定坑略多,而且现在使用的人比较少,前期了解学习一下是可以的,生产环境肯定要慎重考虑。且需要进行严格的测试。生产环境中redis的集群可以考虑使用Twitter开源的twemproxy,以及豌豆荚开源的codis,这两个项目都比较成熟,现在使用的公司很多。已经向业界朋友得到证实。后面也会写博客介绍twemproxy和codis。

参考资料:

http://redis.readthedocs.org/en/latest/topic/cluster-tutorial.html

http://hot66hot.iteye.com/blog/2050676

http://redisdoc.com/topic/cluster-tutorial.html 

http://xiaorui.cc/2015/05/16/%E9%80%9A%E8%BF%87redis-trib-rb%E8%84%9A%E6%9C%AC%E6%9E%84%E5%BB%BA%E5%B9%B6%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5redis-cluster%E9%9B%86%E7%BE%A4/

0 0
原创粉丝点击