当前位置:网站首页>Redis sentinel mode realizes one master, two slave and three Sentinels
Redis sentinel mode realizes one master, two slave and three Sentinels
2022-07-04 15:10:00 【1024 questions】
One 、redis Environmental Science :
Two 、 The sentry introduces :
3、 ... and 、 install redis:
Four 、 Use Redis The role of master-slave replication :
5、 ... and 、 To configure redis One master and two subordinates :
6、 ... and 、 To configure redis Three sentinels :
One 、redis Environmental Science : Environmental Science :redis6.2.6
linux One virtual machine ,contos7;
1. One master, two slaves and three sentinels :
One master, two slaves, three sentinels , When master When the node goes down , Through the Sentinels (sentinel) Re elect a new master node , Ensure cluster availability .
2. The main function of a sentry is :
1. Cluster monitoring : Responsible for monitoring Redis master and slave Is the process working .
2. Notice of news : If a Redis The instance is faulty , Then the sentry is responsible for sending a message as an alarm to the Administrator .
3. Fail over : If master node Hang up , Will automatically transfer to slave node On .
4. Configuration center : If a fail over occurs , notice client Client new master Address .
PS: According to the recommendation mechanism , The number of sentinels in the cluster should be odd (3、5…)
3. Sentinels are used to achieve redis High availability of clusters , It's also distributed , Run as a sentinel cluster , Working together .
When failing over , Judge a master node Is it down , It needs the consent of most of the Sentinels , It's about distributed elections .
Even if some of the sentinel nodes are down , The Sentinels are still working , Because if a failover system, which is an important part of high availability mechanism, is single point , Then it's very embarrassing .
4. The core knowledge of sentinels :
Sentinels need at least 3 An example , To ensure their own robustness .
sentry + redis Master slave deployment architecture , There is no guarantee of zero data loss , Only guarantee redis High availability of clusters .
For the sentry + redis Master slave complex deployment architecture , Try to test the environment and the production environment , They all have enough tests and drills .
3、 ... and 、 install redis:Omit here redis Installation .
Four 、 Use Redis The role of master-slave replication :Master slave copy , It means to put one Redis Data of master node server , Copy to other Redis From the node server .
The primary node is called (master/leader), The slave node is called (slave/follower);
1, data redundancy : Master-slave replication realizes hot backup of data , This is another way to implement persistence .
2, Fault recovery : 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, Read / write separation :master Services are mainly used to write ,slave Services are mainly used to read data . It can improve the load capacity of the server , According to the change of demand , Add the number of slave nodes .
4, Load balancing : At the same time, it is combined with read-write separation , The master node provides the write service , Provide read service from node , Share the load on the server . Write less and read more , Sharing read load through multiple slave nodes , Can greatly improve Redis Concurrency and load of services .
5, High availability cornerstone , Master-slave replication is the basis for the implementation of sentinel and cluster modes .
5、 ... and 、 To configure redis One master and two subordinates :In normal production, we prepare multiple servers , Like three , Each is in its own linux Installation in system redis.
But here for the convenience of demonstration , I use a computer to install virtual machine , Install three copies on the virtual machine redis service , To simulate the effect of one master and two slaves .
First step : Modify the original redis.conf The configuration file :
redis.conf The configuration file adopts the default mixed persistence , Separate RDB Persistence , Because the essence of master-slave replication is RDB The way , So there must be RDB Just participate in the way .
primary redis.conf There are several places in the configuration file that need to be modified :
1. First, comment out the line of stand-alone connection in the network section , Note out bind 127.0.0.1 2. Similarly, we need to turn on the background operation :daemonize no, Set to yes.3. take Protection mode is off :protected-mode yes Change it to :protected-mode no 4. open RDB Persistent configuration :#RDB Persistence strategy There are three default methods ,[900 In seconds 1 Time modification ],#[300 In seconds 10 Time modification ],[60 In seconds 10000 Time modification ] Trigger RDB Persistence ,# We can manually modify this parameter or add a new policy save 900 1save 300 10save 60 10000 #RDB file name dbfilename dump.rdb#RDB File storage path dir ./ Policy configuration :# stay seconds In seconds changes This data modification triggers RDB Persistence 5. Turn on AOF Persistent configuration appendonly yes#AOF file name appendfilename "appendonly.aof"#AOF File storage path And RDB Is the same parameter , Share a file path dir ./ # namely bin Under the table of contents #AOF Strategy , Usually choose the first [always: Every command records ],#[everysec: Once per second ],[no: When you are happy to see the machine, you can record ,linux Generally, it is synchronized once every half an hour ]#appendfsync alwaysappendfsync everysec# appendfsync no#aof The size of the file is larger than the last time it was rewritten , growth 100%( The configuration can be greater than 100%) when , Trigger override .#[ If the size is... After last rewriting 10MB, When AOF The document reaches 20MB The override will also be triggered again , And so on auto-aof-rewrite-percentage 100 #aof The file size exceeds 64MB*2 when , Trigger override ,# Why multiply by 2, because auto-aof-rewrite-percentage 100 Yes, double that 100%,# Rewrite only after doubling auto-aof-rewrite-min-size 64mb 6. Turn on hybrid persistence :#6.aof-use-rdb-preamble yes # Check whether hybrid persistence is on ,redis5.0 After the default on
The second step : Will modify the redis.conf Three copies of the configuration file :
1. Get into redis Default installation path :cd /usr/local/binll # View folder contents cp redis.conf redis_bk.conf # Try to back up a configuration file , Prevent mistakes in modification , It can be reused cp redis.conf redis6379.confcp redis.conf redis6380.confcp redis.conf redis6381.conf
The third step : Modify three configuration files respectively :
vim redis6379.conf Revised content :pidfile /var/run/redis_6379.pidport 6379dbfilename dump6379.rdb
vim redis6380.conf Revised content :pidfile /var/run/redis_6380.pidport 6380dbfilename dump6380.rdb
vim redis6381.conf Revised content :pidfile /var/run/redis_6381.pidport 6381dbfilename dump6381.rdb
redis Preparation before start up :
Check to see if the fire wall of the virtual machine is closed , Firewall needs to be turned off
1: View fire protection status
systemctl status firewalldservice iptables status
2: Turn off the firewall temporarily
systemctl stop firewalldservice iptables stop
3: Permanently shut down firewall
systemctl disable firewalldchkconfig iptables off
Check the firewall status after closing
Open designated port 6379:
Mode 1 of opening designated ports :
stay linux In the implementation of : /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
redis Default port number 6379 Remote connection is not allowed , So set in the firewall 6379 Turn on remote service ;
Mode 2 of opening designated ports :
Open the firewall before opening the specified port :systemctl start firewalld
firewall-cmd --zone=public --add-port=6379/tcp --permanent
Step four : Start three redis The server , And check to see if it started successfully :
redis-server redis6379.confredis-server redis6380.confredis-server redis6381.confps -ef | grep redis
Step five : Configure the slave library instead of the master library :
Connect... First redis client :
redis-cli -p 6380
And then 6380 The machine is configured as a slave :
slaveof host ip Host port number #slaveof 127.0.0.1 6379info replication
And then 6381 The machine is configured as a slave :
redis-cli -p 6381#slaveof host ip Host port number slaveof 127.0.0.1 6379info replication
View the slave information on the host : Discovery configuration succeeded .
redis-cli -p 6379info replication
6、 ... and 、 To configure redis Three sentinels :Bring the official sentinel.conf( This file is in redis Under the source directory , That is, under the decompression path ) Copied to the /usr/local/bin:
Then make three copies :
The changes are as follows :
vim sentinel0.conf The document is amended as follows :protected-mode nodaemonize yesport 26379 #sentinel port dir “/usr/local/bin”sentinel monitor mymaster 127.0.0.1 6379 2
And then modify sentinel1.conf The information is as follows :
protected-mode nodaemonize yesport 26380 # sentinel port , Because we are on a virtual machine , So the ports are different dir “/usr/local/bin”sentinel monitor mymaster 127.0.0.1 6379 2 # there mymaster The two copies should be the same ,mymaster Same hostname
And then modify sentinel2.conf The information is as follows :
protected-mode nodaemonize yesport 26381 # sentinel port , Because we are on a virtual machine , So the ports are different dir “/usr/local/bin”sentinel monitor mymaster 127.0.0.1 6379 2 # there mymaster The two copies should be the same
Configuration file details
The configuration of Sentry is mainly to modify sentinel.conf Parameters in the configuration file , stay Redis This configuration file can be seen in the installation directory , The detailed explanation of each parameter is as follows :
# sentry sentinel The port on which the instance runs , Default 26379 port 26379# sentry sentinel Working directory of dir ./# Whether to turn on the protection mode , Default on .protected-mode:no# Whether it is set to background startup .daemonize:yes# sentry sentinel Log file logfile:./sentinel.log# sentry sentinel Monitored redis The master node ## ip: host ip Address ## port: Sentry port number ## master-name: You can name the master node by yourself ## quorum: When these quorum Number sentinel The sentry thought master Primary node lost connection Then at this time # It is objectively believed that the primary node is disconnected # sentinel monitor <master-name> <ip> <redis-port> <quorum> sentinel monitor mymaster 127.0.0.1 6379 2# When in Redis In the example, the requirepass, All connections Redis The client of the instance must provide the password .# sentinel auth-pass <master-name> <password> sentinel auth-pass mymaster 123456 # Specify the master node to answer the sentinel sentinel The maximum time interval , Beyond that time , Sentinels subjectively think that the primary node is offline ,# Default 30 second # sentinel down-after-milliseconds <master-name> <milliseconds>sentinel down-after-milliseconds mymaster 30000 # Specifies when failover When switching between active and standby , How many at most slave Simultaneously on the new master To synchronize .# The smaller the number , complete failover The longer it takes ; conversely , But if the number is bigger , Means # The more slave because replication And is not available . You can do this by setting this value to zero 1, To make sure there's only one at a time slave,# In a state where command requests cannot be processed .# sentinel parallel-syncs <master-name> <numslaves>sentinel parallel-syncs mymaster 1 # Timeout for failover failover-timeout, Default three minutes , It can be used in the following ways :## 1. The same sentinel To the same master two failover The time between . ## 2. When one slave From a wrong one master It starts when you synchronize data there , until slave Be corrected from correct # Of master Where the synchronization ends . ## 3. When you want to cancel an ongoing failover The time required for .## 4. When doing failover when , Configure all slaves Point to the new master Maximum time required . however ,# Even after this timeout ,slaves It will still be correctly configured to point master, But it won't parallel-syncs The configured rules to synchronize data # sentinel failover-timeout <master-name> <milliseconds> sentinel failover-timeout mymaster 180000# When sentinel When any warning level event occurs ( for instance redis Subjective failure and objective failure of examples, etc ),# Will call this script . The maximum execution time of a script is 60s, If I exceed that time ,# The script will be a SIGKILL Signal termination , And then re execute .# There are the following rules for the result of the script : ## 1. If the script returns 1, Then the script will be executed again later , The number of repetitions currently defaults to 10.## 2. If the script returns 2, Or 2 A higher return value , The script will not repeat . ## 3. If the script is terminated during execution due to receiving a system interrupt , Then the same return value is 1 The same behavior when .# sentinel notification-script <master-name> <script-path> sentinel notification-script mymaster /var/redis/notify.sh# This script should be generic , Can be called many times , It's not targeted .# sentinel client-reconfig-script <master-name> <script-path>sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
Finally, start three services first , Then get up and send off three sentinels , And then the client ;
Activate the sentry :
redis-sentinel sentinel0.confredis-sentinel sentinel1.confredis-sentinel sentinel2.conf
View after startup sentinel Information :redis-cli -p sentinel Port number
redis-cli -p 26379info sentinel
This is about Redis The sentinel mode realizes one master, two slave and three sentinels, which is introduced here , More about Redis Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !
边栏推荐
猜你喜欢
How did the beyond concert 31 years ago get super clean and repaired?
Expose Ali's salary and position level
Preliminary exploration of flask: WSGI
Preliminary exploration of flask: WSGI
关于FPGA底层资源的细节问题
Leecode learning notes - Joseph problem
Weibo and Huya advance into interest communities: different paths for peers
AI做题水平已超过CS博士?
Redis的4种缓存模式分享
内存管理总结
随机推荐
Weibo and Huya advance into interest communities: different paths for peers
Is BigDecimal safe to calculate the amount? Look at these five pits~~
5G电视难成竞争优势,视频资源成中国广电最后武器
31年前的Beyond演唱会,是如何超清修复的?
MySQL组合索引(多列索引)使用与优化案例详解
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
Preliminary exploration of flask: WSGI
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
Luo Gu - some interesting questions 2
开源人张亮的 17 年成长路线,热爱才能坚持
Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model
2022 financial products that can be invested
深度学习 神经网络的优化方法
AI做题水平已超过CS博士?
TechSmith Camtasia studio 2022.0.2 screen recording software
【学习笔记】拟阵
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
Why do domestic mobile phone users choose iPhone when changing a mobile phone?
如何搭建一支搞垮公司的技术团队?
PXE网络