Redis集群

docker 搭建 redis 伪集群

参考资料

创建网段

1
2
3
docker network create redis-net
docker network ls
docker inspect redis-net

模板配置 redis-cluster.tmpl

1
2
3
4
5
6
7
8
9
port ${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.1
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes

根据模板批量创建配置文件

1
2
3
4
5
for port in `seq 6000 6005`; do \
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done

批量运行(注意修改目录)

1
2
3
4
5
6
7
for port in `seq 6000 6005`; do \
docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
-v /root/w/cluster/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /root/w/cluster/redis-cluster/${port}/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis:5.0.3 redis-server /usr/local/etc/redis/redis.conf; \
done

运行和查看

1
2
3
4
5
6
7
docker exec -it redis-6000 bash
redis-cli --cluster create 172.20.0.1:6000 172.20.0.1:6001 172.20.0.1:6002 172.20.0.1:6003 172.20.0.1:6004 172.20.0.1:6005 --cluster-replicas 1

# 连接客户端,查看主从信息
redis-cli -c -p 6000
redis> keys *
redis> info replication

开放防火墙

1
2
3
4
5
6
7
for port in `seq 7000 7005`; do \
firewall-cmd --zone=public --add-port=${port}/tcp --permanent
done

#重新载入
firewall-cmd --reload

停止和移除

1
2
3
4
5
# 暂停容器并删除容器
for port in `seq 6000 6005`; do \
docker stop redis-${port};
docker rm redis-${port};
done