docker-compose搭建redis集群--Cluster模式
Redis有三种集群模式
* 主从模式
* Sentinel模式
* Cluster模式
参考:Redis集群详解
1.首先需要docker 、docker-compose 环境
2.下载相关配置文件
Gitee地址: https://gitee.com/lifeToSpring/redis-cluster
目录结构:
redis.sh
redis-server /config/nodes-${PORT}.conf
3.修改对应nodes-${PORT}.conf的配置文件(参考):
# bind 127.0.0.1 //加上注释#
protected-mode no //关闭保护模式
port 6391 //绑定自定义端口
# daemonize yes //禁止redis后台运行
pidfile /var/run/redis_6391.pid
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_6391.conf //集群的配置 配置文件首次启动自动生成
appendonly yes //开启aof
cluster-announce-ip 10.xx.xx.xx //要宣布的IP地址。nat模式要指定宿主机IP
cluster-announce-port 6391 //要宣布的数据端口。
cluster-announce-bus-port 16391 //要宣布的集群总线端口
4.1 节点192.168.255.225:
docker-compose.yml
services:
redis-master1:
image: redis:5.0 # 基础镜像
container_name: node1 # 容器名称
working_dir: /config # 切换工作目录
environment: # 环境变量
- PORT=6391 # 会使用config/nodes-${PORT}.conf这个配置文件
ports: # 映射端口,对外提供服务
- 6391:6391 # redis的服务端口
- 16391:16391 # redis集群监控端口
stdin_open: true # 标准输入打开
tty: true # 后台运行不退出
network_mode: host # 使用host模式
privileged: true # 拥有容器内命令执行的权限
volumes:
- /mydata/redis-cluster/config:/config #配置文件目录映射到宿主机
entrypoint: # 设置服务默认的启动程序
- /bin/bash
- redis.sh
redis-master2:
image: redis:5.0
working_dir: /config
container_name: node2
environment:
- PORT=6392
ports:
- 6392:6392
- 16392:16392
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
redis-master3:
image: redis:5.0
container_name: node3
working_dir: /config
environment:
- PORT=6393
ports:
- 6393:6393
- 16393:16393
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
启动redis实例
docker-compose -f docker-compose.yml up
4.2 节点192.168.255.226:
docker-compose.yml
version: "3"
services:
redis-slave1:
image: redis:5.0
container_name: node4
working_dir: /config
environment:
- PORT=6394
ports:
- 6394:6394
- 16394:16394
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
redis-slave2:
image: redis:5.0
working_dir: /config
container_name: node5
environment:
- PORT=6395
ports:
- 6395:6395
- 16395:16395
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
redis-slave3:
image: redis:5.0
container_name: node6
working_dir: /config
environment:
- PORT=6396
ports:
- 6396:6396
- 16396:16396
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
启动redis实例
docker-compose -f docker-compose.yml up
5 使用docker镜像 zvelo/redis-trib 构建redis集群
docker run --rm -it zvelo/redis-trib create --replicas 1 192.168.255.225:6391 192.168.255.225:6392 192.168.255.225:6393 192.168.255.226:6394 192.168.255.226:6395 192.168.255.226:6396
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义