最近研究redis-cluster,正好搭建了一个环境,遇到了不少坑,系统的总结下,等到redis3 release出来后,换掉memCache 集群java
reids-cluster计划在redis3.0中推出,能够看做者antirez的声明:http://antirez.com/news/49 (ps:跳票了很久,今年貌似加快速度了),目前的最新版本见:https://raw.githubusercontent.com/antirez/redis/3.0/00-RELEASENOTESnode
目前redis支持的cluster特性(已测试):git
1):节点自动发现github
2):slave->master 选举,集群容错redis
3):Hot resharding:在线分片shell
4):集群管理:cluster xxxapi
5):基于配置(nodes-port.conf)的集群管理ruby
6):ASK 转向/MOVED 转向机制.bash
架构细节:架构
(1)全部的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
(2)节点的fail是经过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不须要中间proxy层.客户端不须要链接集群全部节点,链接集群中任何一个可用节点便可
(4)redis-cluster把全部的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value
(1)领着选举过程是集群中全部master参与,若是半数以上master节点与master节点通讯超过(cluster-node-timeout),认为当前master节点挂掉.
(2):何时整个集群不可用(cluster_state:fail)?
a:若是集群任意master挂掉,且当前master没有slave.集群进入fail状态,也能够理解成集群的slot映射[0-16383]不完成时进入fail状态. ps : redis-3.0.0.rc1加入cluster-require-full-coverage参数,默认关闭,打开集群兼容部分失败.
b:若是集群超过半数以上master挂掉,不管是否有slave集群进入fail状态.
ps:当集群不可用时,全部对集群的操做作都不可用,收到((error) CLUSTERDOWN The cluster is down)错误
安装redis-cluster依赖:redis-cluster的依赖库在使用时有兼容问题,在reshard时会遇到各类错误,请按指定版本安装.
确保系统安装zlib,不然gem install会报(no such file to load -- zlib)
#download:zlib-1.2.6.tar ./configure make make install
# ruby1.9.2 cd /path/ruby ./configure -prefix=/usr/local/ruby make make install sudo cp ruby /usr/local/bin
# rubygems-1.8.16.tgz cd /path/gem sudo ruby setup.rb sudo cp bin/gem /usr/local/bin
gem install redis --version 3.0.0 #因为源的缘由,可能下载失败,就手动下载下来安装 #download地址:http://rubygems.org/gems/redis/versions/3.0.0 gem install -l /data/soft/redis-3.0.0.gem
cd /path/redis make sudo cp /opt/redis/src/redis-server /usr/local/bin sudo cp /opt/redis/src/redis-cli /usr/local/bin sudo cp /opt/redis/src/redis-trib.rb /usr/local/bin
使用包含(include)把通用配置和特殊配置分离,方便维护.
#GENERAL daemonize no tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice databases 16 dir /opt/redis/data slave-serve-stale-data yes #slave只读 slave-read-only yes #not use default repl-disable-tcp-nodelay yes slave-priority 100 #打开aof持久化 appendonly yes #每秒一次aof写 appendfsync everysec #关闭在aof rewrite的时候对新的写操做进行fsync no-appendfsync-on-rewrite yes auto-aof-rewrite-min-size 64mb lua-time-limit 5000 #打开redis集群 cluster-enabled yes #节点互连超时的阀值 cluster-node-timeout 15000 cluster-migration-barrier 1 slowlog-log-slower-than 10000 slowlog-max-len 128 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
#包含通用配置 include /opt/redis/redis-common.conf #监听tcp端口 port 6379 #最大可用内存 maxmemory 100m #内存耗尽时采用的淘汰策略: # volatile-lru -> remove the key with an expire set using an LRU algorithm # allkeys-lru -> remove any key accordingly to the LRU algorithm # volatile-random -> remove a random key with an expire set # allkeys-random -> remove a random key, any key # volatile-ttl -> remove the key with the nearest expire time (minor TTL) # noeviction -> don't expire at all, just return an error on write operations maxmemory-policy allkeys-lru #aof存储文件 appendfilename "appendonly-6379.aof" #不开启rdb存储,只用于添加slave过程 dbfilename dump-6379.rdb #cluster配置文件(启动自动生成) cluster-config-file nodes-6379.conf #部署在同一机器的redis实例,把auto-aof-rewrite搓开,由于cluster环境下内存占用基本一致. #防止赞成机器下瞬间fork全部redis进程作aof rewrite,占用大量内存(ps:cluster必须开启aof) auto-aof-rewrite-percentage 80-100
cluster集群相关命令,更多redis相关命令见文档:http://redis.readthedocs.org/en/latest/
# 集群 CLUSTER INFO 打印集群的信息 CLUSTER NODES 列出集群当前已知的全部节点(node),以及这些节点的相关信息。 节点 CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。 CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。 CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。 CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。 # 槽(slot) CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。 CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。 CLUSTER FLUSHSLOTS 移除指派给当前节点的全部槽,让当前节点变成一个没有指派任何槽的节点。 CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,若是槽已经指派给另外一个节点,那么先让另外一个节点删除该槽>,而后再进行指派。 CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。 CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。 CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。 # 键 CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪一个槽上。 CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。 CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。
(1)启动集群相关节点(必须是空节点,beta3后能够是有数据的节点),指定配置文件和输出日志
redis-server /opt/redis/conf/redis-6380.conf > /opt/redis/logs/redis-6380.log 2>&1 & redis-server /opt/redis/conf/redis-6381.conf > /opt/redis/logs/redis-6381.log 2>&1 & redis-server /opt/redis/conf/redis-6382.conf > /opt/redis/logs/redis-6382.log 2>&1 & redis-server /opt/redis/conf/redis-7380.conf > /opt/redis/logs/redis-7380.log 2>&1 & redis-server /opt/redis/conf/redis-7381.conf > /opt/redis/logs/redis-7381.log 2>&1 & redis-server /opt/redis/conf/redis-7382.conf > /opt/redis/logs/redis-7382.log 2>&1 &
(2):使用自带的ruby工具(redis-trib.rb)构建集群
#redis-trib.rb的create子命令构建 #--replicas 则指定了为Redis Cluster中的每一个Master节点配备几个Slave节点 #节点角色由顺序决定,先master以后是slave(为方便辨认,slave的端口比master大1000) redis-trib.rb create --replicas 1 10.10.34.14:6380 10.10.34.14:6381 10.10.34.14:6382 10.10.34.14:7380 10.10.34.14:7381 10.10.34.14:7382
(3):检查集群状态
#redis-trib.rb的check子命令构建 #ip:port能够是集群的任意节点 redis-trib.rb check 10.10.34.14:6380
最后输出以下信息,没有任何警告或错误,表示集群启动成功并处于ok状态
[OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
(1)添加一个master节点:建立一个空节点(empty node),而后将某些slot移动到这个空节点上,这个过程目前须要人工干预
a):根据端口生成配置文件(ps:establish_config.sh是我本身写的输出配置脚本)
sh establish_config.sh 6386 > conf/redis-6386.conf
b):启动节点
redis-server /opt/redis/conf/redis-6386.conf > /opt/redis/logs/redis-6386.log 2>&1 &
c):加入空节点到集群
add-node 将一个节点添加到集群里面, 第一个是新节点ip:port, 第二个是任意一个已存在节点ip:port
redis-trib.rb add-node 10.10.34.14:6386 10.10.34.14:6381
node:新节点没有包含任何数据, 由于它没有包含任何slot。新加入的加点是一个主节点, 当集群须要将某个从节点升级为新的主节点时, 这个新节点不会被选中
d):为新节点分配slot
redis-trib.rb reshard 10.10.34.14:6386 #根据提示选择要迁移的slot数量(ps:这里选择500) How many slots do you want to move (from 1 to 16384)? 500 #选择要接受这些slot的node-id What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf #选择slot来源: #all表示从全部的master从新分配, #或者数据要提取slot的master节点id,最后用done结束 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 #打印被移动的slot后,输入yes开始移动slot以及对应的数据. #Do you want to proceed with the proposed reshard plan (yes/no)? yes #结束
a):前三步操做同添加master同样
b)第四步:redis-cli链接上新节点shell,输入命令:cluster replicate 对应master的node-id
cluster replicate 2b9ebcbd627ff0fd7a7bbcc5332fb09e72788835
note:在线添加slave 时,须要dump整个master进程,并传递到slave,再由 slave加载rdb文件到内存,rdb传输过程当中Master可能没法提供服务,整个过程消耗大量io,当心操做.
例如本次添加slave操做产生的rdb文件
-rw-r--r-- 1 root root 34946 Apr 17 18:23 dump-6386.rdb -rw-r--r-- 1 root root 34946 Apr 17 18:23 dump-7386.rdb
对于负载/数据不均匀的状况,能够在线reshard slot来解决,方法与添加新master的reshard同样,只是须要reshard的master节点是老节点.
#redis-trib del-node ip:port '<node-id>' redis-trib.rb del-node 10.10.34.14:7386 'c7ee2fca17cb79fe3c9822ced1d4f6c5e169e378'
a):删除master节点以前首先要使用reshard移除master的所有slot,而后再删除当前节点(目前只能把被删除
master的slot迁移到一个节点上)
#把10.10.34.14:6386当前master迁移到10.10.34.14:6380上 redis-trib.rb reshard 10.10.34.14:6380 #根据提示选择要迁移的slot数量(ps:这里选择500) How many slots do you want to move (from 1 to 16384)? 500(被删除master的全部slot数量) #选择要接受这些slot的node-id(10.10.34.14:6380) What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2 (ps:10.10.34.14:6380的node-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:f51e26b5d5ff74f85341f06f28f125b7254e61bf(被删除master的node-id) Source node #2:done #打印被移动的slot后,输入yes开始移动slot以及对应的数据. #Do you want to proceed with the proposed reshard plan (yes/no)? yes
b):删除空master节点
redis-trib.rb del-node 10.10.34.14:6386 'f51e26b5d5ff74f85341f06f28f125b7254e61bf'
private static BinaryJedisCluster jc; static { //只给集群里一个实例就能够 Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>(); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6380)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6381)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6382)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6383)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6384)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7380)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7381)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7382)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7383)); jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7384)); jc = new BinaryJedisCluster(jedisClusterNodes); } @Test public void testBenchRedisSet() throws Exception { final Stopwatch stopwatch = new Stopwatch(); List list = buildBlogVideos(); for (int i = 0; i < 1000; i++) { String key = "key:" + i; stopwatch.start(); byte[] bytes1 = protostuffSerializer.serialize(list); jc.setex(key, 60 * 60, bytes1); stopwatch.stop(); } System.out.println("time=" + stopwatch.toString()); }
1)cluster环境下redis的slave不接受任何读写操做,
2)client端不支持keys批量操做,不支持select dbNum操做,只有一个db:select 0
3)JedisCluster 的info()等单机函数没法调用,返回(No way to dispatch this command to Redis Cluster)错误,.
4)JedisCluster 没有针对byte[]的API,须要本身扩展(附件是我加的基于byte[]的BinaryJedisCluster api)
参考文档:
http://redis.io/topics/cluster-spec
http://redis.io/topics/cluster-tutorial