Redis High Availability - Cluster Mode

1. Redis cluster mode

Cluster, namely Redis Cluster, is a distributed storage solution introduced by Redis 3.0.

The cluster consists of multiple groups of nodes (Nodes), and Redis data is distributed among these nodes. The nodes in the cluster are divided into master nodes and slave nodes: only the master node is responsible for the maintenance of read and write requests and cluster information; the slave nodes only replicate the data and status information of the master node.

2. The role of clusters

1. Data partition

Data partitioning (or data sharding) is the core function of the cluster.

The cluster distributes data to multiple nodes. On the one hand, it breaks through the limit of Redis single-machine memory size, and the storage capacity is greatly increased; on the other hand, each master node can provide external read and write services, which greatly improves the responsiveness of the cluster.
Redis stand-alone memory size is limited, which was mentioned in the introduction of persistence and master-slave replication; As a result, the slave node cannot provide services for a long time, and the replication buffer of the master node may overflow during the full replication phase.

2. High availability

The cluster supports master-slave replication and automatic failover of the master node (similar to sentry); when any node fails, the cluster can still provide external services.

3. Data fragmentation of Redis cluster

The Redis cluster introduces the concept of hash slots. The Redis cluster has 16384 hash slots (number 0-16383). Each group of nodes in the cluster is responsible for a part of the hash slots. Each Key is determined by taking the remainder of 16384 after passing the CRC16 check. Which hash slot to place, use this value to find the node corresponding to the corresponding slot, and then directly and automatically jump to the corresponding node for access operations

Take a cluster composed of 3 nodes as an example
Node A contains hash slots 0 to 5460
Node B contains hash slots 5461 to 10922 Node
C contains hash slots 10923 to 16383

4. Master-slave replication model of Redis cluster

There are three nodes A, B, and C in the cluster. If node B fails, the entire cluster will be unavailable due to the lack of slots in the range of 5461-10922.
Add a slave node A1, B1, and C1 to each node, and the entire cluster consists of three master nodes and three slave nodes. After node B fails, the cluster elects the master node with B1 to continue serving. When both B and B1 fail, the cluster will be unavailable.

5. Build Redis cluster mode

A redis cluster generally requires 6 nodes, 3 masters and 3 slaves. For convenience, here all nodes are simulated on the same server:
distinguished by port numbers: 3 master node port numbers: 6001/6002/6003, corresponding slave node port numbers: 6004/6005/6006.

1. Create a directory for each redis node and copy the required files

cd /usr/local/redis/
mkdir -p redis-cluster/redis600{
    
    1..6}

for i in {
    
    1..6}
do
cp /opt/redis-7.0.9/redis.conf /usr/local/redis/redis-cluster/redis600$i
cp /opt/redis-7.0.9/src/redis-cli /opt/redis-7.0.9/src/redis-server /usr/local/redis/redis-cluster/redis600$i
done

2. Turn on the cluster function

#其他5个文件夹的配置文件以此类推修改,注意6个端口都要不一样。
cd /usr/local/redis/redis-cluster/redis6001
vim redis.conf
#bind 127.0.0.1									#87行,注释掉bind项,默认监听所有网卡
protected-mode no								#111行,关闭保护模式
port 6001										#138行,修改redis监听端口
daemonize yes									#309行,设置为守护进程,后台启动
pidfile /usr/local/redis/log/redis_6001.pid		#341行,指定 PID 文件
logfile "/usr/local/redis/log/redis_6001.log"	#354行,指定日志文件
dir ./											#504行,指定持久化文件所在目录
appendonly yes									#1379行,开启AOF
cluster-enabled yes								#1576行,取消注释,开启群集功能
cluster-config-file nodes-6001.conf				#1584行,取消注释,群集名称文件设置
cluster-node-timeout 15000						#1590行,取消注释群集超时时间设置

3. Start the redis node

分别进入那六个文件夹,执行命令:redis-server redis.conf ,来启动redis节点
cd /usr/local/redis/redis-cluster/redis6001
redis-server redis.conf

for d in {
    
    1..6}
do
cd /usr/local/redis/redis-cluster/redis600$d
./redis-server redis.conf
done

ps -ef | grep redis

insert image description here

4. Start the cluster

redis-cli --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1

#六个实例分为三组,每组一主一从,前面的做主节点,后面的做从节点。下面交互的时候 需要输入 yes 才可以创建。
--replicas 1 表示每个主节点有1个从节点。

insert image description here

5. Test cluster

redis-cli -p 6001 -c					#加-c参数,节点之间就可以互相跳转
127.0.0.1:6001> cluster slots			#查看节点的哈希槽编号范围
1) 1) (integer) 0
   2) (integer) 5460                    
   3) 1) "127.0.0.1"
      2) (integer) 6001
      3) "0d628964bddcb3da9a0ff27d5fac81f2ee008f6b"
      4) (empty array)
   4) 1) "127.0.0.1"
      2) (integer) 6004
      3) "150dcfca95586a08ccc7c195ea618b7080273abe"
      4) (empty array)
2) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "127.0.0.1"
      2) (integer) 6002
      3) "b5ac2edf4eede8243f8249efa4b274814b04cc25"
      4) (empty array)
   4) 1) "127.0.0.1"
      2) (integer) 6005
      3) "2356aa0a7190fefab1f950ee9ec4b6b6c630d479"
      4) (empty array)
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "127.0.0.1"
      2) (integer) 6003
      3) "dfcdaf1693e5ea79e1776eaee331ddb7f6818aca"
      4) (empty array)
   4) 1) "127.0.0.1"
      2) (integer) 6006
      3) "38852ffe064783bf1456374b0d23fe8c03320176"
      4) (empty array)


127.0.0.1:6001> set name zhangsan
127.0.0.1:6001> cluster keyslot name					#查看name键的槽编号

redis-cli -p 6002 -c
127.0.0.1:6002> keys *							#对应的slave节点也有这条数据,但是别的节点没有
1) "name"


redis-cli -p 6001 -c cluster nodes

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_67300995/article/details/131516158