前提 创建专用redis的网络
创建网络指令:
docker network create redis --subnet 172.38.0.0/16
1.创建本地文件和目录 这里的目录在/mydata/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
[root@docker-jjy-1 ~]$ tree /mydata/redis/
/mydata/redis/
├── node-1
│ └── conf
│ └── redis.conf
├── node-2
│ └── conf
│ └── redis.conf
├── node-3
│ └── conf
│ └── redis.conf
├── node-4
│ └── conf
│ └── redis.conf
├── node-5
│ └── conf
│ └── redis.conf
└── node-6
└── conf
└── redis.conf
2.接下来是启动redis(6台redis组成三主三从的结构)
docker run -p 6371:6379 -p 16671: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
docker run -p 6372:6379 -p 16672:16379 --name redis-2 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
docker run -p 6373:6379 -p 16673:16379 --name redis-3 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
docker run -p 6374:6379 -p 16674:16379 --name redis-4 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
docker run -p 6375:6379 -p 16675:16379 --name redis-5 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
docker run -p 6376:6379 -p 16676:16379 --name redis-6 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
3.进入redis其中一个docker指令行中
docker exec -it redis-1 /bin/sh
4.执行redis集群的操作(创建该项集群)
/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 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
>>> Performing 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: 20695d373df1b3724581bb5007846bf8be4b124c 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 238dc9cb0d88c037c95b637010b728677dd2989d 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: af5c353fc5dfb6b26e39ebc04d1b572135640dde 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 08a08328164fd3d132b6dab0bbf27c00cc58f5a0 172.38.0.14:6379
replicates af5c353fc5dfb6b26e39ebc04d1b572135640dde
S: 797632194695a3f679ce07e0026a166912868e02 172.38.0.15:6379
replicates 20695d373df1b3724581bb5007846bf8be4b124c
S: 2d673e1c3569f8d0c7a77832bd8705db799aa5c4 172.38.0.16:6379
replicates 238dc9cb0d88c037c95b637010b728677dd2989d
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: 20695d373df1b3724581bb5007846bf8be4b124c 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 08a08328164fd3d132b6dab0bbf27c00cc58f5a0 172.38.0.14:6379
slots: (0 slots) slave
replicates af5c353fc5dfb6b26e39ebc04d1b572135640dde
M: af5c353fc5dfb6b26e39ebc04d1b572135640dde 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 797632194695a3f679ce07e0026a166912868e02 172.38.0.15:6379
slots: (0 slots) slave
replicates 20695d373df1b3724581bb5007846bf8be4b124c
M: 238dc9cb0d88c037c95b637010b728677dd2989d 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 2d673e1c3569f8d0c7a77832bd8705db799aa5c4 172.38.0.16:6379
slots: (0 slots) slave
replicates 238dc9cb0d88c037c95b637010b728677dd2989d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
/data #
5.查看Redis集群状态
/data # redis-cli -c
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:387
cluster_stats_messages_pong_sent:400
cluster_stats_messages_sent:787
cluster_stats_messages_ping_received:395
cluster_stats_messages_pong_received:387
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:787
127.0.0.1:6379> cluster nodes
08a08328164fd3d132b6dab0bbf27c00cc58f5a0 172.38.0.14:6379@16379 slave af5c353fc5dfb6b26e39ebc04d1b572135640dde 0 1602865753588 4 connected
20695d373df1b3724581bb5007846bf8be4b124c 172.38.0.11:6379@16379 myself,master - 0 1602865754000 1 connected 0-5460
af5c353fc5dfb6b26e39ebc04d1b572135640dde 172.38.0.13:6379@16379 master - 0 1602865753993 3 connected 10923-16383
797632194695a3f679ce07e0026a166912868e02 172.38.0.15:6379@16379 slave 20695d373df1b3724581bb5007846bf8be4b124c 0 1602865754596 5 connected
238dc9cb0d88c037c95b637010b728677dd2989d 172.38.0.12:6379@16379 master - 0 1602865754000 2 connected 5461-10922
2d673e1c3569f8d0c7a77832bd8705db799aa5c4 172.38.0.16:6379@16379 slave 238dc9cb0d88c037c95b637010b728677dd2989d 0 1602865753000 6 connected
127.0.0.1:6379>