使用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 小时前
12-Docker发布微服务
java·docker·微服务
Arc星语3 小时前
Docker Redis集群3主3从模式
redis·docker
不惑_3 小时前
Redis与MySQL双写一致性的缓存模式
redis·mysql·缓存
工作不忙6 小时前
不使用docker-compose不使用zookeeper启动ApacheKafka3.8.0单机运行KRAFT模式
ubuntu·docker·zookeeper·kafka·apache
盒马盒马6 小时前
Docker:存储卷
docker·容器
走,我们去吹风6 小时前
redis实现分布式锁,go实现完整code
redis·分布式·golang
Hadoop_Liang7 小时前
Docker Compose一键部署Spring Boot + Vue项目
vue.js·spring boot·docker
三日看尽长安花7 小时前
【Redis:原理、架构与应用】
数据库·redis·架构
诡异森林。8 小时前
Docker:容器化和虚拟化
java·docker·容器
斯普信专业组12 小时前
K8s企业应用之容器化迁移
云原生·容器·kubernetes