# bind 127.0.0.1 protected-mode no port ${PORT} daemonize no dir /data/redis appendonly yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 15000
FROM ruby:2.5.5-slim MAINTAINER xinchen<xcmelody@gmail.com> RUN gem install redis RUN mkdir /redis WORKDIR /redis # redis-trib.rb 可在https://github.com/antirez/redis中找到 # 此处已经 wget https://raw.githubusercontent.com/antirez/redis/4.0/src/redis-trib.rb ADD ./redis-trib.rb /redis/redis-trib.rb
构建镜像
1
docker build -t redis-trib .
3.创建docker内部网络
1 2
# docker network ls 可查看 docker network create redis-cluster-net
4.创建 master 和 slave 文件夹并生成配置文件
make_master_slave.sh
1 2 3 4 5 6 7 8 9
# 创建 master 和 slave 文件夹 for port in `seq 7000 7005`; do ms="master" if [ $port -ge 7003 ]; then ms="slave" fi mkdir -p ./$ms/$port/ && mkdir -p ./$ms/$port/data \ && PORT=$port envsubst < ./redis-cluster.tmpl > ./$ms/$port/redis.conf; done
5.运行docker redis 的 master 和 slave 实例
run_master_slave.sh
1 2 3 4 5 6 7 8 9 10 11 12
# 运行docker redis 的 master 和 slave 实例 for port in `seq 7000 7005`; do ms="master" if [ $port -ge 7003 ]; then ms="slave" fi docker run -d -p $port:$port -p 1$port:1$port \ -v $PWD/$ms/$port/redis.conf:/data/redis.conf \ -v $PWD/$ms/$port/data:/data/redis \ --restart always --name redis-$ms-$port --net redis-cluster-net \ redis redis-server /data/redis.conf; done
6.组装masters : slaves 节点参数
组装masters : slaves 节点参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 组装masters : slaves 节点参数 matches="" for port in `seq 7000 7005`; do ms="master" if [ $port -ge 7003 ]; then ms="slave" fi matches=$matches$(docker inspect --format '{{(index .NetworkSettings.Networks "redis-cluster-net").IPAddress}}'"redis-$ms-${port}"):${port}" "; done