Redis master-slave mode and sentinel mode

Redis master-slave mode and sentinel mode

Master-slave mode

principle

Although Redis reads and writes very fast, it also produces a situation where the read pressure is extremely 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. 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.

Full sync

Redis full replication generally occurs during the initialization phase of the slave, and then the slave needs to copy all the data on the master. Specific steps are as follows:

  • The slave server connects to the master server and sends the SYNC command;
  • After the main server receives the SYNC name, it starts to execute the BGSAVE command to generate the RDB file and uses the buffer to record all write commands executed thereafter;
  • After the master server BGSAVE is executed, it sends the snapshot file to all slave servers, and continues to record the executed write commands during the sending;
  • After receiving the snapshot file from the server, discard all old data and load the received snapshot;
  • After the master server has finished sending the snapshot, it starts to send the write command in the buffer to the slave server;
  • The slave server finishes loading the snapshot, begins to receive command requests, and executes the write command from the buffer of the master server;

Incremental copy

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.

Master-slave synchronization strategy

Redis master-slave synchronization strategy When the master-slave is just connected, full synchronization is performed; after full synchronization is over, incremental synchronization is performed. Of course, if necessary, the slave can initiate full synchronization at any time. The redis strategy is that, in any case, it will first try to perform incremental synchronization. If it is unsuccessful, request the slave to perform full synchronization.

Deploy the redis environment first

  • Main ip: 20.0.0.10
  • From ip: 20.0.0.11 20.0.0.12 20.0.0.13

Unzip the package first (one master and three slaves at the same time)

tar zxvf redis-5.0.4.tar.gz
cd redis-5.0.4/
make
cd
ln -s /usr/local/redis/bin/* /usr/local/bin/
cd redis-5.0.4/utils/
./install_server.sh 
netstat -anpt | grep redis
cd

Operate on the main

[root@client1 ~]# vi /etc/redis/6379.conf 

70	bind 0.0.0.0 #修改监听地址为0.0.0.0(在实验环境),实际建议绑定从服务器IP地址
137	daemonize yes #开启守护进程
172	logfile /var/log/redis_6379.log #修改日志文件目录
264	dir /var/lib/redis/6379 #修改工作目录
700	appendonly yes #开启AOF持久化功能
[root@server1 ~]# /etc/init.d/redis_6379 restart #重启服务

Operate from above

和主上修改一致,需要多修改一个IP和端口
[root@client1 ~]# vi /etc/redis/6379.conf
287	replicaof 20.0.0.10 6379

Insert picture description here

View from the main
Insert picture description here

Sentinel mode

Sentinel mode configuration file

[root@client1 ~]# 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 20.0.0.11 6379 2  #至少几个哨兵检测到主服务器故障了,才会进行故障迁移
113	sentinel down-after-milliseconds mymaster 3000 #判定服务器down掉的时间周期,默认30000毫秒(30秒)
146	sentinel failover-timeout mymaster 100000 #故障节的的最大超时时间为180000180秒)
把配置文件直接拷贝:
scp redis-5.0.4/sentinel.conf root@20.0.0.11:/root/redis-5.0.4
scp redis-5.0.4/sentinel.conf root@20.0.0.12:/root/redis-5.0.4
scp redis-5.0.4/sentinel.conf root@20.0.0.13:/root/redis-5.0.4

Background running program (all PCs)

[root@client1 ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[1] 60069
[root@client2 ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[1] 47300
[root@client3 ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[1] 52210
[root@glt4 ~]# redis-sentinel redis-5.0.4/sentinel.conf &
[1] 59081
[root@client1 ~]# ps aux | grep sentinel
root      60070  0.2  0.2 153836  4688 ?        Ssl  15:34   0:00 redis-sentinel *:26379 [sentinel]
root      60083  0.0  0.0 112676   976 pts/1    S+   15:35   0:00 grep --color=autosentinel
[1]+  完成                  redis-sentinel redis-5.0.4/sentinel.conf

View sentinel mode

[root@client1 ~]# redis-cli -h 20.0.0.10 -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=20.0.0.10:6379,slaves=3,sentinels=4

Turn off the main service

[root@client1 ~]# ps -ef | grep redis
root      60070      1  0 15:34 ?        00:00:12 redis-sentinel *:26379 [sentinel]
root      60839      1  0 16:41 ?        00:00:00 /usr/local/bin/redis-server 0.0.0.0:6379
root      60853  16647  0 16:42 pts/1    00:00:00 grep --color=auto redis
[root@client1 ~]# kill -9 60839
[root@client1 ~]# redis-cli -h 20.0.0.10 -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=20.0.0.13:6379,slaves=3,sentinels=4

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_50346902/article/details/111476903