Redis Detailed Introduction to Sentinel

无忌哥哥
Release: 2023-04-03 06:40:02
Original
3226 people have browsed it

Directory

  • Configure redis master-slave replication

    • ##Use the ping command to check whether it is started

    • View link information on the master node

  • Start deploying sentinel node

  • Deploy sentinel

  • Start sentinel

  • Demonstrate failover

    • ##View the node information currently monitored by sentinel
    • Interrupt the main node of redis
    • Verify whether the main node of redis has changed
    • This is the end of the introduction to sentinel, not enough Corrections are welcome.
    Configure redis master-slave replication

One master node (port 6379), two slave nodes (port 6380, 6381), no For configuration, please see my previous article on redis master-slave replication.

Use the ping command to check whether it is started

redis-cli -h 127.0.0.1 -p 6379 ping

As follows, return PONG to indicate that all three redis services have been started Startup completed

One master node:


Redis Detailed Introduction to SentinelTwo slave nodes:


Redis Detailed Introduction to Sentinel

Redis Detailed Introduction to SentinelView the link information on the master node

查看6379的连接信息 命令:info replication 可以发现6379的角色是master,有两个从节点,也就是端口为6380,6381的从节点(redis)
Copy after login

同样在6380 从节点中,该节点的角色是slave(从节点),与主节点的连接状态为up(master_link_status),通过这个命令可以方便的查看各个节点的连接状态等信息。
Copy after login
Redis Detailed Introduction to Sentinel

除了用info replication命令外,如下命令也可以快速查看链接信息 redis-cli -h 127.0.0.1 -p 6381 info replication 看个人喜好用哪个命令。
Copy after login
Redis Detailed Introduction to Sentinel

Redis Detailed Introduction to SentinelStart deploying the sentinel node

Through sentinel node deployment, redis master-slave reset can be made more highly available. So what is the use of sentinel? From the literal meaning, sentinel means "sentinel", which is to monitor redis nodes. , the redis node monitored by sentinel is the master node. Sentinel has the function of failover. What is failover? When the redis node monitored by sentinel is unexpectedly interrupted, sentinel will automatically select a redis with the role of slave node as the master node to maintain the subsequent and ensure the correct master-slave replication relationship, and the original slave node is still a slave node, but Their master node becomes the redis service of the newly upgraded master node. Let's start building sentinel step by step.

Deploy sentinel
进入安装redis的目录下的配置目录,我的本地是/etc/redis
Copy after login

我这边就直接使用原本的sentinel.conf配置,来作为第一个sentinel节点。我们可以进入sentinel.conf配置看下里面到底有啥配置信息: sentinel monitor mymaster 127.0.0.1 6379 2 //监控的主节点IP和端口,其中mymaster是该节点的别名,2表示判断主节点失败至少需要两个节点同意 sentinel down-after-milliseconds mymaster 30000 //通过ping命令,各个sentinel节点向redis节点是否可达,超过30000毫秒,就判定不可达 sentinel failover-timeout mymaster 180000 //故障转移超时时间为180000毫秒 如果还有其它的配置,可以自己在探索下
Copy after login
Redis Detailed Introduction to SentinelStart sentinel
启动命令: sudo redis-sentinel sentinel.conf --sentinel
Copy after login

查看sentinel启动情况 redis-cli -h 127.0.0.1 -p 26379 INFO Sentinel 可以看出与查看redis启动的情况命令类似,截图中可以看到此时sentinel监控redis节点是端口为6379的服务,6379有两个从节点,状态为OK,别名是mymaster
Copy after login
Redis Detailed Introduction to Sentinel

继续再配置两个sentinel节点, sudo cp sentinel.conf sentinel2.conf sudo cp sentinel.conf sentinel3.conf 修改两个配置的端口为26380, 26381,其它配置可以不用修改 修改完后,分别启动 sudo redis-sentinel sentinel2.conf --sentinel sudo redis-sentinel sentinel3.conf --sentinel
Copy after login
Redis Detailed Introduction to Sentinel

启动后再次查看sentinel.conf的配置,可以发现配置中多了两个从节点的信息,截图如下:
Copy after login
Redis Detailed Introduction to Sentinel

Redis Detailed Introduction to Sentinel

##Demo failoverRedis Detailed Introduction to Sentinel

View the current sentinel monitoring Node information

查看26379端口的Sentinel节点监控的主节点信息
Copy after login

查看目前redis的进程ps -ef | grep redis
Copy after login

Redis Detailed Introduction to Sentinel

Interrupt redis master nodeRedis Detailed Introduction to Sentinel

之前我们sentinel监控的主节点是6379这个端口,这时候我们kill这个端口的进程,或者使用shutdown命令: redis-cli -h 127.0.0.1 -p 6379 shutdown 过了30秒之后查看26379端口的Sentinel节点监控的主节点信息,发现主节点信息变成了6380端口的redis(之前的从节点)
Copy after login

Verification Whether the main node of redis changesRedis Detailed Introduction to Sentinel

验证6380端口的redis变成主节点,此时我们在6380 的redis上设置一个key值,在6381端口的redis上验证是否能获取到该key值
Copy after login

此时发现,6380变成了主节点,6381从节点没有变化,还是从节点。 下面我再次启动6379端口的redis
Copy after login

Redis Detailed Introduction to Sentinel

发现原先的主节点6379已经变成了从节点,而它的主节点变成了6380。此时我们再回过头看下26379的监控节点变化,还是6380端口。
Copy after login

Redis Detailed Introduction to Sentinel

Redis Detailed Introduction to Sentinel
To this point sentinel That’s it for the introduction. Any shortcomings are welcome to be corrected.Redis Detailed Introduction to Sentinel

The above is the detailed content of Redis Detailed Introduction to Sentinel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!