How to configure sentinels in Redis? ? ?

Redis sentinel mode

Sentinel mode: Assign a guard to the cluster.

The role of the sentinel is to monitor the operation of the Redis system. It is an independent process and its functions:

  1. Monitor whether the master database and the slave database are operating normally;
  2. After the main data fails, it will automatically convert from the database to the main database;

If the host is down, start the election work and choose one to be the host.

Environment preparation: one master and two slaves, when starting any slave, start sentinel mode

Although sentinel is released as a separate executable file redis-sentinel, it is actually just a Redis server running in a special mode. You can start a normal Redis server by specifying the --sentinel option. Activate the sentinel.

Insert picture description here

Step 1: Configure sentry:

The sentinel is mainly used to monitor the master server, so the sentinel is generally deployed on the slave server to monitor

Configure sentry

  • To start the sentinel process, you first need to create the sentinel configuration file vi sentinel.conf, which can be copied from the source code configuration redis-5.0.5/sentinel.conf, or you can directly customize the file to the bin directory
  • Enter in the configuration: sentinel monitor mastername Intranet IP (127.0.0.1) 6379 1
  • Description:
  • mastername monitor the name of the master data, custom
  • 127.0.0.1: monitor the IP of the main database;
  • 6379: port

Step 2: Activate the sentry

The sentinel is a separate process. Before starting, make sure that the master-slave service is normal. Start the master service first, then start the slave service

Insert picture description here

Write the log to the specified file

[root@localhost bin]# ./redis-sentinel ./sentinel.conf >sent.log &
[1] 3373

After starting the redis service, the program will automatically configure the file sentinel.conf and generate content. Note: If you restart it, you need to delete the generated content. Sentinel start method:

[root@localhost bin]# ./redis-server sentinel.conf --sentinel

Sentinel Process Console: Added a monitor for the master database.

Insert picture description here

At the same time there are more sentinel processes:

Insert picture description here

Query the content generated in the configuration file sentinel.conf:

When starting the sentry, the configuration file of the sentry was modified. If you need to start the sentry again, you need to delete the myid unique mark. (The insurance method is to start it once and configure it once)
Insert picture description here

Step 3: Host is down

Computer room accident: power outage. Hardware failure: the hard disk is broken

List:
Insert picture description here

Kill the host: kill -9 pid

[root@localhost redis6380]# kill -9 2910

Sentinel console: The slave library is automatically promoted to the master library.

Sentinel work, confirm the slave before connecting:

127.0.0.1:6381> info replication
# Replication
role:slave
master_host:192.168.197.129
master_port:6379

Sentinel replaces operation and maintenance. Automatic monitoring is complete.

At the same time, the master-slave configuration file of redis.conf will be automatically modified.

replicaof 127.0.0.1 6380

Pointed to the new host. Start the original master again, the original master will become the slave.

to sum up:

Master-slave cluster: The host has write permissions. There is no slave, only readable.

Unexpected downtime scenario:

Manual recovery: manually restart the server, the host is down, and the slave is set as the host.

Automatic recovery: use sentinel monitoring. Automatically switch between master and slave

Guess you like

Origin blog.csdn.net/weixin_43515837/article/details/113096021