引子

Master挂了,如何保证可用性,实现继续读写

什么是哨兵

Sentinel(哨兵)是用于监控Redis集群中Master状态的工具,是Redis高可用解决方案,哨兵可以监视一个或者多个redis master服务,以及这些master服务的所有从服务。 某个master服务宕机后,会把这个master下的某个从服务升级为master来替代已宕机的master继续工作。

(顺带提一句,即使后来之前的master重启服务,也不会变回master了,而是作为slave从服务)

哨兵模式原理图

哨兵模式具体实现:

基于之前的搭建redis主从复制(读写分离)_p&f°的博客-CSDN博客

下面进入正题

本节实现哨兵模式示意图


1、 进入151主机的redis的解压目录,拷贝sentinel.conf到redis工作目录/usr/local/redis/ (当然这步看个人的安装情况)

cp sentinel.conf /usr/local/redis/

2、修改配置文件

cd /usr/local/redis

vim sentinel.conf

(先贴上全部文件内容,需要修改的都写了中文注释。当然也可以用后面我提供的精简版本,两个内容是一样的,只是精简版本把官方英文注释去除了而已。建议先看精简版本)

# 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.
#
# 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,只有指定的ip地址才能访问此redis
# bind 127.0.0.1 192.168.1.1
#
# 保护模式关闭,这样其他服务起就可以访问此台redisprotected-mode no# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379# By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis-sentinel.pid when
# daemonized.
# 哨兵模式是否后台启动,默认no,改为yes
daemonize yes# When running daemonized, Redis Sentinel writes a pid file in
# /var/run/redis-sentinel.pid by default. You can specify a custom pid file
# location here.
pidfile /var/run/redis-sentinel.pid# Specify the log file name. Also the empty string can be used to force
# Sentinel to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
# log日志保存位置
logfile /usr/local/redis/sentinel/redis-sentinel.log# 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 /usr/local/redis/sentinel# 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.
#
# 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.
#
# Replicas are auto-discovered, so you don't need to specify replicas in
# any way. Sentinel itself will rewrite this configuration file adding
# the replicas using additional configuration options.
# Also note that the configuration file is rewritten when a
# replica 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 ".-_".
#
# 核心配置。
# 第三个参数:哨兵名字,可自行修改。(若修改了,那后面涉及到的都得同步)
# 第四个参数:master主机ip地址
# 第五个参数:redis端口号
# 第六个参数:哨兵的数量。比如2表示,当至少有2个哨兵发现master的redis挂了,
#               那么就将此master标记为宕机节点。
#               这个时候就会进行故障的转移,将其中的一个从节点变为master
sentinel monitor mymaster 192.168.217.151 6379 2# sentinel auth-pass <master-name> <password>
#
# Set the password to use to authenticate with the master and replicas.
# Useful if there is a password set in the Redis instances to monitor.
#
# Note that the master password is also used for replicas, so it is not
# possible to set a different password in masters and replicas 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:
#
# master中redis的密码sentinel auth-pass mymaster 123456# sentinel auth-user <master-name> <username>
#
# This is useful in order to authenticate to instances having ACL capabilities,
# that is, running Redis 6.0 or greater. When just auth-pass is provided the
# Sentinel instance will authenticate to Redis using the old "AUTH <pass>"
# method. When also an username is provided, it will use "AUTH <user> <pass>".
# In the Redis servers side, the ACL to provide just minimal access to
# Sentinel instances, should be configured along the following lines:
#
#     user sentinel-user >somepassword +client +subscribe +publish \
#                        +ping +info +multi +slaveof +config +client +exec on# sentinel down-after-milliseconds <master-name> <milliseconds>
#
# Number of milliseconds the master (or any attached replica 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).
#
# Default is 30 seconds.
# 哨兵从master节点宕机后,等待多少时间(毫秒),认定master不可用。
# 默认30s,这里为了测试,改成10s
sentinel down-after-milliseconds mymaster 10000# IMPORTANT NOTE: starting with Redis 6.2 ACL capability is supported for
# Sentinel mode, please refer to the Redis website https://redis.io/topics/acl
# for more details.# Sentinel's ACL users are defined in the following format:
#
#   user <username> ... acl rules ...
#
# For example:
#
#   user worker +@admin +@connection ~* on >ffa9203c493aa99
#
# For more information about ACL configuration please refer to the Redis
# website at https://redis.io/topics/acl and redis server configuration
# template redis.conf.# ACL LOG
#
# The ACL Log tracks failed commands and authentication events associated
# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked
# by ACLs. The ACL Log is stored in memory. You can reclaim memory with
# ACL LOG RESET. Define the maximum entry length of the ACL Log below.
acllog-max-len 128# Using an external ACL file
#
# Instead of configuring users here in this file, it is possible to use
# a stand-alone file just listing users. The two methods cannot be mixed:
# if you configure users here and at the same time you activate the external
# ACL file, the server will refuse to start.
#
# The format of the external ACL user file is exactly the same as the
# format that is used inside redis.conf to describe users.
#
# aclfile /etc/redis/sentinel-users.acl# requirepass <password>
#
# You can configure Sentinel itself to require a password, however when doing
# so Sentinel will try to authenticate with the same password to all the
# other Sentinels. So you need to configure all your Sentinels in a given
# group with the same "requirepass" password. Check the following documentation
# for more info: https://redis.io/topics/sentinel
#
# IMPORTANT NOTE: starting with Redis 6.2 "requirepass" is a compatibility
# layer on top of the ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# New config files are advised to use separate authentication control for
# incoming connections (via ACL), and for outgoing connections (via
# sentinel-user and sentinel-pass)
#
# The requirepass is not compatable with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.# sentinel sentinel-user <username>
#
# You can configure Sentinel to authenticate with other Sentinels with specific
# user name. # sentinel sentinel-pass <password>
#
# The password for Sentinel to authenticate with other Sentinels. If sentinel-user
# is not configured, Sentinel will use 'default' user with sentinel-pass to authenticate.# sentinel parallel-syncs <master-name> <numreplicas>
#
# How many replicas we can reconfigure to point to the new replica simultaneously
# during the failover. Use a low number if you use the replicas to serve query
# to avoid that all the replicas will be unreachable at about the same
# time while performing the synchronization with the master.
# 当替换主节点后,剩余从节点并行同步的数量,默认为 1
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 replica 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 replica).
#
# - The maximum time a failover in progress waits for all the replicas to be
#   reconfigured as replicas of the new master. However even after this time
#   the replicas will be reconfigured by the Sentinels anyway, but not with
#   the exact parallel-syncs progression as specified.
#
# Default is 3 minutes.
# 主备切换的时间,若在3分钟内没有切换成功,换另一个从节点切换
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>
#
# 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 replica
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh# SECURITY
#
# By default SENTINEL SET will not be able to change the notification-script
# and client-reconfig-script at runtime. This avoids a trivial security issue
# where clients can set the script to anything and trigger a failover in order
# to get the program executed.sentinel deny-scripts-reconfig yes# REDIS COMMANDS RENAMING
#
# Sometimes the Redis server has certain commands, that are needed for Sentinel
# to work correctly, renamed to unguessable strings. This is often the case
# of CONFIG and SLAVEOF in the context of providers that provide Redis as
# a service, and don't want the customers to reconfigure the instances outside
# of the administration console.
#
# In such case it is possible to tell Sentinel to use different command names
# instead of the normal ones. For example if the master "mymaster", and the
# associated replicas, have "CONFIG" all renamed to "GUESSME", I could use:
#
# SENTINEL rename-command mymaster CONFIG GUESSME
#
# After such configuration is set, every time Sentinel would use CONFIG it will
# use GUESSME instead. Note that there is no actual need to respect the command
# case, so writing "config guessme" is the same in the example above.
#
# SENTINEL SET can also be used in order to perform this configuration at runtime.
#
# In order to set a command back to its original name (undo the renaming), it
# is possible to just rename a command to itself:
#
# SENTINEL rename-command mymaster CONFIG CONFIG# HOSTNAMES SUPPORT
#
# Normally Sentinel uses only IP addresses and requires SENTINEL MONITOR
# to specify an IP address. Also, it requires the Redis replica-announce-ip
# keyword to specify only IP addresses.
#
# You may enable hostnames support by enabling resolve-hostnames. Note
# that you must make sure your DNS is configured properly and that DNS
# resolution does not introduce very long delays.
#
SENTINEL resolve-hostnames no# When resolve-hostnames is enabled, Sentinel still uses IP addresses
# when exposing instances to users, configuration files, etc. If you want
# to retain the hostnames when announced, enable announce-hostnames below.
#
SENTINEL announce-hostnames no

上述内容的精简版本(推荐使用,修改好后直接用下面文件替换系统的sentinel.conf文件)

###普通配置port 26379
# 保护模式关闭,这样其他服务起就可以访问此台redis
protected-mode no
# 哨兵模式是否后台启动,默认no,改为yes
daemonize yes
pidfile /var/run/redis-sentinel.pid
# log日志保存位置
logfile /usr/local/redis/sentinel/redis-sentinel.log
# 工作目录
dir /usr/local/redis/sentinel###核心配置
# 核心配置。
# 第三个参数:哨兵名字,可自行修改。(若修改了,那后面涉及到的都得同步)
# 第四个参数:master主机ip地址
# 第五个参数:redis端口号
# 第六个参数:哨兵的数量。比如2表示,当至少有2个哨兵发现master的redis挂了,
#               那么就将此master标记为宕机节点。
#               这个时候就会进行故障的转移,将其中的一个从节点变为master
sentinel monitor mymaster 192.168.217.151 6379 2
# master中redis的密码
sentinel auth-pass mymaster 123456
# 哨兵从master节点宕机后,等待多少时间(毫秒),认定master不可用。
# 默认30s,这里为了测试,改成10s
sentinel down-after-milliseconds mymaster 10000
# 当替换主节点后,剩余从节点重新和新master做同步的并行数量,默认为 1
sentinel parallel-syncs mymaster 1
# 主备切换的时间,若在3分钟内没有切换成功,换另一个从节点切换
sentinel failover-timeout mymaster 180000

3、将上述修改好的配置文件,复制到129和139从机中。我这里直接使用scp命名复制

scp sentinel.conf root@192.168.217.129:/usr/local/redis/scp sentinel.conf root@192.168.217.130:/usr/local/redis/

4、因为配置文件中设定了自己的log存储位置,所以要把相应的文件创建出来,在151、129和130中都需要执行

mkdir /usr/local/redis/sentinel -p

5、分别在151、129和130中执行下面命令,启动哨兵

redis-sentinel sentinel.conf

6、可以在151主机中,进入到日志所在的目录下执行如下命令,让日志在前台显示查看监控

tail -f redis-sentinel.log

测试:

一、关闭master的redis,查看redis集群主从机切换情况

1、先登录151主机redis,查看redis集群情况info replication

2、此时151为master,同理查看129和130。

3、 模拟151master节点宕机,把redis关闭,

继续查看各个节点的redis集群情况,

先看129

在看130

结果符合预期。

二、重启151节点,查看redis集群情况,看是否还会恢复为主节点,还是作为从节点。

从测试结果可以看到,即使后来重启之前的master,也不会替换,而是作为slave。

相关衍生:解决原Master恢复后不同步问题

相信细心的同学会发现原来的Master(151)恢复成Slave后,他的同步状态不OK,状态为master_link_status:down,这是为什么呢?这是因为我们只设置了129和130的masterauth(redis密码),这是用于同步master的数据,但是151一开始是master是不受影响的,当master转变为slave后,由于他没有auth,所以他不能从新的master同步数据,随之导致info replication的时候,同步状态为down,所以只需要修改redis.conf中的masterauth为123456

一般master数据无法同步给slave的方案检查为如下:

  • 1.网络通信问题,要保证互相ping通,内网互通。
  • 2.关闭防火墙,对应的端口开发(虚拟机中建议永久关闭防火墙,云服务器的话需要保证内网互通)。
  • 3.统一所有的密码,不要漏了某个节点没有设置

Redis的哨兵模式相关推荐

  1. Redis:哨兵模式(针对某一模块,数据量有限)

    Redis:哨兵模式(针对某一模块,数据量有限) 关键词 主从(读写分离,高新能),没有实现高可用 主从+哨兵:可以实现主从切换,实现高可用 哨兵检测(1s1ping),哨兵确认(接受回复,主观下线: ...

  2. ip地址转换数字函数 iton_redis深度剖析: 03 redis读写哨兵模式

    什么叫系统不可用: 什么是99.99高可用性: 高可用计算规则,全年 系统可用的时间 / 全年 redis不可用是什么? redis主从基于哨兵模式的高可用: 哨兵的主要功能: (1)集群监控,负责监 ...

  3. Redis的哨兵模式搭建

    Redis的哨兵模式搭建 1.Redis的哨兵模式 Redis的哨兵是反客为主的自动版,能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库.哨兵(sentinel) 是一个分布式系统, ...

  4. Redis(3)--哨兵模式,集群

    Redis的哨兵模式 什么是哨兵模式 Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行.其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例. ...

  5. Redis之哨兵模式

    文章目录 Redis之哨兵模式 1.哨兵介绍 2.哨兵作用 3.哨兵相关配置 4.配置哨兵 5.演示(配置哨兵) 6.主从切换过程 7.阶段一监控阶段 8.阶段二通知阶段 9.阶段三故障转移阶段 Re ...

  6. redis的哨兵模式测试

    上篇文章我们说到,一主二从的模式,如果主机挂掉,从机等待主机连上之后,继续成为主机的slave. 但是这样会影响我们业务的正常运转呀,我们怎么来做呢? 目前的我的服务器: 192.128.116.12 ...

  7. docker+网桥+redis主从+哨兵模式

    docker+网桥+redis主从+哨兵模式 我是在两台服务器上实验的,一台服务器的ip是192.168.213.144,另一台服务器的ip是192.168.213.145 1. 搭建网桥 关于第一部 ...

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

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

  9. 【redis】哨兵模式

    哨兵模式是redis高可用的一种解决方案. 哨兵必须用三个实例取保证自己的高可用,但是哨兵+主从模式是不能保证消息不丢失的. 为什么用三个来保证呢? 假设现在有两个服务器,第一台有redis主节点M1 ...

  10. Redis:哨兵模式(Sentinel)原理

    1. 前言 Redis主从复制模式不具备自动容错和恢复功能,主机从机的宕机都会导致前端部分读写请求失败,需要等待机器重启或者手动切换前端的IP才能恢复.Redis哨兵模式就解决了Redis主从复制模式 ...

最新文章

  1. java ls_linux ls 命令
  2. 利用该0 day漏洞的攻击活动情况
  3. 扩展--使用队列来优化递归操作完成文件下载
  4. ffplay.c学习-8-暂停、逐帧、⾳量
  5. jquery插件导出WORD
  6. c++语言截取字符串,C/C++知识点之C语言截取字符串
  7. gitlab root
  8. JQuery 插件之Ajax Autocomplete(ajax自动完成)
  9. 移动滚动条显示或隐藏元素事件
  10. openssl构建CA认证
  11. springboot 微服务相关收藏
  12. 2022新版完整版青龙面板对接机器人,傻妞对接onebot(oicq)协议实现机器人功能
  13. java工程师职业价值观_什么是职业价值观?舒伯职业价值观测试
  14. 罗伊 我的火箭我的处 (原创)
  15. 帐号登录:oAuth2.0流程
  16. 东进的语音卡编程:最简单的电话外呼程序
  17. 任正非《以客户为中心》
  18. 搭建属于你的家庭网络实时监控–HTML5在嵌入式系统中的应用·高级篇
  19. java 夏令时jvm设置问题
  20. HTTP/HTPPS协议

热门文章

  1. UWP 卡片视图 Card View
  2. Ubuntu14.04 ftp服务安装配置
  3. 云备份是一块不容易吃的‘大蛋糕’
  4. 闪闪发光的你,华泰张庆的交易哲学观
  5. 数据预处理的几种方法
  6. 博客开发日记-阿里云服务器linux系统安装mysql
  7. linux 终端显示emoji,如何从Linux命令行快速搜索表情符号 | MOS86
  8. python如何并发上千个get_Python爬虫实战入门六:提高爬虫效率—并发爬取智联招聘...
  9. NLP经典论文:ELMo 笔记
  10. 2022 年 5 月产品大事记