使用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 小时前
操作系统、虚拟化技术与云原生及云原生AI简述
docker·ai·云原生·容器·华为云·云计算·k8s
2401_840192278 小时前
k8s的StorageClass存储类和pv、pvc、provisioner、物理存储的链路
docker·容器·kubernetes
Kyrie_Li9 小时前
Redis-Sentinel(哨兵模式)
数据库·redis·sentinel
炬火初现10 小时前
redis-cpp-cpp如何使用lua脚本
数据库·redis·lua
hxung10 小时前
Redis 数据类型详解
数据库·redis·缓存
小王不会写code12 小时前
Docker安装、配置Redis
redis·docker
爱的叹息12 小时前
SpringBoot集成Redis 灵活使用 TypedTuple 和 DefaultTypedTuple 实现 Redis ZSet 的复杂操作
spring boot·redis·bootstrap
RedCong12 小时前
如何在k8s中对接s3存储
云原生·容器·kubernetes
SDL大华12 小时前
【备忘】在Docker中安装宝塔面板,实现环境隔离,又能快速迁移服务器环境
服务器·docker·容器
TC139814 小时前
docker 终端打不开rviz2界面,报错qt.qpa.xcb: could not connect to display
docker·容器