Redis sentinel mode, redissentinel

Source: Internet
Author: User

Redis sentinel mode, redissentinel
I. Introduction to sentinel

Role of Sentinel: 
1): Master status detection
2) If the Master node is abnormal, the Master-Slave switch will be performed, and one of the Slave will be used as the Master node and the previous Master will be used as the Slave.
3) after the Master-Slave switch, master_redis.conf, slave_redis.conf, and sentinel. the content of conf will change, that is, master_redis.conf will have one more slaveof configuration, sentinel. the conf monitoring target will be changed accordingly.

How Sentinel works: 
1): Each Sentinel sends a PING command to the Master, Slave, and other Sentinel instances it knows every second.
2) If the time between an instance and the last valid PING command reply exceeds the value specified by the "down-after-milliseconds" option, this instance is marked as subjective offline by Sentinel.
3): If a Master is marked as a subjective deprecation, all Sentinel of the Master being monitored should confirm that the Master is in the subjective deprecation status once per second.
4): when there is a sufficient number of Sentinel (greater than or equal to the value specified in the configuration file) within the specified time range, it is confirmed that the Master has entered the subjective deprecation status, the Master will be marked as an objective offline
5): In general, each Sentinel sends the INFO command to all known masters every 10 seconds.
6): When the Master is marked as an objective deprecation by Sentinel, the frequency of sending the INFO command from Sentinel to all Slave of the offline Master is changed from 10 seconds to once per second.
7): If there is not enough Sentinel to agree that the Master has been deprecated, the objective deprecation status of the Master will be removed.
8): If the Master returns a valid reply to the Sentinel PING command again, the Master's subjective offline status will be removed.

Subjective offline and objective offline 
Subjective deprecation: Subjectively Down (SDOWN) refers to the deprecation judgment made by the current Sentinel instance on a redis server.
Objective deprecation: Objectively Down (ODOWN) refers to the SDOWN judgment performed by multiple Sentinel instances on the Master Server, after the SENTINEL is-master-down-by-addr command is used to communicate with each other, the Master Server is taken offline for judgment and failover is enabled.

In general: 
Redis's sentinel system is used to manage multiple redis servers. It can implement a function to implement HA clusters. The system mainly executes three tasks:
① Monitoring (Monitoring): Redis Sentinel monitors the running status of the master server and slave server in real time.
② Notification: When a monitored Redis server encounters a problem, Redis Sentinel can send a notification to the system administrator or send a notification to other programs through the API.
The architecture of a simple master-slave structure and sentinel cluster is as follows:

It is a master node and a slave node. sentinel clusters communicate with each other, communicate with each other about the status of redis nodes, and make corresponding judgments and processes, the subjective offline status and objective offline status are important. They determine whether to subscribe to the specified channel for failover, when the server fails, notify the Administrator client that Sentinel can be considered as a Redis server that only provides the subscription function. You cannot use the PUBLISH command to send messages to this server, however, you can use the SUBSCRIBE command or the PSUBSCRIBE command to SUBSCRIBE to a given channel to obtain corresponding Event Notifications.

A channel can receive events with the same name as this channel. For example, a channel named + sdown can receive events in the subjective SDOWN status of all instances.

2. Build a redis-sentinel Cluster Environment

1. Download The redis compilation installation package: redis-2.8.19.tar.gz, and then decompress to compile and install. Install redis in the/opt/redis directory.

1 [root @ idxs21-108 redis] # mkdir bin # redis script file 2 [root @ idxs21-108 redis] # mkdir conf # redis configuration file 3 [root @ idxs21-108 redis] # mkdir data # redis local data storage 4 [root @ idxs21-108 redis] # mkdir logs # redis Log File 5 [root @ idxs21-108 redis] # tar zxvf redis-2.8.19.tar.gz cd redis-2.8.19 6 [root @ idxs21-108 redis] # make PREFIX =/opt/redis install cp *. conf/opt/redis/conf # Install. copy the conf configuration file to the conf directory under the installation directory. 7 [root @ idxs21-108 redis] # cd/opt/redis/conf cp redis. cof redis. conf. bak cp sentinel. conf sentinel. conf. bak

2. After redis is successfully installed, you can view the following content in the/opt/redis/bin directory:

1 [root@idxs21-108 bin]# ll2 total 152203 -rwxr-xr-x 1 root root 4586251 Mar 10  2015 redis-benchmark4 -rwxr-xr-x 1 root root   22177 Mar 10  2015 redis-check-aof5 -rwxr-xr-x 1 root root   45387 Mar 10  2015 redis-check-dump6 -rwxr-xr-x 1 root root 4679739 Mar 10  2015 redis-cli7 lrwxrwxrwx 1 root root      27 Jun 20 15:59 redis-sentinel -> /opt/redis/bin/redis-server8 -rwxr-xr-x 1 root root 6241748 Mar 10  2015 redis-server

3. modify the configuration file of the master machine (replace the sed command here ):

1 # -- redis. conf # modify redis. conf 2 sed-I's/daemonize no/daemonize yes/G' redis. conf # Run 3 sed-I's/logfile ""/logfile "/opt/redis/logs/redis in the background. log "/g'redis. conf # configure the Log Path 4 # no AOF and RDB for master # configure the data synchronization policy, master node Comment off dump data synchronization 5 sed-I's/save 900 1/# save 900 1/G' redis. conf 6 sed-I's/save 300 10/# save 300 10/G' redis. conf 7 sed-I's/save 60 10000/# save 60 10000/G' redis. conf 8 sed-I's/appendonly no/appendonly yes/G' redis. conf # enable aof to synchronize 9 sed-I's/dir \. \ // dir "/opt/redis/data/"/G' redis. conf # configure the data file storage path 10 sed-I's/slowlog-log-slower-than 10000/slowlog-log-slower-than 5000/G' redis. conf11 sed-I's/slowlog-max-len 128/slowlog-max-len 1000/G' redis. conf12 13 # -- sentinel. conf # modify sentinel. conf file 14 echo 'daemonize yes'> sentinel. conf # Run sentinel15 echo 'logfile/opt/redis/logs/sentinel in the background. log'> sentinel. conf16 sed-I's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 10.135.40.118 6379 2/g 'sentinel. conf # configure the Sentinel Mode

 

4. Modify the Server Load balancer configuration file. The two Server Load balancer instances have the same configuration:

1 # -- redis. conf 2 sed-I's/daemonize no/daemonize yes/G' redis. conf 3 sed-I's/logfile ""/logfile "/opt/redis/logs/redis. log "/g'redis. conf 4 sed-I's/# save ""/save ""/G' redis. conf 5 sed-I's/appendonly no/appendonly yes/G' redis. conf 6 sed-I's/dir \. \ // dir "/opt/redis/data/"/G' redis. conf 7 8 # slowlog get or slowlog get number 9 sed-I's/slowlog-log-slower-than 10000/slowlog-log-slower-than 5000/G' redis. conf10 sed-I's/slowlog-max-len 128/slowlog-max-len 1000/G' redis. conf12 echo "slaveof 10.135.40.118 6379"> redis. conf # specify master node 13 14 # -- sentinel. conf15 echo 'daemonize yes'> sentinel. conf 16 echo 'logfile/opt/redis/logs/sentinel. log'> sentinel. conf17 sed-I's/sentinel monitor mymaster 127.0.0.1 6379 2/sentinel monitor mymaster 10.135.40.118 6379 2/g 'sentinel. conf

 

5. After the configuration file is modified, start the redis service and start from the master. The startup methods and commands of all nodes are the same.

1 start redis and sentinel services: 2 [root @ idxs21-108 redis] #/opt/redis/bin/redis-server/opt/redis/conf/redis. conf3 [root @ idxs21-108 redis] #/opt/redis/bin/redis-sentinel/opt/redis/conf/sentinel. conf4 view Service Process status: 5 [root @ idxs21-108 redis] # ps-ef | grep redis6 root 10024 9807 0 00:00:00 pts/0 grep redis7 root 68378 1 0 Jul28? 01:30:09/opt/redis/bin/redis-sentinel *: 26379 8 root 68379 1 0 Jul28? 00:35:16/opt/redis/bin/redis-server *: 6379

6. Test

1>. Test Data Synchronization

1 primary redis write data: 2 [root @ idxs40-118 redis] # redis-cli-p 6379 3 127.0.0.1: 6379> set name abc 4 OK 5 127.0.0.1: 6379> get name 6 "abc" 7 127.0.0.1: 6379> 8 read data from redis: 9 [root @ idxs40-117 redis] # redis-cli-p 637910 127.0.0.1: 6379> get name11 "abc" 12 127.0.0.1: 6379> 13 read data from redis: 14 [root @ idxs40-119 redis] # redis-cli-p 637915 127.0.0.1: 6379> get name16 "abc" 17 127.0.0.1: 6379>

2>. redis adopts read/write splitting by default. Only the master can write data, and the slave can only read:

1 [root@idxs40-117 ~]# redis-cli -p 63792 127.0.0.1:6579> set name 1233 (error) READONLY You can't write against a read only slave.

Here, we can see that the slave node has only the read permission by default and cannot write data.

7. Introduction to some sentinel commandsTo use the sentinel command, we need to use the redis-cli command to enter sentinel:

[Root @ idxs21-108 redis] # redis-cli-h 10.135.40.118-p 26379 ① INFO sentinel basic status information ② SENTINEL masters list all monitored master servers, and the current status of these master servers ③ SENTINEL slaves lists all slave servers of a given master server, and the current status of these slave servers ④ SENTINEL get-master-addr-by-name returns the IP address and port number of the master server with the given name ⑤ SENTINEL reset resets all names and the given pattern matched master server. The reset operation clears all current statuses of the master server, including ongoing failover, and removes all slave servers and Sentinel that have been found and associated with the master server. ⑥ When the primary server fails, SENTINEL failover forces an automatic failover without asking any other Sentinel comments, but it will send the latest configuration to other sentinel instances, other sentinel will be updated based on this configuration

 

8. If the master node goes down, one machine is automatically elected from the two slave instances to replace the master role. If the old master node is restored later, it can only be a salve node if it is added to the cluster again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.