- 3主3从的方式
- 使用命令或手动一个个创建
for port in $(seq 7001 7006); \
do \
mkdir -p /software/docker/redis/node-${port}/conf
touch /software/docker/redis/node-${port}/conf/redis.conf
cat << EOF >/software/docker/redis/node-${port}/conf/redis.conf port ${port}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 10.211.55.6
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
appendonly yes
EOF
docker run -p ${port}:${port} -p 1${port}:1${port} --name redis-${port} \ -v /software/docker/redis/node-${port}/data:/data \
-v /software/docker/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \ -d redis:latest redis-server /etc/redis/redis.conf; \
done
- 建立redis集群
docker exec -it redis-7001 bash
redis-cli --cluster create 10.211.55.6:7001 10.211.55.6:7002 10.211.55.6:7003 10.211.55.6:7004 10.211.55.6:7005 10.211.55.6:7006 --cluster-replicas 1
Snipaste_2024-03-10_09-05-39.jpg
- 测试
#随便进入一个redis容器
docker exec -it redis-7001 /bin/bash
#使用redis-cli cluster方式连接,然后使用get set测试就可以了
redis-cli -c -h 10.211.55.6 -p 7001
#获取集群信息
cluster info
#获取群节点信息
cluster nodes
#主节点宕机,slave会自动提升为master,主节点恢复后会变成slave