Redis Master and Standby automatic switching

Source: Internet
Author: User
Tags failover redis cluster redis server

Sentinel (Sentinel) is a tool for monitoring the Master state in a Redis cluster.

I. Sentinel role
1. Master Status Detection
2, if the master exception, will be master-slave switch, one of the slave as master, the previous master as slave;
3, Master-slave switch, the contents of master_redis.conf, slave_redis.conf and sentinel.conf will change, that is Master_ Redis.conf will be more than one line slaveof configuration, sentinel.conf monitoring target will be replaced;

Ii. Sentinel Mode of work

  1, each Sentinel sends a PING command to its known master,slave and other Sentinel instances at a frequency of once per second
  2, if one instance (instance) is last valid reply When the PING command exceeds the value specified by the Down-after-milliseconds option, the instance is marked as a subjective downline by Sentinel.
  3, if a master is marked as a subjective downline, all Sentinel monitoring of this master is to confirm that master does enter a subjective downline at a frequency of once per second.
  4, when there is a sufficient number of Sentinel (greater than equal to the value specified by the profile) to confirm that master does enter the subjective downline status within the specified time frame, master is marked as objective offline
  5, and in general, each Sentinel sends the INFO command to all master,slave known to it every 10 seconds
  6, when Master is marked as an objective downline by Sentinel, all Slave of Sentinel to the offline Master The frequency of the INFO command is changed from 10 seconds to once per second
  7, and if there is not enough Sentinel consent master has been offline, the objective downline status of master will be removed. If Master re-returns a valid reply to Sentinel's PING command, Master's subjective downline status is removed.

Subjective downline and objective downline
subjective offline : subjectively down, referred to as Sdown, refers to the current Sentinel instance of a Redis server to make the offline judgment;
objective offline : Objectively down, referred to as Odown, refers to a number of Sentinel instances in the master server to make Sdown judgments, and through Sentinel is-master-down-by-ad After the DR command communicates with each other, the resulting master server is judged and then turned on failover.
Sdown is suitable for master and slave, as long as a Sentinel discovers that Master enters the Odown, this sentinel may be selected by other Sentinel and perform an automatic failover operation on the primary server.
Odown only works with master, for slave Redis instances, Sentinel does not need to negotiate before judging them as a downline, so slave Sentinel will never reach Odown.

Third, sentinel configuration to achieve the main standby automatic switching

In Sentinel, you only need to configure Sentinel Monitor as Master in each node (including the master node), without needing to configure slave information. In a sentinel.conf file in each Redis node.

sentinel.conf configuration for slave:

Port 26379

#master2

Sentinel Monitor Master2 192.168.15.129 6379 1---Master address name is the name of the whistle name, different servers can be obtained differently

Sentinel Down-after-milliseconds Master2 5000

Sentinel Failover-timeout Master2 900000

#sentinel Can-failover Master2 Yes

Sentinel Parallel-syncs Master2 2

sentinel.conf Configuration on Master machine

Property

Value

Describe

Port

16379

Monitor the whistle port

Daemonize

Yes

The process started is a background process

LogFile

"/opt/oracle/redis-2.8.19/logs/sentinel.log"

Log file

Sentinel Monitor <master-name> <ip> <redis-port> <quorum>

Sentinel Monitor MyMaster 127.0.0.1 6379 1

IP and port is the master IP address and port, MyMaster is the cluster name, can be modified to the desired name, client access requires this name

Sentinel Down-after-milliseconds <master-name> 30000

Sentinel Down-after-milliseconds MyMaster 30000

Master-name for a unified cluster name

Sentinel Failover-timeout <master-name> 180000

Sentinel Failover-timeout MyMaster 180000

Sentinel Parallel-syncs <master-name> <numslaves>

Sentinel Parallel-syncs MyMaster 1

Sentinel configuration on slave machine

The Sentinel configuration on the slave is the same as the configuration on master, and you only need to modify the Master-name

The boot sequence is as follows:

1, redis-server redis.conf start master host;

2, Redis-server redis.conf start slave host;

3, Redis-sentinel sentinel.conf start the master host on the monitoring whistle;

4. Redis-sentinel sentinel.conf Start the Monitor on the slave host

Note: On first boot, you must first start the master
If Master has been judged to be offline, Sentinel has chosen a new master, and has changed old master to slave, but has not changed it to new master. If you restart Old master at this point, the Redis cluster will be in a no Master state, and you can only manually modify the configuration file and then restart the cluster

Iv. examples are as follows:
master:192.168.15.129,

slave:192.168.15.128

Master's profile is normal, add a line to the slave configuration file: slaveof 192.168.15.129 6379 Master sentinel.conf configuration (here my file name is sentinel-test.conf): port 26379#master1sentinel Monitor Master1 192.168.15.129 6379 1sentinel down-after-milliseconds master1 5000sentinel Failover-timeout master1 900000#sentinel can-failover master1 yessentinel parallel-syncs master1 2 Slave sentinel.conf configuration (here my file name is sentinel-test.conf): Port 26379#master2sentinel monitor Master2 192.168.15.129 6379 1sentinel down-after-milliseconds master2 5000sentinel failover-timeout master2 900000#sentinel can-failover master2 Yessentinel Parallel-syncs Master2 2

Start the Redis service and Sentinel service for Master and slave, respectively

[email protected]src]#./redis-server. /redis.conf

[email protected]Src]#./redis-sentinel. /sentinel-test.conf

Then switch the main standby according to the following scenarios, the switch is successful, when the test is passed, it is configured to complete

To test the primary and standby switching scenario:

Master: 129, Ready: 128,sen:128,129

Turn off 129 Redis and Sen to see if you can switch to 128, delete data on 128, start 129 Redis and Sen, see if the data syncs, and then turn off 128 Redis and Sen to see if it switches to 129

Run the following command to connect the client to view the slave status:./redis-cli-h ip-p Port

Redis Master and Standby automatic switching

Related Article

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.