author:徐振东

createTime:2022-05-16

updateTime:2022-06-22


2022-06-10: 培训结束

# 一、前言

集群是Redis 3.0 版本才引入的分布式存储方案,由多个节点组成。节点有主节点和从节点两种类型,主节点向外提供读写服务,同时负责集群信息的维护;从点同步主节点的数据和状态信息,当主节点下线后从节点可替代成为新的主节点从而实现集群的高可用性。

集群的作用:

  • 数据分片、写负载均衡 --- 引入哈希槽概念,一个Redis集群包含16384个哈希槽,按照一定规则将这些slot(可均匀分配、或者按照机器的性能高低差异分配)分散映射到主节点。操作key时,先通过CRC16(KEY) mod 16384计算属于哪个slot,再通过slot与节点的映射关系获取到对应的节点信息,最后再进行读取。通过这种方式将原来只能写入单个节点的数据分散写入了不同的节点中,每个节点负责了一部分的slot对外提供了读写服务,使redis的存取不再受限于单机的内存,存储容量大大增加,也大大提高了集群的响应能力
  • 故障自动转移 -- 主节点挂了,从节点可替补转正,提升集群的稳定性,高可用性

# 二、集群的搭建

以下面三台机器为例搭建三主三从的集群模式,6379端口为主节点,7000端口为从节点,redis版本5.0.3

192.168.20.195:6379 192.168.20.195:7000

192.168.20.210:6379 192.168.20.210:7000

192.168.7.198:6379 192.168.7.198:7000

# 1、修改配置文件

vim redis.conf
cluster-enabled yes  # 开启集群模式
cluster-config-file node-6379.conf # 集群中的每个节点都需要有一个集群的配置信息,由redis自己维护,节点重启后根据该文件重新加入集群
cluster-node-timeout 15000 # 判断节点下线的临界时间

Redis实例有单机(standalone)和集群(cluster)两种模式,默认以单机模式启动,将属性 cluster-enabled 的值改为yes 即可以集群模式启动。

# 2、启动节点

修改完各个节点的配置文件后,依次启动各节点

./redis-server redis.conf

----------------------------------------------------------------------------------------------------------------------------
31643:C 03 Dec 2020 16:11:05.404 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31643:C 03 Dec 2020 16:11:05.404 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=31643, just started
31643:C 03 Dec 2020 16:11:05.404 # Configuration loaded
31644:M 03 Dec 2020 16:11:05.405 * Node configuration loaded, I'm 8fec64aebde610fa3d1577721177408b9e2a1b81
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 31644
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

31644:M 03 Dec 2020 16:11:05.405 # Server initialized
31644:M 03 Dec 2020 16:11:05.405 * DB loaded from disk: 0.000 seconds
31644:M 03 Dec 2020 16:11:05.405 * Ready to accept connections
----------------------------------------------------------------------------------------------------------------------------

# 3、 节点加入集群

各个节点虽然都开启了cluster模式,但互相并不知道对方的存在,仍处于孤军奋战的状态中,所以还需要发送握手信号,让节点组成一个网。

./redis-cli -a 123456 --cluster create 192.168.20.195:6379 192.168.20.195:7000 192.168.20.210:6379 192.168.20.210:7000 192.168.7.198:6379 192.168.7.198:7000 --cluster-replicas 1

# 这里的参数 cluster-replicas 代表每个主节点的副本数量,这里代表每个主节点都存在一个从节点。
# 根据参数自适应主节点和从节点数量,并默认将16384个slots均匀分散至各个主节点中

----------------------------------------------------------------------------------------------------------------------------
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.20.210:7000 to 192.168.20.195:6379
Adding replica 192.168.20.195:7000 to 192.168.20.210:6379
Adding replica 192.168.7.198:7000 to 192.168.7.198:6379
>>> Trying to optimize slaves allocation for anti-affinity
[OK] Perfect anti-affinity obtained!
M: c1475a90a61435e82bf1dd6aa1eb750d673dc965 192.168.20.195:6379
   slots:[0-5460] (5461 slots) master
S: 6440bc8a9af667d71a7b858184109eb193e88a41 192.168.20.195:7000
   replicates fc9671d55372253e14414667212592d3f30c264c
M: f34cfbfd5b857dc5e687dad5cc0b9738b7d66689 192.168.20.210:6379
   slots:[5461-10922] (5462 slots) master
S: 8fec64aebde610fa3d1577721177408b9e2a1b81 192.168.20.210:7000
   replicates c1475a90a61435e82bf1dd6aa1eb750d673dc965
M: 

fc9671d55372253e14414667212592d3f30c264c 192.168.7.198:6379
   slots:[10923-16383] (5461 slots) master
S: 7b52121b15f22fb323be2c736d6df41657e25fee 192.168.7.198:7000
   replicates f34cfbfd5b857dc5e687dad5cc0b9738b7d66689
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 192.168.20.195:6379)
M: c1475a90a61435e82bf1dd6aa1eb750d673dc965 192.168.20.195:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 8fec64aebde610fa3d1577721177408b9e2a1b81 192.168.20.210:7000
   slots: (0 slots) slave
   replicates c1475a90a61435e82bf1dd6aa1eb750d673dc965
M: f34cfbfd5b857dc5e687dad5cc0b9738b7d66689 192.168.20.210:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 6440bc8a9af667d71a7b858184109eb193e88a41 192.168.20.195:7000
   slots: (0 slots) slave
   replicates fc9671d55372253e14414667212592d3f30c264c
S: 7b52121b15f22fb323be2c736d6df41657e25fee 192.168.7.198:7000
   slots: (0 slots) slave
   replicates f34cfbfd5b857dc5e687dad5cc0b9738b7d66689
M: fc9671d55372253e14414667212592d3f30c264c 192.168.7.198:6379
   slots:[10923-16383] (5461 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.
----------------------------------------------------------------------------------------------------------------------------

由上边的日志输出可知哈希槽映射和主从节点关系
Master[0] -> Slots 0 - 5460  		【192.168.20.195:6379	<--- 192.168.20.210:7000】
Master[1] -> Slots 5461 - 10922 	【192.168.20.210:6379	<--- 192.168.7.198:7000】
Master[2] -> Slots 10923 - 16383 	【192.168. 7.198:6379	<--- 192.168.20.195:7000】

至此,集群搭建完毕。

# 三、集群原理