使用docker搭建redis集群

创建网卡

docker network create redis --subnet 172.20.0.0/16 --gateway 172.20.0.1

通过脚本创建6个redis配置

for i in $(seq 1 6); \
do \
mkdir -p /test/redis/node-${i}/conf
touch /test/redis/node-${i}/conf/redis.conf
cat << EOF > /test/redis/node-${i}/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.20.0.1${i}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

启动6个redis

for i in $(seq 1 6); \
do \
docker run -p 637${i}:6379 -p 1637${i}:16379 --name redis-${i} \
-v /test/redis/node-${i}/data:/data \
-v /test/redis/node-${i}/conf/redis.conf:/etc/redis/redis.conf \
--network redis --ip 172.20.0.1${i} \
-d redis redis-server /etc/redis/redis.conf
done

进入任一redis容器

docker exec -it redis-1 /bin/sh

在容器里创建集群

redis-cli --cluster create 172.20.0.11:6379 172.20.0.12:6379 172.20.0.13:6379 172.20.0.14:6379 172.20.0.15:6379 172.20.0.16:6379 --cluster-replicas 1

# 客户端连接,-c 代表连接集群
redis-cli -c

 

posted @ 2020-08-04 19:29  就俗人一个  阅读(219)  评论(0编辑  收藏  举报