preface

In the last article we talked about the master-slave replication setup of Redis, but here is a question for you to consider: if my master node fails, then I only have the slave node. There would be no machine to accept Redis’s write requests.

Our sentinel mechanism here is to solve this problem: failover. If the master node fails, a master/slave switchover is performed so that the slave node can be upgraded to the master node and continue to provide external services.

End your essay with a few questions or suggestions. Your feedback will help my brother write better articles.

The sentry introduction

Sentinel is a highly available Redis solution that can run multiple Sentinels to form a Sentinel distributed system.

Use gossip Protocols to check if a host is offline; And use agreement protocols to decide whether to perform automatic failover. And which slave server to choose as the new master server.

Sentinel Sentinel duties are as follows:

  • monitoring(Monitoring) : Sentinel continuously checks your primary and secondary servers regularly to see if they are functioning properly.
  • remind(Notification) Sentinel can send notifications to administrators or other applications via the API when a monitored Redis server has a problem.
  • Automatic failover(Automaticfailover): as a master serverNot working properly, Sentinel will start onceAutomatic failoverAction, which invalidates one of the master’s slave serversupgradeFor the new master server, and have the other secondary servers of the failed master server replicate the new master server; whenThe clientWhen an attempt is made to connect to a failed primary server, the cluster also returns the address of the new primary server to the client, allowing the cluster to use the new primary server in place of the failed server.
  • Unified configuration managementThe: linker asks sentinel to obtain the address of the primary and secondary.

The sentry building

The machine to prepare

Master copy of the build, the previous article talked about, you can go to reference

Server name The node type The IP address port
Node1 Redis Service 1(Master node) 192.168.14.101 6379
Node2 Redis service 2(from node Slave1) 192.168.14.102 6380
Node3 Redis service 3(from node Slave2) 192.168.14.103 6381
Sentinel1 Sentinel Service 1 192.168.14.101 26379
Sentinel2 Sentinel Service 2 192.168.14.102 26380
Sentinel3 Xiao Bing Service 3 192.168.14.103 26381

Five main configurations are explained

Create a file named sentinel.conf in each primary and secondary Redis directory and run the following commands in the file.

General command format: sentinel


One: Configure sentinel to monitor the master

Example: Sentinel Monitor MyMaster 127.0.0.1 6380 1

A: The name of the sentinel monitor master is myMaster, and the address is 127.0.0.1:6380. Sentinel In a cluster, multiple sentinels need to communicate with each other to confirm whether a master is really dead. The number 1 indicates that the master is considered unavailable only when one sentinel in the cluster thinks it is dead.

Two: Configure the Sentinel heartbeat

Example: Sentinel down-after-milliseconds myMaster 5000

A: Sentinel sends a heartbeat PING to the master to verify that the master is alive. If the master doesn’t respond to PONG or send sentinel an error message within the down-after-milliseconds range, So sentinel would subjectively say that this master is not available

3. When configuring a master/slave switchover, synchronize the number of new Master Salves

Example: Sentinel parallel- Syncs MyMaster 1

A: In the event of a failover, this option specifies the maximum number of slaves that can simultaneously synchronize data to the new master. The smaller the number, the longer it takes to complete failover, but the larger the number, the more slaves are unavailable because of Replication. This can be set to 1 to ensure that only one slave at a time is unable to process command requests

Four: Set the maximum failover time

Example: Sentinel Failover -timeout myMaster 60000 (ms)

Description: If the Sentinel process fails to complete the failover operation within the specified value, the failover operation is considered to have failed.

Five: configure the alarm script

Example: Sentinel notification-script mymaster

A: Alarm script to be invoked when Sentinel detects an exception on the Master server.

Example sentinel configuration file

According to this configuration, the three Sentinel nodes were configured respectively

# Sentry Sentinel instance runs on a default port26379
port 26379# daemonize yes # sentinel working directory dir/TMP # logfile name logfile"sentinel_26379.log"Sentinel monitor myMaster192.1681.108. 6379 2# sentinel auth-pass <master-name> <password> Sentinel auth-pass mymaster1234# specifies the number of milliseconds after the primary node does not answer sentinel sentinel at this point the sentinel subjectively considers the primary node offline by default30Second sentinel down - after - milliseconds mymaster30000

sentinel parallel-syncs mymaster 1Sentinel failover-timeout myMaster180000If the script path is present, the script must be executed in the path, otherwise sentinel will not start successfully. sentinel notification-script mymaster /var/redis/notify.sh  
Copy the code

Start the sentinel

Mode 1: redis-sentinel redis-sentinel.conf

Mode 2: redis-server sentinel.conf –sentinel

Verify the primary/secondary switchover

Kill the master node to simulate a host fault

Switch-master mymaster 192.168.14.101 6379 192.168.14.103 6381 Indicates the redis server whose master server switches from port 6379 to port 6381.

PS:+switch-master Switches the primary node

View port 6381 Redis server

By running the info replication command, we found that the Redis service on 6381 has been switched to the master node.

In addition, the sentinel monitor myMaster 192.168.14.101 6379 2 is automatically changed to 6381

At this point, the sentinel mode setup verification is complete.

IT brother

A big factory to do advanced Java development program ape

Follow wechat public account: IT elder brother

Java actual combat project video tutorial: you can get 200G, 27 sets of actual combat project video tutorial

Reply: Java learning route, you can get the latest and most complete a learning roadmap

Re: Java ebooks, get 13 must-read books for top programmers

Java foundation, Java Web, JavaEE all tutorials, including Spring Boot, etc

Reply: Resume template, you can get 100 beautiful resumes