[Redis] Redis master-slave replication

foreword

Let's take a look at the master-slave replication of redis.
In order to improve the performance of redis, it is necessary to classify reading and writing, for example: one redis is responsible for writing, and other redis is responsible for reading. In this case, redis read-write separation is needed, namely: Master-Slave.

1. Configure master-slave replication

We take one master and two slaves (1 master, 2 slaves) as an example to carry out the master-slave configuration of redis. In reality, three machines are needed. In order to facilitate the configuration on one machine, we can realize master-slave replication.

1.1 Create a master-slave folder and copy redis.conf to this folder

Create a master-slave file, all operations are performed under this folder

mkdir /opt/master-slave

cp /usr/local/redis/redis-6.2.1/redis.conf /opt/master-slave/

insert image description here

1.2 Create a master configuration file

In order to distinguish, the configuration file of the master is named: redis-6379.conf. Modify the configuration file information as follows:

cp /opt/master-slave/redis.conf /opt/master-slave/redis-6379.conf
vim redis-6379.conf

#设置当前redis后台运行,其实是设置为守护线程
daemonize yes
#本机IP
bind 192.168.23.102
#配置密码
requirepass 123456
#持久化文件的目录
dir /opt/master-slave/
#日志文件名称
logfile /opt/master-slave/log_6379.log
#端口
port 6379
#rdb文件
dbfilename dump_6379.rdb
#pid文件
pidfile /var/run/redis_6379.pid

1.3 Create a slave configuration file

Named from redis: redis-6380.conf, redis-6381.conf. Modify the configuration information as follows:

cp /opt/master-slave/redis.conf /opt/master-slave/redis-6380.conf
vim redis-6380.conf


daemonize yes
bind 192.168.23.102
requirepass 123456
dir /opt/master-slave/
port 6380
dbfilename dump_6380.rdb
pidfile /var/run/redis_6380.pid
logfile /opt/master-slave/log_6380.log
#用来指定主机:旧版本:slaveof 主机ip 端口,新版本:replicaof 主机ip 端口 
replicaof 192.168.23.102 6379
#主机的密码
masterauth 123456

To configure redis-6381.conf, you only need to change the 6380 configured above to 6381.

1.4 start master/slave

redis-server /opt/master-slave/redis-6379.conf

redis-server /opt/master-slave/redis-6380.conf

redis-server /opt/master-slave/redis-6381.conf

After the 3 machines are started, 3 log files are generated, indicating that the machines are started normally.

1.5 View master/slave information

redis-cli -h 192.168.23.102 -p 6379 -a 123456

redis-cli -h 192.168.23.102 -p 6380 -a 123456

redis-cli -h 192.168.23.102 -p 6381 -a 123456

insert image description here

Check the master, you can see that its role is master and two slaves
insert image description here

Check the slave, its role is slave, master address, port and other information.

1.6 Verify master-slave replication

We write data on the master and read data on the slave.

set key1 val1
set key2 val2

insert image description here

Read data on slave:

mget key1 key2

insert image description here
insert image description here

2. The principle of master-slave replication

insert image description here

  1. After the slave successfully connects to the master, it sends a sync command to the master to request data synchronization.

  2. After receiving the message sent by the slave, the master persists the data of the master server to the RDB file, and at the same time collects the received command to modify the data, and the master transfers the RDB file to the slave to complete a full synchronization.

    Full synchronization: After the slave service receives the RDB file sent by the master, it saves it and loads it into the memory. Incremental
    synchronization: The master continues to pass the collected modification commands to the slave at one time to complete the synchronization

  3. Every time the slave connects to the master, it will perform a full synchronization.

summary:

  • After the main redis goes down, will the slave redis choose one as the main redis, or wait?
    From redis will continue to wait, unless the sentinel mode is set or manually switched
  • After the slave redis goes down, will the recovery continue to synchronize data from the main redis?
    Data will be synchronized. Every time redis connects to the master, a full synchronization will be completed, and then the master will transmit the modified command

3. Passing the torch

The passing of the torch of redis means: the slave can also act as the master, and then set the slave for data synchronization.

3.1 Flowchart

insert image description here

3.1.1 Add redis-6382.conf in /opt/master-slave/, and configure

cp /opt/master-slave/redis.conf /opt/master-slave/redis-6382.conf
vim redis-6382.conf

daemonize yes
bind 192.168.23.102
requirepass 123456
dir /opt/master-slave/
port 6382
dbfilename dump_6382.rdb
pidfile /var/run/redis_6382.pid
logfile /opt/master-slave/log_6382.log
#用来指定主机:旧版本:slaveof 主机ip 端口,新版本:replicaof 主机ip 端口 
replicaof 192.168.23.102 6380
#主机的密码
masterauth 123456

We changed the replicaof in the configuration file of 6382 to 6380, and 6380 is also a slave.

3.1.2 Connect to 6382, view configuration, and obtain data

redis-cli -h 192.168.23.102 -p 6382 -a 123456

info Replication

mget key1 key2

Execute query results:
insert image description here

At this time, the configuration is successfully passed on from generation to generation.
Note: If the slave is down as the master at this time, the slave under it will be in a waiting state.

4. Sentinel mode

There is an anti-customer-oriented phenomenon in redis, that is, when the master is down, one of the slaves will be selected as the master, and the master can only provide services as the slave after recovery.

4.1 Manual implementation

4.1.1 Select a slave and execute replicaof no one or slaveof no one

Executing replicaof no one (old version: slave no one) is to convert the current slave into a master. For example: we choose the above 6380 as the new master.

replicaof no one

info replication

insert image description here

4.1.2 Set replicaof/slave on other slaves and bind new master

Execute the command or modify the corresponding conf configuration file information. For example: we modify the previous 6381 and change its master to 6380.

replicaof 192.168.23.102 6380

info replication

insert image description here

After checking 6381, we found that his master has been changed to 6380.

4.1.3 Disadvantages

It is necessary to manually execute the command, which is inconvenient and has no practical effect.

4.2 Sentinel mode

Sentinel mode (Sentinel) is an anti-customer-based automatic version, which can monitor whether the master fails. After a failure occurs, a master will be selected from the slaves according to the number of votes, and the other slaves will automatically switch to the new master to achieve automatic failover.

4.2.1 Principle

insert image description here

  • Create an odd number of sentinels (Sentinel) and send ping requests to the master regularly
  • The master needs to make a normal response within the specified time. If more than half of the responses are abnormal, the master is considered to be down
  • Sentinel votes a slave as the new master, and other slaves that lose the master point to the new master

Note: In redis, multiple sentinels need to be selected, generally an odd number. If there is only one sentinel, the response from the master may not be received within the specified time due to network problems, resulting in misjudgment.

4.2.2 Implementation

4.2.2.1 Create a sentinel directory and copy sentinel.conf to the current directory

mkdir sentinel

cp /usr/local/redis/redis-6.2.1/sentinel.conf /opt/sentinel/

4.2.2.2 Copy three sentinel.conf and configure

The ports of the three sentinels we configured are 16380, 16381, and 16382 respectively.

cp sentinel.conf sentinel-16380.conf
vim sentinel-16380.conf


# 配置文件目录
dir /opt/sentinel/
# 日志文件位置
logfile "./sentinel-16380.log"
# pid文件
pidfile /var/run/sentinel_16380.pid
# 是否后台运行
daemonize yes
# 端口
port 16380
# 监控主服务器master的名字:mymaster,IP:192.168.200.129,port:6379,最后的数字2表示当Sentinel集群中有2个Sentinel认为master存在故障不可用,则进行自动故障转移
sentinel monitor mymaster 192.168.23.102 6380 2
# master响应超时时间(毫秒),Sentinel会向master发送ping来确认master,如果在60秒内,ping不通master,则主观认为master不可用
sentinel down-after-milliseconds mymaster 60000
# 故障转移超时时间(毫秒),如果3分钟内没有完成故障转移操作,则视为转移失败
sentinel failover-timeout mymaster 180000
# 故障转移之后,进行新的主从复制,配置项指定了最多有多少个slave对新的master进行同步,那可以理解为1是串行复制,大于1是并行复制
sentinel parallel-syncs mymaster 1
# 指定mymaster主的密码(没有就不指定)
# sentinel auth-pass mymaster 123456

For other port configurations, copy sentinel.conf and modify the port number.

4.2.2.3 start sentinel

redis-sentinel sentinel-16380.conf

redis-sentinel sentinel-16381.conf

redis-sentinel sentinel-16382.conf

insert image description here

4.2.2.4 View sentinel information

redis-cli -p sentinel的端口

info sentinel

insert image description here

From the execution results, we can see that the master monitored by sentinel is 192.168.23.102, two slaves, and three sentinels.

4.2.2.5 Verify sentinel

To verify sentinel, we only need to shut down the current master, wait for the time set by the failover, and then check the result.
1. Close the master

#查询redis信息
ps -ef|grep redis

#关闭master
kill -9 130516

insert image description here

2. View transfer results

insert image description here
insert image description here

From the results of the above query, it is found that 6382, which was originally in slave, has changed to master.

4.2.2.6 Restoring the old master

Reboot the old master6380

redis-server /opt/master-slave/redis-6380.conf 

insert image description here

From the execution results, we found that the old master is no longer the master, but has become a slave, and what he is listening to is the new master selected by sentinel.

Summarize

The above is the master-slave replication of Redis, I hope it will be helpful for everyone to understand redis. Thanks

Guess you like

Origin blog.csdn.net/u011397981/article/details/130972575