使用Docker快速搭建一套三主三从Redis集群用于开发、测试

文章目录

    • [一 基本概念](#一 基本概念)
    • [二 部署流程](#二 部署流程)
      • [2.1 准备配置文件](#2.1 准备配置文件)
      • [2.2 启动6个 Docker 容器](#2.2 启动6个 Docker 容器)
      • [2.3 集群搭建](#2.3 集群搭建)
    • [三 总结](#三 总结)
      • [四 参考资料](#四 参考资料)

一 基本概念

Redis集群是一种通过将多个Redis节点连接在一起以实现高可用性、数据分片和负载均衡的技术。它允许Redis在不同节点上同时提供服务,提高整体性能和可靠性。根据搭建的方式和集群的特性,Redis集群主要有三种模式:主从复制模式(Master-Slave)、哨兵模式(Sentinel)和Cluster模式。Cluster模式是Redis的一种高级集群模式,它通过数据分片和分布式存储实现了负载均衡和高可用性。在Cluster模式下,Redis将所有的键值对数据分散在多个节点上。每个节点负责一部分数据,称为槽位。通过对数据的分片,Cluster模式可以突破单节点的内存限制,实现更大规模的数据存储。Redis Cluster将数据分为16384个槽位,每个节点负责管理一部分槽位。当客户端向Redis Cluster发送请求时,Cluster会根据键的哈希值将请求路由到相应的节点。具体来说,Redis Cluster使用CRC16算法计算键的哈希值,然后对16384取模,得到槽位编号。

二 部署流程

2.1 准备配置文件

准备6个配置文件,在当前工作目录创建conf目录,将配置文件放置于conf目录下面。配置文件具体内容如下(IP为搭建主机的本机IP):

第一个配置文件redis-01.conf:

conf 复制代码
bind 172.31.164.149
port 6001
cluster-enabled yes
cluster-config-file nodes-6001.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b # Master 节点密码
requirepass C*x#1a2b # 集群密码

第二个配置文件redis-02.conf:

conf 复制代码
bind 172.31.164.149
port 6002
cluster-enabled yes
cluster-config-file nodes-6002.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b

第三个配置文件redis-03.conf:

conf 复制代码
bind 172.31.164.149
port 6003
cluster-enabled yes
cluster-config-file nodes-6003.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b

第四个配置文件redis-04.conf:

conf 复制代码
bind 172.31.164.149
port 6004
cluster-enabled yes
cluster-config-file nodes-6004.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b

第五个配置文件redis-05.conf:

conf 复制代码
bind 172.31.164.149
port 6005
cluster-enabled yes
cluster-config-file nodes-6005.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b

第六个配置文件redis-06.conf:

conf 复制代码
bind 172.31.164.149
port 6006
cluster-enabled yes
cluster-config-file nodes-6006.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b

2.2 启动6个 Docker 容器

bash 复制代码
docker run -d --restart always --name redis-01 -v ${PWD}/conf/redis-01.conf:/etc/redis-01.conf --net host redis:5.0.8 redis-server /etc/redis-01.conf
docker run -d --restart always --name redis-02 -v ${PWD}/conf/redis-02.conf:/etc/redis-02.conf --net host redis:5.0.8 redis-server /etc/redis-02.conf
docker run -d --restart always --name redis-03 -v ${PWD}/conf/redis-03.conf:/etc/redis-03.conf --net host redis:5.0.8 redis-server /etc/redis-03.conf
docker run -d --restart always --name redis-04 -v ${PWD}/conf/redis-04.conf:/etc/redis-04.conf --net host redis:5.0.8 redis-server /etc/redis-04.conf
docker run -d --restart always --name redis-05 -v ${PWD}/conf/redis-05.conf:/etc/redis-05.conf --net host redis:5.0.8 redis-server /etc/redis-05.conf
docker run -d --restart always --name redis-06 -v ${PWD}/conf/redis-06.conf:/etc/redis-06.conf --net host redis:5.0.8 redis-server /etc/redis-06.conf

2.3 集群搭建

进入其中一个节点:

shell 复制代码
docker exec -it redis-01 bash

输入以下命令,IP为搭建主机的本地IP:

shell 复制代码
redis-cli -a 'C*x#1a2b' --cluster create 172.31.164.149:6001 172.31.164.149:6002 172.31.164.149:6003 172.31.164.149:6004 172.31.164.149:6005 172.31.164.149:6006 --cluster-replicas 1

待通信完成后,查看集群信息。显示内容如下则搭建成功:

log 复制代码
172.31.164.149:6001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:308
cluster_stats_messages_pong_sent:313
cluster_stats_messages_sent:621
cluster_stats_messages_ping_received:313
cluster_stats_messages_pong_received:308
cluster_stats_messages_received:621

查看集群节点信息:

log 复制代码
172.31.164.149:6001> cluster nodes
2c3e75ba9ba690330fe12f644dee245536ae03c8 172.31.164.149:6005@16005 slave a917ef7525101f73e5b72b49ec4158a613c02bbc 0 1702455066301 5 connected
a917ef7525101f73e5b72b49ec4158a613c02bbc 172.31.164.149:6002@16002 master - 0 1702455066502 2 connected 5461-10922
a0238143b17860ae51a80096a0a7418f3f30d964 172.31.164.149:6001@16001 myself,master - 0 1702455066000 1 connected 0-5460
99902aacf4a2d697d000d123d16663ce0fe84428 172.31.164.149:6004@16004 slave a0238143b17860ae51a80096a0a7418f3f30d964 0 1702455067304 4 connected
05c3ff9b8316e22a2dd241642fac37f9a291fb8f 172.31.164.149:6003@16003 master - 0 1702455066101 3 connected 10923-16383
589515d5cc3c9ee9458daf7b719a6e6ee3f3b405 172.31.164.149:6006@16006 slave 05c3ff9b8316e22a2dd241642fac37f9a291fb8f 0 1702455066000 6 connected

三 总结

1、目前使用容器部署Docker仅能使用host这种网络模式,其他模式暂未实验成功,如果有了解的小伙伴欢迎赐教;

2、使用host网络模式部署集群是因为Redis除了节点暴露端口之外,还有需要开放一个集群总线端口,官网说明:Every Redis Cluster node requires two open TCP connections: a Redis TCP port used to serve clients, e.g., 6379, and second port known as the cluster bus port. By default, the cluster bus port is set by adding 10000 to the data port (e.g., 16379); however, you can override this in the cluster-port configuration;

3、需要注意的是:本文使用一台主机部署6个节点的集群,仅用于开发、测试环境,一般不用于生产环境。如需在多台主机部署集群,请根据实际情况对IP和端口进行规划,但需确保各主机之间能够正常通信。

四 参考资料

1、Scale with Redis Cluster

相关推荐
爱写Bug的浮游几秒前
RTA高频请求下的Redis短缓存工程闭环
redis
没有气的汽水4 分钟前
redis加了个分片,把线上写挂了一个多小时
redis
风翼靓崽15 分钟前
mysql数据备份/恢复
数据库·mysql
贝锐1 小时前
金仓、达梦、TDSQL全适配:向日葵补齐信创数据库层远程运维能力
运维·数据库·信创
xixiaoyunya1 小时前
Redis缓存三大问题实战:穿透、击穿、雪崩,我用一个电商秒杀场景讲清楚
数据库·redis·缓存
这个DBA有点耶2 小时前
Merge还是Materialize?一文讲透MySQL 8.0 CTE底层的执行计划博弈与性能陷阱
数据库·mysql·架构
鶴哥只手遮天3 小时前
《Redis 8.10.3 深度剖析》系列 · 第 1 章·全景与源码地图:版本谱系、构建体系、全局状态与启动链路
redis·后端·开源
70后写java的老码农3 小时前
docker-compose 部署 Redis Stack Server 6.2.6-v19
redis·docker
Thanks_ks3 小时前
MySQL 的 REPEATABLE READ 到底能不能防幻读——从快照读、当前读到 Next-Key Lock
数据库·mysql·innodb·mvcc·锁机制·事务隔离级别·间隙锁
.冰块.3 小时前
流量大了自动加 Pod!K8s HPA 扩缩容与 ResourceQuota/LimitRange 资源管控实战
linux·云原生·容器·kubernetes·hpa·quota·limitrange