官网:
https://redis.io/topics/sentinel
Redis的主从只能实现数据热备份的功能,主宕机后从无法自动接管服务,因此Redis推出了Sentinel的主从监控模式。
Sentinel实质上是类似于MHA的一个监控主从并自动切换的monitor,实现的功能类似MongoDB自动切换的replica set,其命令行自带于redis的安装包中,
即:redis-sentinel。
而针对超大的数据量redis 3.0后还推出了Redis Cluster,实质上是分片,类似于MongoDB的auto sharding。
一、Sentinel.conf配置文件
Sentinel.conf实例:

daemonize yes
logfile "/redis/sentinel1/sentinel.log"
port 26379
dir "/redis/sentinel1"
protected-mode no  --必须设置为no,否则无法自动故障转移。
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster leo --必须写在"sentinel monitor mymaster 127.0.0.1 6379 2"之后,否则会报找不到master name的错误。
sentinel down-after-milliseconds mymaster 60000
sentinel config-epoch mymaster 0

Sentinel配置文件官网网址:http://download.redis.io/redis-stable/sentinel.conf
很多配置项都可以在这里找到详细的解释,以下只翻译了最重要的几个配置项。

##############################################################################
# 需要注意的是虽然本说明只列出了以下几种参数,但其实一些redis.conf的参数也是可以在sentinel.conf中设置的,例如
# daemonize、logfile等参数。
# 此外如果主从设置了auth验证,那么这里还需要配置:sentinel auth-pass <master-name> <password>
##############################################################################
# Example sentinel.conf
# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
# 默认Sentinel是不允许除localhost以外的服务器连接的,因此要么在bind中指明网段,要么设置protected-mode no
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#
protected-mode no --必须设置为no,或者bind 127.0.0.1以及本地IP,否则sentinel之间无法通信不能进行自动failover
# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379
# sentinel announce-ip <ip>
# sentinel announce-port <port>
#
# The above two configuration directives are useful in environments where,
# because of NAT, Sentinel is reachable from outside via a non-local address.
#
# When announce-ip is provided, the Sentinel will claim the specified IP address
# in HELLO messages used to gossip its presence, instead of auto-detecting the
# local address as it usually does.
#
# Similarly when announce-port is provided and is valid and non-zero, Sentinel
# will announce the specified TCP port.
#
# The two options don't need to be used together, if only announce-ip is
# provided, the Sentinel will announce the specified IP and the server port
# as specified by the "port" option. If only announce-port is provided, the
# Sentinel will announce the auto-detected local IP and the specified port.
#
# Example:
#
# sentinel announce-ip 1.2.3.4
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir /tmp
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
# 此命令告诉哨兵去监控主节点,如果至少有<quorum>个setinel检测到主S_DOWN,那么就将主设置为O_DOWN状态,然后就可以failover。
# O_DOWN S_DOWN Objectively/Subjectively:意思分别是客观下线和主观下线,前者表示多个sentinel实例共同作出了master已下线的判断,
# 后者表示单个sentinel实例做出了master已下线的判断。只有至少<quorum>个sentinel进程检测到主S_DOWN,才会做出主O_DOWN的判断,然后其中一个sentinel就会开始进行failover。
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
# 这段说明setinel也是需要多数存活才能实现故障转移投票,因此为防止脑裂建议配置奇数个sentinel。
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor mymaster 127.0.0.1 6379 2
# sentinel auth-pass <master-name> <password>
#
# Set the password to use to authenticate with the master and slaves.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for slaves, so it is not
# possible to set a different password in masters and slaves instances
# if you want to be able to monitor these instances with Sentinel.
#
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
# sentinel down-after-milliseconds <master-name> <milliseconds>
#
# Number of milliseconds the master (or any attached slave or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
# 这段的意思是sentinel在和主失联多少毫秒后,可以做出主节点S_DOWN的判断。
# Default is 30 seconds.
sentinel down-after-milliseconds mymaster 30000
# sentinel parallel-syncs <master-name> <numslaves>
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel parallel-syncs mymaster 1
# sentinel failover-timeout <master-name> <milliseconds>
#
# Specifies the failover timeout in milliseconds. It is used in many ways:
#
# - The time needed to re-start a failover after a previous failover was
# already tried against the same master by a given Sentinel, is two
# times the failover timeout.
#
# - The time needed for a slave replicating to a wrong master according
# to a Sentinel current configuration, to be forced to replicate
# with the right master, is exactly the failover timeout (counting since
# the moment a Sentinel detected the misconfiguration).
#
# - The time needed to cancel a failover that is already in progress but
# did not produced any configuration change (SLAVEOF NO ONE yet not
# acknowledged by the promoted slave).
#
# - The maximum time a failover in progress waits for all the slaves to be
# reconfigured as slaves of the new master. However even after this time
# the slaves will be reconfigured by the Sentinels anyway, but not with
# the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
sentinel failover-timeout mymaster 180000
# SCRIPTS EXECUTION
#
# sentinel notification-script and sentinel reconfig-script are used in order
# to configure scripts that are called to notify the system administrator
# or to reconfigure clients after a failover. The scripts are executed
# with the following rules for error handling:
#
# If script exits with "1" the execution is retried later (up to a maximum
# number of times currently set to 10).
#
# If script exits with "2" (or an higher value) the script execution is
# not retried.
#
# If script terminates because it receives a signal the behavior is the same
# as exit code 1.
#
# A script has a maximum running time of 60 seconds. After this limit is
# reached the script is terminated with a SIGKILL and the execution retried.
# NOTIFICATION SCRIPT
#
# sentinel notification-script <master-name> <script-path>
# 这里可以配置发生failover时的可执行脚本,可以配置邮件发送脚本。
# Call the specified notification script for any sentinel event that is
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
# This script should notify the system administrator via email, SMS, or any
# other messaging system, that there is something wrong with the monitored
# Redis systems.
#
# The script is called with just two arguments: the first is the event type
# and the second the event description.
#
# The script must exist and be executable in order for sentinel to start if
# this option is provided.
#
# Example:
#
# sentinel notification-script mymaster /var/redis/notify.sh
# CLIENTS RECONFIGURATION SCRIPT
#
# sentinel client-reconfig-script <master-name> <script-path>
#
# When the master changed because of a failover a script can be called in
# order to perform application-specific tasks to notify the clients that the
# configuration has changed and the master is at a different address.
#
# The following arguments are passed to the script:
#
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
#
# <state> is currently always "failover"
# <role> is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh

二、Sentinel的启动
redis-sentinel /redis/sentinel_1/sentinel.conf
redis-server /redis/sentinel_1/sentinel.conf
以上两种方式都可以,一般我们需要启动至少3个以上的奇数个这样的sentinel进程。
三、Sentinel API(即Sentinel shell内可以使用的命令)
参考:https://redis.io/topics/sentinel 的Sentinel API部分。
例如我们可以使用sentinel failover来实现手动failover,还可以通过sentinel remove/monitor来实现主节点的重新配置。
四、其他注意事项
Redis Sentinel在和docker使用时需要特别注意,由于docker存在端口映射可能会导致sentinel的自动failover失效。
同样的NAT和端口映射等机制也会导致Sentinel失效,需要进行特别的配置。
总结下来就是:凡是涉及到IP转换或者端口映射时,部署Sentinel都需要注意。

转载于:https://www.cnblogs.com/leohahah/p/8692449.html

Redis高可用 Sentinel相关推荐

  1. Redis高可用sentinel

    1.sentine介绍 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括 ...

  2. Redis高可用与集群

    文章目录 Redis高可用(Sentinel.哨兵) 启动并初始化 获取服务器信息 获取主服务器信息 获取从服务器信息 检查服务器在线状态 选举领导Sentinel 故障转移 集群 节点 槽指派 重新 ...

  3. 采用 redis主从 + 哨兵(sentinel) + vip漂移搭建一套redis高可用集群

    一.单个实例 当系统中只有一台redis运行时,一旦该redis挂了,会导致整个系统无法运行. 单个实例 二.备份 由于单台redis出现单点故障,就会导致整个系统不可用,所以想到的办法自然就是备份( ...

  4. Redis高可用方案:sentinel(哨兵模式)和集群

    一. redis高可用方案–sentinel(哨兵模式) 当我们搭建好redis主从复制方案后会发现一个问题,那就是当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费事费力, ...

  5. Redis高可用解决方案:sentinel(哨兵模式)和集群

    一. redis高可用方案–sentinel(哨兵模式) 当我们搭建好redis主从复制方案后会发现一个问题,那就是当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费事费力, ...

  6. Linux的企业-Redis数据库、缓存和哨兵Sentinal、Redis高可用

    一.Redis简介 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件.     它支持多种类型的数据结构,如        字符串(strings) ...

  7. Redis高可用之集群配置(六)

    0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...

  8. 调研Redis高可用两种方案

    导读:Redis是被广泛使用的基础软件之一.对于工程师和,架构师,运维人员来说,了解Redis的高可用方案和背后的原理,是必备的基础知识.本文作者深入分析了Redis高可用的方方面面,并且做了有效总结 ...

  9. Redis高可用原理

    Redis 是被广泛使用的基础软件之一,对于架构师和运维人员来说,了解 Redis 的高可用方案和背后的原理,是必备的基础知识. 本文作者深入分析了 Redis 高可用的方方面面,并且做了有效总结,相 ...

最新文章

  1. python小乌龟消除_悄悄告诉你,Python 里面有一只小乌龟
  2. mysql 1366 hy000_ERROR 1366 (HY000): Incorrect string value错误解决办法
  3. 【Python】/ 和 // 的区别
  4. bat代码小游戏_程序员入职被27岁领导告诫:我被BAT录用过,是算法方面泰斗大哥...
  5. java中多条件与不定条件查询
  6. Host ‘host_name’ is blocked
  7. 解决 Linux 端口被占用问题
  8. 2、Intellij IDEA中启动Broker
  9. SM4国密标准 GB/T 32907-2016
  10. 【321天】跃迁之路——程序员高效学习方法论探索系列(实验阶段79-2017.12.23)...
  11. Atitit.创建快捷方式 windows快捷方式的原理
  12. php公众号第三方登录,微信公众号开发小记——3.接入三方登录
  13. QQ 微信转链,如何实现淘宝京东苏宁唯品会拼多多,返利转链思路
  14. Excel函数IF的多条件通配使用方法
  15. kafka seek方法
  16. JavaScript数组方法(最全)
  17. JavaScript获取当前时区 时间转换 (实用)
  18. 【月刊】在四月奔跑起来,即使道路泥泞,也会收获遍野的烂漫
  19. QT中的Singal\slot机制
  20. 硬件辅助虚拟化 之EPT(内存虚拟化)介绍

热门文章

  1. DTMF 编码及解码
  2. 深入浅出FPGA-4-数字电路设计基础
  3. 电子签名行业2017新风向
  4. java 动态切换数据源_Java动态切换数据源(AOP)
  5. Django - ContentType
  6. 【Excel常用函数】VLookup函数使用教程,附视频教程
  7. 计算机网络学习——王道教材书(持续更新)
  8. php tree view,VB.Net树视图(TreeView)
  9. adb操作提示Read-only file system问题
  10. 什么是WAF?WAF的功能有哪些?