学习使用docker-compose搭建Redis哨兵集群
搭建的记录
- Redis的容器部署后,redis-server的默认安装目录为 /usr/local/bin。
- docker volumes 映射的文件与本地的文件共享,修改本地文件后,需要重启docker容器。
- redis集群内部需要网络互通,可以使用host网络,也可以使用容器名称+端口来代替ip+端口等。
- 容器启动时,使用redis配置文件启动,使用command,redis-server /usr/local/redis/conf/redis.conf(存储卷映射的地址文件)。
- 在redis的配置文件中配置外网可以访问,redis主从,都需要对外提高服务,客户端可以读取所有的主从服务器,客户端的写入,只能写入Master,由Master同步到所有的Slave。
- 使用redis.conf文件启动redis,由于docker-compose是单机部署多个Redis的实例,在服务器上搭建集群都是分为多个服务器,每个服务器上部署不同的实例,启动redis使用配置文件的方式,可以适用于直接在linux上搭建,改动比较小。
Redis配置文件
protected-mode设置为No,bind 0.0.0.0,外网才能访问,masterauth 密码,redis主从切换时密码认证
bind 0.0.0.0
protected-mode no
port 6380
timeout 30
daemonize no
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename "dump.rdb"
dir "/data"
appendonly yes
appendfsync everysec
requirepass 123456li
masterauth "123456li"
Redis哨兵配置文件
任何一个想成为 Leader 的哨兵,要满足两个条件:第一,拿到半数以上的赞成票;第二,拿到的票数同时还需要大于等于哨兵配置文件中的 quorum 值
sentinel monitor 名称 ip 端口 quorum 值,sentinel auth-pass redis Master的密码
port 26380
dir "/data"
sentinel myid 60535b6a4eaaec3ee4c555c659eb4f2934e4c5dc
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 172.26.0.4 6381 2
sentinel failover-timeout mymaster 10000
sentinel auth-pass mymaster 123456li
sentinel config-epoch mymaster 8
docker-compose.yml
搭建redis一主二从三哨兵的docker-compose.yml文件
version: '3'
services:
master:
image: redis
container_name: redis-master
ports:
- 6380:6380
volumes:
- /root/redis/masterslave/master/data:/data
- /root/redis/masterslave/master/conf:/usr/local/redis/conf
command:
- /bin/bash
- -c
- |
cd /usr/local/bin
redis-server /usr/local/redis/conf/redis.conf
slave1:
image: redis
container_name: redis-slave-1
ports:
- 6381:6381
volumes:
- /root/redis/masterslave/slave1/data:/data
- /root/redis/masterslave/slave1/conf:/usr/local/redis/conf
command:
- /bin/bash
- -c
- |
cd /usr/local/bin
redis-server /usr/local/redis/conf/redis.conf
slave2:
image: redis
container_name: redis-slave-2
ports:
- 6382:6382
volumes:
- /root/redis/masterslave/slave2/data:/data
- /root/redis/masterslave/slave2/conf:/usr/local/redis/conf
command:
- /bin/bash
- -c
- |
cd /usr/local/bin
redis-server /usr/local/redis/conf/redis.conf
sentinel1:
image: redis
container_name: redis-sentinel-1
ports:
- "26380:26380"
volumes:
- /root/redis/masterslave/master/sentinelconf:/usr/local/redis/sentinelconf
command: redis-sentinel /usr/local/redis/sentinelconf/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
ports:
- "26381:26381"
volumes:
- /root/redis/masterslave/slave1/sentinelconf:/usr/local/redis/sentinelconf
command: redis-sentinel /usr/local/redis/sentinelconf/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- "26382:26382"
volumes:
- /root/redis/masterslave/slave2/sentinelconf:/usr/local/redis/sentinelconf
command: redis-sentinel /usr/local/redis/sentinelconf/sentinel.conf
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端