Introduction to Redis master-slave replication and sentinel mode

1.Redis master-slave replication

1.1 Overview

  • Redis, although the speed of reading and writing is very fast, but it will also produce a situation where the reading pressure is very high. In order to share the read pressure, Redis supports master-slave replication to ensure that the data content of the master database is completely consistent with the content of the slave database.

1.2 Redis structure classification

  • The master-slave structure of Redis can adopt a master-multi-slave or cascade structure,
  • Redis master-slave replication can be divided into full synchronization and incremental synchronization according to whether it is full.

Insert picture description here

1.2.1 Redis full synchronization

  • Redis full replication generally occurs during the initialization phase of the slave. At this time, the slave needs to copy all the data on the master.
  • Specific steps are as follows:
从服务器连接主服务器,发送SYNC命令;
主服务器接收到sYNC命名后,开始执行BGSAVE命令生成RDB文件并使用缓冲区记录此后执行的所有写命令;
主服务器BGSAVE执行完后,向所有从服务器发送快照文件,并在发送期间继续记录被执行的写命令;
从服务器收到快照文件后丢弃所有旧数据,载入收到的快照;
主服务器快照发送完毕后开始向从服务器发送缓冲区中的写命令;
从服务器完成对快照的载入,开始接收命令请求,并执行来自主服务器缓冲区的写命令。

Insert picture description here

1.2.2 Redis incremental replication

  • Redis incremental replication refers to the process of synchronizing write operations from the master server to the slave server when the slave is initialized and starts to work normally. ·

  • The process of incremental replication is mainly that the master server sends the same write command to the slave server every time it executes a write command, and the slave server receives and executes the received write command.

1.3 master-slave synchronization strategy

  • When the master and slave are just connected, perform full synchronization;
  • After the full synchronization is over, perform incremental synchronization;
  • Of course, if necessary, the slave can initiate full synchronization at any time. The redis strategy is to try incremental synchronization first anyway; if unsuccessful, the slave is required to perform full synchronization.

2. Build Redis master-slave replication

  • Project environment
一台mater主服务器
IP:192.168.140.20
两台slave备选服务器
IP:192.168.140.21
IP:192.168.140.22

2.1 Open the redis_server service on the server

Note: The redis service needs to be turned on on the primary and standby servers

发送所有会话,关闭防火墙
systemctl stop firewalld
setenforce 0
  • Import the following software packages on all three servers and place them in the root directory
    Insert picture description here

2.1.1 Unzip and install

[root@master ~]# tar zxvf redis-5.0.4.tar.gz
[root@master ~]# cd redis-5.0.4/
[root@master redis-5.0.4]# make
[root@master redis-5.0.4]# make PREFIX=/usr/local/redis install
[root@master redis-5.0.4]# cd

2.1.2 Create a link file and start the service

[root@master ~]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@master ~]# cd redis-5.0.4/utils/
[root@master utils]# ./	//按table 查看相关命令
create-cluster/           graphs/                   hyperloglog/              lru/                      redis_init_script.tpl     speed-regression.tcl
generate-command-help.rb  hashtable/                install_server.sh         redis_init_script         releasetools/             whatisdoing.sh
[root@master utils]# ./install_server.sh
...//弹出的信息,按回车即可
[root@master utils]# netstat -anpt | grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      60219/redis-server 

Insert picture description here

2.2 Modify the configuration file

  • On the main server
[root@master ~]# vi /etc/redis/6379.conf 
...
69	bind 0.0.0.0	//修改监听地址为 0.0.0.0 (在实验环境使用),现网环境建议绑定从服务器IP地址
136	daemonize yes	//开启守护进程
171	logfile /var/log/redis_6379.log	//修改日志文件目录
263	dir /var/lib/redis/6379	//修改工作目录
699	appendonly yes	//开启AOF持久化功能
  • On the alternate server, first enable the same functions as the main server, and then perform the following configuration
[root@slave1 ~]# vi /etc/redis/6379.conf 
...
replicaof 192.168.140.20 6379	//开启复制功能
[root@slave1 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
  • View logs on the main server
    Insert picture description here

2.2 Verify the slave node on the master server

  • Connect to the database and edit content on the main server
[root@master ~]# redis-cli		//连接数据库
127.0.0.1:6379> info replication	//查看节点信息
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.140.21,port=6379,state=online,offset=448,lag=1
slave1:ip=192.168.140.22,port=6379,state=online,offset=448,lag=1
master_replid:8e6ded5a687920d545a8133c8c0093d8e962056d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:448
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:448
127.0.0.1:6379> set cp 9	//写入内容,并验证
OK
127.0.0.1:6379> get cp
"9"

Insert picture description here

  • Verify the edited content on the master server from the server
[root@slave1 ~]# redis-cli
127.0.0.1:6379> get cp
"9"
127.0.0.1:6379> 

2.3 Verification results

  • When the master node goes down
[root@master ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped

(1) View the log on the slave node
Insert picture description here
(2) View on the server whether the edited content on the master server exists

[root@slave1 ~]# redis-cli
127.0.0.1:6379> get cp
"9"
127.0.0.1:6379> 

Insert picture description here

  • At this time, it means that the redis master-slave is successfully built

  • Conclusion: Redis master-slave replication, when the master server fails, the standby server will not be automatically switched. Although reading data is normal, writing data will cause problems.

3. Sentry mode

注意:哨兵模式,一定是基于redis主从复制模式下做的

3.1 Principles 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 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.

3.2 The role of sentinel mode

  • Monitoring
    Continuously check whether master and slave are running normally;
    master survival detection, master and slave running status detection.

  • Notification (Reminder)
    When the monitored server has a problem, send a notification to other (sentinel room, client).

  • Automatic failover
    Disconnect the master and slave, select one slave as the master, connect other slaves to the new master, and inform the client of the new server address

Supplement: Sentinel is also a redis server, but does not provide data services

3.3 Start of Sentry Mode

  • The startup of the sentry depends on the master-slave mode, so the sentry mode must be installed when the master-slave mode is installed. The sentry mode needs to be deployed on all nodes;
  • The sentinel mode will monitor whether all redis working nodes are normal. When the master has a problem, because other nodes lose contact with the master node, it will vote. After the vote is half of the vote, it is considered that the master does have a problem, and then the sentry room will be notified, and then from Select one of the slaves as the new master

3.4 Sentinel configuration

3.4.1 Edit configuration file

[root@master ~]# vi redis-5.0.4/sentinel.conf
17	protected-mode no		//关闭保护模式
26	daemonize yes		//指定sentinel为后台启动(开启守护进程)
36	logfile "/var/log/sentinel.log"	//指定日志存放路径
65	dir /var/lib/redis/6379		//指定数据库存放路径
84	sentinel monitor mymaster 192.168.140.20 6379 2	
		//至少几个哨兵检测到主服务器故障了,才会进行故障迁移(当主服务器故障,切换到slave为主服务器时,会将地址改为一个ID号)
113	sentinel down-after-milliseconds mymaster 3000	//判定服务器down掉的时间周期,默认30000毫秒(30S)
146	sentinel failover-timeout mymaster 180000	//故障节点的最大超时时间为180000(180秒)

3.4.2 Copy configuration file to slave server

[root@master ~]# scp redis-5.0.4/sentinel.conf root@192.168.140.21:/root/redis-5.0.4

Insert picture description here

3.4.3 Start Sentry Mode

先启动master,再启slave
[root@master ~]# redis-sentinel redis-5.0.4/sentinel.conf &	//&表示在后台启动
[1] 63649

[root@slave1 ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[1] 62327
[root@slave2 ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[1] 60481

3.4.4 View sentinel information

  • View log
[root@master ~]# tail -f /var/log/sentinel.log

Insert picture description here

  • View process
[root@master ~]# ps aux | grep sentinel
root      63650  0.2  0.1 153836  2668 ?        Ssl  17:23   0:01 redis-sentinel *:26379 [sentinel]
root      63739  0.0  0.0 112676   980 pts/1    S+   17:31   0:00 grep --color=auto sentinel
[root@master ~]# ps aux | grep redis
root      62860  0.1  0.1 156396  2800 ?        Ssl  16:03   0:06 /usr/local/bin/redis-server 0.0.0.0:6379
root      63650  0.2  0.1 153836  2668 ?        Ssl  17:23   0:01 redis-sentinel *:26379 [sentinel]
root      63742  0.0  0.0 112676   984 pts/1    S+   17:31   0:00 grep --color=auto redis

Insert picture description here

  • View sentry status
[root@master ~]# redis-cli -h 192.168.140.20 -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.140.20:6379,slaves=2,sentinels=3
[root@master ~]# redis-cli -h 192.168.140.20 -p 6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.140.21,port=6379,state=online,offset=174385,lag=0
slave1:ip=192.168.140.22,port=6379,state=online,offset=174385,lag=0
master_replid:75cd6e32461f20e72b1ae7a09bffff02803fb9f9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:174385
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:174385

3.5 Simulate failure

  • Stop the redis service on the main server
[root@master ~]# /etc/init.d/redis_6379 stop	 //停止redis服务
也可以用 kill 命令
kill -9 进程号

3.6 Verification results

  • View the log on the slave server
[root@slave1 ~]# tail -f /var/log/sentinel.log 

Insert picture description here

[root@slave2 ~]# tail -f /var/log/sentinel.log 

Insert picture description here

[root@slave2 ~]# redis-cli -h 192.168.140.22 -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.140.22:6379,slaves=2,sentinels=3

Insert picture description here

  • When the master server master restarts,
    check the log and the sentinel status and find that the master does not preempt the slave server
[root@master ~]# /etc/init.d/redis_6379 start

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42449832/article/details/111353841