“This is the fourth day of my participation in the Gwen Challenge.

preface

Single Redis construction: Redis installation and boot from the boot integration of Springboot actual combat, Redis installation.

Server Planning

  • 192.168.79.120: Master + Sentinel
  • 192.168.79.121: Slave + Sentinel
  • 192.168.79.122: Slave + Sentinel

Sentinel can be deployed as a separate cluster, in this case on the same machine as the Redis instance. We have to have an odd number of sentinels because we’re designing to vote for the Master.

Configure one master and two slave nodes

Modifying a Configuration File

192.168.79.120

Bind 0.0.0.0 dir /usr/local/redis/db masterauth root1234 replica-read-only Yes requirepass root1234 appendonly yesCopy the code

192.168.79.121

Bind 0.0.0.0 dir /usr/local/redis/db masterauth root1234#Slave read-onlyReplica -read-only yes requirepass root1234 appendonly Yes replicaof 192.168.79.120 6379Copy the code

192.168.79.122

Bind 0.0.0.0 dir /usr/local/redis/db masterauth root1234#Slave read-onlyReplica -read-only yes requirepass root1234 appendonly Yes replicaof 192.168.79.120 6379Copy the code

When the configuration is complete, restart Redis

/etc/init.d/redis stop
/etc/init.d/redis start
Copy the code

Connect to Redis, and the info Repliccation command is used to check. As shown in the following figure, the configuration is successful. Next, configure Sentinel.

Configuration Sentinel

Redis Sentinel provides high availability for Redis. In practice, this means that using Sentinel allows you to create Redis deployments that are resistant to certain types of failures without human intervention. When configuring the architecture of Redis with one Master and many slaves, we will do read/write separation in many cases, Master only writes, Slave only reads. When the Master is down and becomes unavailable, the whole Redis cluster becomes unavailable. In this case, Sentinel is used. When the Master is unavailable, the Sentinel cluster elects a new Master from the Slave to make the Redis cluster available. It is also the official Redis high availability solution.

The Sentinel configuration is as follows:

sentinel auth-pass mymaster root1234 sentinel down-after-milliseconds mymaster 10000 sentinel monitor mymaster 192.168.79.120 6379 2 bind 0.0.0.0 daemonize yes logfile/usr/local/redis/sentinel/redis - sentinel. Log dir /usr/local/redis/sentinelCopy the code

With Sentinel enabled on each machine, you can view the Sentinel logs.

[root@localhost sentinel]# tail -f redis-sentinel.log 4593:X 03 May 2021 22:32:35.051 # Configuration loaded 4594:X 03 May 2021 22:32:35.063 * Increased maximum number of open files to 10032 (it was originally set to 1024). 4594:X 03 May 2021 22:32:35.065 * Running mode=sentinel, port= 26379.4594 :X 03 May 2021 22:32:35.065 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 4594: X 3 May 2021 22:32:35. 065 # Sentinel ID is 5 b9c2ed15b4a31394a9b3b75f46bbe98a4bd9078 4594: X 3 May 2021 22:32:35.065 # +monitor master myMaster 192.168.79.120 6379 Quorum 2 4594:X 03 May 2021 22:32:35.065 * +slave slave 192.168.79.121:6379 192.168.79.121 6379 @mymaster 192.168.79.120 6379 4594:X 03 May 2021 22:32:35.068 * +slave slave 192.168.79.122:6379 192.168.79.122 6379 @myMaster 192.168.79.120 6379 4594:X 03 May 2021 22:32:37.060 * +sentinel Sentinel b5b7d7bea19d8e34d7936a8cdc683472b61fb4af 192.168.79.121 26379 @ mymaster 192.168.79.120 4594:6379 X 3 May 2021 22:32:37. 137 * + sentinel sentinel 5830 ec2a347b91d24bb3a2ffce0a2a68f03d0216 192.168.79.122 26379 @ mymaster 192.168.79.120 6379Copy the code

Description:

192.168.79.120 6379 Redis name myMaster, with two copies

+monitor master mymaster 192.168.79.120 6379 quorum 2

In the next two, we have two slaves joining the Master. The Master name is mymaster and the location is 192.168.79.120 6379

+slave slave 192.168.79.121:6379 192.168.79.121 6379 @myMaster 192.168.79.120 6379

+slave slave 192.168.79.122:6379 192.168.79.122 6379 @myMaster 192.168.79.120 6379

The next two, we have two Sentinels. The Master name is mymaster and the location is 192.168.79.120 6379

26379 @ + sentinel sentinel b5b7d7bea19d8e34d7936a8cdc683472b61fb4af 192.168.79.121 mymaster 192.168.79.120 6379

5830 + sentinel sentinel ec2a347b91d24bb3a2ffce0a2a68f03d0216 192.168.79.122 26379 @ mymaster 192.168.79.120 6379

Demonstrate Master fault

At this point, turn off Master and review the Redis cluster

The Sentinel log output: 192.168.79.120:6379 is not available and myMaster is switched to 192.168.79.122:6379. Sentinel guarantees the availability of the Redis cluster. When 192.168.79.120:6379 is restarted, the node is demoted to Slave. This can also be seen in the Sentinel log.

4594:X 03 May 2021 22:46:24.333 # +sdown Master MyMaster 192.168.79.120 6379 4594:X 03 May 2021 22:46:24.365 # 3 4594 + new epoch: X 3 May 2021 22:46:24. 365 # + vote - for - leader ec2a347b91d24bb3a2ffce0a2a68f03d0216 3, 4594:5830 X 3 May 2021 22:46:24.396 # +odown Master MyMaster 192.168.79.120 6379 #quorum 3/2 4594:X 03 May 2021 22:46:24.397 # Next failover delay: I will not start a failover before Mon May 3 22:52:24 2021 4594:X 03 May 2021 22:46:24.812 # +config-update-from Sentinel 5830 ec2a347b91d24bb3a2ffce0a2a68f03d0216 192.168.79.122 26379 @ mymaster 192.168.79.120 4594:6379 X 3 May 2021 22:46:24.812 # +switch-master myMaster 192.168.79.120 6379 192.168.79.122 6379 4594:X 03 May 2021 22:46:24.812 * +slave Slave 192.168.79.121:6379 192.168.79.121 6379 @mymaster 192.168.79.122 6379 4594:X 03 May 2021 22:46:24.812 * +slave Slave 192.168.79.120:6379 192.168.79.120 6379 @mymaster 192.168.79.122 6379 4594:X 03 May 2021 22:46:34.861 # +sdown Slave 192.168.79.120:6379 192.168.79.120 6379 @mymaster 192.168.79.122 6379Copy the code

Integrated SpringBoot

<dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Copy the code

In the configuration file

spring:
  redis:
    database: 1
    password: root1234
    sentinel:
      master: mymaster
      nodes: 192.16879.120.: 26379192168 79.121:26379192168 79.122:26379
Copy the code

In a cluster using Sentinel, our client reads and writes to Redis via Sentinel, so Sentinel instances need to be configured. This allows Redis to be operated via SpringBoot’s RedisTemplate.