Redis集群部署

以下是为 Redis 7.2.4(最新版本-我替换成了latest)编写的完整 docker-compose.yml 文件,包含 6 个节点 (3 主 + 3 从) 的完整配置:

单个部署#

docker run --name redis-single -p 6379:6379 -v $(pwd)/data/single:/data redis:latest redis-server --appendonly yes

安装#

version: '3.8'

services:
  redis-7000:
    image: redis:latest
    container_name: redis-7000
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - ./config/redis-7000.conf:/usr/local/etc/redis/redis.conf
      - ./data/7000:/data
    networks:
      - redis-cluster-net
    ports:
      - "7000:7000"
      - "17000:17000"

  redis-7001:
    image: redis:latest
    container_name: redis-7001
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - ./config/redis-7001.conf:/usr/local/etc/redis/redis.conf
      - ./data/7001:/data
    networks:
      - redis-cluster-net
    ports:
      - "7001:7001"
      - "17001:17001"

  redis-7002:
    image: redis:latest
    container_name: redis-7002
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - ./config/redis-7002.conf:/usr/local/etc/redis/redis.conf
      - ./data/7002:/data
    networks:
      - redis-cluster-net
    ports:
      - "7002:7002"
      - "17002:17002"

  redis-7003:
    image: redis:latest
    container_name: redis-7003
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - ./config/redis-7003.conf:/usr/local/etc/redis/redis.conf
      - ./data/7003:/data
    networks:
      - redis-cluster-net
    ports:
      - "7003:7003"
      - "17003:17003"

  redis-7004:
    image: redis:latest
    container_name: redis-7004
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - ./config/redis-7004.conf:/usr/local/etc/redis/redis.conf
      - ./data/7004:/data
    networks:
      - redis-cluster-net
    ports:
      - "7004:7004"
      - "17004:17004"

  redis-7005:
    image: redis:latest
    container_name: redis-7005
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - ./config/redis-7005.conf:/usr/local/etc/redis/redis.conf
      - ./data/7005:/data
    networks:
      - redis-cluster-net
    ports:
      - "7005:7005"
      - "17005:17005"

networks:
  redis-cluster-net:
    driver: bridge

配套操作步骤#

  1. 创建目录和配置文件
mkdir -p redis-cluster/{config,data/{7000,7001,7002,7003,7004,7005}}
cd redis-cluster
  1. 生成所有配置文件(一键生成脚本)
for port in {7000..7005}; do
  cat > config/redis-${port}.conf <<EOF
port ${port}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
dir /data
# 重要:替换为你的宿主机IP(非容器IP)
cluster-announce-ip 192.168.1.100
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
EOF
done
  1. 启动所有容器
podman-compose up -d

或者