四条命令利用docker创建redis集群
先创建自己的网卡
docker network create redis --subnet 172.38.1.0/27
23表示的是子网掩码中1的位数,即子网掩码为:255.255.255.11100000 =255.255.255.224
能够分配 2^5-2=30个ip,我们这里使用了搭建6台redis,只会用到6个ip,已经足够了
创建配置文件
for port in {1..6};
do
mkdir -p /mydata/redis/cluster/node-${port}/conf
touch /mydata/redis/cluster/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/cluster/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.1.1${port}
cluster-announce-port 6379
appendonly yes
EOF
done
启动这6个redis
# 这里使用的是 redis6.2.6 的版本
for i in {1..6};
do
docker run -p 679${i}:6379 --name redis-${i} -v /mydata/redis/cluster/node-${i}/data:/data -v /mydata/redis/cluster/node-${i}/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.1.1${i} redis:6.2.6 redis-server /etc/redis/redis.conf;
done
创建集群
docker exec -it redis-1 redis-cli --cluster create 172.38.1.11:6379 172.38.1.12:6379 172.38.1.13:6379 172.38.1.14:6379 172.38.1.15:6379 172.38.1.16:6379 --cluster-replicas 1