Redis-simple and intuitive look at the effect of sentry

Sentinel is also a separate redis process. It does not provide external services. It is mainly used to monitor the operation of the primary database and the secondary database. Then, when the primary database fails, it actively upgrades the secondary database to the primary database, avoiding manual switching Operation.

First start a redis instance with port 6379 as the primary database, and then use the following two commands to start two redis services, the ports are 6380 and 6381 respectively, and they are designated as the slave databases of the redis service on port 6379:

redis-server /usr/local/etc/redis.conf --port 6380 --slaveof 127.0.0.1 6379
redis-server /usr/local/etc/redis.conf --port 6381 --slaveof 127.0.0.1 6379

Check the replication information of the master database with port 6379 Insert picture description here
at this time : you can find that the master database has two slave databases at this time, the ports are 6380 and 6381 respectively.

Next, configure the sentinel and create a configuration file named sentinel.conf. The content of the file is:

# 配置监听的主服务器
sentinel monitor masterone 127.0.0.1 6379 1
sentinel monitor 表示哨兵监控

masterone表示自己要监控的主数据库的名字,可以自定义。不过这个名字必须只能由大小写字母、数字和“.-_”这 3 个字符组成。

127.0.0.1 6379表示要监控服务的ip地址和端口号。

1表示最低通过票数,代表只有1个或1个以上的哨兵认为主数据库主观下线的时候,才会进行故障切换(failover)操作。

As follows: Insert picture description here
Use the command to redis-sentinel +哨兵配置文件的地址start the sentry service, as follows: Insert picture description here
output the following information:Insert picture description here

1表示哨兵进程的运行id
2表示哨兵经常监控的主数据库
3,4表示哨兵已经自动发现了它监控的主数据库下的两个从数据库

Then close the main database with port 6379, as follows: Insert picture description here
wait for a period of time, the default is 30 seconds, the sentinel will appear as follows:Insert picture description here

1的+sdown表示sentinel主观上的认为6379端口的服务已经停止了,这是sentinel自己作出的判断。
2的+odown表示sentinel客观上的认为6379端口的服务已经停止了,不过客观下线是需要一定数量的sentinel达成一致意见才能认为一个master已经停止了服务,由于此时只有一个sentinel,所以也做了客观下线。
3表示达到了failover条件,要开始执行故障恢复,等待其他sentinel的选举,挑选出一个从数据库,将其升格为主数据库。
4表示故障恢复完成。
5表示从数据库127.0.0.1:6380被升格为了主数据库。
6,7表示新的主数据库有两个从库,分别为127.0.0.1:6379 ,127.0.0.1:6381,此时127.0.0.1:6379 服务虽然已经断开,但是哨兵并没有清除该无效实例,是为了实例恢复后,方便重新加入时以从数据库的身份继续服务。
8表示发现127.0.0.1:6379服务已经宕机,等待恢复。

At this time, check the replication information of the service with port 6380, as follows: Insert picture description here
you can find that the service on port 6380 is the primary database at this time, and there is a connected slave library, which is the redis service with port 6381.

At this point, restart the 6379 service and look Insert picture description here
at the output of the sentinel: you can see that the redis instance with port number 6379 has resumed service by looking at the output.

Then look at the replication information of the 6380 instance as the primary database at this time, as follows: Insert picture description here
After the redis instance with the port number 6379 is found to be restored, it is set as the slave database of the service instance with the port number 6380.

Finally, look at the replication information of the redis instance with port number 6379, as follows: Insert picture description here
you can see that the role of the redis instance with port number 6379 is already the slave database, and its primary database is the service instance with port number 6380.

Guess you like

Origin blog.csdn.net/weixin_38106322/article/details/108522417