docker-compose测试搭建redis集群
#此处使用了自定义的网络,需要新建一个docker网络myredis并指定网关(192.165.0.1)和子网(192.165.0.0/16)
docker network create --driver=bridge --gateway=192.165.0.1 --subnet=192.165.0.0/16 myredis
#删除此文件夹
rm -rf /mycontainerdata/
#创建6个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /mycontainerdata/redis/node-${port}/conf
touch /mycontainerdata/redis/node-${port}/conf/redis.conf
cat << EOF >/mycontainerdata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.165.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
#移除所有容器
docker rm -f $(docker ps -aq)
#运行redis容器
for port in $(seq 1 6); \
do \
docker run -dit -p 637${port}:6379 -p 1637${port}:16379 --name=redis-${port} \
-v /mycontainerdata/redis/node-${port}/data:/data \
-v /mycontainerdata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
--net myredis --ip 192.165.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf; \
done
#排查配置文件是否正确
docker run -it -p 6371:6379 -p 16371:16379 --name=redis-1 \
-v /mycontainerdata/redis/node-1/data:/data \
-v /mycontainerdata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
--net myredis --ip 192.165.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
#进入redis-1容器
docker exec -it redis-1 /bin/sh
#创建集群
redis-cli --cluster create 192.165.0.11:6379 192.165.0.12:6379 192.165.0.13:6379 192.165.0.14:6379 192.165.0.15:6379 192.165.0.16:6379 --cluster-replicas 1
#docker-compose.yml
version: "3"
services:
redis-1:
image: redis:5.0.9-alpine3.11
container_name: redis-1
networks:
myredis:
ipv4_address: 192.165.0.11
ports:
- 6371:6379
- 16371:16379
volumes:
- /mycontainerdata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf
- /mycontainerdata/redis/node-1/data:/data
command: /bin/sh -c "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
redis-2:
image: redis:5.0.9-alpine3.11
container_name: redis-2
networks:
myredis:
ipv4_address: 192.165.0.12
ports:
- 6372:6379
- 16372:16379
volumes:
- /mycontainerdata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf
- /mycontainerdata/redis/node-2/data:/data
command: /bin/sh -c "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
redis-3:
image: redis:5.0.9-alpine3.11
container_name: redis-3
networks:
myredis:
ipv4_address: 192.165.0.13
ports:
- 6373:6379
- 16373:16379
volumes:
- /mycontainerdata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf
- /mycontainerdata/redis/node-3/data:/data
command: /bin/sh -c "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
redis-4:
image: redis:5.0.9-alpine3.11
container_name: redis-4
networks:
myredis:
ipv4_address: 192.165.0.14
ports:
- 6374:6379
- 16374:16379
volumes:
- /mycontainerdata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf
- /mycontainerdata/redis/node-4/data:/data
command: /bin/sh -c "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
redis-5:
image: redis:5.0.9-alpine3.11
container_name: redis-5
networks:
myredis:
ipv4_address: 192.165.0.15
ports:
- 6375:6379
- 16375:16379
volumes:
- /mycontainerdata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf
- /mycontainerdata/redis/node-5/data:/data
command: /bin/sh -c "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
redis-6:
image: redis:5.0.9-alpine3.11
container_name: redis-6
networks:
myredis:
ipv4_address: 192.165.0.16
ports:
- 6376:6379
- 16376:16379
volumes:
- /mycontainerdata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf
- /mycontainerdata/redis/node-6/data:/data
command: /bin/sh -c "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
networks:
myredis:
external:
name: myredis
redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
networks:
myredis:
external:
name: myredis