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

Example Analysis of Master-Slave replication, Sentinel and Cluster in Redis

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

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail the example analysis of master-slave replication, sentry and cluster in Redis. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. Redis master-slave replication

1. An overview of master-slave replication

Master-slave replication refers to copying the data from one server to another Redis server. The former is called master node (Master) and the latter is called slave node (Slave); data replication is one-way and can only be from master node to slave node.

By default, each Redis server is a master; and a master can have multiple slaves, but a slave can have only one master. [related recommendation: Redis video tutorial]

two。 The role of master-slave replication

● data redundancy: master-slave replication realizes the hot backup of data, which is a way of data redundancy besides persistence.

● fault recovery: when there is a problem with the master node, the slave node can provide services to achieve rapid fault recovery; in fact, it is a kind of service redundancy.

● load balancer: on the basis of master-slave replication, combined with read-write separation, the master node can provide write services, and the slave node can provide read services (that is, the slave node is connected to the master node when writing Redis data, and the slave node is connected to the slave node when reading Redis data). In the scenario of writing less and reading more, the concurrency of the Redis server can be greatly improved by sharing the read load among multiple slave nodes.

● high availability cornerstone: in addition to the above functions, master-slave replication is the basis on which sentinels and clusters can be implemented, so master-slave replication is the basis of Redis high availability.

3. The process of master-slave replication

(1) if you start a Slave machine process, it will send a "sync command" command to the Master machine to request a synchronous connection.

(2) whether it is the first time to connect or reconnect, the Master machine will start a background process to save the data snapshot to the data file (perform rdb operation). At the same time, Master will record all commands to modify the data and cache it in the data file.

(3) after the background process completes the caching operation, the Master machine will send the data file to the Slave machine. The server will save the data file to the hard disk, then load it into memory, and then the Master machine will send all the operations to modify the data to the server. If there is an outage caused by a failure of the Slave, it will automatically reconnect if it returns to normal.

(4) after receiving the connection from the server, the Master machine sends its complete data file to the server. If Master receives synchronization requests from multiple Slave at the same time, Master will start a process in the background to save the data file, and then send it to all Slave machines to ensure that all Slave machines are normal.

4. Build Redis master-slave replication

4.1 Server IP configuration

Server hostname IPMaster node master192.168.122.10Slave1 node slave1192.168.122.11Slave2 node slave2192.168.122.12

4.2 each server firewall environment

Systemctl stop firewalld & & systemctl disable firewalldsetenforce 0

4.3 install Redis on each server

For details of redis installation, please see the previous blog:

Detailed explanation of redis of NoSQL

Pass the installation package to the / opt directory yum install-y gcc gcc-c++ maketar zxvf redis-5.0.7.tar.gz-C / opt/cd / opt/redis-5.0.7/makemake PREFIX=/usr/local/redis installcd / opt/redis-5.0.7/utils./install_server.sh.Please select the redis executable path [] # enter / uar/local/redis/bin/redis-serverln-s / usr/local/redis/bin/* / usr/local/bin/

4.4 modify Redis configuration file (Master node operation)

Master:192.168.122.10

Line [root@master ~] # vim / etc/redis/6379.conf # 70, modify the listener address to 0.0.0.0, which means listening on any address bind 0.0.0.0, opening the daemon daemonize yes##172 line, specifying the log file directory logfile / var/log/redis_6379.log##264 line, specifying the working log dir / var/lib/redis/6379##700 line, and enabling the AOF persistence function appendonly yes

4.5 modify Redis configuration file (Slave node operation)

Slave1:192.168.122.11

[root@slave1 utils] # vim / etc/redis/6379.conf # 70 line, modify the listening address to 0.0.0.0, which means listening on any address bind 0.0.0.0. Open the daemon daemonize yes##172 line, specify the log file directory logfile / var/log/redis_6379.log##264 line, and specify the working log dir / var/lib/redis/6379##288 line. Add the Master node IP and port replicaof 192.168.122.10 6379synchronization 700 lines to be synchronized, and enable the AOF persistence function appendonly yes [root@slave1 utils] # / etc/init.d/redis_6379 restartStopping... Redis stoppedStarting Redis server...

Slave2:192.168.122.12

[root@slave2 utils] # vim / etc/redis/6379.conf # 70 line, modify the listening address to 0.0.0.0, which means listening on any address bind 0.0.0.0. Open the daemon daemonize yes##172 line, specify the log file directory logfile / var/log/redis_6379.log##264 line, and specify the working log dir / var/lib/redis/6379##288 line. Add the Master node IP and port replicaof 192.168.122.10 6379synchronization 700 lines to be synchronized, and enable the AOF persistence function appendonly yes [root@slave2 utils] # / etc/init.d/redis_6379 restartStopping... Redis stoppedStarting Redis server...

4.6 verify the master-slave effect

4.6.1 View the log on the Master node

[root@master] # tail-f / var/log/redis_6379.log 1002 Sep M 23 Sep 2021 16V 33.569 * Background saving terminated with success1002:M 23 Sep 2021 16V 33.569 * Synchronization with replica 192.168.122.11 V 6379 succeeded1002:M 23 Sep 2021 16V 34.519 * Replica 192.168.122.12V 6379 asks for synchronization1002:M 23 2021 16V 34.519 * Full resync requested by replica 192.168.122.12 23 Sep 2021 16 Sep 46 Background saving started by pid 34.519 * Starting BGSAVE for SYNC with target: disk1002:M 23 Sep 2021 16V 34.519 * Background saving started by pid 79417941 V C 23 Sep 2021 16V 34.521 * DB saved on disk7941:C 23 Sep 2021 16 V 34.521 * RDB: 0 MB of memory used by copy-on-write1002:M 23 Sep 2021 16V 34.591 * Background saving terminated with success1002:M 23 Sep 2021 16V 34.591 * Synchronization with replica 192.168.122.12V 6379 succeeded

4.6.2 verify the slave node at the Master node

[root@master] # redis-cli info replication# Replicationrole:masterconnected_slaves:2slave0:ip=192.168.122.11,port=6379,state=online,offset=910,lag=0slave1:ip=192.168.122.12,port=6379,state=online,offset=910,lag=0master_replid:9d7fa17fc64cd573f5b81457183831d97dfad7dcmaster_replid2:0000000000000000000000000000000000000000master_repl_offset:910second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen: 910II, Redis Sentinel Mode

1. Core functions of Sentinel Mode

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

two。 The principle of Sentinel Mode

Sentinel is a distributed system that monitors each server in the master-slave structure, selects a new Master through a voting mechanism and connects all Slave to the new Master when a failure occurs. Therefore, the number of clusters running the Sentinel must not be less than 3 nodes.

3. The role of Sentinel Mode

● monitoring: the Sentinel will constantly check whether the master node and slave node are functioning properly.

● automatic failover: when the master node does not work properly, the sentry starts an automatic failover operation, which upgrades one of the slaves of the failed master node to the new master node and allows the other slave nodes to copy the new master node.

● notification reminder: the sentry can send the result of the failover to the client.

4. The structure of the Sentinel model

The sentinel structure consists of two parts, the sentinel node and the data node:

● Sentinel Node: the Sentinel system consists of one or more nodes. The Sentinel node is a special redis node that does not store data.

● data nodes: both master and slave nodes are data nodes.

5. The working form of the Sentinel mode

The start-up of the Sentinel depends on the master-slave mode, so you must install the master-slave mode before doing the Sentinel mode. Sentinel mode needs to be deployed on all nodes. Sentinel mode will monitor whether all Redis working nodes are normal. When there is a problem with the Master, because other nodes have lost contact with the master node, they will vote, and more than half of the vote will assume that the Master is indeed a problem, and then notify the sentinel. Then select one from the Slaves as the new Master.

6. Failover mechanism

Regular monitoring by the sentinel node to find out whether the primary node is malfunctioning. Every second, each sentinel node sends a ping command to the master node, slave node and other sentinel nodes for heartbeat detection. If the master node does not reply or reply to an error message within a certain period of time, then the sentry will think that the master node has been subjectively offline (unilaterally). When more than half of the Sentinel nodes think that the master node is subjectively offline, it is objectively offline.

When the primary node fails, the sentinel node will use the Raft algorithm (election algorithm) to implement the election mechanism to jointly elect a sentry node as leader, which is responsible for handling the failure and notification of the primary node. Therefore, the number of hosts in the Sentinel cluster must not be less than three nodes.

The failover is performed by the leader Sentinel node as follows:

● upgrades a slave node to a new master node so that other slave nodes point to the new master node

● becomes a slave node if the original master node is restored, and points to the new master node.

● notifies the client that the primary node has been replaced.

It should be noted that objective offline is the only concept of the master node; if the slave node and the sentry node fail, after being subjectively offline by the sentry, there will be no subsequent objective offline and failover operations.

7. Election of the master node

Filter out unhealthy (offline) slave nodes that do not respond to Sentinel ping responses.

Select the one with the highest slave node priority in the configuration file (replica-priority, the default is 100).

Select the slave node with the largest replication offset, that is, the most complete copy.

8. Build Redis Sentinel mode

8.1 Server IP configuration

Server hostname IPMaster node master192.168.122.10Slave1 node slave1192.168.122.11Slave2 node slave2192.168.122.12

8.2 Firewall environment for each server

Systemctl stop firewalld & & systemctl disable firewalldsetenforce 0

Modify the configuration file for Redis Sentinel mode (all node operations)

Line vim / opt/redis-5.0.7/sentinel.conf # # 17, uncomment, close protected mode protected-mode no##21 line, default listening port of Redis Sentinel port 26379launch line 26, specify sentienel as background startup daemonize yes##36 line, specify log storage path logfile "/ var/log/sentinel.log" # 65 line, specify database storage path dir "/ var/lib/redis/6379" # 84 line, modify Specify that the sentinel node monitors the master node 192.168.122.10 6379. The name of the master node is mymaster##. The meaning of the last 2 is related to the fault diagnosis of the master node. At least two sentinel nodes are required to agree to determine the fault and fail over line 113 of sentinel monitor mymaster 192.168.122.10 6379, and determine the time period that the server down dropped. The default is 30000 milliseconds (30 seconds) sentinel down-after-milliseconds mymaster 30000 seconds 146lines, and the maximum timeout time of the failed node is 180000 milliseconds (180s) sentinel failover-timeout mymaster 180000

8.4 start Sentinel mode

Note: master needs to be started before slave

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

8.5 View Sentinel Information

Master:192.168.122.10

[root@master redis-5.0.7] # redis-cli-p 26379 info sentinel# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.122.10:6379,slaves=2,sentinels=3 III, Redis Cluster Mode

1. Overview of Redis clusters

Clustering, or Redis Cluster, is a distributed storage scheme introduced by Redis 3.0.

2. Redis cluster

The cluster consists of multiple nodes (Node) in which Redis data is distributed. The nodes in the cluster are divided into master node and slave node; only the master node is responsible for the maintenance of read and write requests and cluster information; the slave node only replicates the master node data and state information.

3. The function of Redis cluster

The role of clusters can be summarized into two points:

3.1 data Partition

Data partitioning (or data fragmentation) is the core function of the cluster.

The cluster distributes the data to multiple nodes, on the one hand, it breaks through the limit of the memory size of the Redis single machine, and the storage capacity is greatly increased; on the other hand, each main node can provide read and write services, which greatly improves the response ability of the cluster.

Due to the limited memory size of Redis standalone, there is a volume when introducing 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 master process to block, the master switching in the master-slave environment may cause the slave node to be unable to provide services for a long time, and the replication buffer of the master node may overflow during the full replication phase.

3.2 High availability

The cluster supports master-slave replication and automatic failover of master nodes (similar to sentinels); when any node fails, the cluster can still provide services.

4. Data fragmentation of Redis cluster

● Redis cluster introduces the concept of hash slot

The ● Redis cluster has 16384 hash slots (numbered 0-16383)

Each node of the ● cluster is responsible for part of the hash slot

Each key of ● decides which hash slot to place by taking the remainder of 16384 after CRC16 verification, through this value, to find the corresponding node, and then jump directly to the corresponding node for access operation.

5. Hash slot

5.1 allocation of hash slots

Hash slots can be evenly distributed according to the number of cluster hosts (default allocation)

Take a cluster composed of three nodes as an example:

Node A contains hash slot no. 0-5460

Node B contains hash slots 5461-10922

Node C contains hash slots 10923-16383

You can also customize the allocation according to the performance and function of the host.

Take a cluster composed of three nodes as an example:

Node A has the worst performance, including hash values of 0-2000

Node B has medium performance, including hash values of 2001-7000

Node C has the best performance, including hash values of 7001-16383

5.2 use of hash slots

When setting up a cluster, you need to assign slots to the nodes of the cluster, 0# 16383

Execute set an an in node1

Use the crc16 algorithm to calculate the key, get a number, and then make a remainder of 16384 (crc16: a = 26384l% 16384 = 10000)

Find the node that contains 10000 slots, find node2, and automatically jump to node2

Execute the set an a command on node2

Execute get an on node3

A-> 10000

Jump to node2

Execute get an in node2

6. Master-slave replication model of Redis cluster

There are three nodes A, B and C in the cluster. If node B fails, the whole cluster will be unavailable due to the lack of slots in the range of 5461-10922.

Adding a slave node A1, B1, C1 to each node, the whole cluster consists of three Master nodes and three Slave nodes. After the failure of node B, the cluster elects B1 bit to continue to serve the new master node. When both B and B1 fail, the cluster will not be available.

7. Set up Redis cluster mode

7.1Server IP configuration

Redis clusters generally need 6 nodes, 3 masters and 3 slaves. For convenience, it is simulated on the same server

Distinguished by the port number, the port number of the three master nodes is 6001Universe 6002Universe 6003, and the corresponding slave node port number 6004Universe 6005Universe 6006.

Server hostname IP master port slave port Node1 node node192.168.122.1060016004Node2 node node192.168.122.1060026005Node3 node node192.168.122.1060036006

7.2 Server firewall environment

Systemctl stop firewalld & & systemctl disable firewalldsetenforce 0

7.3 create cluster configuration directories and files

[root@node ~] # cd / etc/redis [root@node redis] # mkdir-p redis-cluster/redis600 {1.. 6} [root@node redis] # for i in {1.. 6} > do > cp / opt/redis-5.0.7/redis.conf / etc/redis/redis-cluster/redis600 $I > cp / opt/redis-5.0.7/src/redis-cli / opt/redis-5.0.7/src/redis-server / etc/redis/redis-cluster/redis600 $I > done [root@node redis] # ls-R redis-cluster/redis-cluster/:redis6001 redis6002 redis6003 redis6004 redis6005 redis6006 redis-cluster/redis6001:redis-cli redis.conf redis-server redis-cluster/redis6002:redis-cli redis.conf redis-server redis-cluster/redis6003:redis-cli redis.conf redis-server redis-cluster/redis6004:redis-cli redis.conf redis-server redis-cluster/redis6005:redis-cli redis.conf redis-server redis-cluster/redis6006:redis-cli redis.conf redis-server

7.4 enable clustering

Just take redis6001 as an example, the configuration files of the other five folders are modified and so on, paying special attention to the modification of the port number.

Line [root@node redis] # cd redis-cluster/redis6001 [root@node redis6001] # vim redis.conf # # 69, comment out the bind entry, default listen on all network cards # bind 127.0.0.1 listen for line 88, modify, turn off protected mode protected-mode no##92 line, modify, redis listen port port 6001 listen for line 136, open daemon, start daemonize yes##832 line with independent process, uncomment, open cluster function cluster-enabled yes##840 line Logout comment, cluster name file setting cluster-config-file nodes-6001.conf##846 line, logout comment, cluster timeout setting cluster-node-timeout 15000 timeout 700 lines, modify, turn on AOF persistence appendonly yes

7.5 start the redis node

Enter each of the six folders and execute the command: "redis-server redis.conf" to start the redis node

[root@node redis6001] # for d in {1.. 6} > do > cd / etc/redis/redis-cluster/redis600 $I > ^ C [root@node redis6001] # for d in {1... 6} > do > cd / etc/redis/redis-cluster/redis600 $d > redis-server redis.conf > done [root@node1 redis6006] # ps-ef | grep redisroot 992 10 13:45? 00:00:07 / usr/local/redis/bin/redis-server 0.0.0.0: 6379root 2289 10 14:41? 00:00:00 redis-server *: 6001 [cluster] root 2294 10 14:41? 00:00:00 redis-server *: 6002 [cluster] root 2299 10 14:41? 00:00:00 redis-server *: 6003 [cluster] root 2304 10 14:41? 00:00:00 redis-server *: 6004 [cluster] root 2309 10 14:41? 00:00:00 redis-server *: 6005 [cluster] root 2314 10 14:41? 00:00:00 redis-server *: 6006 [cluster] root 2450 2337 0 14:50 pts/0 00:00:00 grep-- color=auto redis

7.6 start the cluster

[root@node redis6006] # redis-cli-- cluster create 127.0.0.1 cluster create 6001 127.0.1 cluster-replicas 1

The six instances are divided into three groups, each with one master and one slave, with the front as the master node and the latter as the slave node. You need to enter yes for the following interaction before you can successfully create it.

-replicas 1 means that each master node has 1 slave node.

7.7 Test Cluster

[root@node1 redis6006] # redis-cli-p 6001-c # plus-c parameter Before the nodes can jump to 127.0.0.1 cluster slots# to view the node's hash slot number range 1) 1) (integer) hash slot start number 2) (integer) 546 "hash slot termination number 3) 1)" 127.0.0.1 "2) (integer) 6001#node node main 3)" 18e59f493579facea29abf90ca4050f566d66339 "4) 1)" 127.0.0. 1 "2) (integer) 6004#node node from 3)" 2635bf6a0c286ef910ec5da03dbdc7cde308c588 "2) 1) (integer) 10923 2) (integer) 16383 3) 1)" 127.0.0.1 "2) (integer) 6003 3)" 51460d417eb56537e5bd7e8c9581c66fdd817b3c "4) 1)" 127.0.0.1 "2) (integer) 6006 3)" 51a75667dcf21b530e69a3242a3e9f81f577168d "3) 1) (integer) 5461 2 ) (integer) 10922 3) 1) "127.0.0.1" 2) (integer) 6002 3) "6381d68c06ddb7ac43c8f7d7b8da0644845dcd59" 4) 1) "127.0.0.1" 2) (integer) 6005 3) "375ad927116d3aa845e95ad5f0586306e7ff3a96" 127.0.1 6381d68c06ddb7ac43c8f7d7b8da0644845dcd59 6001 > set num 1OK127.0.0.1:6001 > get num "1" 127.0.0.1 6381d68c06ddb7ac43c8f7d7b8da0644845dcd59 6001 > keys * 1) "num" 127.0.0. 1redis-cli 6001 > quit [root@node1 redis6006] # redis-cli-p 6002-c127.0.0.1 redis-cli 6002 > keys * # 6002 Port keyless value pair (empty list or set) 127.0.0.1 redis-cli 6002 > get num- > Redirected to slot [2765] Port located at 127.0.0.1 redis-cli 6001 "1" # 6002 Port get the num key at port 6001 Switch to port 6001 and display the key value 127.0.0.1 set key1 6001 > Redirected to slot 11111-> Redirected to slot [9189] located at 127.0.0.1:6002OK#6001 port to create a key value pair, save it to port 6002, and switch to port 6002 127.0.0.1 located at 127.0.0.1:6002OK#6001 6002 > this article on "example Analysis of Master-Slave replication, Sentinel and Cluster in Redis" ends here. I hope the above content can be of some help to you. So that you can learn more knowledge, if you think the article is good, please share it for more people to see.

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