Redis master-slave replication, sentinel mode, cluster

One, Redis master-slave replication

1. Redis master-slave replication concept

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 and can only be from the master node to the slave node.
By default, each Redis server is the 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.

2. The role of Redis master-slave replication

(1) Data redundancy: Master-slave replication realizes hot backup of data, which is a data redundancy method besides persistence.

(2) Failure recovery: When the master node has a problem, the slave node can provide services to achieve rapid failure recovery; in fact, it is a kind of service redundancy.

(3) Load balancing: On the basis of master-slave replication, with the separation of read and write, the master node can provide the write service, and the slave node can provide the read service (that is, the application connects to the master node when writing Redis data, and the application connects when reading Redis data. Slave nodes) to share the server load; especially in the scenario of writing less and reading more, sharing the read load by multiple slave nodes can greatly increase the concurrency of the Redis server.

(3) High-availability cornerstone: 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 Redis high availability.

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 the rdb operation), and the Master will also record all the commands to modify the data and cache them in the data file in.

(3) After the background process completes the cache operation, the Maste 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 the data. The operation is sent to the slave machine. 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 side machine, it sends its complete data file to the Slave side machine. If Mater receives synchronization requests from multiple slaves at the same time, the Master will start a process in the background to save The data file is then sent to all slave-side machines to ensure that all slave-side machines are normal.

4. Build Redis master-slave replication

(1) Environmental configuration

Host operating system IP Installation package
master cetos7 192.168.177.8 redis-5.0.7.tar.gz
slave1 centos7 192.168.177.11 redis-5.0.7.tar.gz
slave2 centos7 192.168.177.18 redis-5.0.7.tar.gz

(2) Install redis

systemctl stop firewalld
setenforce 0

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 [] /usr/local/redis/bin/redis-server  	

ln -s /usr/local/redis/bin/* /usr/local/bin/

(3) Modify the redis configuration file of the master node

vim /etc/redis/6379.conf
bind 0.0.0.0						#70行,修改bind 项,0.0.0.0监听所有网段
daemonize yes						#137行,开启守护进程
logfile /var/log/redis_6379.log		#172行,指定日志文件目录
dir /var/lib/redis/6379				#264行,指定工作目录
appendonly yes						#700行,开启AOF持久化功能

/etc/init.d/redis_6379 restart

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

(4) Modify the Redis configuration file of the slave node

vim /etc/redis/6379.conf
bind 0.0.0.0						#70行,修改bind 项,0.0.0.0监听所有网卡
daemonize yes						#137行,开启守护进程
logfile /var/log/redis_6379.log		#172行,指定日志文件目录
dir /var/lib/redis/6379				#264行,指定工作目录
replicaof 192.168.177.8 6379		#288行,指定要同步的Master节点IP和端口
appendonly yes						#700行,开启AOF持久化功能

/etc/init.d/redis_6379 restart

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

(5) Verify the master-slave effect

a. Look at the log on the Master node

tail -f /var/log/redis_6379.log 

Insert picture description here
b. Verify the slave node on the Master node

redis-cli info replication

Insert picture description here

Two, Redis sentinel mode

Note: On the basis of master-slave replication, Sentinel introduces automatic failover of the master node

1. The principle of sentinel mode

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

2. The role of sentinel mode

(1) Monitoring: The sentinel will constantly check whether the master node and slave node are operating normally.

(2) Automatic failover: When the master node fails to work normally, the 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 other slave nodes copy the new one Master node.

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

3. The structure of sentinel mode

(1) Sentinel node: The sentinel system consists of one or more sentinel nodes. The sentinel node is a special redis node and does not store data.
(2) Data node: Both the master node and the slave node are data nodes.
The startup of the sentinel depends on the master-slave mode, so you must install the master-slave mode before going to the sentinel mode. All nodes need to deploy the sentinel mode. The sentinel mode will monitor whether all Redis working nodes are normal. When the Master appears When there is a problem, because other nodes have lost contact with the master node, they will vote. More than half of the vote is considered that there is a problem with this Master, and then the sentry room will be notified, and then one of the Slaves will be selected as the new Master.

4. Build sentinel mode

(1) Environmental configuration

Host operating system IP Installation package
master centos7 192.168.177.8 redis-5.0.7.tar.gz
slave1 centos7 192.168.177.11 redis-5.0.7.tar.gz
slave2 centos7 192.168.177.18 redis-5.0.7.tar.gz

(2) Modify the redis configuration file (master-slave)

systemctl stop firewalld
setenforce 0

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

Insert picture description here
Insert picture description here

(3) Start sentinel mode (master and slave)

cd /opt/redis-5.0.7/
redis-sentinel sentinel.conf &

Insert picture description here
Insert picture description here
Insert picture description here

(4) Fault simulation

View redis-server process number

ps aux | grep redis

Insert picture description here
Kill the process ID of the redis-server on the Master node
Insert picture description here

(5) Verification result

tail -f /var/log/sentinel.log

Insert picture description here

redis-cli -p 26379 INFO Sentinel

Insert picture description here

Three, redis cluster mode (Redis Cluster)

1. The concept of cluster

The cluster is composed of multiple nodes (Node), 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 partition (or data sharding) is the core function of the cluster.
The cluster disperses data to multiple nodes. On the one hand, it breaks through the memory size limit of Redis single machine, 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 limitation is mentioned in the introduction of persistence and master-slave replication; for example, if the stand-alone memory is too large, the fork operation of bgsave and bgrewriteaof may cause the main process to block, and it may be possible when the host is switched in the master-slave environment 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.

3. Redis cluster data sharding

(1) Redis cluster introduces the concept of hash slots.
Redis cluster has 16384 hash slots (numbered 0-16383)
. Each node of the cluster is responsible for a part of the hash slots.
Each key is checked by CRC16 and then 16384 is taken to determine the remainder. Which hash slot to place, use this value to find the node corresponding to the corresponding slot, and then automatically jump to the corresponding node for access operations
(2) 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
(3) Redis cluster master-slave replication model
cluster has three nodes A, B, and C If node B fails, the entire cluster will be unavailable due to lack of slots in the range of 5461-10922.
Add a slave node A1, B1, and C1 for each node. The entire cluster consists of three Master nodes and three slave nodes. After node B fails, the cluster elects the master node with B1 as the main node to continue serving. When B and B1 both fail, the cluster will be unavailable
Insert picture description here

4. Build redis cluster mode

A redis cluster generally requires 6 nodes, 3 masters and 3 slaves. For convenience, all nodes are performed on the same server. Use port numbers to distinguish, the three master node port numbers are: 1701, 1702, 1703, and the corresponding slave node port numbers are 1704, 1705, 1706

cd /etc/redis/
mkdir -p redis-cluster/redis170{1..6}

for i in {1. .6}
do
cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis170$i
cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis170$i
done
 
#开启群集功能:
#其他5个文件夹的配置文件以此类推修改
cd /etc/redis/redis-cluster/redis1701
vim redis.conf
#bind 127.0.0.1         #69行,注释掉bind项,默认监听所有网卡
protected-mode no       #88行,修改,关闭保护模式
port 1701               #92行,修改redis监听端口
daemonize yes           #136行,开启守护进程,以独立进程启动
appendonly yes          #699行,修改,开启AOF持久化
cluster-enabled yes     #832行,取消注释,开启群集功能
cluster-config-file nodes-1701.conf    #840行,取消注释,群集名称文件设置
cluster-node-timeout 15000             #846行,取消注释群集超时时间设置

写一个for循环将1701的文件复制给1702~1706,
for i in {2..6}
do
/usr/bin/cp -f /etc/redis/redis-cluster/redis1701/redis.conf /etc/redis/redis-cluster/redis170$i/redis.conf
done
#之后稍微修改文件即可

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

for d in {1..6}
do
cd /etc/redis/redis-cluster/redis170$d
redis-server redis.conf
done

ps -ef | grep redis

#启动集群
redis-cli --cluster create 127.0.0.1:1701 127.0.0.1:1702 127.0.0.1:1703 127.0.0.1:1704 127.0.0.1:1705 127.0.0.1:1706 --cluster-replicas 1

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

#测试群集
redis-cli -p 1701 -c              #加-c参数,节点之间就可以互相跳转
127.0.0.1:1701> cluster slots     #查看节点的哈希槽编号范围

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

127.0.0.1:1701> cluster keyslot name    #查看name键的槽编号
(integer) 5798

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/tefuiryy/article/details/114302873