cluster集群
- cluster翻译就是集群,所以cluster集群也叫做redis集群
- 相比于哨兵模式,cluster集群能支持扩容,并且无需额外的节点来监控状态,所以使用这种模式集群的系统会用的更多些
- redis cluster采用的是去中心化网络拓扑架构,所有节点既是数据储存节点也是控制节点
- 引入槽(slot),通过crc+hashslot哈希算法支持多个主节点(分片),每个主节点分别负责储存一部分数据,这样理论上支持无限主节点的水平扩容以便支持海量吞吐量
- 内置类似哨兵(sentinal)高可用机制,能够实现自动故障转移,保证每个主节点(分片)的高可用
哈希槽
通过某cluster集群是由六台redis服务器组成的,那么每台服务器上也会被平均分配一定数量的的哈希槽,此外,cluster集群里也支持主从复制,即分配到一定数量哈希槽的redis服务器也可以携带一个或多个从节点。
cluster集群
搭建cluster集群
这里搭建三主三从的cluster集群
新建自定义目录并且加777权限
mkdir -p /root/redis/clusterchmod -R 777 /root/redis/cluster
创建三主三从的配置文件
配置文件位置放到上面创建的目录下
1.cluster-m1.conf
port 6379
dir /redisConfig
logfile cluster-m1.log
cluster-enabled yes
cluster-config-file nodes-6379.conf
解释:
第一行:端口
第二、三行:指定了该节点的日志目录和文件名
第四行:开启cluster集群模式
第五行:自动生成cluster相关配置文件
2.cluster-m2.conf
port 6380
dir /redisConfig
logfile cluster-m2.log
cluster-enabled yes
cluster-config-file nodes-6380.conf
3.cluster-m3.conf
port 6381
dir /redisConfig
logfile cluster-m3.log
cluster-enabled yes
cluster-config-file nodes-6381.conf
4.cluster-s1.conf
port 16379
dir /redisConfig
logfile cluster-s1.log
cluster-enabled yes
cluster-config-file nodes-16379.conf
5.cluster-s2.conf
port 16380
dir /redisConfig
logfile cluster-s2.log
cluster-enabled yes
cluster-config-file nodes-16380.conf
6.cluster-s3.conf
port 16381
dir /redisConfig
logfile cluster-s3.log
cluster-enabled yes
cluster-config-file nodes-16381.conf
启动6个节点的容器
docker run -itd --name redis-m1 -v /root/redis/cluster:/redisConfig:z -p 6379:6379 redis:latest redis-server /redisConfig/cluster-m1.conf
由于通过redis-server启动redis服务器时传入了cluster-m1.conf的配置文件,因此redis-m1自动加入了该集群,此时集群中只有这一个节点
继续将其他节点启动加入集群
docker run -itd --name redis-m2 -v /root/redis/cluster:/redisConfig:z -p 6380:6380 redis:latest redis-server /redisConfig/cluster-m2.confdocker run -itd --name redis-m3 -v /root/redis/cluster:/redisConfig:z -p 6381:6381 redis:latest redis-server /redisConfig/cluster-m3.confdocker run -itd --name redis-s1 -v /root/redis/cluster:/redisConfig:z -p 16379:16379 redis:latest redis-server /redisConfig/cluster-s1.confdocker run -itd --name redis-s2 -v /root/redis/cluster:/redisConfig:z -p 16380:16380 redis:latest redis-server /redisConfig/cluster-s2.confdocker run -itd --name redis-s3 -v /root/redis/cluster:/redisConfig:z -p 16381:16381 redis:latest redis-server /redisConfig/cluster-s3.conf
全部启动,但还未配置主从关系
进入其中一个节点查看生成的配置文件
[root@localhost cluster]# docker exec -it redis-m1 /bin/bash
root@9d29016966fe:/data# cd /redisConfig/
root@9d29016966fe:/redisConfig# cat nodes-6379.conf
863340d68ac280317d410f2c0fc8f863fb9a74f4 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
从第一行可以看出主节点只连接到自身
确定每个节点的ip
docker inspect redis-m1|grep IPAddress
节点名称 | ip地址 | 端口 |
---|---|---|
redis-m1 | 172.17.0.2 | 6379 |
redis-m2 | 172.17.0.3 | 6380 |
redis-m3 | 172.17.0.4 | 6381 |
redis-s1 | 172.17.0.5 | 16379 |
redis-s2 | 172.17.0.6 | 16380 |
redis-s3 | 172.17.0.7 | 16381 |
进入redis-m1容器中,执行命令
docker exec -it redis-m1 /bin/bash
用以下命令连接节点
redis-cli -p 6379 cluster meet 172.17.0.3 6380
redis-cli -p 6379 cluster meet 172.17.0.4 6381
redis-cli -p 6379 cluster meet 172.17.0.5 16379
redis-cli -p 6379 cluster meet 172.17.0.6 16380
redis-cli -p 6379 cluster meet 172.17.0.7 16381
使用cluster info查看
root@9d29016966fe:/redisConfig# redis-cli
127.0.0.1:6379> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:43
cluster_stats_messages_pong_sent:55
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:103
cluster_stats_messages_ping_received:55
cluster_stats_messages_pong_received:48
cluster_stats_messages_received:103
因为还没有分配槽,所以状态时fail
给主节点分配槽
命令
redis-cli -h 172.17.0.2 -p 6379 cluster addslots n
使用脚本批量执行
vi /root/redis/cluster/setHashSlots.sh
#!/bin/bash
for i in $(seq 0 5460)
do
/usr/local/bin/redis-cli -h 172.17.0.2 -p 6379 cluster addslots $i
done
进入对应容器节点执行
bash /redisConfig/setHashSlots.sh
然后redis-m2分配
#!/bin/bash
for i in $(seq 5461 10922)
do
/usr/local/bin/redis-cli -h 172.17.0.3 -p 6380 cluster addslots $i
done
redis-m3分配
#!/bin/bash
for i in $(seq 10923 16383)
do
/usr/local/bin/redis-cli -h 172.17.0.4 -p 6381 cluster addslots $i
done
以此修改sh文件执行即可
等三个主节点分配完槽之后,查看redis-m1状态
root@9d29016966fe:/data# redis-cli
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:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:796
cluster_stats_messages_pong_sent:788
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:1589
cluster_stats_messages_ping_received:788
cluster_stats_messages_pong_received:801
cluster_stats_messages_received:1589
获取nodes的id,设置主从关系
在redis-m1上执行
127.0.0.1:6379> cluster nodes
788bb0674543fda50fb9bfabbc4dd598c041e4f6 172.17.0.7:16381@26381 master - 0 1726305277000 5 connected
8211c4e11468743394dc102778a8985708d88925 172.17.0.4:6381@16381 master - 0 1726305278355 2 connected 10923-16383
3c429e47ee9e8cb79457cc9f1556ca5df2a5df92 172.17.0.3:6380@16380 master - 0 1726305276000 4 connected 5461-10922
863340d68ac280317d410f2c0fc8f863fb9a74f4 172.17.0.2:6379@16379 myself,master - 0 1726305276000 1 connected 0-5460
b2adb4177ef4c28715b9aca9b965668a0c6407d1 172.17.0.6:16380@26380 master - 0 1726305278000 0 connected
997c06402f48db3c3925c8d820af5f1695347c36 172.17.0.5:16379@26379 master - 0 1726305279380 3 connected
设置主从关系命令,到从节点中执行
cluster replicate 主节点nodeid
进入redis-s1
docker exec -it redis-s1 /bin/bashredis-cli -p 16379#从节点一对应主节点一的nodeid
cluster replicate 863340d68ac280317d410f2c0fc8f863fb9a74f4
依次将其他从节点也执行
设置完进入主节点一看主从关系
[root@localhost cluster]# docker exec -it redis-m1 /bin/bash
root@9d29016966fe:/data# redis-cli
127.0.0.1:6379> cluster nodes
788bb0674543fda50fb9bfabbc4dd598c041e4f6 172.17.0.7:16381@26381 slave 8211c4e11468743394dc102778a8985708d88925 0 1726305959000 2 connected
8211c4e11468743394dc102778a8985708d88925 172.17.0.4:6381@16381 master - 0 1726305957000 2 connected 10923-16383
3c429e47ee9e8cb79457cc9f1556ca5df2a5df92 172.17.0.3:6380@16380 master - 0 1726305956000 4 connected 5461-10922
863340d68ac280317d410f2c0fc8f863fb9a74f4 172.17.0.2:6379@16379 myself,master - 0 1726305958000 1 connected 0-5460
b2adb4177ef4c28715b9aca9b965668a0c6407d1 172.17.0.6:16380@26380 slave 3c429e47ee9e8cb79457cc9f1556ca5df2a5df92 0 1726305958000 4 connected
997c06402f48db3c3925c8d820af5f1695347c36 172.17.0.5:16379@26379 slave 863340d68ac280317d410f2c0fc8f863fb9a74f4 0 1726305959588 1 connected
在cluster集群中读写数据
root@9d29016966fe:/data# redis-cli -c
127.0.0.1:6379> set name yohoo
-> Redirected to slot [5798] located at 172.17.0.3:6380
OK
解释:
-c是为了避免因为槽导致的设置数据失败
在到其他节点去读
root@2564e5d44d2e:/data# redis-cli -p 16379 -c
127.0.0.1:16379> get name
-> Redirected to slot [5798] located at 172.17.0.3:6380
"yohoo"