redis7 Cluster模式 集群

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

1.Redis集群模式介绍

  • Cluster模式是Redis3.0开始推出的
  • Redis Cluster属于AP模型
  • 采用无中心结构每个节点保存数据和整个集群状态, 每个节点都和其他所有节点连接
  • 官方要求至少6个节点才可以保证高可用即3主3从扩展性强、更好做到高可用
  • 各个节点会互相通信采用gossip协议交换节点元数据信息
  • 数据分散存储到各个节点上

2.集群和哨兵的区别

Sentinel哨兵是为系统提供高可用特性,每一个Redis节点数据是同步的,且每一个Redis节点保存的都是全量数据

Cluster集群是将整体数据打散到多台Redis服务器,可对存储规模进行水平扩容,每一个Redis节点存储的都是完整数据的一部分

3.Redis集群的哈希槽设计

Redis集群预分好16384个槽当需要在 Redis 集群中放置一个 key-value 时根据 CRC16(key) mod 16384的值决定将一个key放到哪个桶中

假设主节点的数量为3将16384个槽位按照【用户自己的规则】去分配这3个节点每个节点复制一部分槽位

  • 节点1的槽位区间范围为0-5460
  • 节点2的槽位区间范围为5461-10922
  • 节点3的槽位区间范围为10923-16383

注意从节点是没有槽位的只有主节点才有

存储查找

对要存储查找的键进行crc16哈希运算,得到一个值并取模16384判断这个值在哪个节点的范围区间

使用哈希槽的好处就在于可以方便的添加或移除节点。

  • 当需要增加节点时只需要把其他节点的某些哈希槽挪到新节点就可以了
  • 当需要移除节点时只需要把移除节点上的哈希槽挪到其他节点就行了;

4.Redis 集群搭建

前提已安装好redis这里在一台宝塔的环境上作为演示做1主1从 共3主从搭建3主3从。我们生产环境肯定是多台服务器上。

        1.我们在redis目录下面新增clusters目录作为集群目录。

       注意后续的文件夹和文件都要设置权限组为redis

        2.准备redis.conf文件

bind IP地址
port 端口号
daemonize yes
requirepass "密码"
logfile "日记文件"
dbfilename "数据库文件"
dir "目录地址"
masterauth "master密码"

# 是否开启集群
cluster-enabled yes
# 生成的node文件记录集群节点信息默认为nodes.conf会自动生成
cluster-config-file nodes.conf
#节点连接超时时间
cluster-node-timeout 15000
#集群节点映射端口
cluster-announce-port 端口号
#集群节点总线端口,节点之间互相通信常规端口+1万用port + 10000
cluster-announce-bus-port 端口号

3. 创建6个目录 (1101-1106),并把redis.conf放在每个目录下面

 

注意每个目录下面redis.conf根据目录名修改一下如下参数

4.启动服务

[root@VM-32-8-centos redis]# ./src/redis-server ./clusters/1101/redis.conf 
[root@VM-32-8-centos redis]# ./src/redis-server ./clusters/1102/redis.conf 
[root@VM-32-8-centos redis]# ./src/redis-server ./clusters/1103/redis.conf 
[root@VM-32-8-centos redis]# ./src/redis-server ./clusters/1104/redis.conf 
[root@VM-32-8-centos redis]# ./src/redis-server ./clusters/1105/redis.conf 
[root@VM-32-8-centos redis]# ./src/redis-server ./clusters/1106/redis.conf 

 查看启动进程和生成的文件。在每个目标下面都会成nodes.conf 和 redis.log文件。是对应我们redis.conf里配置的。出现以下截图就启动服务成功了。

[root@VM-32-8-centos redis]# ps -aux | grep redis
root      259700  0.0  0.0  62632 10380 ?        Ssl  17:08   0:00 ./src/redis-server 0.0.0.0:1101 [cluster]
root      259724  0.0  0.0  62632 10352 ?        Ssl  17:08   0:00 ./src/redis-server 0.0.0.0:1102 [cluster]
root      259736  0.0  0.0  62632 10400 ?        Ssl  17:09   0:00 ./src/redis-server 0.0.0.0:1103 [cluster]
root      259763  0.0  0.0  62632 10388 ?        Ssl  17:09   0:00 ./src/redis-server 0.0.0.0:1104 [cluster]
root      259772  0.0  0.0  62632 10436 ?        Ssl  17:09   0:00 ./src/redis-server 0.0.0.0:1105 [cluster]
root      259787  0.0  0.0  62632 10444 ?        Ssl  17:09   0:00 ./src/redis-server 0.0.0.0:1106 [cluster]
root      260066  0.0  0.0  12136  1072 pts/0    S+   17:10   0:00 grep --color=auto redis

 

5.云服务器和本地服务器要先放行端口

  6.创建集群

./src/redis-cli -a 123456 -p 1101 --cluster-replicas 1 --cluster create 服务器IP:1101 服务器IP:1102 服务器IP:1103 服务器IP:1104 服务器IP:1105 服务器IP:1106
  • 此处不要用127.0.0.1 请用真实服务器IP地址

  • 一个集群至少要有三个主节点。

  • 选项 --cluster-replicas 1 表示我们希望为集群中的每个主节点创建1个从节点。根据需要自行配置比如配置为2时就需要9个主从节点了

  • 分配原则尽量保证每个主数据库运行在不同的IP地址每个从库和主库不在一个IP地址上。

执行完创建集群命令之后执行到时输入yes确认Can I set the above configuration? (type 'yes' to accept)

看到如下输出说明集群搭建成功

[root@VM-32-8-centos redis]# ./src/redis-cli -a 123456 -p 1101 --cluster-replicas 1 --cluster create 你的服务器IP:1101 你的服务器IP:1102 你的服务器IP:1103 你的服务器IP:1104 你的服务器IP:1105 你的服务器IP:1106
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 你的服务器IP:1105 to 你的服务器IP:1101
Adding replica 你的服务器IP:1106 to 你的服务器IP:1102
Adding replica 你的服务器IP:1104 to 你的服务器IP:1103
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: e3c7ae473c4a88be0b2f70ae78176b63f45236b6 你的服务器IP:1101
   slots:[0-5460] (5461 slots) master
M: ef2c2fb51447bede89ff84b84a2bfa89eb30dcd1 你的服务器IP:1102
   slots:[5461-10922] (5462 slots) master
M: f29263deb78a3332f2390e7fc00fe91d55d87ae9 你的服务器IP:1103
   slots:[10923-16383] (5461 slots) master
S: ddc1e41c9e973b737b8d2f770c062b7b47cc4029 你的服务器IP:1104
   replicates f29263deb78a3332f2390e7fc00fe91d55d87ae9
S: 271bc3e819eb49e27af765dd7cec413fea2f89a9 你的服务器IP:1105
   replicates e3c7ae473c4a88be0b2f70ae78176b63f45236b6
S: 0b19d704809817e6665c205d34e4e1a980579c7b 你的服务器IP:1106
   replicates ef2c2fb51447bede89ff84b84a2bfa89eb30dcd1
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 你的服务器IP:1101)
M: e3c7ae473c4a88be0b2f70ae78176b63f45236b6 你的服务器IP:1101
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 271bc3e819eb49e27af765dd7cec413fea2f89a9 你的服务器IP:1105
   slots: (0 slots) slave
   replicates e3c7ae473c4a88be0b2f70ae78176b63f45236b6
M: f29263deb78a3332f2390e7fc00fe91d55d87ae9 你的服务器IP:1103
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: ef2c2fb51447bede89ff84b84a2bfa89eb30dcd1 你的服务器IP:1102
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 0b19d704809817e6665c205d34e4e1a980579c7b 你的服务器IP:1106
   slots: (0 slots) slave
   replicates ef2c2fb51447bede89ff84b84a2bfa89eb30dcd1
S: ddc1e41c9e973b737b8d2f770c062b7b47cc4029 你的服务器IP:1104
   slots: (0 slots) slave
   replicates f29263deb78a3332f2390e7fc00fe91d55d87ae9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@VM-32-8-centos redis]# 

7.客户端访问集群

[root@VM-32-8-centos redis]# ./src/redis-cli -c -p 1101
127.0.0.1:1101> auth 123456
OK
127.0.0.1:1101> keys *
(empty array)
127.0.0.1:1101> 

8.查看集群状态

127.0.0.1:1101> cluster info

9.查看集群节点信息

127.0.0.1:1101> cluster nodes

10.常见疑问

1、kill 掉一个从节点集群正常

2、kill 掉一个主节点自动故障转移从节点提升为主节点故障恢复后以从节点身份执行任务

3、kill 掉一组主从节点集群停止响应 CLUSTERDOWN The cluster is down

4、只能在主节点操作数据增删改查从节点只是做备份数据

5、集群只有一个库db0

11.扩容和收容

 以后再测试

12.使用RESP管理redis时修改集群重定向要取消掉

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: redis