Redis以主从的模式搭建集群后,如果主节点Master挂掉,虽然可以实现将备用节点Slave切换成主节点,但是Redis本身并没有自动监控机制,需要借助Sentinel哨兵模式,实现监控并实现自动切换。为了实现Sentinel的高可用,需要sentinel也以集群模式来搭建,这里通过一台机器的不同端口来模拟。相关环境信息如下:

1、Redis集群信息:

角色 IP地址 监听端口
Master 127.0.0.1 6379
Slave 127.0.0.1 6380
Slave 127.0.0.1 6381

Sentinel集群信息:

哨兵角色 IP地址 监听端口
Node-1 127.0.0.1 26379
Node-2 127.0.0.1 26380
Node-3 127.0.0.1 26381

2、Redis集群搭建过程参考上篇,这里不再描述。首先启动Redis的主节点和两个Slave节点。

进入Master节点,查看信息:

 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p  -a  funnyboy
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:> info replication
# Replication
role:master
connected_slaves:
slave0:ip=127.0.0.1,port=,state=online,offset=,lag=
slave1:ip=127.0.0.1,port=,state=online,offset=,lag=
master_replid:d5802af0905736ae28201050ce4871ee2921c16c
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
127.0.0.1:>

显示当前节点role为master,并且连接的slave个数为2,OK。

分别进入两个slave节点查看信息:

 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p  -a  funnyboy
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_replid:d5802af0905736ae28201050ce4871ee2921c16c
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
127.0.0.1:>
 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p  -a  funnyboy
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_replid:d5802af0905736ae28201050ce4871ee2921c16c
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
127.0.0.1:>

角色都为slave,并且master信息正常。确保Redis集群OK后,开始准备搭建sentinel集群。

3、Sentinel集群搭建

step1、将redis-sentinel拷贝到redis对应的执行目录bin(该命令是redis-server的一个连接,用redis-server也OK,后面讲到,启动sentinel会有两种方式),然后拷贝sentinel.config到redis配置文件目录config。

step2、分别编辑sentinel的配置文件(没特别强调的保持默认即可)

sentinel-26379.conf配置如下:

 daemonize yes                                            #开启后台守护进程
port #端口配置
pidfile "/usr/local/redis/pid/redis-sentinel-26379.pid" #PID文件
logfile "/usr/local/redis/logs/sentinel-26379.log" #日志文件
sentinel monitor mymaster 127.0.0.1 #哨兵监控配置。注意,如果配置了认证,改配置必须在auth-pass配置之前,否则启动报找不到master的错误
sentinel auth-pass mymaster funnyboy #认证配置
sentinel down-after-milliseconds mymaster    #master或者slave多少时间(默认30秒)不能使用标记为down状态。
sentinel failover-timeout mymaster    #若哨兵在配置值内未能完成故障转移操作,则任务本次故障转移失败。

sentinel-26380.conf配置如下:

 daemonize yes                                            #开启后台守护进程
port #端口配置
pidfile "/usr/local/redis/pid/redis-sentinel-26380.pid" #PID文件
logfile "/usr/local/redis/logs/sentinel-26380.log" #日志文件
sentinel monitor mymaster 127.0.0.1 #哨兵监控配置。注意,如果配置了认证,改配置必须在auth-pass配置之前,否则启动报找不到master的错误
sentinel auth-pass mymaster funnyboy #认证配置
sentinel down-after-milliseconds mymaster    #master或者slave多少时间(默认30秒)不能使用标记为down状态。
sentinel failover-timeout mymaster    #若哨兵在配置值内未能完成故障转移操作,则任务本次故障转移失败。

sentinel-26381.conf配置如下:

 daemonize yes                                            #开启后台守护进程
port #端口配置
pidfile "/usr/local/redis/pid/redis-sentinel-26381.pid" #PID文件
logfile "/usr/local/redis/logs/sentinel-26381.log" #日志文件
sentinel monitor mymaster 127.0.0.1 #哨兵监控配置。注意,如果配置了认证,改配置必须在auth-pass配置之前,否则启动报找不到master的错误
sentinel auth-pass mymaster funnyboy #认证配置
sentinel down-after-milliseconds mymaster    #master或者slave多少时间(默认30秒)不能使用标记为down状态。
sentinel failover-timeout mymaster    #若哨兵在配置值内未能完成故障转移操作,则任务本次故障转移失败。

step3、启动哨兵监控程序:

  [root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-.conf
[root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-.conf
[root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-.conf

启动有两种方式:

一是执行:redis-sentinel  sentinel.conf

二是执行:redis-server sentinel --sentinel

step4、通过哨兵连接,并检查信息:

 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p  -a funnyboy
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:> info sentinel
# Sentinel
sentinel_masters:
sentinel_tilt:
sentinel_running_scripts:
sentinel_scripts_queue_length:
sentinel_simulate_failure_flags:
master0:name=mymaster,status=ok,address=127.0.0.1:,slaves=,sentinels=
127.0.0.1:>

可以看到监控的redis服务,一个Master、两个Slave、sentinels = 3 说明配置OK。

4、模拟场景:Redis Master节点挂掉,查看Redis集群状态。

step1、关掉Master节点:

 [root@VM_0_14_centos bin]# ./redis-cli -p  -a funnyboy
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:> shutdown
not connected>
[root@VM_0_14_centos bin]#

step2、通过哨兵查看集群状态:

 127.0.0.1:> info sentinel
# Sentinel
sentinel_masters:
sentinel_tilt:
sentinel_running_scripts:
sentinel_scripts_queue_length:
sentinel_simulate_failure_flags:
master0:name=mymaster,status=ok,address=127.0.0.1:,slaves=,sentinels=
127.0.0.1:>

通过sentinel信息可以看到,Master节点已经自动切换到6380端口了,说明主节点挂掉后,6380 Slave节点自动升级成为了Master节点。

step3、启动6379 redis服务,然后查看节点角色,此时6379变成了Slave,6380为Master节点,OK。

 [root@VM_0_14_centos redis]# ./bin/redis-server ./config/redis-.conf
[root@VM_0_14_centos redis]# ./bin/redis-cli -p -a funnyboy
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:>
127.0.0.1:> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_replid:c77763408266bcebf233bdc9e59e3bcf14dc7a08
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:

Redis笔记-Sentinel哨兵模式的更多相关文章

  1. SpringBoot进阶教程(三十)整合Redis之Sentinel哨兵模式

    Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案时,假如master宕机了,redis本身(以及其很多客户端)都没有实现自动进行主备切换,而 ...

  2. 【Redis】Sentinel 哨兵模式

    Sentinel(哨兵模式) 目录 Sentinel(哨兵模式) 哨兵模式的三个定时任务 Sentinel(哨兵)与Sentinel .主服务器.从服务器之间的连接 检测下线状态 选择领头 Senti ...

  3. (六) Docker 部署 Redis 高可用集群 (sentinel 哨兵模式)

    参考并感谢 官方文档 https://hub.docker.com/_/redis GitHub https://github.com/antirez/redis happyJared https:/ ...

  4. Linux基于Docker的Redis主从复制、哨兵模式搭建

    本教程基于CentOS7,开始本教程前,请确保您的Linux系统已安装Docker. 1.使用docker下载redis镜像 docker pull redis 安装完成后,使用docker imag ...

  5. Redis——(主从复制、哨兵模式、集群)的部署及搭建

    Redis--(主从复制.哨兵模式.集群)的部署及搭建 重点: 主从复制:主从复制是高可用redis的基础,主从复制主要实现了数据的多机备份,以及对于读操作的负载均衡和简单的故障恢复. 哨兵和集群都是 ...

  6. 浅谈:redis的主从复制 + 哨兵模式

    浅谈:redis的主从复制 + 哨兵模式 主从模式 ​ 在谈论redis的主从复制之前,我们先回想下mysql的主从搭建过程,第一步呢首先要在主库服务器中修改my.cnf,开启一下bin_log功能, ...

  7. Redis sentinel 哨兵模式集群方案配置

    第一个方案是创建 redis cluster,第二种方案就是用哨兵模式来进行主从替换以及故障恢复.兵模式集群方案配置 一.sentinel介绍 Sentinel作用: 1):Master状态检测 2) ...

  8. Redis sentinel 哨兵模式

    一.sentinel介绍 Sentinel作用: 1):Master状态检测 2):如果Master异常,则会进行Master-Slave切换,将其中一个Slave作为Master,将之前的Maste ...

  9. redis sentinel哨兵模式集群搭建教程

    1.环境说明 我们将使用192.168.220.128.192.168.220.129两台机器搭建sentinel交叉主从为例 当前我们已在192.168.220.128上按redis安装教程安装了r ...

随机推荐

  1. SQLite 的 EXISTS 与 NOT EXISTS

    话不多说先来看看表结构: 显而易见 Pid 存放的 Person 的 id :下面重点(奇葩需求!!!!) 我需要向表一(Person)里插入几条数据(...)这时候不会对表二做任何操作. 需求:查询 ...

  2. windows 无法启动网络发现

    1.先启动三个服务,按顺序启动(有依存关系),都改为自启动 Function Discovery Resource Publication SSDP Discovery UPnP Device Hos ...

  3. Window10 输入法不能删除问题解决

    有时候我们想删除一个输入法,却发现输入法Remove 按钮是Disalbe 的,这时可以使用下面方法回避这个问题. 1.使用 Administrator 运行 Windows powerSheell. ...

  4. 【Teradata SQL】一个字段为空即取另外一个字段(连续取4个字段)-case when

    目标:如果col1为空则取col2的值,如果col2也为空则取col3的值,如果col3还为则取col4的值,如果四个字段均为空则取默认值 1.数据准备 create multiset table t ...

  5. 中国IT史上两大严重事故对我们的警醒及预防措施

    20190121 一,历史回顾:20150528携程运维大事故 2015年5月28日上午11点开始,携程旅行网官方网站突然显示404错误页,App也无法使用,业务彻底中断. 据称是因为乌云网公布了携程 ...

  6. 用ASP.NET Core 2.1 建立规范的 REST API -- HATEOAS

    本文所需的一些预备知识可以看这里: http://www.cnblogs.com/cgzl/p/9010978.html 和 http://www.cnblogs.com/cgzl/p/9019314 ...

  7. python接口自动化(四)--接口测试工具介绍(详解)

    简介 “工欲善其事必先利其器”,通过前边几篇文章的介绍,大家大致对接口有了进一步的认识.那么接下来让我们看看接口测试的工具有哪些. 目前,市场上有很多支持接口测试的工具.利用工具进行接口测试,能够提供 ...

  8. Python + PyQt5 实现美剧爬虫可视工具(二)

    美剧<权力的游戏>终于开播最后一季了,在上周写了个简单的可视化美剧的爬虫软件来爬取美剧,链接:https://www.cnblogs.com/weijiutao/p/10614694.ht ...

  9. LindDotNetCore~授权中间件的介绍

    回到目录 LindDotNetCore中间件 大叔认识中间件就是主要对http请求进行拦截,然后添加具体个性化功能的逻辑,这种把请求切开,添加新逻辑的方式一般称为面向方面的逻辑AOP! 授权中间件 请 ...

  10. Spring Boot配置定时任务

    在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...