Redis master-slave replication sentinel cluster

1. Redis master-slave replication

1.1 Introduction to Redis master-slave replication

  • Master-slave replication refers to copying the data of one Redis server to other Redis servers. The former is called the master node (Master), and the latter is called the slave node (Slave); data replication is one-way, only from the master node to the slave node.
  • By default, each Redis server is a master node; and a master node can have multiple slave nodes (or no slave nodes), but a slave node can only have one master node.

1. Master-slave replication: Master-slave replication is the basis of highly available Redis. Sentinels and clusters are based on master-slave replication to achieve high availability. Master-slave replication mainly implements multi-machine backup of data, as well as load balancing for read operations and simple fault recovery. Defects: Failure recovery cannot be automated; write operations cannot be load-balanced; storage capacity is limited by a single machine.
2. Sentinel: Based on master-slave replication, Sentinel realizes automatic fault recovery. Defects: Write operations cannot be load-balanced; storage capacity is limited by a single machine; Sentinel cannot automatically failover slave nodes. In the scenario of read-write separation, slave node failure will cause read services to be unavailable, and additional monitoring of slave nodes is required , Switch operation.
3. Clustering: Through clustering, Redis solves the problem that write operations cannot be load-balanced, and the storage capacity is limited by a single machine, and realizes a relatively complete high-availability solution.

1.2 The role of master-slave replication

1. Data redundancy: master-slave replication realizes hot backup of data, which is a data redundancy method other than persistence.
2. Fault recovery: When there is a problem with the master node, the slave node can provide services to achieve rapid fault recovery; it is actually a kind of service redundancy.
3. Load balancing: On the basis of master-slave replication, combined with read-write separation, the master node can provide write services, and the slave nodes can provide read services (that is, the application connects to the master node when writing Redis data, and the application connects to the slave node when reading Redis data. node) to share the server load; especially in the scenario of writing less and reading more, sharing the read load through multiple slave nodes can greatly increase the concurrency of the Redis server.
4. The cornerstone of high availability: In addition to the above functions, master-slave replication is also the basis for the implementation of sentinels and clusters. Therefore, master-slave replication is the basis for high availability of Redis.

1.3 The process of master-slave replication

1. If a Slave machine process is started, it will send a "sync command" command to the Master machine to request a synchronous connection.
2. Whether it is the first connection or reconnection, the Master machine will start a background process to save the data snapshot to the data file (execute rdb operation), and the Master will also record all commands for modifying the data and cache them in the data file .
3. After the background process completes the cache operation, the Master machine will send the data file to the Slave machine, and the Slave machine will save the data file to the hard disk, and then load it into the memory, and then the Master machine will modify all operations of the data Send it to the Slave end machine together. If the Slave fails and causes a downtime, it will automatically reconnect after returning to normal.
4. After the master machine receives the connection from the slave machine, it sends its complete data file to the slave machine. If the master receives synchronization requests from multiple slaves at the same time, the master will start a process in the background to save the data file, and then send it to all Slave-side machines to ensure that all Slave-side machines are normal.

2. Build Redis master-slave replication

Master节点:192.168.80.10
Slave1节点:192.168.80.11
Slave2节点:192.168.80.12

2.1 Install Redis

//环境准备
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config

#修改内核参数
vim /etc/sysctl.conf
vm.overcommit_memory = 1
net.core.somaxconn = 2048

sysctl -p


//安装redis
yum install -y gcc gcc-c++ make

tar zxvf /opt/redis-7.0.9.tar.gz -C /opt/
cd /opt/redis-7.0.9
make
make PREFIX=/usr/local/redis install
#由于Redis源码包中直接提供了 Makefile 文件,所以在解压完软件包后,不用先执行 ./configure 进行配置,可直接执行 make 与 make install 命令进行安装。

2.2 Create redis working directory

mkdir /usr/local/redis/{conf,log,data}

cp /opt/redis-7.0.9/redis.conf /usr/local/redis/conf/

useradd -M -s /sbin/nologin redis
chown -R redis.redis /usr/local/redis/

2.3 Environment variables

vim /etc/profile 
PATH=$PATH:/usr/local/redis/bin		#增加一行

source /etc/profile

2.4 Define systemd service management script

vim /usr/lib/systemd/system/redis-server.service
[Unit]
Description=Redis Server
After=network.target

[Service]
User=redis
Group=redis
Type=forking
TimeoutSec=0
PIDFile=/usr/local/redis/log/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

2.5 Modify the Redis configuration file (Master node operation)

vim /usr/local/redis/conf/redis.conf
bind 0.0.0.0									#87行,修改监听地址为0.0.0.0
protected-mode no								#111行,将本机访问保护模式设置no
port 6379										#138行,Redis默认的监听6379端口
daemonize yes									#309行,设置为守护进程,后台启动
pidfile /usr/local/redis/log/redis_6379.pid		#341行,指定 PID 文件
logfile "/usr/local/redis/log/redis_6379.log"	#354行,指定日志文件
dir /usr/local/redis/data						#504行,指定持久化文件所在目录
#requirepass abc123								#1037行,可选,设置redis密码
appendonly yes									#1380行,开启AOF


systemctl restart redis-server.service

2.6 Modify the Redis configuration file (Slave node operation)

vim /usr/local/redis/conf/redis.conf
bind 0.0.0.0									#87行,修改监听地址为0.0.0.0
protected-mode no								#111行,将本机访问保护模式设置no
port 6379										#138行,Redis默认的监听6379端口
daemonize yes									#309行,设置为守护进程,后台启动
pidfile /usr/local/redis/log/redis_6379.pid		#341行,指定 PID 文件
logfile "/usr/local/redis/log/redis_6379.log"	#354行,指定日志文件
dir /usr/local/redis/data						#504行,指定持久化文件所在目录
#requirepass abc123								#1037行,可选,设置redis密码
appendonly yes									#1380行,开启AOF
replicaof 192.168.80.10 6379					#528行,指定要同步的Master节点IP和端口
#masterauth abc123								#535行,可选,指定Master节点的密码,仅在Master节点设置了requirepass


systemctl restart redis-server.service

2.7 Verify the master-slave effect

在Master节点上看日志:
tail -f /usr/local/redis/log/redis_6379.log 
Replica 192.168.80.11:6379 asks for synchronization
Replica 192.168.80.12:6379 asks for synchronization
Synchronization with replica 192.168.80.11:6379 succeeded
Synchronization with replica 192.168.80.12:6379 succeeded

在Master节点上验证从节点:
redis-cli info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.80.11,port=6379,state=online,offset=1246,lag=0
slave1:ip=192.168.80.12,port=6379,state=online,offset=1246,lag=1

insert image description here
insert image description here

insert image description here
insert image description here

3. Redis sentinel mode

3.1 What is Sentry Mode

  • The method of master-slave switching technology is: when the server is down, it is necessary to manually switch a slave machine to the master machine, which requires manual intervention, which is not only time-consuming and laborious, but also causes the service to be unavailable for a period of time. In order to solve the shortcomings of master-slave replication, there is a sentinel mechanism.

  • Sentinel's core function: Based on master-slave replication, Sentinel introduces automatic failover of the master node.

3.2 The role of sentinel mode

1. Monitoring: Sentinel will constantly check whether the master node and slave nodes are operating normally.

2. Automatic failover: When the master node fails to work normally, Sentinel will start an automatic failover operation. It will upgrade one of the slave nodes of the failed master node to a new master node, and let other slave nodes copy the new master node. node.

3. Notification (reminder): Sentinel can send the result of failover to the client.

3.3 The sentinel structure consists of two parts, the sentinel node and the data node

1. Sentinel node: The sentinel system consists of one or more sentinel nodes, which are special redis nodes that do not store data.
2. Data node: Both the master node and the slave node are data nodes.

3.4 Failover mechanism

1. Each sentinel node will send a ping command to the master node, slave node and other sentinel nodes every 1 second for a heartbeat detection. If the master node does not reply within a certain time frame or replies with an error message, then the sentinel will consider the master node to be offline subjectively (unilaterally). When more than half of the sentinel nodes think that the master node is offline subjectively, it is objectively offline.

2. When the master node fails, the sentinel node will implement the election mechanism through the Raft algorithm (election algorithm) to jointly elect a sentinel node as the leader to be responsible for handling the failover and notification of the master node. So the number of clusters running Sentinels must not be less than 3 nodes.

3. The failover is performed by the leader sentinel node, the process is as follows:
(1) Upgrade a certain slave node to a new master node, and let other slave nodes point to the new master node;
(2) If the original master node recovers, it also becomes a slave node , and point to the new primary node;
(3) notify the client that the primary node has been replaced.

需要特别注意的是,客观下线是主节点才有的概念;如果从节点和哨兵节点发生故障,被哨兵主观下线后,不会再有后续的客观下线和故障转移操作。

3.5 Election of the master node

1. Filter out unhealthy (offline) slave nodes that do not respond to sentinel ping responses.
2. Select the slave node with the highest priority configuration in the configuration file. (replica-priority, the default value is 100)
3. Select the slave node with the largest replication offset, that is, the most complete replication.

哨兵的启动依赖于主从模式,所以须把主从模式安装好的情况下再去做哨兵模式

4. Build Redis sentinel mode

Master节点:192.168.80.10
Slave1节点:192.168.80.11
Slave2节点:192.168.80.12

systemctl stop firewalld
setenforce 0

4.1 Modify the configuration file of Redis sentinel mode (all node operations)

cp /opt/redis-7.0.9/sentinel.conf /usr/local/redis/conf/
chown redis.redis /usr/local/redis/conf/sentinel.conf

vim /usr/local/redis/conf/sentinel.conf
protected-mode no									#6行,关闭保护模式
port 26379											#10行,Redis哨兵默认的监听端口
daemonize yes										#15行,指定sentinel为后台启动
pidfile /usr/local/redis/log/redis-sentinel.pid		#20行,指定 PID 文件
logfile "/usr/local/redis/log/sentinel.log"			#25行,指定日志存放路径
dir /usr/local/redis/data							#54行,指定数据库存放路径
sentinel monitor mymaster 192.168.80.10 6379 2		#73行,修改 指定该哨兵节点监控192.168.80.10:6379这个主节点,该主节点的名称是mymaster,最后的2的含义与主节点的故障判定有关:至少需要2个哨兵节点同意,才能判定主节点故障并进行故障转移
#sentinel auth-pass mymaster abc123					#76行,可选,指定Master节点的密码,仅在Master节点设置了requirepass
sentinel down-after-milliseconds mymaster 3000		#114行,判定服务器down掉的时间周期,默认30000毫秒(30秒)
sentinel failover-timeout mymaster 180000			#214行,同一个sentinel对同一个master两次failover之间的间隔时间(180秒)

4.2 Start sentry mode

先启master,再启slave
cd /usr/local/redis/conf/
redis-sentinel sentinel.conf &

4.3 View sentinel information

redis-cli -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.80.10:6379,slaves=2,sentinels=3

4.4 Fault simulation

#查看redis-server进程号:
ps -ef | grep redis
root      57031      1  0 15:20 ?        00:00:07 /usr/local/bin/redis-server 0.0.0.0:6379
root      57742      1  1 16:05 ?        00:00:07 redis-sentinel *:26379 [sentinel]
root      57883  57462  0 16:17 pts/1    00:00:00 grep --color=auto redis

#杀死 Master 节点上redis-server的进程号
kill -9 57031			#Master节点上redis-server的进程号

4.5 Verification Results

tail -f /usr/local/redis/log/sentinel.log
6709:X 13 Mar 2023 12:27:29.517 # +sdown master mymaster 192.168.80.10 6379
6709:X 13 Mar 2023 12:27:29.594 * Sentinel new configuration saved on disk
6709:X 13 Mar 2023 12:27:29.594 # +new-epoch 1
6709:X 13 Mar 2023 12:27:29.595 * Sentinel new configuration saved on disk
6709:X 13 Mar 2023 12:27:29.595 # +vote-for-leader c64fac46fcd98350006900c330998364d6af635d 1
6709:X 13 Mar 2023 12:27:29.620 # +odown master mymaster 192.168.80.10 6379 #quorum 2/2
6709:X 13 Mar 2023 12:27:29.621 # Next failover delay: I will not start a failover before Mon Mar 13 12:33:30 2023
6709:X 13 Mar 2023 12:27:30.378 # +config-update-from sentinel c64fac46fcd98350006900c330998364d6af635d 192.168.80.11 26379 @ mymaster 192.168.80.10 6379
6709:X 13 Mar 2023 12:27:30.378 # +switch-master mymaster 192.168.80.10 6379 192.168.80.11 6379
6709:X 13 Mar 2023 12:27:30.378 * +slave slave 192.168.80.13:6379 192.168.80.13 6379 @ mymaster 192.168.80.11 6379
6709:X 13 Mar 2023 12:27:30.378 * +slave slave 192.168.80.10:6379 192.168.80.10 6379 @ mymaster 192.168.80.11 6379
6709:X 13 Mar 2023 12:27:30.381 * Sentinel new configuration saved on disk
6709:X 13 Mar 2023 12:27:33.379 # +sdown slave 192.168.80.10:6379 192.168.80.10 6379 @ mymaster 192.168.80.11 6379


2.redis-cli -p 26379 INFO Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.80.11:6379,slaves=2,sentinels=3

insert image description here
insert image description here
insert image description here

Five, Redis cluster mode

5.1 Redis Cluster

  • 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.

5.2 The role of the cluster

1. Data partitioning: 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 Sentinel); when any node fails, the cluster can still provide external services.

5.3 Data Fragmentation of Redis Cluster

1. The Redis cluster introduces the concept of hash slots. 2.
The Redis cluster has 16384 hash slots (number 0-16383)
.
16384 take the remainder to decide 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
    redis-cluster集群会用crc16算法对键进行换算,之后会得到一个数字,在用这个数字除以16384取余数,余数对应的Hash槽数值在哪个节点范围内,那么客户端输入的命令就会在哪个节点进行处理

5.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.

6. Build Redis cluster mode

redis的集群一般需要6个节点,3主3从。方便起见,这里所有节点在同一台服务器上模拟:
以端口号进行区分:3个主节点端口号:6001/6002/6003,对应的从节点端口号:6004/6005/6006。
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

6.1 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行,取消注释群集超时时间设置

6.2 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

6.3 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个从节点。

6.4 Test cluster

redis-cli -p 6001 -c					#加-c参数,节点之间就可以互相跳转
127.0.0.1:6001> cluster slots			#查看节点的哈希槽编号范围
1) 1) (integer) 5461
   2) (integer) 10922									#哈希槽编号范围
   3) 1) "127.0.0.1"
      2) (integer) 6003									#主节点IP和端口号
      3) "fdca661922216dd69a63a7c9d3c4540cd6baef44"
   4) 1) "127.0.0.1"
      2) (integer) 6004									#从节点IP和端口号
      3) "a2c0c32aff0f38980accd2b63d6d952812e44740"
2) 1) (integer) 0
   2) (integer) 5460
   3) 1) "127.0.0.1"
      2) (integer) 6001
      3) "0e5873747a2e26bdc935bc76c2bafb19d0a54b11"
   4) 1) "127.0.0.1"
      2) (integer) 6006
      3) "8842ef5584a85005e135fd0ee59e5a0d67b0cf8e"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "127.0.0.1"
      2) (integer) 6002
      3) "816ddaa3d1469540b2ffbcaaf9aa867646846b30"
   4) 1) "127.0.0.1"
      2) (integer) 6005
      3) "f847077bfe6722466e96178ae8cbb09dc8b4d5eb"

127.0.0.1:6001> set name zhangsan
-> Redirected to slot [5798] located at 127.0.0.1:6003
OK

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

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


redis-cli -p 6001 -c cluster nodes

insert image description here

insert image description here

insert image description here

insert image description here
insert image description here
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/2301_76875445/article/details/131477589