Docker-Redis集群部署实战
Redis集群
启动6个容器 3主3从
# 新建redis网络
docker network create redis --subnet 172.38.0.0/16
# 通过脚本创建6个redis配置
for port in $(seq 1 6);
do
mkdir -p /Users/docker-study/mydata/redis/node-${port}/conf
touch /Users/docker-study/mydata/redis/node-${port}/conf/redis.conf
cat <<EOF >/Users/docker-study/mydata/redis/node-${port}/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.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
# 启动6个redis容器
for port in $(seq 1 6); \
do \
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /Users/docker-study/mydata/redis/node-${port}/data:/data \
-v /Users/docker-study/mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
done
# redis容器没有bash命令
docker exec -it redis-1 /bin/bash
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown
docker exec -it redis-1 /bin/sh
# 设置集群
redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0
.16:6379 --cluster-replicas 1
# 测试
redis-cli -c
cluster info
cluster nodes
发现是主服务器172.38.0.13保存的值。停止reids-3服务,然后获取值
docker stop redis-3
发现获取值是通过14机器,13机器 master fail故障转移,14机器自动选举为master
问题记录
docker: Error response from daemon: error while creating mount source path '/Users/docker-study/mydata/redis/node-6/data': mkdir /Users/docker-study/mydata/redis/node-6/data: operation not permitted.
chmod -R 777 redis