目录
1. 3主3从架构说明
2. 3主3从Redis集群配置
2.1关闭防火墙+启动docker后台服务
2.2 新建6个docker容器实例
2.3 进去任意一台redis容器,为6台机器构建集群关系
2.4 进去6381,查看集群状态
3. 主从容错切换迁移
3.1 数据读写存储
3.1.1 查看集群情况
3.2 容错切换迁移
3.2.1 Master6381 宕机
3.2.2 Master6381 重新启动
4. 主从扩容
4.1 新增机器 6387 6388 (run image 变成镜像)
4.2 将6387节点(空槽号)作为master节点加入原集群
4.3 第一次检查集群情况
4.4 重新分配槽号
4.5 第二次检查集群情况
4.6 为6387主节点分配6388从节点
4.7 第三次检查集群情况
5. 主从缩容 (6387和 6388 下线,退出集群)
5.1 删除6388 (从集群中移除6388从节点)
5.2 将6387(Master)的槽号清空,重新分配 (平均分给其他3个master)
5.3 第一次检查集群
5.4 将6387删除
5.5 第三次检查集群
1~2亿条数据需要存储,轻微如何设计这个存储案例?
答:单机单台100%不可能,肯定是分布式存储。redis如何落地?
一般业界有3种解决方案:
(1) 哈希取余分区
(2)一致性哈希算法分区
(3)哈希槽分区
详细解释参考之前的文档: Redis 集群(cluster)_redis集群不用哨兵-CSDN博客
1. 3主3从架构说明
2. 3主3从Redis集群配置
2.1关闭防火墙+启动docker后台服务
sudo systemctl stop firewalld
systemctl start docker
2.2 新建6个docker容器实例
docker run -d --name redis-node-1 --net host --privileged=true -v /mydocker/redis/share/redis-node-1:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true -v /mydocker/redis/share/redis-node-2:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true -v /mydocker/redis/share/redis-node-3:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true -v /mydocker/redis/share/redis-node-4:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true -v /mydocker/redis/share/redis-node-5:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true -v /mydocker/redis/share/redis-node-6:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6386
--net host : 使用宿主机deIP和端口,默认
--privileged=true :容器卷,宿主机地址:docker内部地址
--cluster-enabled yes : 开启redis集群
--appendonly yes : 开启持久化
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6618c6a22bc3 redis:7.2.4 "docker-entrypoint.s…" 7 seconds ago Up 6 seconds redis-node-6
edab1b644f47 redis:7.2.4 "docker-entrypoint.s…" 15 seconds ago Up 14 seconds redis-node-5
d4cd712628c4 redis:7.2.4 "docker-entrypoint.s…" 23 seconds ago Up 22 seconds redis-node-4
7f09330818f1 redis:7.2.4 "docker-entrypoint.s…" 32 seconds ago Up 31 seconds redis-node-3
3fab905c25d6 redis:7.2.4 "docker-entrypoint.s…" 40 seconds ago Up 39 seconds redis-node-2
8fa8521a26d3 redis:7.2.4 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes redis-node-1
[root@192 ~]#
2.3 进去任意一台redis容器,为6台机器构建集群关系
redis-cli --cluster create 192.168.217.149:6381 192.168.217.149:6382 192.168.217.149:6383 192.168.217.149:6384 192.168.217.149:6385 192.168.217.149:6386 --cluster-replicas 1
--cluster-replicas 1 : 表示为每个master创建一个slave节点
2.4 进去6381,查看集群状态
127.0.0.1:6381> 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:208
cluster_stats_messages_pong_sent:227
cluster_stats_messages_sent:435
cluster_stats_messages_ping_received:222
cluster_stats_messages_pong_received:208
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:435
total_cluster_links_buffer_limit_exceeded:0
127.0.0.1:6381> cluster nodes
826c4e0230f439eb8aca84e3fa6842e0740ee7e2 192.168.217.149:6385@16385 slave 9829684324fd8294650429a00a6ad7dc32ca5357 0 1711277651000 2 connected
2359f4b4eb3d7843ce721a4032b8931f20752cf1 192.168.217.149:6381@16381 myself,master - 0 1711277651000 1 connected 0-5460
da41e72bb921e0c1d013f82df1c481dafb89e91e 192.168.217.149:6386@16386 slave b8b0f5a32e98ef3790108a0009a2e7c8c6115114 0 1711277651740 3 connected
9cd36c05e6125a3251fe7ba937af24da95599617 192.168.217.149:6384@16384 slave 2359f4b4eb3d7843ce721a4032b8931f20752cf1 0 1711277652748 1 connected
b8b0f5a32e98ef3790108a0009a2e7c8c6115114 192.168.217.149:6383@16383 master - 0 1711277652000 3 connected 10923-16383
9829684324fd8294650429a00a6ad7dc32ca5357 192.168.217.149:6382@16382 master - 0 1711277653757 2 connected 5461-10922
127.0.0.1:6381>
3. 主从容错切换迁移
3.1 数据读写存储
root@192:/data# redis-cli -p 6381
127.0.0.1:6381> keys *
(empty array)
127.0.0.1:6381> set k1 v1
(error) MOVED 12706 192.168.217.149:6383
127.0.0.1:6381> set k2 v2
OK
添加数据时出现:(error) MOVED 12706 192.168.217.149:6383
需要 redis-cli -p 6381 -c 以集群的方式连接
root@192:/data# redis-cli -p 6381 -c
127.0.0.1:6381> set k2 v2
OK
127.0.0.1:6381> set k1 v1
-> Redirected to slot [12706] located at 192.168.217.149:6383
OK
3.1.1 查看集群情况
root@192:/data# redis-cli --cluster check 192.168.217.149:6381
192.168.217.149:6381 (2359f4b4...) -> 1 keys | 5461 slots | 1 slaves.
192.168.217.149:6383 (b8b0f5a3...) -> 1 keys | 5461 slots | 1 slaves.
192.168.217.149:6382 (98296843...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 2 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.217.149:6381)
M: 2359f4b4eb3d7843ce721a4032b8931f20752cf1 192.168.217.149:6381
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 826c4e0230f439eb8aca84e3fa6842e0740ee7e2 192.168.217.149:6385
slots: (0 slots) slave
replicates 9829684324fd8294650429a00a6ad7dc32ca5357
S: da41e72bb921e0c1d013f82df1c481dafb89e91e 192.168.217.149:6386
slots: (0 slots) slave
replicates b8b0f5a32e98ef3790108a0009a2e7c8c6115114
S: 9cd36c05e6125a3251fe7ba937af24da95599617 192.168.217.149:6384
slots: (0 slots) slave
replicates 2359f4b4eb3d7843ce721a4032b8931f20752cf1
M: b8b0f5a32e98ef3790108a0009a2e7c8c6115114 192.168.217.149:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 9829684324fd8294650429a00a6ad7dc32ca5357 192.168.217.149:6382
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@192:/data#
3.2 容错切换迁移
3.2.1 Master6381 宕机
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6618c6a22bc3 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-6
edab1b644f47 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-5
d4cd712628c4 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-4
7f09330818f1 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-3
3fab905c25d6 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-2
8fa8521a26d3 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-1
[root@192 ~]# docker stop redis-node-1
redis-node-1
[root@192 ~]# docker exec -it redis-node-2 /bin/bash
root@192:/data# redis-cli -p 6382 -c
127.0.0.1:6382> cluster nodes
b8b0f5a32e98ef3790108a0009a2e7c8c6115114 192.168.217.149:6383@16383 master - 0 1711283738000 3 connected 10923-16383
da41e72bb921e0c1d013f82df1c481dafb89e91e 192.168.217.149:6386@16386 slave b8b0f5a32e98ef3790108a0009a2e7c8c6115114 0 1711283740817 3 connected
9829684324fd8294650429a00a6ad7dc32ca5357 192.168.217.149:6382@16382 myself,master - 0 1711283740000 2 connected 5461-10922
9cd36c05e6125a3251fe7ba937af24da95599617 192.168.217.149:6384@16384 master - 0 1711283739000 7 connected 0-5460
2359f4b4eb3d7843ce721a4032b8931f20752cf1 192.168.217.149:6381@16381 master,fail - 1711283644541 1711283639000 1 disconnected
826c4e0230f439eb8aca84e3fa6842e0740ee7e2 192.168.217.149:6385@16385 slave 9829684324fd8294650429a00a6ad7dc32ca5357 0 1711283741828 2 connected
127.0.0.1:6382>
结论:master 宕机,slave会自动上位变成master
3.2.2 Master6381 重新启动
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6618c6a22bc3 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-6
edab1b644f47 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-5
d4cd712628c4 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-4
7f09330818f1 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-3
3fab905c25d6 redis:7.2.4 "docker-entrypoint.s…" 2 hours ago Up 2 hours redis-node-2
[root@192 ~]# docker start redis-node-1
redis-node-1
[root@192 ~]# docker exec -it redis-node-1 /bin/bash
root@192:/data# redis-cli -p 6381 -c
127.0.0.1:6381> cluster nodes
da41e72bb921e0c1d013f82df1c481dafb89e91e 192.168.217.149:6386@16386 slave b8b0f5a32e98ef3790108a0009a2e7c8c6115114 0 1711284415980 3 connected
826c4e0230f439eb8aca84e3fa6842e0740ee7e2 192.168.217.149:6385@16385 slave 9829684324fd8294650429a00a6ad7dc32ca5357 0 1711284414000 2 connected
9829684324fd8294650429a00a6ad7dc32ca5357 192.168.217.149:6382@16382 master - 0 1711284413000 2 connected 5461-10922
2359f4b4eb3d7843ce721a4032b8931f20752cf1 192.168.217.149:6381@16381 myself,slave 9cd36c05e6125a3251fe7ba937af24da95599617 0 1711284415000 7 connected
9cd36c05e6125a3251fe7ba937af24da95599617 192.168.217.149:6384@16384 master - 0 1711284416997 7 connected 0-5460
b8b0f5a32e98ef3790108a0009a2e7c8c6115114 192.168.217.149:6383@16383 master - 0 1711284415000 3 connected 10923-16383
结论: 即使之前宕机的Master 重新启动,加入集群之后也是slave。
4. 主从扩容
4.1 新增机器 6387 6388 (run image 变成镜像)
docker run -d --name redis-node-7 --net host --privileged=true -v /mydocker/redis/share/redis-node-7:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host --privileged=true -v /mydocker/redis/share/redis-node-8:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6388
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d55857af1d99 redis:7.2.4 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds redis-node-8
cfa322ef90bc redis:7.2.4 "docker-entrypoint.s…" 20 seconds ago Up 18 seconds redis-node-7
6618c6a22bc3 redis:7.2.4 "docker-entrypoint.s…" 23 hours ago Up 11 minutes redis-node-6
edab1b644f47 redis:7.2.4 "docker-entrypoint.s…" 23 hours ago Up 11 minutes redis-node-5
d4cd712628c4 redis:7.2.4 "docker-entrypoint.s…" 23 hours ago Up 8 minutes redis-node-4
7f09330818f1 redis:7.2.4 "docker-entrypoint.s…" 23 hours ago Up 11 minutes redis-node-3
3fab905c25d6 redis:7.2.4 "docker-entrypoint.s…" 23 hours ago Up 11 minutes redis-node-2
8fa8521a26d3 redis:7.2.4 "docker-entrypoint.s…" 23 hours ago Up 11 minutes redis-node-1
4.2 将6387节点(空槽号)作为master节点加入原集群
[root@localhost ~]# docker exec -it redis-node-7 /bin/bash
root@localhost:/data# redis-cli --cluster add-node 192.168.217.149:6387 192.168.217.149:6381
4.3 第一次检查集群情况
root@localhost:/data# redis-cli --cluster check 192.168.217.149:6381
4.4 重新分配槽号
redis-cli --cluster reshard 192.168.217.149:6381
4.5 第二次检查集群情况
root@localhost:/data# redis-cli --cluster check 192.168.217.149:6381
4.6 为6387主节点分配6388从节点
root@localhost:/data# redis-cli --cluster add-node 192.168.217.149:6388 192.168.217.149:6387 --cluster-slave --cluster-master-id c88d4e7a747ec37c1bea6563dedd87289a6fe367
redis-cli --cluster add-node ip:新slave端口 ip:新master端口 --cluster-slave --cluster-master-id slave的编号
4.7 第三次检查集群情况
redis-cli --cluster check 192.168.217.149:6381
5. 主从缩容 (6387和 6388 下线,退出集群)
5.1 删除6388 (从集群中移除6388从节点)
root@localhost:/data# redis-cli --cluster del-node 192.168.217.149:6388 7048f2115ed42bac1c2e53a6ba14ecaf30af4304
redis-cli --cluster del-node ip:从机端口 从机节点ID
5.2 将6387(Master)的槽号清空,重新分配 (平均分给其他3个master)
redis-cli --cluster reshard 192.168.217.149:6381
5.3 第一次检查集群
5.4 将6387删除
root@localhost:/data# redis-cli --cluster del-node 192.168.217.149:6387 7048f2115ed42bac1c2e53a6ba14ecaf30af4304
redis-cli --cluster del-node ip:端口 节点ID
5.5 第三次检查集群
redis-cli --cluster check 192.168.217.149:6381