1、简述redis集群的实现原理
在哨兵sentinel机制中,可以解决redis高可用问题,即当master故障后可以自动将slave提升为master,从而可以保证redis服务的正常使用,但是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受限于单机的内存大小、并发数量、网卡速率等因素。为了解决单机性能的瓶颈,提高Redis 性能,可以使用分布式集群的解决方案
早期Redis 分布式集群部署方案:
- 客户端分区:由客户端程序决定key写分配和写入的redis node,但是需要客户端自己实现写入分 配、高可用管理和故障转移等
- 代理方案:基于三方软件实现redis proxy,客户端先连接之代理层,由代理层实现key的写入分 配,对客户端来说是有比较简单,但是对于集群管节点增减相对比较麻烦,而且代理本身也是单点 和性能瓶颈。 ==redis 3.0版本之后推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存 当前节点数据和整个集群状态,每个节点都和其他所有节点连接==
Redis Cluster特点如下:
- 所有Redis节点使用(PING机制)互联
- 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
- 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
- redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担 16384/N个槽位
- Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使 用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点 上,从而有效解决单机瓶颈。
2、基于redis5的redis cluster部署
创建 redis cluster集群的环境准备
- 每个redis 节点采用相同的相同的redis版本、相同的密码、硬件配置
- 所有redis服务器必须没有任何数据
- 准备六台主机,地址如下:
10.0.0.8
10.0.0.18
10.0.0.28
10.0.0.38
10.0.0.48
10.0.0.58
启用 redis cluster 配置
所有6台主机都执行以下配置
dnf -y install redis
- 每个节点修改redis配置,必须开启cluster功能的参数
#手动修改配置文件
vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456 #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes #取消此行注释,必须开启集群,开启后redis 进程会有cluster标识
cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护cluster-require-full-coverage no #默认值为yes,设为no可以防止一个节点不可用导致整个cluster不能用
#或者执行下面命令,批量修改
sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-filenodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf
systemctl enable --now redis
- 验证当前Redis服务状态:
#开启了16379的cluster的端口,实际的端口=redis port + 10000
[root@centos8-1 ~]# ss -ntlu
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 0.0.0.0:111 0.0.0.0:*
udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:*
udp UNCONN 0 0 [::]:111 [::]:*
udp UNCONN 0 0 [::1]:323 [::]:*
tcp LISTEN 0 511 0.0.0.0:6379 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
tcp LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
tcp LISTEN 0 511 0.0.0.0:16379 0.0.0.0:*
tcp LISTEN 0 128 [::]:111 [::]:*
tcp LISTEN 0 128 [::]:22 [::]:*
tcp LISTEN 0 100 [::1]:25 [::]:*
#注意进程有[cluster]状态
[root@centos8-1 ~]# ps -ef|grep redis
redis 30651 1 0 11:20 ? 00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root 30663 1518 0 11:20 pts/0 00:00:00 grep --color=auto redis
创建集群
#命令redis-cli的选项 --cluster-replicas 1 表示每个master对应一个slave节点
[root@centos8-1 ~]# redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58:6379 --cluster-replicas 1
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 10.0.0.38:6379 to 10.0.0.8:6379
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
M: 70fb55ae7253ad026dc1912c8cfad56859734d99 10.0.0.8:6379
slots:[0-5460] (5461 slots) master
M: 2f72beb9085b3e412c9fa62c1e709480868bbaaa 10.0.0.18:6379
slots:[5461-10922] (5462 slots) master
M: 0089cf4e489efdaf70c3446246aa6f7892a2bb98 10.0.0.28:6379
slots:[10923-16383] (5461 slots) master
S: d71d89a8dc3f2e7e587bd18a77356626f2a6dfbd 10.0.0.38:6379
replicates 70fb55ae7253ad026dc1912c8cfad56859734d99
S: 12c3267f142424d8e5f8fcdc89e1477de8b6111e 10.0.0.48:6379
replicates 2f72beb9085b3e412c9fa62c1e709480868bbaaa
S: 64ff5e0ef9e00ed62c4fdea411dd28f73690b826 10.0.0.58:6379
replicates 0089cf4e489efdaf70c3446246aa6f7892a2bb98
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 10.0.0.8:6379)
M: 70fb55ae7253ad026dc1912c8cfad56859734d99 10.0.0.8:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 12c3267f142424d8e5f8fcdc89e1477de8b6111e 10.0.0.48:6379
slots: (0 slots) slave
replicates 2f72beb9085b3e412c9fa62c1e709480868bbaaa
S: d71d89a8dc3f2e7e587bd18a77356626f2a6dfbd 10.0.0.38:6379
slots: (0 slots) slave
replicates 70fb55ae7253ad026dc1912c8cfad56859734d99
S: 64ff5e0ef9e00ed62c4fdea411dd28f73690b826 10.0.0.58:6379
slots: (0 slots) slave
replicates 0089cf4e489efdaf70c3446246aa6f7892a2bb98
M: 0089cf4e489efdaf70c3446246aa6f7892a2bb98 10.0.0.28:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 2f72beb9085b3e412c9fa62c1e709480868bbaaa 10.0.0.18:6379
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@centos8-1 ~]# redis-cli -a 123456 CLUSTER INFO
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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:82
cluster_stats_messages_pong_sent:95
cluster_stats_messages_sent:177
cluster_stats_messages_ping_received:90
cluster_stats_messages_pong_received:82
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:177
查看集群node对应关系
[root@centos8-1 ~]# redis-cli -a 123456 CLUSTER nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
12c3267f142424d8e5f8fcdc89e1477de8b6111e 10.0.0.48:6379@16379 slave 2f72beb9085b3e412c9fa62c1e709480868bbaaa 0 1652326269000 5 connected
d71d89a8dc3f2e7e587bd18a77356626f2a6dfbd 10.0.0.38:6379@16379 slave 70fb55ae7253ad026dc1912c8cfad56859734d99 0 1652326269974 4 connected
64ff5e0ef9e00ed62c4fdea411dd28f73690b826 10.0.0.58:6379@16379 slave 0089cf4e489efdaf70c3446246aa6f7892a2bb98 0 1652326269000 6 connected
0089cf4e489efdaf70c3446246aa6f7892a2bb98 10.0.0.28:6379@16379 master - 0 1652326270981 3 connected 10923-16383
70fb55ae7253ad026dc1912c8cfad56859734d99 10.0.0.8:6379@16379 myself,master - 0 1652326267000 1 connected 0-5460
2f72beb9085b3e412c9fa62c1e709480868bbaaa 10.0.0.18:6379@16379 master - 0 1652326270000 2 connected 5461-10922