Docker环境搭建高可用Redis-Sentinel集群
环境介绍
在单机docker环境下搭建 “3哨兵+1主2从” 的 Redis Sentinel 集群。
准备工作
- 一台装有docker环境的 Linux/Wins/macOS 设备
- 了解基础的docker命令
- 了解docker-compose的使用
正式开始
拉取redis镜像
编写Redis集群配置
-
编写主从节点配置:保持为默认配置即可
-
编写哨兵配置:其他保持默认配置即可,只需添加一行以下配置:
sentinel monitor <master-name> <ip> <redis-port> <quorum>
|
至此,Redis集群就算配置好了。接下来,开始编写 docker-compose 文件来编排容器
编排容器
以下给出一个笔者搭建的一个 docker-compose 文件示例
version: '3.7'
services: redis-1: image: redis:alpine container_name: redis-1 command: redis-server /config/redis-1.conf ports: - 6379:6379 volumes: - ./data/redis-1:/data - ./logs/redis-1.log:/var/log/redis.log - ./config:/config networks: - web
redis-2: image: redis:alpine container_name: redis-2 command: redis-server /config/redis-2.conf ports: - 6380:6379 volumes: - ./data/redis-2:/data - ./config:/config - ./logs/redis-2.log:/var/log/redis.log networks: - web depends_on: - redis-1
redis-3: image: redis:alpine container_name: redis-3 command: redis-server /config/redis-3.conf ports: - 6381:6379 volumes: - ./logs/redis-3.log:/var/log/redis.log - ./data/redis-3:/data - ./config:/config networks: - web depends_on: - redis-1
sentinel-1: image: redis:alpine container_name: sentinel-1 command: redis-sentinel /config/sentinel-1.conf ports: - 26379:26379 volumes: - ./data/sentinel-1:/data - ./logs/sentinel-1.log:/var/log/sentinel.log - ./config:/config networks: - web
sentinel-2: image: redis:alpine container_name: sentinel-2 command: redis-sentinel /config/sentinel-2.conf ports: - 26380:26379 volumes: - ./data/sentinel-2:/data - ./config:/config - ./logs/sentinel-2.log:/var/log/sentinel.log networks: - web depends_on: - redis-1
sentinel-3: image: redis:alpine container_name: sentinel-3 command: redis-sentinel /config/sentinel-3.conf ports: - 26381:26379 volumes: - ./logs/sentinel-3.log:/var/log/sentinel.log - ./data/sentinel-3:/data - ./config:/config networks: - web depends_on: - redis-1
networks: web: name: "common.network" external: true
|
运行服务
进入到 docker-compose 文件所在目录,执行:
mkdir logs data
docker network create web
docker-compose up -d
|
至此,大功告成~