Docker 创建 Redis Cluster

部署环境信息:Redis 版本 4.0.14、Linux centos 7.4、Redis Cluster: 3主3从

 

1、创建redis_cluster目录和下载Redis 4.0配置文件

mkdir -p /opt/data/redis-cluster && \
cd /opt/data/redis-cluster && \
curl -sO https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf

2、Redis 4.0文件修改内容

port ${PORT}
#禁止外网访问redis 否
protected-mode no
appendonly yes
aof-use-rdb-preamble yes
cluster-enabled yes
cluster-config-file node.conf
cluster-node-timeout 5000
cluster-announce-ip ${IP}
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}

3、在服务器 10.200.11.16 中创建redis节点目录文件 

cd /opt/data/redis-cluster/ && \
export IP=10.200.11.16 && \
for port in `seq 7000 7005`; do \
  mkdir -p ./${port}/conf \
  && PORT=${port} envsubst < ./redis.conf > ./${port}/conf/redis.conf \
  && mkdir -p ./${port}/data; \
done && \
unset IP

4、创建 redis-cluster-net 网络

docker network create redis-cluster-net

5、单机启动 redis 4.0.14 700[0-5]{1} 节点

for port in `seq 7000 7005`; do \
  docker run -dit -p ${port}:${port} -p 1${port}:1${port} \
  -v /opt/data/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
  -v /opt/data/redis-cluster/${port}/data:/data \
  --restart always \
  --name redis-${port} \
  --net redis-cluster-net \
  --sysctl net.core.somaxconn=1024 \
  redis:4.0.14 redis-server /usr/local/etc/redis/redis.conf; \
done

6、使用 zvelo/redis-trib 启动集群节点

docker run --rm -it --net redis-cluster-net zvelo/redis-trib create --replicas 1 \
  $(for port in `seq 7000 7005`; do \
    echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-cluster-net").IPAddress }}' "redis-${port}")":${port} ' ' ; \
  done)

 

你可能感兴趣的:(Docker)