Redis Cluster 集群
在 Redis 中,集群的解决方案有三种
- 主从复制
- 哨兵机制
- Cluster
主从复制
Redis 虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况。为了分担读压力,Redis 支持主从复制,读写分离。一个 Master 可以有多个 Slaves。

优点
缺点
- 不能自动故障恢复,RedisHA 系统(需要开发)
- 无法实现动态扩容
哨兵机制
Redis Sentinel 是社区版本推出的原生高可用解决方案,其部署架构主要包括两部分:Redis Sentinel 集群和 Redis 数据集群。
其中 Redis Sentinel 集群是由若干 Sentinel 节点组成的分布式集群,可以实现故障发现、故障自动转移、配置中心和客户端通知。Redis Sentinel 的节点数量要满足 2n+1(n>=1)的奇数个。
优点
- 自动化故障恢复
缺点
- Redis 数据节点中 slave 节点作为备份节点不提供服务
- 无法实现动态扩容
Cluster

Redis Cluster 是社区版推出的 Redis 分布式集群解决方案,主要解决 Redis 分布式方面的需求,比如,当遇到单机内存,并发和流量等瓶颈的时候,Redis Cluster 能起到很好的负载均衡的目的。
Redis Cluster 着眼于提高并发量。
群集至少需要 3 主 3 从,且每个实例使用不同的配置文件。
在 redis-cluster 架构中,redis-master 节点一般用于接收读写,而 redis-slave 节点则一般只用于备份, 其与对应的 master 拥有相同的 slot 集合,若某个 redis-master 意外失效,则再将其对应的 slave 进行升级为临时 redis-master。
在 redis 的官方文档中,对 redis-cluster 架构上,有这样的说明:在 cluster 架构下,默认的,一般 redis-master 用于接收读写,而 redis-slave 则用于备份,当有请求是在向 slave 发起时,会直接重定向到对应 key 所在的 master 来处理。 但如果不介意读取的是 redis-cluster 中有可能过期的数据并且对写请求不感兴趣时,则亦可通过 readonly 命令,将 slave 设置成可读,然后通过 slave 获取相关的 key,达到读写分离。具体可以参阅 redis 官方文档等相关内容
优点
- 解决分布式负载均衡的问题。具体解决方案是分片/虚拟槽 slot。
- 可实现动态扩容
- P2P 模式,无中心化
缺点
- 为了性能提升,客户端需要缓存路由表信息
- Slave 在集群中充当“冷备”,不能缓解读压力
下面通过 docker-compose 搭建 redis cluster 集群
目录结构
端口(9001-9006)
conf
redis.conf
data
redis.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| # 端口 port 9001 # 密码 requirepass 123456 # Master节点密码 masterauth 123456 # 关闭保护模式 protected-mode no # 开启集群 cluster-enabled yes # 集群节点配置 cluster-config-file nodes.conf # 超时 cluster-node-timeout 5000 # 集群节点IP host模式为宿主机IP cluster-announce-ip 你自己的ip # 集群节点端口 9001 - 9006 cluster-announce-port 9001 cluster-announce-bus-port 19001 # 开启 appendonly 备份模式 appendonly yes # 每秒钟备份 appendfsync everysec # aof文件进行压缩时,是否执行同步操作 no-appendfsync-on-rewrite no # 当aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写 auto-aof-rewrite-percentage 100 # 重写前AOF文件的大小最小值 默认 64mb auto-aof-rewrite-min-size 64mb
# 日志配置 # debug:打印信息会很多,混乱的感觉。(开发/测试阶段) # verbose:比debug级别好一点,仍然有很多不必要的信息。 # notice:于生产环境使用。 # warning:记录关键的警告消息。 loglevel notice # 日志文件路径 logfile "/data/redis.log"
|
目录结构
9001 - 9006 每个目录格式如下:

docker-compose 编排文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| version: '3.7'
services: redis9001: image: redis:6 container_name: redis9001 command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ] volumes: - ./9001/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./9001/data:/data ports: - "9001:9001" - "19001:19001" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai logging: options: max-size: '100m' max-file: '10'
redis9002: image: redis:6 container_name: redis9002 command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ] volumes: - ./9002/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./9002/data:/data ports: - "9002:9002" - "19002:19002" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai logging: options: max-size: '100m' max-file: '10'
redis9003: image: redis:6 container_name: redis9003 command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ] volumes: - ./9003/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./9003/data:/data ports: - "9003:9003" - "19003:19003" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai logging: options: max-size: '100m' max-file: '10'
redis9004: image: redis:6 container_name: redis9004 command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ] volumes: - ./9004/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./9004/data:/data ports: - "9004:9004" - "19004:19004" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai logging: options: max-size: '100m' max-file: '10'
redis9005: image: redis:6 container_name: redis9005 command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ] volumes: - ./9005/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./9005/data:/data ports: - "9005:9005" - "19005:19005" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai logging: options: max-size: '100m' max-file: '10'
redis9006: image: redis:6 container_name: redis9006 command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ] volumes: - ./9006/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./9006/data:/data ports: - "9006:9006" - "19006:19006" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai logging: options: max-size: '100m' max-file: '10'
networks: app_net: external: true
|
启动 Redis 集群


Redis 集群配置
1
| docker exec -it redis9001 redis-cli -p 9001 -a 123456 --cluster create 192.168.0.100:9001 192.168.0.100:9002 192.168.0.100:9003 192.168.0.100:9004 192.168.0.100:9005 192.168.0.100:9006 --cluster-replicas 1
|


测试 Redis 集群
Ping 本机
1 2 3
| docker exec -it redis9001 /bin/sh redis-cli -h 192.168.0.100 -p 9001 -a 123456 ping
|

Ping 其他机器
1 2
| quit redis-cli -h 192.168.0.100 -p 9005 -a 123456 ping
|

Redis 集群 查看信息

Redis 集群 查看节点

Redis 集群 查看 slots 分片

Redis 集群 写入数据测试


Redis 集群 灾难测试


| 添加微信 |
公众号更多内容 |
 |
 |