先创建一个网卡
docker network create redis --subnet 172.38.0.0/16
通过脚本创建六个redis配置
for port in $(seq 1 6);
do
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/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
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port}
-v /mydata/redis/node-${port}/data:/data
-v /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 (通过脚本启动)
docker run -p 6371:6379 -p 16371:16379 --name redis-1
-v /mydata/redis/node-1/data:/data
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf (一个一个启动)
创建集群
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
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 1
72.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: 43ecac57d81cd97030dc1fd35728e5387faa758f 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: aeda148ebf832e2ab7684595813c7858261b6f8e 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 91ce5c16a7b2dc724145ae5bc0137c4b8b1264b9 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 322880e37e9f401cc705729aaec8789e5919808d 172.38.0.14:6379
replicates 91ce5c16a7b2dc724145ae5bc0137c4b8b1264b9
S: bff4df376b3d8af9643ccced17084dcfdf8f9c75 172.38.0.15:6379
replicates 43ecac57d81cd97030dc1fd35728e5387faa758f
S: 6bd617ce197a910ff88a996fc3f3139bb5661967 172.38.0.16:6379
replicates aeda148ebf832e2ab7684595813c7858261b6f8e
Can I set the above configuration? (type 'yes' to accept): yes
Nodes configuration updated
Assign a different config epoch to each node
Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
Performing Cluster Check (using node 172.38.0.11:6379)
M: 43ecac57d81cd97030dc1fd35728e5387faa758f 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 6bd617ce197a910ff88a996fc3f3139bb5661967 172.38.0.16:6379
slots: (0 slots) slave
replicates aeda148ebf832e2ab7684595813c7858261b6f8e
S: 322880e37e9f401cc705729aaec8789e5919808d 172.38.0.14:6379
slots: (0 slots) slave
replicates 91ce5c16a7b2dc724145ae5bc0137c4b8b1264b9
M: aeda148ebf832e2ab7684595813c7858261b6f8e 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 91ce5c16a7b2dc724145ae5bc0137c4b8b1264b9 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: bff4df376b3d8af9643ccced17084dcfdf8f9c75 172.38.0.15:6379
slots: (0 slots) slave
replicates 43ecac57d81cd97030dc1fd35728e5387faa758f
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
查看集群信息 cluster info
查看节点 cluster nodes
测试主从机制
随便set一个值,然后把对应主机停了,看从机是否可用
可见在14从机上
查看集群节点,13故障14变成master
至此docker搭建集群完成