命令示例
创建自定义网络
docker network create redis --subnet 172.18.0.0/16
创建redis挂载目录
for port in $(seq 1 6);
do
mkdir -p /home/apple/workspace/db/redis/node-${port}/conf
touch /home/apple/workspace/db/redis/node-${port}/conf/redis.conf
cat << EOF >/home/apple/workspace/db/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 172.18.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
启动多个容器脚本
for i in $(seq 1 6)
do
echo ${i}
docker run -p 637${i}:6379 -p 1637${i}:16379 --name redis-${i} \
-v /home/apple/workspace/db/redis/node-${i}:/data \
-v /home/apple/workspace/db/redis/node-${i}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.18.0.1${i} redis redis-server /etc/redis/redis.conf
done
在容器中配置redis集群
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-replicas 1