文章目录
- 1. 创建redis网卡
- 2. 创建redis配置
- 3. 启动redis集群
- 4. 创建集群
- 5. 记录与调试
- 6. 故障转移
1. 创建redis网卡
# 删除容器
docker rm -f ${docker ps -aq}# 创建redis网卡
docker network create redis --subnet 172.38.0.0/16# 删除redis网卡
docker network rm redis# 查看docker网络
docker network ls# 查看具体信息
docker network inspect redis
2. 创建redis配置
# 通过脚本创建6个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /app/mydata/redis/node-${port}/conf
touch /app/mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/app/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
3. 启动redis集群
- redis-1
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /app/mydata/redis/node-1/data:/data \
-v /app/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-2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v /app/mydata/redis/node-2/data:/data \
-v /app/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
- redis-3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v /app/mydata/redis/node-3/data:/data \
-v /app/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
- redis-4
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v /app/mydata/redis/node-4/data:/data \
-v /app/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
- redis-5
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v /app/mydata/redis/node-5/data:/data \
-v /app/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
- redis-6
docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v /app/mydata/redis/node-6/data:/data \
-v /app/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
4. 创建集群
进入任意一个redis容器,这里演示redis-1
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
5. 记录与调试
[root@localhost node-1]# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof nodes.conf
/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-rep
licas 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: 9487301023ddb4d28fc0ae6fdb9f1f5a165a1532 172.38.0.11:6379slots:[0-5460] (5461 slots) master
M: 7ae515caa00ad54517756fbb6f8a5bfab271a787 172.38.0.12:6379slots:[5461-10922] (5462 slots) master
M: b9e57a5d66e08e163bc41d71ef28070b4f20ce02 172.38.0.13:6379slots:[10923-16383] (5461 slots) master
S: a23ba18c346a75e8eb106bcab760fdfb6789a256 172.38.0.14:6379replicates b9e57a5d66e08e163bc41d71ef28070b4f20ce02
S: abac69c275d92b85a1f55bd9455dd7a123caa6d5 172.38.0.15:6379replicates 9487301023ddb4d28fc0ae6fdb9f1f5a165a1532
S: 674ae1ba47f19c68d79e90986d432914d436010e 172.38.0.16:6379replicates 7ae515caa00ad54517756fbb6f8a5bfab271a787
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: 9487301023ddb4d28fc0ae6fdb9f1f5a165a1532 172.38.0.11:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
M: b9e57a5d66e08e163bc41d71ef28070b4f20ce02 172.38.0.13:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
S: abac69c275d92b85a1f55bd9455dd7a123caa6d5 172.38.0.15:6379slots: (0 slots) slavereplicates 9487301023ddb4d28fc0ae6fdb9f1f5a165a1532
S: 674ae1ba47f19c68d79e90986d432914d436010e 172.38.0.16:6379slots: (0 slots) slavereplicates 7ae515caa00ad54517756fbb6f8a5bfab271a787
M: 7ae515caa00ad54517756fbb6f8a5bfab271a787 172.38.0.12:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
S: a23ba18c346a75e8eb106bcab760fdfb6789a256 172.38.0.14:6379slots: (0 slots) slavereplicates b9e57a5d66e08e163bc41d71ef28070b4f20ce02
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
/data # # 客户端测试
/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:289
cluster_stats_messages_pong_sent:294
cluster_stats_messages_sent:583
cluster_stats_messages_ping_received:289
cluster_stats_messages_pong_received:289
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:583
127.0.0.1:6379> # 查看集群节点信息
127.0.0.1:6379> cluster nodes
b9e57a5d66e08e163bc41d71ef28070b4f20ce02 172.38.0.13:6379@16379 master - 0 1643181809583 3 connected 10923-16383
abac69c275d92b85a1f55bd9455dd7a123caa6d5 172.38.0.15:6379@16379 slave 9487301023ddb4d28fc0ae6fdb9f1f5a165a1532 0 1643181809583 5 connected
674ae1ba47f19c68d79e90986d432914d436010e 172.38.0.16:6379@16379 slave 7ae515caa00ad54517756fbb6f8a5bfab271a787 0 1643181808569 6 connected
7ae515caa00ad54517756fbb6f8a5bfab271a787 172.38.0.12:6379@16379 master - 0 1643181809000 2 connected 5461-10922
a23ba18c346a75e8eb106bcab760fdfb6789a256 172.38.0.14:6379@16379 slave b9e57a5d66e08e163bc41d71ef28070b4f20ce02 0 1643181809583 4 connected
9487301023ddb4d28fc0ae6fdb9f1f5a165a1532 172.38.0.11:6379@16379 myself,master - 0 1643181807000 1 connected 0-5460
127.0.0.1:6379> # 设置值
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
172.38.0.13:6379>
6. 故障转移
再开一个窗口
思路:停止172.38.0.13对应的master节点,然后,再获取a的值,
-
停止a key存储主节点的docker容器
略 -
获取a的值
get a