基础信息:

Unbutu 20.04
docker版本: 20.10.12
公网ip:172.121.123.221
redis密码:123456
redis集群搭建后端口:7361-7366

一. 构建集群

1. 下载镜像

docker pull redis:6.2.6

2. 准备本机映射文件(为6个redis容器创建映射文件维持持久化存储与自定义redis.conf)

for port in $(seq 1 6); \
do \
mkdir -p /root/volume/redis/node-${port}/conf
touch /root/volume/redis/node-${port}/conf/redis.conf
cat << TTT > /root/volume/redis/node-${port}/conf/redis.conf
protected-mode no
port 736${port}
masterauth 123456
requirepass 123456
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.121.123.221
cluster-announce-port 736${port}
cluster-announce-bus-port 1736${port}
appendonly yes
TTT
done

3. 运行容器

for port in $(seq 1 6); \
do \
docker run --privileged=true --name redis-node${port} --restart=always \
-p 736${port}:736${port} -p 1736${port}:1736${port} \
-v /root/volume/redis/node-${port}/data:/data \
-v /root/volume/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d redis:6.2.6 redis-server /etc/redis/redis.conf
done

4. 构建集群

# 1. 进入redis-node1容器
docker exec -it 823dewq29e bash
# 2.  -a 密码
redis-cli -a 123456 --cluster create \
172.121.123.221:7361 172.121.123.221:7362 \
172.121.123.221:7363 172.121.123.221:7364 \
172.121.123.221:7365 172.121.123.221:7366 --cluster-replicas 1

5. 查看集群

# 进入容器
docker exec -it 9f39dff65503 bash
# -a 密码 -h 地址 -p 端口
redis-cli -c -a 123456 -h 172.121.123.221 -p 7361
# 查看集群
cluster nodes
Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐