当前位置:网站首页>Redis master-slave replication, sentinel mode and cluster
Redis master-slave replication, sentinel mode and cluster
2022-06-11 23:55:00 【qq_ fifty-two million eight hundred and twenty-five thousand si】
Catalog
One 、Redis Master-slave replication of
1、 Overview of master-slave replication
2、 Redis The role of master-slave replication
3、 Redis Master slave replication process
4、Redis The construction of master and slave
1、 The principle of sentinel mode
3、 The construction of sentry mode
3、 ... and 、Redis The cluster of
2、 Fragmented data in cluster mode
One 、Redis Master-slave replication of
1、 Overview of master-slave replication
Master slave copy , It means to put one Redis Server data , Copy to other Redis The server . The former is called the main node (Master), The latter is called the slave node (Slave); Data replication is one-way , From master to slave only .
By default , Each station Redis Servers are all primary nodes ; And a master node can have multiple slave nodes ( Or no slave ), But a slave node can only have one master node .
2、Redis The role of master-slave replication
(1) data redundancy
Master-slave replication realizes hot backup of data , It's a way of data redundancy beyond persistence .
(2) Troubleshooting
When there is a problem with the master node , Can be served by a slave node , Fast fault recovery ; It's actually a redundancy of services .
(3) Load balancing
On the basis of master-slave replication , Cooperate with the separation of reading and writing , Write service can be provided by the master node , Read service provided by slave node ( The write Redis Connect master node when data is applied , read Redis Apply connection from node when data ), Share server load ; Especially in the situation of less writing and more reading , Sharing read load through multiple slave nodes , Can be greatly improved Redis Concurrency of servers .
(4) High availability cornerstone
In addition to the above functions , Master slave replication is also the foundation for sentinels and clusters to implement , So master-slave replication is Redis High availability Foundation .
3、Redis Master slave replication process
(1) If you start a Slave Machine processes , Then it will go to Master The machine sends a message “sync command" command , Request synchronous connection .
(2) Whether it's the first connection or reconnection ,Master machine Will start a background process , Save data snapshot to data file ( perform rdb operation ) , meanwhile Master All commands to modify data are also recorded and cached in the data file .
(3) After the background process completes the cache operation ,Master The machine will turn to Slave The machine sends data files ,Slave The end machine saves the data file to the hard disk , Then load it into memory , next Master The machine will send all operations to modify the data to Slave End machine . if Slave Failure causes downtime , It will automatically reconnect after returning to normal .
(4)Master The machine received Slave After the connection of the end machine , Send its complete data file to Slave End machine , If Mater Receive multiple at the same time Slave Synchronization request from , be Master A process will be started in the background to save the data file , Then send it to all Slave End machine , Ensure that all Slave The end machine is normal .
4、Redis The master-slave structure
(1) Environment building
1、master node
ip Address :192.168.222.10 redis-5.0.7.tar. gz
2、slave1
ip Address :192.168.222.100 redis-5.0.7.tar. gz
3、slave 2
ip Address :192.168.222.20 redis-5.0.7.tar. gz
(2) install redis
Use the one click deployment script
#!/bin/bash
# install redis
read -p " Please enter the local IP Address :" ip
systemctl stop firewalld
setenforce 0
# install gcc gcc-c++ compiler
yum install -y gcc gcc-c++ make &> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[34;1m Compiler installation complete ! \033[0m"
fi
# take redis-5.0.7.tar.gz Upload the compressed package to /opt Directory , decompression , And compile and install
cd /opt
tar zxvf redis-5.0.7.tar.gz -C /opt/ &> /dev/null
cd /opt/redis-5.0.7/
make &> /dev/null
make PREFIX=/usr/local/redis install &> /dev/null
if [ $? -eq 0 ];then
echo -e "\033[34;1m redis Compile and install complete ! \033[0m"
fi
# Execute the... Provided by the software package installserver.sh Script file settings Redis Relevant configuration files required by the service
yum -y install expect &> /dev/null
/usr/bin/expect <<EOF
cd /opt/redis-5.0.7/utils
spawn /opt/redis-5.0.7/utils/install_server.sh
expect "instance:" {send "\r"}
expect "/etc/redis/6379.conf" {send "\r"}
expect "/var/log/redis_6379.log" {send "\r"}
expect "/var/lib/redis/6379" {send "\r"}
expect "path" {send "/usr/local/redis/bin/redis-server\r"}
expect "ENTER" {send "\r"}
expect eof
EOF
if [ $? -eq 0 ];then
echo -e "\033[34;1m redis Configuration complete ! \033[0m"
fi
# hold redis The executable program file of is put into the directory of path environment variable to facilitate system identification
ln -s /usr/local/redis/bin/* /usr/local/bin/
netstat -natp | grep redis
# Redis Service control
/etc/init.d/redis_6379 start
/etc/init.d/redis_6379 status
# Modify the configuration /etc/redis/6379.conf Parameters
sed -i '70s/bind 127.0.0.1/bind '$ip'/' /etc/redis/6379.conf
sed -i 's/appendonly no/appendonly yes/' /etc/redis/6379.conf
/etc/init.d/redis_6379 restart
if [ $? -eq 0 ];then
echo -e "\033[34;1m redis installation is complete ! \033[0m"
fi
netstat -natp | grep redis(3) modify Master Node profile (192.168.222.10)
(1)# modify master Master profile
vim /etc/redis/6379.conf
bind 192.168.222.10 #70 That's ok , Comment out bind term , Or modify to 192.168.222.10, network card
daemonize yes #137 That's ok , Start Daemons
logfile /var/log/redis_6379.log #172 That's ok , Specify the log file directory
dir /var/lib/redis/6379 #264 That's ok , assign work directory
appendonly yes #700 That's ok , Turn on AOF Persistence function
(2)# restart redis
/etc/init.d/redis_6379 restart
![]()
![]()
![]()
![]()
![]()
![]()

(3) modify Slave Node profile
(1)# modify slave1 node 、slave2 Node profile ,slave1 and slave2 The modification steps are the same
vim /etc/redis/6379.conf
bind 0.0.0.0 #70 That's ok , Modify the listening address
daemonize yes #137 That's ok , Start Daemons
logfile /var/log/redis_6379.log #172 That's ok , Specify the log file directory
dir /var/lib/redis/6379 #264 That's ok , assign work directory
replicaof 192.168.222.10 6379 #287 That's ok , Uncomment and specify the to synchronize Master node IP And port
appendonly yes #700 That's ok , Turn on AOF Persistence function
(2)# restart slave1 Nodes and slave2 node redis service
/etc/init.d/redis_6379 restart
![]()

![]()


![]()
![]()
slave2 ditto
(4) Verify the master-slave effect
stay Master View the log on the node
tail -f /var/log/redis_6379.log 
stay Master Verify the slave node on the node
[[email protected] ~]# redis-cli info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.222.100,port=6379,state=online,offset=84,lag=1
slave1:ip=192.168.222.20,port=6379,state=online,offset=84,lag=1
master_replid:fa5e578f6ee562d98eb5bf4b9f2dcc8868b90231
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84

Two 、Redis Sentry mode
1、 The principle of sentinel mode
sentry (sentinel): It's a distributed system , It is used to monitor each server in the master-slave structure , When there is a failure, the voting mechanism is used to select a new one Master And put all Slave Connect to the new Master. Therefore, the number of clusters running sentinels should not be less than 3 Nodes .
2、 The role of sentinel mode
(1) monitor : The Sentry will constantly check whether the master and slave nodes are working properly .
(2) Automatic failover : When the master node is not working properly , Sentinels will start automatic failover operations , It will upgrade one of the failed master nodes from a node to a new master , And let the other slave node copy the new master node .
(3) notice ( remind ): Sentinels can send failover results to clients .
3、 The construction of sentry mode
(1) modify Redis The configuration file ( All node operations )
vim /opt/redis-5.0.7/sentinel.conf
protected-mode no #17 That's ok , Turn off protection mode
port 26379 #21 That's ok ,Redis Sentry's default listening port
daemonize yes #26 That's ok , Appoint sentinel Start for background
logfile "/var/log/sentinel.log" #36 That's ok , Specify the log storage path
dir "/var/lib/redis/6379" #65 That's ok , Specify the database storage path
sentinel monitor mymaster 192.168.222.10 6379 2 #84 That's ok , modify Specify the sentinel node to monitor 192.168.222.10:6379 This master node , The name of the master node is mymaster, final 2 The meaning of is related to the fault determination of the master node : Need at least 2 Sentinel nodes agree , To determine the failure of the primary node and carry out failover
sentinel down-after-milliseconds mymaster 30000 #113 That's ok , Decision server down Lost time period , Default 30000 millisecond (30 second )
sentinel failover-timeout mymaster 180000 #146 That's ok , The maximum timeout of the failed node is 180000(180 second )

![]()
![]()
![]()
![]()
![]()
![]()
(2) Start sentinel mode
cd /opt/redis-5.0.7/
redis-sentinel sentinel.conf &
Be careful ! Start the main server first , Then start the slave server

(3) fault simulation
see redis-server Process number
[[email protected] redis-5.0.7]# ps aux | grep redis
root 8781 0.1 0.2 162548 9880 ? Ssl 21:19 0:05 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 11500 0.2 0.2 153844 7944 ? Ssl 22:25 0:00 redis-sentinel *:26379 [sentinel]
root 11530 0.0 0.0 112676 984 pts/1 R+ 22:29 0:00 grep --color=auto redis

Kill Master Node redis-server The process number of
kill -9 8781![]()
(4) The verification results
Dynamically view sentinel log results
tail -f /var/log/sentinel.log
redis-cli -p 26379 INFO Sentinel
3、 ... and 、Redis colony
1、 The role of clusters
(1) Data partition
Data partition ( Or data fragmentation ) It is the core function of cluster .
Clusters spread data across multiple nodes , On the one hand, it has broken through Redis The limit of single machine memory size , There's a huge increase in storage capacity ; On the other hand, each master node can provide external reading and writing services , It greatly improves the response ability of the cluster .
Redis The size of single machine memory is limited , Both persistence and master-slave replication are mentioned ; for example , If the single machine memory is too large ,bgsave and bgrewriteaof Of fork Operation may cause the main process to block , When the host is switched in the master-slave environment, the slave node may be unable to provide services for a long time , During the full replication phase, the replication buffer of the master node may overflow .
(2) High availability
Cluster supports master-slave replication and automatic fail over of master nodes ( Like a sentry ); When any node fails , Clusters can still provide external services .
2、Redis Data fragmentation of cluster
Redis Cluster introduces the concept of hash slot
Redis Cluster has 16384 Hash slot ( Number 0-16383)
Each node of the cluster is responsible for a portion of the hash slot
Every Key adopt CRC16 Check pair 16384 Take the rest to decide which Hashi slot to place , By this value , Go to find the node corresponding to the corresponding slot , Then directly and automatically jump to the corresponding node for access operation
# With 3 Take a cluster of nodes as an example :
node A contain 0 To 5460 Hash slot
node B contain 5461 To 10922 Hash slot
node C contain 10923 To 16383 Hash slot
#Redis The master-slave replication model of the cluster
There are A、B、C Three nodes , If node B failed , The whole cluster will be short of 5461-10922 This range of slots can not be used .
Add a slave node for each node A1、B1、C1 The whole cluster has three Master Nodes and three slave Node composition , At the node B After failure , A cluster election B1 The master node with bit continues to serve . When B and B1 After failure , The cluster will not be available
3、 Cluster deployment
(1) Environment building
master 1
192.168.222.10 redis-5.0.7.tar.gz
slave1
192.168.222.100 redis-5.0.7.tar.gz
master 2
192.168.222.20 redis-5.0.7.tar.gz
slave 2
192.168.222.30 redis-5.0.7.tar.gz
master 3
192.168.222.40 redis-5.0.7.tar.gz
slave 3
192.168.222.50 redis-5.0.7.tar.gz
(2) All nodes
cd /etc/redis/
mkdir -p redis-cluster/redis6379
cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis6379/
cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis6379/

(3)master1 node
# other 5 The configuration files of folders are modified, and so on , Be careful 6 Each port is different .
cd /etc/redis/redis-cluster/redis6379
vim redis.conf
bind 192.168.222.10 #69 That's ok , modify bind term , Monitor your own IP
protected-mode no #88 That's ok , modify , Turn off protection mode
port 7001 #92 That's ok , modify ,redis Listening port ,
daemonize yes #136 That's ok , Start as a separate process
cluster-enabled yes #832 That's ok , uncomment , Turn on the cluster function
cluster-config-file nodes-6379.conf #840 That's ok , uncomment , Cluster name file settings , There is no need to modify
cluster-node-timeout 15000 #846 That's ok , Uncomment cluster timeout setting
appendonly yes #699 That's ok , modify , Turn on AOF Persistence


![]()
![]()
![]()
![]()
![]()
![]()
![]()
scp /etc/redis/redis-cluster/redis6379/redis.conf [email protected]:/etc/redis/redis-cluster/redis6379/redis.conf
scp /etc/redis/redis-cluster/redis6379/redis.conf [email protected]:/etc/redis/redis-cluster/redis6379/redis.conf
scp /etc/redis/redis-cluster/redis6379/redis.conf [email protected]:/etc/redis/redis-cluster/redis6379/redis.conf
scp /etc/redis/redis-cluster/redis6379/redis.conf [email protected]:/etc/redis/redis-cluster/redis6379/redis.conf
scp /etc/redis/redis-cluster/redis6379/redis.conf [email protected]:/etc/redis/redis-cluster/redis6379/redis.conf





(4) The rest of the nodes
vim /etc/redis/redis-cluster/redis6379/redis.conf
69 Line modify native ip Address
92 No. modify the port number
![]()

![]()
(5) Start cluster
All nodes
cd /etc/redis/redis-cluster/redis6379/
redis-server redis.conf

redis-cli --cluster create 192.168.222.10:7001 192.168.222.20:7003 192.168.222.40:7005 192.168.222.50:7006 192.168.222.30:7004 192.168.222.100:7002 --cluster-replicas 1


redis-cli -h 192.168.222.10 -p 7001 -c # Add -c Parameters , Nodes can jump to each other
cluster slots # View the hash slot number range of the node
set sky blue
cluster keyslot sky # see name Key slot number

边栏推荐
- Notes on knowledge points of dynamic planning
- Divide the array into three equal parts [problem analysis]
- Achievements in science and Technology (XV)
- 2022 safety officer-b certificate theoretical question bank and simulation test
- Graph and graph traversal
- Integrate工具之Jenkins
- My creation anniversary
- Mathematical modeling experience ----- summary of three modeling
- sonarqube介紹和安裝步驟
- 明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级
猜你喜欢

移印工艺流程及应用注意事项

Solve the problem of slow downloading plug-ins for idea

HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications

【juc学习之路第5天】引用原子类和属性修改器

The road of global evolution of vivo global mall -- multilingual solution

(dp+ group backpack) acwing 9 Group knapsack problem

显示商品详情【项目 商城】

Jenkins of the integrate tool

In order to stimulate inspiration and creativity, Shanghai daoning united with XMIND to bring you full-featured mind mapping and brainstorming software

SAP SD 创建/修改价格表
随机推荐
[flume] notes
Lake Shore - supervaritemp low temperature thermostat
Cisco private dynamic routing protocol: EIGRP
2022 618 notebook shopping guide
JS to add an attribute to an array object
Jetpack架构组件学习(3)——Activity Results API使用
El scrollbar display horizontal scroll bar
二叉排序树
MySQL some simple commands
C language leetcode deleting duplicate items in an ordered array
Handwritten simple promise
Redis的主从复制、哨兵模式和集群
Here we go! Dragon lizard community enters PKU classroom
Teach you to play with SSM framework
Mysql5 and mysql8 are installed at the same time
Les produits antigéniques entrent dans la famille et les entreprises chinoises de dispositifs médicaux accueillent un nouvel océan bleu
(linear DP | monotone queue) acwing 895 Longest ascending subsequence
On the knowledge points of cookie attributes and the differences between webstorage and cookies?
Graph and graph traversal
Setting alias alias and @ reference note