This article will take you through the master-slave replication, sentinels, and clusters in Redis. I hope it will be helpful to you!
1. Redis master-slave replication
1. Overview of master-slave replication
Master-slave replication refers to copying data from one 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, 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 recommendations: Redis video tutorial]
2. The role of master-slave replication
● data Redundancy
: Master-slave replication implements hot backup of data, which is a data redundancy method in addition to 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
(1) If you start a Slave machine process, Then 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 (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
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 |
4.2 Firewall environment of each server
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
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/
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
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...
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...
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
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
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 Monitor and select a new Master through a voting mechanism when a failure occurs and connect all Slaves 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
: The sentinel will constantly check whether the master node and slave node It works fine.
● 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. The 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 nodes. Sentinel nodes are special redis nodes that do not store data.
● Data node: Both the master node and the slave node are data nodes.
5. Working form of sentry mode
The start of sentinel depends on the master-slave mode, so the master must be After the slave mode is installed, go to sentry mode. 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 lose contact with the master node, Therefore, a vote will be held. If more than half of the votes are cast, it will be considered that there is indeed a problem with this Master, and then the sentinel room will be notified, and 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. Each sentinel node will send a ping command to the master node, slave node and other sentinel nodes every 1 second for heartbeat detection. 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), There is no slave node that replies to the sentinel ping response.
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 |
##8.2 各服务器防火墙环境
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
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
8.4 启动哨兵模式
注意:需先启动master,再启动slave
cd /opt/redis-5.0.7/
redis-sentinel sentinel.conf &
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
三、Redis 群集模式
1. Redis集群的概述
集群,即Redis Cluster,是Redis 3.0开始引入的分布式存储方案。
2. Redis集群
集群由多个节点(Node)组成,Redis的数据分布在这些节点中。集群中的节点分为主节点和从节点;只有主节点负责读写请求和集群信息的维护;从节点只进行主节点数据和状态信息的复制。
3. Redis集群的作用
集群的作用,可以归纳为两点:
3.1 数据分区
数据分区(或称数据分片)是集群最核心的功能。
集群将数据分散到多个节点,一方面突破了Redis单机内存大小的限制,存储容量大大增加;另一方面每个主节点度可以对外提供读服务和写服务,大大提高了集群的响应能力。
Redis单机内存大小受限问题,在介绍持久化和主从复制时都有体积;例如,如果单机内存太大,bgsave和bgrewriteaof的fork操作可能导致主进程阻塞,主从环境下主机切换时可能导致从节点长时间无法提供服务,全量复制阶段主节点的复制缓冲区可能溢出。
3.2 高可用
集群支持主从复制和主节点的自动故障转换(与哨兵类似);当任一节点发生故障时,集群仍然可以对外提供服务。
4. Redis集群的数据分片
● Redis集群引入了哈希槽的概念
● Redis集群有16384个哈希槽(编号0-16383)
● 集群的每个节点负责一部分哈希槽
● 每个key通过CRC16校验后对16384取余来决定放置哪个哈希槽,通过这个值,去找到所对应的节点,然后直接跳转到这个对应的节点上进行存取操作。
5. 哈希槽
5.1 哈希槽的分配
5.2 哈希槽的使用
集群搭建的时候,需要给集群的节点分配插槽,0~16383
在node1执行set a a
node3上执行get a
a --> 10000
跳转到node2
在node2执行get a
6. 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。
服务器 |
主机名 |
IP |
主端口 |
从端口 |
Node1节点 |
node |
192.168.122.10 |
6001 |
6004 |
Node2节点 |
node |
192.168.122.10 |
6002 |
6005 |
Node3节点 |
node |
192.168.122.10 |
6003 |
6006 |
7.2 服务器防火墙环境
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
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
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
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
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
六个实例分为三组,每组一主一从,前面的做主节点,后面的做从节点。下面交互的时候需要输入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>
更多编程相关知识,请访问:编程入门!!
The above is the detailed content of Let’s talk about master-slave replication, sentinels, and clusters in Redis. For more information, please follow other related articles on the PHP Chinese website!