使用docker-compose搭建redis7集群-3主3从

下面是一个用于搭建 Redis 集群的 docker-compose.yml 示例文件,它会启动 6 个 Redis 节点(3 主节点 + 3 从节点)来构成一个最小的 Redis 集群。

同一个容器内网通讯没问题,但是你要是需要暴露到外网你需要用第二个yml

内网的

yml 复制代码
version: '3.8'
services:
  redis-node1:
    image: redis:7.0
    container_name: redis-node1
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node1", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
    ports:
      - "6379:6379"
      - "16379:16379"
    volumes:
      - ./data/redis-node1:/data
    networks:
      - redis-cluster

  redis-node2:
    image: redis:7.0
    container_name: redis-node2
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node2", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
    ports:
      - "6380:6379"
      - "16380:16379"
    volumes:
      - ./data/redis-node2:/data
    networks:
      - redis-cluster

  redis-node3:
    image: redis:7.0
    container_name: redis-node3
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node3", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
    ports:
      - "6381:6379"
      - "16381:16379"
    volumes:
      - ./data/redis-node3:/data
    networks:
      - redis-cluster

  redis-node4:
    image: redis:7.0
    container_name: redis-node4
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node4", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
    ports:
      - "6382:6379"
      - "16382:16379"
    volumes:
      - ./data/redis-node4:/data
    networks:
      - redis-cluster

  redis-node5:
    image: redis:7.0
    container_name: redis-node5
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node5", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
    ports:
      - "6383:6379"
      - "16383:16379"
    volumes:
      - ./data/redis-node5:/data
    networks:
      - redis-cluster

  redis-node6:
    image: redis:7.0
    container_name: redis-node6
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "redis-node6", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
    ports:
      - "6384:6379"
      - "16384:16379"
    volumes:
      - ./data/redis-node6:/data
    networks:
      - redis-cluster

networks:
  redis-cluster:
    driver: bridge

外网的

yml 复制代码
version: '3.3'
services:
  redis-node1:
    image: redis
    container_name: redis-node1
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6379", "--cluster-announce-bus-port", "16379"]
    ports:
      - "26379:6379"
      - "16379:16379"
    volumes:
      - ./data/redis-node1:/data
    networks:
      - redis-cluster-compose

  redis-node2:
    image: redis
    container_name: redis-node2
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6380", "--cluster-announce-bus-port", "16380"]
    ports:
      - "26380:6379"
      - "16380:16379"
    volumes:
      - ./data/redis-node2:/data
    networks:
      - redis-cluster-compose

  redis-node3:
    image: redis
    container_name: redis-node3
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6381", "--cluster-announce-bus-port", "16381"]
    ports:
      - "26381:6379"
      - "16381:16379"
    volumes:
      - ./data/redis-node3:/data
    networks:
      - redis-cluster-compose

  redis-node4:
    image: redis
    container_name: redis-node4
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6382", "--cluster-announce-bus-port", "16382"]
    ports:
      - "26382:6379"
      - "16382:16379"
    volumes:
      - ./data/redis-node4:/data
    networks:
      - redis-cluster-compose

  redis-node5:
    image: redis
    container_name: redis-node5
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6383", "--cluster-announce-bus-port", "16383"]
    ports:
      - "26383:6379"
      - "16383:16379"
    volumes:
      - ./data/redis-node5:/data
    networks:
      - redis-cluster-compose

  redis-node6:
    image: redis
    container_name: redis-node6
    command: ["redis-server", "--port", "6379", "--cluster-enabled", "yes", "--cluster-config-file", "nodes.conf", "--cluster-node-timeout", "5000", "--appendonly", "yes", "--cluster-announce-ip", "你的外网ip或者域名", "--cluster-announce-port", "6384", "--cluster-announce-bus-port", "16384"]
    ports:
      - "26384:6379"
      - "16384:16379"
    volumes:
      - ./data/redis-node6:/data
    networks:
      - redis-cluster-compose

networks:
  redis-cluster-compose:
    driver: bridge

使用步骤:

  1. 创建目录:
    在你想放置 docker-compose.yml 文件的目录下创建数据目录,例如:
bash 复制代码
mkdir -p ./data/redis-node1 ./data/redis-node2 ./data/redis-node3 ./data/redis-node4 ./data/redis-node5 ./data/redis-node6
  1. 启动集群:
    在 docker-compose.yml 文件所在的目录中运行:
bash 复制代码
docker-compose up -d
  1. 初始化 Redis 集群: 启动所有 Redis 容器后,你需要使用 redis-cli 创建集群:
bash 复制代码
docker exec -it redis-node1 redis-cli --cluster create \
  redis-node1:6379 redis-node2:6379 redis-node3:6379 \
  redis-node4:6379 redis-node5:6379 redis-node6:6379 \
  --cluster-replicas 1
  1. 验证 Redis 集群:
    你可以通过以下命令来验证 Redis 集群是否成功创建:
bash 复制代码
docker exec -it redis-node1 redis-cli -c cluster nodes

这会列出集群中的所有节点及其状态。

搭建中遇到的问题

Node redis-node2:6379 replied with error:

ERR Invalid node address specified: redis-node1:6379

问题的原因可能是 redis-cli --cluster create 命令中的节点地址 redis-node1:6379 这样的主机名在 Redis 集群内部可能无法被解析 。这种情况通常发生在 Docker 容器中,因为每个容器有自己的网络配置,而 Redis 集群要求节点间能通过 IP 地址相互通信。

要解决这个问题,可以尝试以下步骤:

**解决方法:**替换成容器的内网ip就行了
获取IP方法

bash 复制代码
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node1
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node2
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node3
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node4
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node5
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-node6

重要提示:

  • Redis 集群使用多个节点,因此你需要确保每个节点的端口(如 6379, 6380, 6381 等)在宿主机上可用。
  • 如果需要持久化数据,确保在 ./data 目录下有写权限,并且这些目录可以正确映射到容器中的 /data 路径。
相关推荐
云游的二狗1 小时前
【VMWare Workstation 17】安装Debian 12.8DVD
运维·docker·debian
cv-daily2 小时前
通过docker overlay2目录名查找容器名和容器ID
运维·docker·容器
明月与玄武2 小时前
放弃使用Dockerfiles 平替 docker init
docker·容器
Clockwiseee2 小时前
docker学习
学习·docker·eureka
阿猿收手吧!2 小时前
【Redis】Redis入门以及什么是分布式系统{Redis引入+分布式系统介绍}
数据库·redis·缓存
落霞的思绪2 小时前
Redis实战(黑马点评)——涉及session、redis存储验证码,双拦截器处理请求
spring boot·redis·缓存
moton20173 小时前
云原生:构建现代化应用的基石
后端·docker·微服务·云原生·容器·架构·kubernetes
一个假的前端男5 小时前
Windows Docker Desktop安装及使用 Docker 运行 MySQL
windows·docker·容器
ahuang12025 小时前
在centos下使用containerd管理容器:5分钟从docker转型到containerd
linux·docker·centos
问道飞鱼5 小时前
【Springboot知识】Springboot结合redis实现分布式锁
spring boot·redis·分布式