Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Master and Slave of redis and Sentinel deployment

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Deployment of stand-alone radis 1, installation of gccyum install gcc2, installation of redistar-zxvf redis-3.2.9.tar.gz-C / usr/src/cd / usr/src/redis-3.2.9/makecd src & & make install3, creation of redis directory to store commands and configuration files mkdir-p / usr/local/redis/ {etc Bin} 4, move the file cd / usr/src/redis-3.2.9/mv redis.conf / usr/local/redis/etccd srcmv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel redis-trib.rb / usr/local/redis/bin5, configure the environment variable echo 'PATH=$ {PATH}: / usr/local/redis/bin/' > > / etc/profilesource / etc/profile6, start the service

To start the redis service, you need to specify the configuration file, and if you start it in the background, you need to modify the redis.conf file, daemonize no-> daemonize yes. The default link port on the redis server is 6379, and it's best to bind IP to native IP as well.

Redis-server / usr/local/redis/etc/redis.conf 7, client connection redis-cli-h 192.168.118.137-p 63798, Stop redis service 192.168.118.137 ps > shutdown or: [root@node1 bin] # ps-ef | grep redisroot 49245 2816 0 10:39 pts/1 00:00:00 redis-server 127.0.0.1:6379root 49559 2816 0 10:59 pts/1 00:00:00 grep-- color=auto redis [root@node1 bin] # kill-9 49245 II, Master-Slave replication 1, replication brief

The replication of redis can realize that when the data in one database is updated, the new data is automatically copied to another database; the replication process itself is asynchronous, that is, when a client executes a write command to master, master immediately returns the result to the client after executing the command, and asynchronously synchronizes the command to slave, instead of waiting for slave to receive the command and return the result to the client. This is an optimistic replication strategy, that is, tolerate master-slave data inconsistency within a certain period of time, but the two will eventually synchronize data. Redis uses optimistic replication (optimistic replication).

2. Master and subordinate

Redis is divided into two categories, one is the master database (master), the other is the slave database (slave). Master can read and write data, and when new data is written, copying data to slave,slave according to the settings of the configuration file is read-only, so generally, the write operation for the database is on master, and a large number of IO read operations are on slave.

3. The principle of master-slave replication.

Let's use a diagram to illustrate the process of redis master-slave replication.

Schematic diagram of Redis master-slave replication process

As can be seen from the diagram above, after the master server establishes a connection with the slave server, the Redis master-slave replication process mainly has the following steps:

(1) the slave server will send a SYNC command to the master server.

(2) after receiving the SYNC command, the master server starts a backstage subprocess and starts to execute BGSAVE, and saves all new write commands to a buffer during the execution of the save operation.

(3) when the BGSAVE is completed, the master server sends the .rdb file obtained from the save operation to the slave server, receives the .rdb file from the server, and loads the data in the file into memory.

(4) the master server sends all the accumulated contents in the write command buffer to the slave server in the format of Redis command protocol.

From the master-slave replication process above, there are two problems: one is that the BGSAVE operation is a complete copy when the master-slave is disconnected, and the other is that each BGSAVE operation master will save the RDB file on the hard disk. Based on these two points, versions later than 2.8 have introduced hard disk-free replication and incremental replication.

No hard disk replication:

The replication of redis is based on the persistence of RDB mode, that is, after master receives the SYNC, it saves the RDB snapshot file, and copies the rdb file to slave. This way in the copy initialization need to create RDB files on the hard disk, when the hard disk performance becomes low, it will lead to a decline in replication performance, so no hard disk replication is that master no longer saves RDB files on the hard disk, but directly transfers the RDB files to slave on the network.

Incremental replication:

When the master and slave database is disconnected, when slave sends SYNC, master will perform a complete copy operation, and the data of the slave database may always be basically the same. Even so, the complete rdb file should be transferred to slave;. In this case, incremental replication can prevent full replication of data every time the master and slave are interrupted, reducing resource consumption and improving efficiency. When set to incremental replication, slave sends PSYNC commands to master.

4. The structure of master-slave replication

The most basic redis master and slave is to have one master database and N slave databases.

Not only the master server can have a slave server, but also the slave server can have its own slave server.

Master in the figure will synchronize data to slave-master1 and slave1,slave-master1 will synchronize data to slave2 and slave3, and writing data to slave-master1 will only be synchronized to slave2 and slave3, not to master and slave1.

Master-slave deployment 1. Deployment from database

Repeat the first step and copy redis.conf to redis-repl.conf

Redis is very simple to configure a slave server, as long as you add the IP address and port number of the master server to the configuration file redis-repl.conf of the slave server. If the master server sets the client password, you also need to configure the password of the master server in the slave server, as follows

Slaveof 192.168.118.137 6379

# masterauth 123456

2. Start the slave server

Redis-server / usr/local/redis/etc/redis-repl.conf

3. Master-slave verification

Master can write, slave can look up

From unwritable

View status on slave

IV. Redis-sentinel

In an one-master and multi-slave redis system, slave plays the role of data redundancy backup and read-write separation in the whole system. When the master encounters a failure, we need to manually switch the slave to master, and we need to move the write operation to the new master, so the write data will be lost during this period. For this reason, redis provides a sentinel mechanism after version 2.8.As the name implies. The role of the Sentinel is to monitor the master-slave database as well as the Sentinel itself, and automatically switch over in the event of a master failure.

Sentinel schematic diagram

This is a schematic diagram of a single sentinel monitoring. There can be multiple sentinels, and the sentinels monitor each other.

Example of Sentinel configuration:

1. Configure stand-alone multi-port master-slave Master: 192.168.118.141:6379Slave1:192.168.118.141:6380Slave2:192.168.118.141:63812, launch master-slave [root@node1 etc] # redis-server / usr/local/redis/etc/redis.conf [root@node1 etc] # redis-server / usr/local/redis/etc/redis-6380.conf [root@node1 etc] # redis-server / usr/local/redis/etc/redis-6381.conf 3, Check the master-slave status [root@node1 ~] # redis-cli-h 192.168.118.141-p 6379192.168.118.141 info replication# Replicationrole:masterconnected_slaves:2slave0:ip=192.168.118.141 Port=6380,state=online,offset=85,lag=1slave1:ip=192.168.118.141,port=6381,state=online,offset=85,lag=1master_repl_offset:85repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:844, prepare Sentinel profile [root@node1 etc] # cat sentinel.conf sentinel monitor mymaster 192.168.118.141 6379 15, Sentinel profile mymaster: name of the monitoring master database Custom 192.168.118.141: monitoring master database IP6379: monitoring master database port 1: indicates that at least several Sentinel nodes are required to agree before performing a fault recovery operation. 6. Open Sentinel [root@node1 etc] # redis-sentinel sentinel.conf 60725 redis-sentinel sentinel.conf X 14 Mar 23 redis-sentinel sentinel.conf 1315 * Increased maximum number of open files to 10032 (it was originally set to 1024). _. _.-`. `_. '' -. _ Redis 3.2.9 (00000000Universe 0) 64 bit.-``.- ```. ```\ / _., _'-. _ (.-`|` ) Running in sentinel mode | `-. _`...-.`` -. _ |'`_. -'| Port: 26379 |` -. _ `. _ / _. -'| PID: 60725` -. _ `-. _`. / _. -'_. -'| `-. `-. _` -'_. | | `-. _` -. _ _. -'_. -'| http://redis.io `-. _` -. _ _. -'. | | `-. _` -. _ `. -'_. -'| |` -. _ `-. -'_. -'. _. -'`-. _`. -'. 60725 cannot be enforced because / proc/sys/net/core/somaxconn is set to the lower value of 128.60725 cannot be enforced because / proc/sys/net/core/somaxconn is set to the lower value of 128.60725 The TCP backlog setting of 14 Mar 23 The TCP backlog setting of 53.925 # Sentinel ID is fe3fb6bef25af9641e46f40f76a0e00fd491acbb60725:X 14 Mar 23 13 Mar 53.925 + monitor master mymaster 192.168.118.141 6379 quorum 160725 14 Mar 2313 Mar 133.927 * + slave slave 192.168.118.141 6380 @ mymaster 192.168.118.141 637960725 slave slave 192.168.118.141 637960725 slave slave 192.168.118.141 6381 @ mymaster 192.168.118.141 63797, Close the main database: Sentinel automatically switches master [root@node1 ~] # redis-cli-h 192.168.118.141-p 6379192.168.118.141redis-cli 6379 > shutdownnot connected > [root@node1 ~] # redis-cli-h 192.168.118.141-p 6380192.168.118.141pur6380 > info replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.118.141 Port=6381,state=online,offset=0,lag=1master_repl_offset:1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:0

Reference article:

Redis official: https://redis.io/documentation

Rookie tutorial: http://www.runoob.com/redis/redis-tutorial.html

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report