docker-compose 搭建伪分布模式redis cluster集群

1、redis配置文件:redis.conf

port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

2、创建文件和目录

for port in `seq 7001 7006`; do \
  mkdir -p ./${port}/conf \
  && PORT=${port} envsubst < ./redis-conf.tmpl > ./${port}/conf/redis.conf \
  && mkdir -p ./${port}/data; \
done

├── 7001
│   ├── conf
│   │   └── redis.conf
│   └── data
│       ├── appendonly.aof
│       ├── dump.rdb
│       └── nodes.conf ..........
├── 7006
│   ├── conf
│   │   └── redis.conf
│   └── data
│       ├── appendonly.aof
│       ├── dump.rdb
│       └── nodes.conf

3、docker-compose.yml文件

version: "3.7"

networks:
  redis-network:
    name: redis-network
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.18.0.0/24

services:
  redis-cluster: #该容器只是执行一条命令,可以删除手动进入执行
    image: redis:6-alpine
    command: 'redis-cli --cluster create
    172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379
    172.18.0.15:6379 172.18.0.16:6379 --cluster-yes --cluster-replicas 1'
    networks:
      - redis-network
    depends_on:
      - redis1
      - redis2
      - redis3
      - redis4
      - redis5
      - redis6

  redis1:
    image: redis:6-alpine
    container_name: redis1
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.11
    expose:
      - 6379
    volumes:
      - /Users/上面2创建的目录/redis/7001/data:/data
      - /Users/上面2创建的目录/redis/7001/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis2:
    image: redis:6-alpine
    container_name: redis2
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.12
    expose:
      - 6379
    volumes:
      - /Users/上面2创建的目录/redis/7002/data:/data
      - /Users/上面2创建的目录/redis/7002/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis3:
    image: redis:6-alpine
    container_name: redis3
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.13
    expose:
      - 6379
    volumes:
      - /Users/上面2创建的目录/redis/7003/data:/data
      - /Users/上面2创建的目录/redis/7003/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis4:
    image: redis:6-alpine
    container_name: redis4
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.14
    expose:
      - 6379
    volumes:
      - /Users/上面2创建的目录/redis/7004/data:/data
      - /Users/上面2创建的目录/redis/7005/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis5:
    image: redis:6-alpine
    container_name: redis5
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.15
    expose:
      - 6379
    volumes:
      - /Users/上面2创建的目录/redis/7005/data:/data
      - /Users/上面2创建的目录/redis/7005/conf/redis.conf:/usr/local/etc/redis/redis.conf

  redis6:
    image: redis:6-alpine
    container_name: redis6
    command: redis-server /usr/local/etc/redis/redis.conf
    networks:
      redis-network:
        ipv4_address: 172.18.0.16
    expose:
      - 6379
    volumes:
      - /Users/上面2创建的目录/redis/7006/data:/data
      - /Users/上面2创建的目录/redis/7006/conf/redis.conf:/usr/local/etc/redis/redis.conf

你可能感兴趣的:(物联网,docker,redis,java)