Establishment and use of sentinel mode of redis high availability cluster

1, Briefly talk about the redis sentinel sentinel mode

1. Make the redis cluster more robust and highly available. Compared with redis cluster, it can automatically failover and automatically transform the master
2. Its workflow is that Sentinel will constantly check whether your master server and slave server are operating normally. If there is a failure, Sentinel will recommend a new master to ensure high service availability and automation

Monitoring: Sentinel will constantly check whether your master server and slave server are working normally.

Notification: when a monitored Redis server has a problem, Sentinel can send a notification to the administrator or other applications through the API.

Automatic failover (automatic failover): when a master server fails to work normally, Sentinel will start an automatic failover operation. It will upgrade one of the failed master servers from the server to a new master server, and change other slave servers of the failed master server to copy the new master server. When the client attempts to connect to the failed master server, the cluster will The address of the new master server will also be returned to the client, so that the cluster can use the new master server instead of the failed server.

3. There are also some disadvantages. There is only one master, so there will be a performance bottleneck.

4. Sentry workflow:

After the sentinel process is started, two links will be established with the master:

1). It is used to obtain other sentinel information that is also monitoring the redis system

2). Send an info command to get the information of the redis system master itself

After establishing the link with the master, the Sentry will do the following three things regularly:

1). The info command is sent to the master and slave every 10 seconds

2). It sends its own information to the master and slave every 2 seconds

3). ping commands will be sent to the master, slave and other sentinels who are also monitoring the redis system every 1 second

The info command allows the sentry to obtain the information of the current database, such as running id, copy information, etc. enter the corresponding node and enter info to view it

Sentinel election process

1). The first sentinel to find the master hung up sends commands to each sentinel to let the other party elect itself to be the leading sentinel

2.) if other sentinels have not elected others, they will vote for the first sentinel who finds that the master is dead

3). The first sentinel who finds that the master is hung up. If it is found that more than half of the Sentinels are cast to him, and the number of sentinels exceeds the set quoram parameter, the sentinel becomes the leading sentinel

4). If multiple sentinels participate in the election at the same time, the process will be repeated until a leading sentinel is selected. After the leading sentinel is selected, the fault repair will start, and a slave database will be selected as the new master

master election process

1. Select the slave database with the highest priority from all online slave databases

2. If there are multiple slave databases with high priority, the offset will be judged and the slave database with the smallest offset will be selected. The offset here is incremental replication

3. If there are still slave databases with the same conditions, you will choose to upgrade from the database to master with a smaller running id

2, The server builds redis instances and sentinel sentries

1.redis configuration. redis configuration is configured according to a single example, rather than the cluster configuration of cluster.
redis.conf example:

port 6382
daemonize yes
requirepass "root123"
#Bind current machine IP
bind 172.22.169.168
#
##Data file storage location
dir "/root/redis-cluster/redis6382/data"
#
##pid 6382 and port should correspond
pidfile "/root/redis-cluster/redis6382/data/redis_6382.pid"
#
##Do not start cluster mode
cluster-enabled no
##6382 and port should correspond
cluster-config-file "47.119.159.55_6382.conf"
cluster-node-timeout 15000
appendonly yes
#Set the password of the master node
masterauth "root123"

Other redis nodes modify ports according to this. If they are different hosts, each host can also be configured this way.

sentinel.conf example:

port 16382
#assign work directory
dir "/root/redis-cluster/redis6382/data"
logfile "./sentinel.log"
#Specify the number of port sentinels of the alias master node address (several sentinels monitor that the master node is down to perform the transfer)
sentinel monitor mymaster 47.119.159.55 6382 1

#If the sentinel does not receive the heartbeat of the primary node within 3s, the sentinel will think that the primary node is down. The default is 30 seconds
sentinel down-after-milliseconds mymaster 3000
#After a new master node is selected, the number of slave nodes that can be connected at the same time

#If the master still does not come back to life after 10 seconds, start failover. The default is 180s
sentinel failover-timeout mymaster 10000
#Configure the password of the primary node connecting to redis
sentinel auth-pass mymaster root123
#Note: we will start four redis instances later. Among them, redis on port 7501 is set as master and the other three are set as slave.
#Therefore, mymaster is followed by the ip and port of the master. The last '2' means that I want to start. As long as two sentinel s think that the master is offline, it is considered that the master is offline
#Start failover and elect a new master. Usually, the last parameter cannot be more than the number of sentinel instances started. It is recommended to start at least three sentinel instances.

Start the redis node:

./src/redis-server ./redis.conf

After all redis nodes are connected, if you want to use the redis instance of server A as the master node, you can enter the redis nodes of other servers and set it as the slave node of server A's redis:

./src/redis-cli -h 120.77.46.185 -p 6382 -a root123 

Enter the command after entering:

SLAVEOF A 6382

A is the server and 6382 is the redis instance port
Example:

Indicates 120.77 46.185 the redis instance with server port 6382 becomes 47.119 159.55 the slave node of the redis instance with server port 6382, i.e. the former 120.77 46.185 becomes 47.119 159.55 slave node.

You can also add the following to the conf file directly before the redis instance starts without setting:

slaveof 47.119.159.55 6382  

Means 47.119 159.55 slave node of the instance. Simply put, it is set to 47.119 159.55 slave node

View node information:

info Replication


Start sentinel

./src/redis-sentinel ./sentinel.conf

When it's done, a highly available redis sentinel cluster will be built!

3, springboot integration

The config configuration of redis is the same as the single instance mode. You only need to modify the of yml

spring
 redis:
    #Sentry configuration
    sentinel:
      master: mymaster
      nodes: 120.77.46.185:16382
      password: root123
    lettuce:
      pool:
        max-active: 1000  #Maximum number of connections in the connection pool (negative value indicates no limit)
        max-idle: 10   #Maximum free connections in the connection pool
        min-idle: 5       # Minimum free connections in connection pool
        max-wait: -1    # Maximum blocking wait time of connection pool (negative value indicates no limit)
    timeout: 5000
    password: root123  #redis master node password

Keywords: Redis Spring Boot Distribution Middleware

Added by jamesmage on Wed, 15 Dec 2021 16:57:54 +0200