Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A brief explanation of how to do Redis sentinel

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to carry out a simple explanation of Redis sentinel, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The principle is that you need to be able to install the REDIS server and configure the master-slave relationship. Redis has two kinds of high availability, redis cluster and redis sentinel. Today we are going to talk about redis's sentinel. Redis sentinel is a redis high-availability function that has been provided since redis 2.8.There are a few problems.

It has been suggested that redis is a cached database, so why should it be highly available? just restart it and use its own log to recover. In fact, this is mainly related to the scenarios used by redis. For example, in architecture design, redis is a design closely related to the application, which makes the system easy to access with high concurrency and high density. And it is also a layer of buffer in the high concurrency of traditional databases and application systems, and the redis designed in many systems is very important. If redis stops working, the whole application will be in a state of shutdown, so even though redis can quickly restart, read relevant logs and restore to the state before failure, some applications are designed to be low tolerance, in order to ensure that the business redis must be designed into a highly available state.

First of all, install redis and configure master and slave. There are two nodes as examples, redis 1 and redis 2 as examples.

Redis 1

Bind 0.0.0.0

Port 6379

Timeout 10

Daemonize yes

Loglevel notice

Logfile "/ redis_data/errorlog.log"

Save 900 1

Save 300 10

Save 60 10000

Dbfilename "dump.rdb"

Dir "/ redis_data"

Masterauth "password"

Replica-priority 100

Requirepass "password"

Maxmemory xxxxxxxkb

Appendonly yes

Appendfilename "appendonly.aof"

Appendfsync everysec

Redis2

Bind 0.0.0.0

Port 6379

Timeout 10

Daemonize yes

Loglevel notice

Logfile "/ redis_data/errorlog.log"

Save 900 1

Save 300 10

Save 60 10000

Dbfilename "dump.rdb"

Dir "/ redis_data"

Masterauth "password"

Replica-priority 100

Requirepass "password"

Maxmemory xxxxxxxkb

Appendonly yes

Appendfilename "appendonly.aof"

Appendfsync everysec

Slaveof 192.168.198.100 6379

With the completion of the configuration, we will not repeat the problem of individual installation of the redis database.

The redis sentinel node itself is flexible to install, in fact, it does not have to be installed in the node of the redis database itself, but most of the habits are installed in the database server with redis. Redis sentinel itself is a distributed architecture, which contains several sentinel nodes and redis nodes. For high availability, the sentinel of redis should be at least 3 nodes, which is in line with most principles. But the actual part of the redis sentinel deployment is two nodes. Each sentinel node monitors the data node and other sentinel nodes. When a node is out of control, the sentinel will be triggered and negotiate with other sentinel nodes to decide, and start the failover, the whole process is completely automatic.

Summarize the functions of sentinel nodes

1 regular monitoring of sentinel nodes periodically detect redis data nodes and other sentinel nodes

2 sentinel will notify the application of the result of node failover

The promotion of failover from node to master node also needs to be set up by sentinel.

Here is the configuration file

Port 26379

Port number for sentinel access

Daemonize yes

Release to run on the backend

Pidfile "/ var/run/redis-sentinel.pid"

Logfile "/ redis_data/sential.log"

Log file storage location

Dir "/ redis_data"

Sentienl data directory

Sentinel monitor redis1 192.168.198.100 6379 1

Sentinel monitors the host address, port number, and several sentinel nodes to determine whether the redis host fails to switch.

Sentinel down-after-milliseconds redis1 5000

How long will it take to connect to redis primary server and start preparing for switching?

Sentinel parallel-syncs redis 1

In the case of multiple copies, how many copies point to the new master after the host is switched

Sentinel auth-pass redis1 1234.com

Password for authentication before sentinel

Requirepass 1234.com

Password for access between sentinel and redis

There are two ways to deploy sentinel

1 for multiple groups of redis, you can deploy three-node sentinel, and you can manage multiple groups of redis master-slave replication through a group of three sentinel nodes.

2 deploy sentinel and redis on the same server for a small number of redis master-slave replication

On the redis node that is already running, after running sentinel, the log graph

In fact, after sentinel starts, its own sentinel.conf will be changed. The above information is added to the sentinel.conf file by sentinel after starting the sentinel service.

Some processes of sentinel in Migration

Here is the beginning of sentinel.c, where the parameters passed to sentinel

It defines how to mark the information of the current node in the incoming parameters, in the passed-in binary number.

Is the first machine master?

The second machine release is slave.

Is the third-bit machine sentinel?

Is the fourth machine subjective down?

The fifth place machine release is objective down.

The sixth sentinel confirms master down

The seventh failover is in progress.

Which slave is the main choice in the eighth place?

Which new master sends slaveof to others from

The tenth place from the progress of synchronization

The synchronization of the eleventh new master and follower

The 12th new owner of master takes effect.

13th SCRIPT KILL already sent on-BUSY

The next two questions about the status of the redis server in sentinal

SDOWN: subjectively down subjective invalidation, since the current sentinel is unavailable to the state of the redis server. And ODOWN: objectively down means that most (at least not one) sentinel thinks that the redis of this node has expired.

When sentinel produces objectively down, the operation of failover will be carried out.

So where does the state of SDOWN come from? in REDIS, the master and slave server will establish a TCP connection and send ping periodically. The default is 1 second. If there is no response between each other during the down-after-milliseconds time, sdown will be enabled. If the sdown is the master server, it is determined that the sentinel of the primary DOWN will start to send is-master-down-by-addr messages. When most of the sentinel is confirmed, ODOWN will be generated and the FAILOVER operation will be started.

First of all, how do sentinel communicate with each other

1 each sentinel indicates that it communicates with the REIDS master for the first time in the configuration

2 each sentinel will send its own hello message, its own ip and port

3 through the medium of redis primary, sentinel communicate with each other.

(4) through the information communication between redis primary and SLAVE, redis primary also gets the information of SLAVE, and sentinel gets the information of slave through redis primary and begins to communicate.

Initial configuration of sentinel

Port 26379

Pidfile "/ redis_data/sentinel.pid"

Dir "/ redis_data"

Daemonize yes

Protected-mode no

Logfile "/ redis_data/sentinel.log"

Sentinel myid 2a9dca6062c5bc4d8811c614970c6aa1d38472b5

Sentinel deny-scripts-reconfig yes

Sentinel monitor redisMaster 192.168.198.101 6379 2

Sentinel down-after-milliseconds redisMaster 1000

Sentinel failover-timeout redisMaster 6000

Sentinel client-reconfig-script redisMaster / redis_data/vip.sh

Requirepass "1234.com"

# Generated by CONFIG REWRITE

Maxclients 4064

Information added by sentinel after running

Sentinel auth-pass redisMaster 1234.com

Sentinel config-epoch redisMaster 7

Sentinel leader-epoch redisMaster 7

Sentinel known-replica redisMaster 192.168.198.100 6379

Sentinel known-replica redisMaster 192.168.198.102 6379

Sentinel known-sentinel redisMaster 192.168.198.102 26379 711a3d1d1e216abc13c275de972f15307c4a7c30

Sentinel known-sentinel redisMaster 192.168.198.101 26379 f698f72600e34df22b878a57a4583fa98200b1d9

Sentinel current-epoch 7

Script for IP switch

#! / bin/bash

MASTER_IP=$ {6}

MY_IP='192.168.198.101' # IP of each Server itself

VIP='192.168.198.106' # VIP

NETMASK='24' # Netmask

INTERFACE='ens33' # interface

If [${MASTER_IP} = ${MY_IP}]; then

Sudo / sbin/ip addr add ${VIP} / ${NETMASK} dev ${INTERFACE}

Sudo / sbin/arping-Q-c 3-A ${VIP}-I ${INTERFACE}

Exit 0

Else

Sudo / sbin/ip addr del ${VIP} / ${NETMASK} dev ${INTERFACE}

Exit 0

Fi

Exit 1

Most of the problems are mainly in the switching scripts.

MASTER_IP=$ {6}, the following figure is a specific explanation of the configuration file. Several parameters are passed when the script is executed.

1 master-name

2 role

3 state

4 from-ip

5 from-port

6 to-ip

7 to-port

So there's a problem here. Are all sentinel executed when reconfig.sh is executed?

If all are executed several times.

In fact, this problem does not have to look at the source code, directly deploy printed statements to the log in the reconfig script, similar to the way we debug some stored procedures.

Finally, with my above configuration, the reconfig script was executed three times on the node except primary.

In addition, for the question of how to test, of course, you can turn off REIDS through the command, but there is also a better way to test whether the system is configured successfully and can be switched.

The DEBUG sleep second mode allows the main redis to trigger your switch when the time you set is unresponsive.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report