Redis Cluster 实践

时间:2023-03-09 08:43:12
Redis Cluster 实践

一:关于redis cluster

1:redis cluster的现状

reids-cluster计划在redis3.0中推出,可以看作者antirez的声明:http://antirez.com/news/49 (ps:跳票了好久,今年貌似加快速度了),目前的最新版本见:https://raw.githubusercontent.com/antirez/redis/3.0/00-RELEASENOTES

作者的目标:Redis Cluster will support up to ~1000 nodes. 赞...

目前redis支持的cluster特性(已测试):

1):节点自动发现

2):slave->master 选举,集群容错

3):Hot resharding:在线分片

4):集群管理:cluster xxx

5):基于配置(nodes-port.conf)的集群管理

6):ASK 转向/MOVED 转向机制.

2:redis cluster 架构

1)redis-cluster架构图

Redis Cluster 实践

架构细节:

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

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

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

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

2) redis-cluster选举:容错

Redis Cluster 实践

(1)领着选举过程是集群中所有master参与,如果半数以上master节点与故障节点通信超过(cluster-node-timeout),认为该节点故障,自动触发故障转移操作.

(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的使用

1:安装redis cluster

1):安装redis-cluster依赖:redis-cluster的依赖库在使用时有兼容问题,在reshard时会遇到各种错误,请按指定版本安装.

(1)确保系统安装zlib,否则gem install会报(no such file to load -- zlib)
  1. .tar
  2. ./configure
  3. make
  4. make install
(2)安装ruby:version(1.9.2)
  1. # ruby1.9.2
  2. cd /path/ruby
  3. ./configure -prefix=/usr/local/ruby
  4. make
  5. make install
  6. sudo cp ruby /usr/local/bin
(3)安装rubygem:version(1.8.16)
  1. .tgz
  2. cd /path/gem
  3. sudo ruby setup.rb
  4. sudo cp bin/gem /usr/local/bin
(4)安装gem-redis:version(3.0.0)
  1. #由于源的原因,可能下载失败,就手动下载下来安装
  2. #download地址:http://rubygems.org/gems/redis/versions/3.0.0
  3. .gem
(5)安装redis-cluster
  1. cd /path/redis
  2. make
  3. sudo cp /opt/redis/src/redis-server /usr/local/bin
  4. sudo cp /opt/redis/src/redis-cli /usr/local/bin
  5. sudo cp /opt/redis/src/redis-trib.rb /usr/local/bin

2:配置redis cluster

1)redis配置文件结构:

Redis Cluster 实践
 使用包含(include)把通用配置和特殊配置分离,方便维护.

2)redis通用配置.

  1. #GENERAL
  2. daemonize no
  3. loglevel notice
  4. dir /opt/redis/data
  5. slave-serve-stale-data yes
  6. #slave只读
  7. slave-read-only yes
  8. #not use default
  9. repl-disable-tcp-nodelay yes
  10. #打开aof持久化
  11. appendonly yes
  12. #每秒一次aof写
  13. appendfsync everysec
  14. #关闭在aof rewrite的时候对新的写操作进行fsync
  15. no-appendfsync-on-rewrite yes
  16. auto-aof-rewrite-min-size 64mb
  17. #打开redis集群
  18. cluster-enabled yes
  19. #节点互连超时的阀值
  20. notify-keyspace-events ""
  21. activerehashing yes
  22. aof-rewrite-incremental-fsync yes

3)redis特殊配置.

  1. #包含通用配置
  2. include /opt/redis/redis-common.conf
  3. #监听tcp端口
  4. #最大可用内存
  5. maxmemory 100m
  6. #内存耗尽时采用的淘汰策略:
  7. # volatile-lru -> remove the key with an expire set using an LRU algorithm
  8. # allkeys-lru -> remove any key accordingly to the LRU algorithm
  9. # volatile-random -> remove a random key with an expire set
  10. # allkeys-random -> remove a random key, any key
  11. # volatile-ttl -> remove the key with the nearest expire time (minor TTL)
  12. # noeviction -> don't expire at all, just return an error on write operations
  13. maxmemory-policy allkeys-lru
  14. #aof存储文件
  15. appendfilename "appendonly-6379.aof"
  16. #不开启rdb存储,只用于添加slave过程
  17. .rdb
  18. #cluster配置文件(启动自动生成)
  19. .conf
  20. #部署在同一机器的redis实例,把auto-aof-rewrite搓开,因为cluster环境下内存占用基本一致.
  21. #防止同意机器下瞬间fork所有redis进程做aof rewrite,占用大量内存
  22. -

3:cluster 操作

cluster集群相关命令,更多redis相关命令见文档:http://redis.readthedocs.org/en/latest/

  1. 集群
  2. CLUSTER INFO 打印集群的信息
  3. CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。
  4. 节点
  5. CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
  6. CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。
  7. CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。
  8. CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。
  9. 槽(slot)
  10. CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。
  11. CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。
  12. CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
  13. CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
  14. CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。
  15. CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。
  16. CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。
  17. CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。
  18. CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。
  19. CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键。

4:redis cluster 运维操作

1)初始化并构建集群

(1)启动集群相关节点(必须是空节点,beta3后可以是有数据的节点),指定配置文件和输出日志

  1. .conf > /opt/redis/logs/redis-.log >& &
  2. .conf > /opt/redis/logs/redis-.log >& &
  3. .conf > /opt/redis/logs/redis-.log >& &
  4. .conf > /opt/redis/logs/redis-.log >& &
  5. .conf > /opt/redis/logs/redis-.log >& &
  6. .conf > /opt/redis/logs/redis-.log >& &

(2):使用自带的ruby工具(redis-trib.rb)构建集群

  1. #redis-trib.rb的create子命令构建
  2. #--replicas 则指定了为Redis Cluster中的每个Master节点配备几个Slave节点
  3. )

(3):检查集群状态

  1. #redis-trib.rb的check子命令构建
  2. #ip:port可以是集群的任意节点

最后输出如下信息,没有任何警告或错误,表示集群启动成功并处于ok状态

  1. [OK] All nodes agree about slots configuration.
  2. >>> Check for open slots...
  3. >>> Check slots coverage...
  4. slots covered.

2):添加新master节点

(1)添加一个master节点:创建一个空节点(empty node),然后将某些slot移动到这个空节点上,这个过程目前需要人工干预

a):根据端口生成配置文件(ps:establish_config.sh是我自己写的输出配置脚本)

  1. > conf/redis-.conf

b):启动节点

  1. .conf > /opt/redis/logs/redis-.log >& &

c):加入空节点到集群
add-node  将一个节点添加到集群里面, 第一个是新节点ip:port, 第二个是任意一个已存在节点ip:port

node:新节点没有包含任何数据, 因为它没有包含任何slot。新加入的加点是一个主节点, 当集群需要将某个从节点升级为新的主节点时, 这个新节点不会被选中,同时新的主节点因为没有包含任何slot,不参加选举和failover。

d):为新节点分配slot

  1. )
  2. to )?
  3. #选择要接受这些slot的node-id
  4. What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf
  5. #选择slot来源:
  6. #all表示从所有的master重新分配,
  7. #或者数据要提取slot的master节点id,最后用done结束
  8. Please enter all the source node IDs.
  9. Type 'all' to use all the nodes as source nodes for the hash slots.
  10. Type 'done' once you entered all the source nodes IDs.
  11. :all
  12. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.
  13. #Do you want to proceed with the proposed reshard plan (yes/no)? yes
  14. #结束

3):添加新的slave节点

a):前三步操作同添加master一样

b)第四步:redis-cli连接上新节点shell,输入命令:cluster replicate 对应master的node-id

  1. cluster replicate 2b9ebcbd627ff0fd7a7bbcc5332fb09e72788835

注意:在线添加slave 时,需要bgsave整个master数据,并传递到slave,再由 slave加载rdb文件到内存,rdb生成和传输的过程中消耗Master大量内存和网络IO,以此不建议单实例内存过大,线上小心操作。

例如本次添加slave操作产生的rdb文件

  1. root root   Apr  : dump-.rdb
  2. root root   Apr  : dump-.rdb

4):在线reshard 数据:

对于负载/数据不均匀的情况,可以在线reshard slot来解决,方法与添加新master的reshard一样,只是需要reshard的master节点是已存在的老节点.

5):删除一个slave节点

  1. #redis-trib del-node ip:port '<node-id>'
  2. 'c7ee2fca17cb79fe3c9822ced1d4f6c5e169e378'

6):删除一个master节点

a):删除master节点之前首先要使用reshard移除master的全部slot,然后再删除当前节点

(redis-trib.rb一次只能把下线节点的slot迁移到一个节点上,如果需要均衡的迁移到其它节点需要执行多次reshard命令)

  1. 节点slot和数据迁移到上
  2. )
  3. to )? (被删除master的所有slot数量)
  4. )
  5. 的node-id)
  6. Please enter all the source node IDs.
  7. Type 'all' to use all the nodes as source nodes for the hash slots.
  8. Type 'done' once you entered all the source nodes IDs.
  9. :f51e26b5d5ff74f85341f06f28f125b7254e61bf(被删除master的node-id)
  10. :done
  11. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.
  12. #Do you want to proceed with the proposed reshard plan (yes/no)? yes

b):删除空master节点

  1. 'f51e26b5d5ff74f85341f06f28f125b7254e61bf'
三:redis cluster 客户端(Jedis)

1:客户端基本操作使用

  1. <span style="font-size: 16px;"> private static BinaryJedisCluster jc;
  2. static {
  3. //只给集群里一个实例就可以
  4. Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
  5. ));
  6. ));
  7. ));
  8. ));
  9. ));
  10. ));
  11. ));
  12. ));
  13. ));
  14. ));
  15. jc = new BinaryJedisCluster(jedisClusterNodes);
  16. }
  17. @Test
  18. public void testBenchRedisSet() throws Exception {
  19. final Stopwatch stopwatch = new Stopwatch();
  20. List list = buildBlogVideos();
  21. ; i < ; i++) {
  22. String key = "key:" + i;
  23. stopwatch.start();
  24. byte[] bytes1 = protostuffSerializer.serialize(list);
  25. * , bytes1);
  26. stopwatch.stop();
  27. }
  28. System.out.println("time=" + stopwatch.toString());
  29. }</span>

2:redis-cluster客户端的一些坑.

1)cluster环境下slave默认不接受任何读写操作,在slave执行readonly命令后,可执行读操作

2)client端不支持多key操作(mget,mset等),但当keys集合对应的slot相同时支持mget操作见:hash_tag

3)不支持多数据库,只有一个db,select 0。

4)JedisCluster 没有针对byte[]的API,需要自己扩展(附件是我加的基于byte[]的BinaryJedisCluster  api)

目前"Jedis-3.0.0-SNAPSHOT"已支持BinaryJedisCluster和基于hash_tag的mget操作.