Home > Database > Redis > Example analysis of master-slave replication, sentry, and clustering in Redis

Example analysis of master-slave replication, sentry, and clustering in Redis

WBOY
Release: 2023-05-29 13:52:54
forward
848 people have browsed it

1. Redis master-slave replication

1. Overview of master-slave replication

Master-slave replication copies data from one Redis server to other Redis servers process. The former is called the master node (Master), and the latter is called the slave node (Slave); data replication is one-way, and can only be 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, but a slave node can only have one master node. [Related recommendation: Redis video tutorial]

2. The role of master-slave replication

Data redundancy: Master-slave replication realizes data Hot backup is a data redundancy method other than persistence.
Failure recovery: When a problem occurs on the master node, the slave node can provide services to achieve rapid failure recovery; it is actually a kind of service redundancy.
Load balancing: Based on 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, when writing Redis data, the application connects to the master node , when reading Redis data, apply the connection slave node) to share the server load; especially in the scenario of less writing and more reading, sharing the read load through multiple slave nodes can greatly increase the concurrency of the Redis server.
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 of Redis high availability.

3. Master-slave replication process

Example analysis of master-slave replication, sentry, and clustering in Redis

(1) If a Slave machine process is started, it will send data to the Master machine Send a "sync command" command 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 (perform RDB operation). At the same time, the Master will also record and cache all commands to modify the data. in the data file.
(3) After the background process completes the caching operation, the Master machine will send the data file to the Slave machine. The Slave machine will save the data file to the hard disk and then load it into the memory. Then the Master machine will modify the data file. All operations on the data are sent to the Slave machine at the same time. If the Slave fails and causes downtime, it will automatically reconnect when it returns 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 multiple synchronization requests from the Slave at the same time, the Master will start a synchronization request in the background. process to save the data file and then send it to all slave machines to ensure that all slave machines are normal.

4. Set up Redis master-slave replication

4.1 Server IP configuration

##

4.2 Firewall environment of each server

systemctl stop firewalld && systemctl disable firewalld
setenforce 0
Copy after login
Copy after login
Copy after login

4.3 Installation of Redis on each server

Redis installation details can be found in previous blogs:
NoSQL Detailed explanation of redis

传入安装包到/opt目录
yum install -y gcc gcc-c++ make
tar zxvf redis-5.0.7.tar.gz -C /opt/
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
cd /opt/redis-5.0.7/utils
./install_server.sh
......
Please select the redis executable path []
#输入/uar/local/redis/bin/redis-server
ln -s /usr/local/redis/bin/* /usr/local/bin/
Copy after login

4.4 Modify Redis configuration file (Master node operation)

Master: 192.168.122.10

[root@master ~]# vim /etc/redis/6379.conf
 
##70行,修改监听地址为0.0.0.0,表示监听任何地址
bind 0.0.0.0
##137行,开启守护进程
daemonize yes
##172行,指定日志文件目录
logfile /var/log/redis_6379.log
##264行,指定工作日志
dir /var/lib/redis/6379
##700行,开启AOF持久化功能
appendonly yes
Copy after login

4.5 Modify Redis Configuration file (Slave node operation)

Slave1:192.168.122.11

[root@slave1 utils]# vim /etc/redis/6379.conf 
 
##70行,修改监听地址为0.0.0.0,表示监听任何地址
bind 0.0.0.0
##137行,开启守护进程
daemonize yes
##172行,指定日志文件目录
logfile /var/log/redis_6379.log
##264行,指定工作日志
dir /var/lib/redis/6379
##288行,添加要同步的Master节点IP和端口
replicaof 192.168.122.10 6379
##700行,开启AOF持久化功能
appendonly yes
 
[root@slave1 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
Copy after login

Slave2:192.168.122.12

[root@slave2 utils]# vim /etc/redis/6379.conf 
 
##70行,修改监听地址为0.0.0.0,表示监听任何地址
bind 0.0.0.0
##137行,开启守护进程
daemonize yes
##172行,指定日志文件目录
logfile /var/log/redis_6379.log
##264行,指定工作日志
dir /var/lib/redis/6379
##288行,添加要同步的Master节点IP和端口
replicaof 192.168.122.10 6379
##700行,开启AOF持久化功能
appendonly yes
 
[root@slave2 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
Copy after login

4.6 Verify master-slave Effect

4.6.1 View the log on the Master node

[root@master ~]# tail -f /var/log/redis_6379.log 
1002:M 23 Sep 2021 16:46:33.569 * Background saving terminated with success
1002:M 23 Sep 2021 16:46:33.569 * Synchronization with replica 192.168.122.11:6379 succeeded
1002:M 23 Sep 2021 16:46:34.519 * Replica 192.168.122.12:6379 asks for synchronization
1002:M 23 Sep 2021 16:46:34.519 * Full resync requested by replica 192.168.122.12:6379
1002:M 23 Sep 2021 16:46:34.519 * Starting BGSAVE for SYNC with target: disk
1002:M 23 Sep 2021 16:46:34.519 * Background saving started by pid 7941
7941:C 23 Sep 2021 16:46:34.521 * DB saved on disk
7941:C 23 Sep 2021 16:46:34.521 * RDB: 0 MB of memory used by copy-on-write
1002:M 23 Sep 2021 16:46:34.591 * Background saving terminated with success
1002:M 23 Sep 2021 16:46:34.591 * Synchronization with replica 192.168.122.12:6379 succeeded
Copy after login

4.6.2 Verify the slave node on the Master node

[root@master ~]# redis-cli info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.122.11,port=6379,state=online,offset=910,lag=0
slave1:ip=192.168.122.12,port=6379,state=online,offset=910,lag=0
master_replid:9d7fa17fc64cd573f5b81457183831d97dfad7dc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:910
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:910
Copy after login

2. Redis Sentinel Mode

1. The core functions of Sentinel mode

Based on master-slave replication, Sentinel introduces automatic failover of the master node.

2. Principle of Sentinel Mode

Sentinel is a distributed system used to monitor each server in the master-slave structure. When a failure occurs A new Master is selected through the voting mechanism and all Slaves are connected to the new Master. Therefore, the number of the entire cluster running Sentinel must not be less than 3 nodes.

3. The role of sentinel mode

Monitoring: Sentinel will constantly check whether the master node and slave node are operating normally.
Automatic failover: When the master node fails to work properly, Sentinel will start an automatic failover operation. It will upgrade one of the slave nodes of the failed master node to the new master node, and let the other The slave node is changed to replicate the new master node.
Notification Reminder: Sentinel can send the failover results to the client.

4. Structure of Sentinel Mode

The sentinel structure consists of two parts, the sentinel node and the data node:
● Sentinel node: The sentinel system consists of one or more Composed of nodes, the sentinel node is a special redis node that does not store data.
● Data node: Both the master node and the slave node are data nodes.

5. How the sentinel mode works

Example analysis of master-slave replication, sentry, and clustering in Redis
The startup of the sentinel depends on the master-slave mode, so the master-slave mode must be installed Let's do the sentry mode next. Sentinel mode needs to be deployed on all nodes. Sentinel mode will monitor whether all Redis working nodes are normal. When there is a problem with the Master, because other nodes have lost contact with the master node, they will vote, and more than half of the votes will be cast. It will be considered that there is indeed a problem with this Master, and then the sentinel room will be notified, and then one of the Slaves will be selected as the new Master.

6. Failover mechanism

  • #The sentinel node regularly monitors whether the master node has failed. The sentinel node will send a ping command once every second to perform heartbeat detection and send the results to the master node, slave nodes and other sentinel nodes. If the master node does not reply within a certain time range or replies with an error message, then the sentinel will think that the master node is subjectively offline (unilaterally). When more than half of the sentinel nodes believe that the master node is subjectively offline, it will be objectively offline.

  • When the master node fails, the sentinel nodes 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 failure of the master node. notify. Therefore, the number of hosts in the Sentinel cluster must not be less than three nodes.

  • The leader sentinel node performs failover. The process is as follows:
    ● Upgrade a slave node to the new master node and let other slave nodes point to the new master node;
    ● If the original master node recovers, it will also become a slave node and point to the new master node;
    ● Notify the client that the master node has been replaced.
    It is important to note that objective offline is a concept that is unique to the master node; if the slave node and sentinel node fail, there will be no subsequent objective offline and failover operations after being subjectively offline by the sentinel.

7. Master node election

  • Filter out unhealthy ones (offline ones) and no reply to the sentinel The slave node to which the ping responds.

  • Select the slave node with the highest priority configuration in the configuration file (replica-priority, the default value is 100).

  • Select the slave node with the largest replication offset, that is, the most complete replication.

8. Build Redis Sentinel Mode

8.1 Server IP Configuration

Server Host Name IP
Master Node master 192.168 .122.10
Slave1 node slave1 192.168.122.11
Slave2 node slave2 192.168.122.12
##Master Nodemaster192.168.122.10Slave1 nodeslave1192.168.122.11Slave2 Nodeslave2192.168.122.12

8.2 各服务器防火墙环境

systemctl stop firewalld && systemctl disable firewalld
setenforce 0
Copy after login
Copy after login
Copy after login

8.3 修改Redis哨兵模式的配置文件(所有节点操作)

vim /opt/redis-5.0.7/sentinel.conf
 
##17行,取消注释,关闭保护模式
protected-mode no
##21行,Redis哨兵默认的监听端口
port 26379
##26行,指定sentienel为后台启动
daemonize yes
##36行,指定日志存放路径
logfile "/var/log/sentinel.log"
##65行,指定数据库存放路径
dir "/var/lib/redis/6379"
##84行,修改,指定该哨兵节点监控192.168.122.10 6379这个主节点,该主节点的名称是mymaster
##最后的2的含义与主节点的故障判定有关;至少需要2个哨兵节点同意,才能判定故障并进行故障转移
sentinel monitor mymaster 192.168.122.10 6379 2
##113行,判定服务器down掉的时间周期,默认30000毫秒(30秒)
sentinel down-after-milliseconds mymaster 30000
##146行,故障节点的最大超时时间为180000毫秒(180秒)
sentinel failover-timeout mymaster 180000
Copy after login

8.4 启动哨兵模式

注意:需先启动master,再启动slave

cd /opt/redis-5.0.7/
redis-sentinel sentinel.conf &
Copy after login

8.5 查看哨兵信息

Master:192.168.122.10

[root@master redis-5.0.7]# 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.122.10:6379,slaves=2,sentinels=3
Copy after login

三、Redis 群集模式

1. Redis集群的概述

集群,即Redis Cluster,是Redis 3.0开始引入的分布式存储方案。

2. Redis集群

Redis数据分布在多个节点(Node)组成的集群中。集群中的节点分为主节点和从节点;只有主节点负责读写请求和集群信息的维护;从节点只进行主节点数据和状态信息的复制。

3. Redis集群的作用

集群的作用,可以归纳为两点:

3.1 数据分区

数据分区(或称数据分片)是集群最核心的功能。
集群将数据分散到多个节点,一方面突破了Redis单机内存大小的限制,存储容量大大增加;另一方面每个主节点度可以对外提供读服务和写服务,大大提高了集群的响应能力。
Redis单机内存大小受限问题,在介绍持久化和主从复制时都有体积;例如,如果单机内存太大,bgsave和bgrewriteaof的fork操作可能导致主进程阻塞,主从环境下主机切换时可能导致从节点长时间无法提供服务,全量复制阶段主节点的复制缓冲区可能溢出。

3.2 高可用

该集群支持主从复制和类似哨兵一样的主节点自动故障转换功能,即使发生节点故障也能够继续为外部提供服务。

4. Redis集群的数据分片

● Redis集群引入了哈希槽的概念
● Redis集群有16384个哈希槽(编号0-16383)
● 集群的每个节点负责一部分哈希槽
● 每个key通过CRC16校验后对16384取余来决定放置哪个哈希槽,通过这个值,去找到所对应的节点,然后直接跳转到这个对应的节点上进行存取操作。

5. 哈希槽

5.1 哈希槽的分配

Example analysis of master-slave replication, sentry, and clustering in Redis

  • 哈希槽可按照集群主机数平均分配(默认分配)
    以3个节点组成的集群为例:
    节点A包含0-5460号哈希槽
    节点B包含5461-10922号哈希槽
    节点C包含10923-16383号哈希槽

  • 也可以根据主机的性能以及功能自定义分配
    以3个节点组成的集群为例:
    节点A性能最差,包含0-2000号哈希值
    节点B性能中等,包含2001-7000号哈希值
    节点C性能最强,包含7001-16383号哈希值

5.2 哈希槽的使用

集群搭建的时候,需要给集群的节点分配插槽,0~16383
在node1执行set a a

  • 使用crc16算法对key进行计算,得到一个数字,然后对这个数字进行求余16384(crc16 : a = 26384l;26384 % 16384 = 10000)

  • 查找包含10000的插槽的节点,找到了node2,自动跳转到node2

  • 在node2上执行set a a命令

node3上执行get a

  • a --> 10000

  • 跳转到node2

  • 在node2执行get a

6. Redis集群的主从复制模型

Example analysis of master-slave replication, sentry, and clustering in Redis
集群中具有A、B、C三个节点,如果节点B失败了,整个集群就会因缺少5461-10922这个范围的槽而不可以用。
以每个节点添加一个从节点A1、B1、C1整个集群便有了三个Master节点和三个Slave节点组成,在节点B失败后,集群选举B1位为新的主节点继续服务。当B和B1都失败后,集群将不可用。

7. 搭建Redis群集模式

7.1 服务器IP配置

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

ServerHost NameIP
服务器主机名IP主端口从端口
Node1节点node192.168.122.1060016004
Node2节点node192.168.122.1060026005
Node3节点node192.168.122.1060036006

7.2 服务器防火墙环境

systemctl stop firewalld && systemctl disable firewalld
setenforce 0
Copy after login
Copy after login
Copy after login

7.3 创建集群配置目录及文件

[root@node ~]# cd /etc/redis
[root@node redis]# mkdir -p redis-cluster/redis600{1..6}
[root@node redis]# for i in {1..6}
> do
> cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis600$i
> cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis600$i
> done
[root@node redis]# ls -R redis-cluster/
redis-cluster/:
redis6001  redis6002  redis6003  redis6004  redis6005  redis6006
 
redis-cluster/redis6001:
redis-cli  redis.conf  redis-server
 
redis-cluster/redis6002:
redis-cli  redis.conf  redis-server
 
redis-cluster/redis6003:
redis-cli  redis.conf  redis-server
 
redis-cluster/redis6004:
redis-cli  redis.conf  redis-server
 
redis-cluster/redis6005:
redis-cli  redis.conf  redis-server
 
redis-cluster/redis6006:
redis-cli  redis.conf  redis-server
Copy after login

7.4 开启群集功能

仅以redis6001为例,其他5个文件夹的配置文件以此类推修改,特别注意端口号的修改。

[root@node redis]# cd redis-cluster/redis6001
[root@node redis6001]# vim redis.conf 
 
##69行,注释掉bind项,默认监听所有网卡
#bind 127.0.0.1
##88行,修改,关闭保护模式
protected-mode no
##92行,修改,redis监听端口
port 6001
##136行,开启守护进程,以独立进程启动
daemonize yes
##832行,取消注释,开启群集功能
cluster-enabled yes
##840行,注销注释,群集名称文件设置
cluster-config-file nodes-6001.conf
##846行,注销注释,群集超时时间设置
cluster-node-timeout 15000
##700行,修改,开启AOF持久化
appendonly yes
Copy after login

7.5 启动redis节点

分别进入那六个文件夹,执行命令:“redis-server redis.conf”,来启动redis节点

[root@node redis6001]# for d in {1..6}
> do
> cd /etc/redis/redis-cluster/redis600$i
> ^C
[root@node redis6001]# for d in {1..6}
> do
> cd /etc/redis/redis-cluster/redis600$d
> redis-server redis.conf
> done
[root@node1 redis6006]# ps -ef | grep redis
root        992      1  0 13:45 ?        00:00:07 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root       2289      1  0 14:41 ?        00:00:00 redis-server *:6001 [cluster]
root       2294      1  0 14:41 ?        00:00:00 redis-server *:6002 [cluster]
root       2299      1  0 14:41 ?        00:00:00 redis-server *:6003 [cluster]
root       2304      1  0 14:41 ?        00:00:00 redis-server *:6004 [cluster]
root       2309      1  0 14:41 ?        00:00:00 redis-server *:6005 [cluster]
root       2314      1  0 14:41 ?        00:00:00 redis-server *:6006 [cluster]
root       2450   2337  0 14:50 pts/0    00:00:00 grep --color=auto redis
Copy after login

7.6 启动集群

[root@node redis6006]# 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
Copy after login

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

7.7 测试集群

[root@node1 redis6006]# 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
#node节点主
      3) "18e59f493579facea29abf90ca4050f566d66339"
   4) 1) "127.0.0.1"
      2) (integer) 6004
#node节点从
      3) "2635bf6a0c286ef910ec5da03dbdc7cde308c588"
2) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "127.0.0.1"
      2) (integer) 6003
      3) "51460d417eb56537e5bd7e8c9581c66fdd817b3c"
   4) 1) "127.0.0.1"
      2) (integer) 6006
      3) "51a75667dcf21b530e69a3242a3e9f81f577168d"
3) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "127.0.0.1"
      2) (integer) 6002
      3) "6381d68c06ddb7ac43c8f7d7b8da0644845dcd59"
   4) 1) "127.0.0.1"
      2) (integer) 6005
      3) "375ad927116d3aa845e95ad5f0586306e7ff3a96"
127.0.0.1:6001> set num 1
OK
127.0.0.1:6001> get num
"1"
127.0.0.1:6001> keys *
1) "num"
127.0.0.1:6001> quit
[root@node1 redis6006]# redis-cli -p 6002 -c
127.0.0.1:6002> keys *
#6002端口无键值对
(empty list or set)
127.0.0.1:6002> get num
-> Redirected to slot [2765] located at 127.0.0.1:6001
"1"
#6002端口获取到num键位于6001端口,切换到6001端口并显示键值
127.0.0.1:6001> set key1 11111
-> Redirected to slot [9189] located at 127.0.0.1:6002
OK
#6001端口创建键值对,将其存至6002端口,并切换至6002端口
127.0.0.1:6002>
Copy after login

The above is the detailed content of Example analysis of master-slave replication, sentry, and clustering in Redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template