欢迎光临我的博客查看最新文章: https://river106.cn
Redis从3.0开始支持Redis Cluster集群部署,在3.0之前使用哨兵模式来实现Redis集群(利用Sentinel来监控master节点的状态,如果master节点异常,则将其中一台slave切换为master),性能不如Redis Cluster。
本篇博文以Redis 7.0.5为例来搭建Redis Cluster高可用集群。
Redis官网:https://redis.io
Redis下载地址:https://redis.io/download/
Redis集群是一个由多个主从节点群组成的分布式服务器群,它具有复制、高可用和分片特性。需要将每个节点设置成集群模式,这种集群模式没有中心节点,可水平扩展,可以线性扩展到上万个节点(官方推荐不超过1000个节点)。
首先需要安装Redis,请参考文章 CentOS7下Redis7安装。
Redis集群需要至少三个master节点,这里搭建三个master节点,并且给每个master再搭建一个slave节点,总共6个redis节点,需要用三台机器部署6个redis实例,每台机器一主一从,不过这里为了方便再简化些,使用一台机器部署6个redis实例;
搭建集群的步骤如下:
1、在第一台机器的/opt/software/redis下创建文件夹redis‐cluster,然后在其下面分别创建6个文件夾7001、7002、7003、7004、7005、7006;
mkdir ‐p /opt/software/redis/redis‐cluster
cd /opt/software/redis/redis‐cluster
mkdir 7001 7002 7003 7004 7005 7006
2、把之前的redis.conf配置文件copy到7001下,修改如下内容:
daemonize yes # 后台启动
port 7001 # 分别对每个机器的端口号进行设置
pidfile /var/run/redis_7001.pid # 把pid进程号写入pidfile配置的文件
dir /opt/software/redis/redis‐cluster/7001/ # 指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据
cluster‐enabled yes # 启动集群模式
cluster‐config‐file nodes‐7001.conf # 集群节点信息文件,这里800x最好和port对应上
cluster‐node‐timeout 10000
# bind 127.0.0.1 # bind绑定的是自己机器网卡的ip,内网一般可以不配置bind,注释掉即可
protected‐mode no #关闭保护模式
appendonly yes # 开启AOF
# 如果要设置密码需要增加如下配置:
requirepass 123456 # 设置redis访问密码
masterauth 123456 # 设置集群节点间访问密码,跟上面一致
再分别将redis.conf配置文件copy至其他文件夹(7002、7003、7004、7005、7006),调整如下配置为对应节点:
port 700x
pidfile /var/run/redis_700x.pid
dir /opt/software/redis/redis‐cluster/700x/
cluster‐config‐file nodes‐700x.conf
src/redis-server /opt/software/redis/redis-cluster/7001/redis.conf
src/redis-server /opt/software/redis/redis-cluster/7002/redis.conf
src/redis-server /opt/software/redis/redis-cluster/7003/redis.conf
src/redis-server /opt/software/redis/redis-cluster/7004/redis.conf
src/redis-server /opt/software/redis/redis-cluster/7005/redis.conf
src/redis-server /opt/software/redis/redis-cluster/7006/redis.conf
使用 ps -ef | grep redis 查看验证,如下表明各节点启动成功:
redis5以前的版本集群是依靠ruby脚本redis‐trib.rb实现,现在redis7可以使用redis-cli来创建集群,创建集群前先关闭防火墙:
systemctl stop firewalld # 临时关闭防火墙
systemctl disable firewalld # 禁止开机启动
创建集群:
src/redis-cli -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
–cluster-replicas 后面的 1 代表每个创建的主服务器节点创建一个从服务器节点,如果没有从,可以设置为0
-a参数 访问服务端密码,如果之前配置了访问密码,这里需要加上-a参数
[root@iZuf6ib0sh7w9e6el7zwlcZ redis-7.0.5]# src/redis-cli -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: d7fdb82e01378603f58ec808b394417d1fec8f0e 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: 6d50abfd77cb7d7bda6ee2b209e7d8e6029310e7 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
M: 07fbd029398efac634cd564ad442d115af55babe 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
S: afc23d25b05f5c7c41fa4008cc715e600bed1a9c 127.0.0.1:7004
replicates 07fbd029398efac634cd564ad442d115af55babe
S: 02e43e0dea2fec5c2742125d46ae3b24070a1478 127.0.0.1:7005
replicates d7fdb82e01378603f58ec808b394417d1fec8f0e
S: e846487495c506c756b2a604eff2ed0118db44fe 127.0.0.1:7006
replicates 6d50abfd77cb7d7bda6ee2b209e7d8e6029310e7
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 127.0.0.1:7001)
M: d7fdb82e01378603f58ec808b394417d1fec8f0e 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 02e43e0dea2fec5c2742125d46ae3b24070a1478 127.0.0.1:7005
slots: (0 slots) slave
replicates d7fdb82e01378603f58ec808b394417d1fec8f0e
S: afc23d25b05f5c7c41fa4008cc715e600bed1a9c 127.0.0.1:7004
slots: (0 slots) slave
replicates 07fbd029398efac634cd564ad442d115af55babe
M: 07fbd029398efac634cd564ad442d115af55babe 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: e846487495c506c756b2a604eff2ed0118db44fe 127.0.0.1:7006
slots: (0 slots) slave
replicates 6d50abfd77cb7d7bda6ee2b209e7d8e6029310e7
M: 6d50abfd77cb7d7bda6ee2b209e7d8e6029310e7 127.0.0.1:7002
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.
连接任一客户端验证:
src/redis-cli -a 123456 -c -p 7001
‐a访问服务端密码,‐c表示集群模式,-p表示连接端口
进入7001客户端
查看集群信息:
cluster info
127.0.0.1:7001> 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:504
cluster_stats_messages_pong_sent:508
cluster_stats_messages_sent:1012
cluster_stats_messages_ping_received:503
cluster_stats_messages_pong_received:504
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1012
total_cluster_links_buffer_limit_exceeded:0
查看节点列表:
cluster nodes
127.0.0.1:7001> cluster nodes
02e43e0dea2fec5c2742125d46ae3b24070a1478 127.0.0.1:7005@17005 slave d7fdb82e01378603f58ec808b394417d1fec8f0e 0 1670132260261 1 connected
afc23d25b05f5c7c41fa4008cc715e600bed1a9c 127.0.0.1:7004@17004 slave 07fbd029398efac634cd564ad442d115af55babe 0 1670132259256 3 connected
07fbd029398efac634cd564ad442d115af55babe 127.0.0.1:7003@17003 master - 0 1670132258252 3 connected 10923-16383
e846487495c506c756b2a604eff2ed0118db44fe 127.0.0.1:7006@17006 slave 6d50abfd77cb7d7bda6ee2b209e7d8e6029310e7 0 1670132261264 2 connected
6d50abfd77cb7d7bda6ee2b209e7d8e6029310e7 127.0.0.1:7002@17002 master - 0 1670132258000 2 connected 5461-10922
d7fdb82e01378603f58ec808b394417d1fec8f0e 127.0.0.1:7001@17001 myself,master - 0 1670132260000 1 connected 0-5460
Redis Cluster将所有数据划分为16384个slots(槽位),每个节点负责其中一部分槽位,槽位的信息存储于每个节点中。
可以看出:
7001主节点(当前节点)分配了 0-5460 槽位,
7002主节点分配了 5461-10922 槽位,
7003主节点分配了 10923-16383 槽位。
集群操作:
集群默认会对 key 值使用 crc16 算法进行 hash 得到槽位值:
HASH_SLOT = CRC16(key) mod 16384
当在7001节点执行 set key 123 命令时, 该节点会发现指令的 key 所在的槽位12539非自己管理的槽位,这时它会向客户端发送一个跳转指令携带目标操作的节点地址,告诉客户端去连这个节点去处理数据,如下:
-> Redirected to slot [12539] located at 127.0.0.1:7003
src/redis‐cli ‐a 123456 -c ‐h 127.0.0.1 ‐p 7001 shutdown
src/redis‐cli ‐a 123456 -c ‐h 127.0.0.1 ‐p 7002 shutdown
src/redis‐cli ‐a 123456 -c ‐h 127.0.0.1 ‐p 7003 shutdown
src/redis‐cli ‐a 123456 -c ‐h 127.0.0.1 ‐p 7004 shutdown
src/redis‐cli ‐a 123456 -c ‐h 127.0.0.1 ‐p 7005 shutdown
src/redis‐cli ‐a 123456 -c ‐h 127.0.0.1 ‐p 7006 shutdown
如果没有设置访问密码, 则省略-a参数
重新启动集群:
1、先关闭集群节点
2、删除集群相关文件
3、启动集群节点
4、创建集群