什么是哨兵?

哨兵是Redis的一种运行模式,它专注于对Redis实例(主节点、从节点)运行状态的监控,并能够在主节点发生故障时通过一系列的机制实现选主及主从切换,实现故障转移,确保整个Redis系统的可用性。

哨兵的作用

监控(Monitoring):持续监控Redis主节点、从节点是否处于预期的工作状态。
通知(Notification):哨兵可以把Redis实例的运行故障信息通过API通知监控系统或者其他应用程序。
自动故障恢复(Automatic failover):当主节点运行故障时,哨兵会启动自动故障恢复流程:某个从节点会升级为主节点,其他从节点会使用新的主节点进行主从复制,通知客户端使用新的主节点进行。
配置中心(Configuration provider):哨兵可以作为客户端服务发现的授权源,客户端连接到哨兵请求给定服务的Redis主节点地址。如果发生故障转移,哨兵会通知新的地址。这里要注意:哨兵并不是Redis代理,只是为客户端提供了Redis主从节点的地址信息。

哨兵如何知道从机的信息

哨兵它每隔一段时间会向主机发起INFO请求,主机收到后会把SLAVE的信息返回给哨兵,这样哨兵就可以知道从机的IP,从而进行主从切换。
另外,当哨兵有了从机的IP后,也会向从机发起INFO请求,从而获取其他信息。

  • 如果INFO命令目标是从节点:哨兵从返回信息中获取从节点所属的最新主节点ip和port,如果与历史记录不一致,则执行更新;获取从节点的优先级、复制偏移量以及与主节点的链接状态并更新。
  • 如果INFO命令目标是主节点:哨兵从返回信息中获取主节点的从机列表,如果从节点是新增的,则将其加入监控列表。
  • 无论目标是主节点还是从节点,都会记录其runId。
  • 如果节点的角色发生变化,哨兵会记录节点新的角色及上报时间。若此时哨兵运行在TILT模式下,则什么都不做。否则,会执行主从切换相关的逻辑,我们后面再细说。

开始进入搭建环节:

以一主二从三哨兵做实验

第一步:配置主从机

主机:

docker run
--name redis
-p 6379:6379
-v /home/redis/data:/data
-v /home/redis/conf/redis.conf:/etc/redis/redis.conf
-d redis:6.0
redis-server /etc/redis/redis.conf

拉去官网的redis.conf到对应的目录下并修改内容:

bind 0.0.0.0       #允许外部访问,127.0.0.1只允许本机访问

从机:

docker run
--name redis-slave2
-p 6381:6379
-v /home/redis-slave2/data:/data
-v /home/redis-slave2/conf/redis.conf:/etc/redis/redis.conf
-d redis:6.0 /etc/redis/redis.conf
docker run
--name redis-slave1
-p 6380:6379
-v /home/redis-slave1/data:/data
-v /home/redis-slave1/conf/redis.conf:/etc/redis/redis.conf
-d redis:6.0 /etc/redis/redis.conf

两个从机的redis.conf配置文件,修改内容:

replicaof master的ip master的端口号 # 例如: replicaof 172.17.0.4 6379
bind 0.0.0.0

如果不知道master的ip 可以使用该命令查询: docker inspect 容器名

最终效果:

[root@VM-8-12-centos conf]# docker exec -it redis /bin/bash
root@111:/data# redis-cli
127.0.0.1:6379> info replication
可以看到以下信息,有两从机
role:master
connected_slaves:2
slave0:ip=172.17.0.5,port=6379,state=online,offset=101706,lag=0
slave1:ip=172.17.0.6,port=6379,state=online,offset=101706,lag=1

第二步:配置三哨兵

执行命令创建哨兵

docker run --name redis-sentinel-16379
-p 16379:16379
--restart=always
-v /home/redis-sentinel-16379/data:/data
-v /home/redis-sentinel-16379/conf/sentinel.conf:/etc/sentinel.conf -d redis:6.0 redis-sentinel /etc/sentinel.conf

在/home/redis-sentinel-16379/conf/ 路径下创建 sentinel.conf,内容如下:

sentinel monitor masters79 172.17.0.4 6379 2
port 16379  #哨兵端口号 16379 16380 16381
logfile "sentinel.log"

解释:sentinel monitor (被监控的名称) host port [number]
number 指的是主机宕机后,多少台哨兵认为挂了才去进行主机迁移,一般为 n台主从机/2 + 1

然后根据上面的步骤 再创建两台redis-sentinel-16380、redis-sentinel-16381

docker run --name redis-sentinel-16380 -p 16380:16380 --restart=always -v /home/redis-sentinel-16380/data:/data -v /home/redis-sentinel-16380/conf/sentinel.conf:/etc/sentinel.conf -d redis:6.0 redis-sentinel /etc/sentinel.confdocker run --name redis-sentinel-16381 -p 16381:16381 --restart=always -v /home/redis-sentinel-16381/data:/data -v /home/redis-sentinel-16381/conf/sentinel.conf:/etc/sentinel.conf -d redis:6.0 redis-sentinel /etc/sentinel.conf

随便进入一台哨兵,查询监控情况:

[root@VM-8-12-centos conf]# docker exec -it redis-sentinel-16379 /bin/bash
root@24ff99a7736f:/data# redis-cli -p 16379
127.0.0.1:16379> infosentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=sentinel79,status=ok,address=172.17.0.4:6379,slaves=2,sentinels=1

可以查询监控日志,vim sentinel.log

1:X 19 Apr 2022 16:06:14.127 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:X 19 Apr 2022 16:06:14.127 # Redis version=6.0.16, bits=64, commit=00000000, modified=0, pid=1, just started
1:X 19 Apr 2022 16:06:14.127 # Configuration loaded
1:X 19 Apr 2022 16:06:14.128 * Running mode=sentinel, port=16381.
1:X 19 Apr 2022 16:06:14.129 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:X 19 Apr 2022 16:06:14.135 # Could not rename tmp config file (Device or resource busy)
1:X 19 Apr 2022 16:06:14.135 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
1:X 19 Apr 2022 16:06:14.135 # Sentinel ID is 68879b62a5565a7ce44d035c8c9e4c91705e935f
1:X 19 Apr 2022 16:06:14.135 # +monitor master masters79 172.17.0.4 6379 quorum 2
1:X 19 Apr 2022 16:06:14.136 * +slave slave 172.17.0.5:6379 172.17.0.5 6379 @ masters79 172.17.0.4 6379
1:X 19 Apr 2022 16:06:14.139 # Could not rename tmp config file (Device or resource busy)
1:X 19 Apr 2022 16:06:14.139 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
1:X 19 Apr 2022 16:06:14.139 * +slave slave 172.17.0.6:6379 172.17.0.6 6379 @ masters79 172.17.0.4 6379
1:X 19 Apr 2022 16:06:14.142 # Could not rename tmp config file (Device or resource busy)
1:X 19 Apr 2022 16:06:14.142 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
1:X 19 Apr 2022 16:06:15.031 * +sentinel sentinel 112ccde5732ace56de4ebc2911dcac97604d710a 172.17.0.9 16380 @ masters79 172.17.0.4 6379
1:X 19 Apr 2022 16:06:15.035 # Could not rename tmp config file (Device or resource busy)
1:X 19 Apr 2022 16:06:15.035 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy

第三步:验证

master宕机 127.0.0.1:6379> shutdown

# Replication
role:slave
master_host:172.17.0.6
master_port:6379
master_link_status:up

由此可见,172.17.0.6 从 slave 转到了 master,
另外当原master(即172.17.0.4)启动后,它不再是master 而是slave

哨兵日志:

1:X 19 Apr 2022 16:13:36.511 # +sdown master masters79 172.17.0.4 6379
1:X 19 Apr 2022 16:13:36.663 # Could not rename tmp config file (Device or resource busy)
1:X 19 Apr 2022 16:13:36.663 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
1:X 19 Apr 2022 16:13:36.663 # +new-epoch 1
1:X 19 Apr 2022 16:13:36.665 # Could not rename tmp config file (Device or resource busy)
1:X 19 Apr 2022 16:13:36.665 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
1:X 19 Apr 2022 16:13:36.665 # +vote-for-leader 68879b62a5565a7ce44d035c8c9e4c91705e935f 1
1:X 19 Apr 2022 16:13:37.629 # +odown master masters79 172.17.0.4 6379 #quorum 2/2
1:X 19 Apr 2022 16:13:37.629 # Next failover delay: I will not start a failover before Tue Apr 19 16:19:37 2022
1:X 19 Apr 2022 16:13:37.670 # +config-update-from sentinel 68879b62a5565a7ce44d035c8c9e4c91705e935f 172.17.0.10 16381 @ masters79 172.17.0.4 6379
1:X 19 Apr 2022 16:13:37.670 # +switch-master masters79 172.17.0.4 6379 172.17.0.6 6379
1:X 19 Apr 2022 16:13:37.670 * +slave slave 172.17.0.5:6379 172.17.0.5 6379 @ masters79 172.17.0.6 6379
1:X 19 Apr 2022 16:13:37.670 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ masters79 172.17.0.6 6379
1:X 19 Apr 2022 16:13:37.673 # Could not rename tmp config file (Device or resource busy)
1:X 19 Apr 2022 16:13:37.673 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
1:X 19 Apr 2022 16:14:07.696 # +sdown slave 172.17.0.4:6379 172.17.0.4 6379 @ masters79 172.17.0.6 6379

Docker Redis 哨兵 小实验相关推荐

  1. docker redis 配置文件_基于Docker搭建Redis一主两从三哨兵

    点击上方"Java知音",选择"置顶公众号" 技术文章第一时间送达! 作者:oscarwin juejin.im/post/5d26b03de51d454fa3 ...

  2. 基于docker搭建redis哨兵模式

    github学习笔记 Redis 哨兵架构 1. redis 哨兵模式介绍 1) 哨兵模式是Redis的高可用方式,哨兵节点是特殊的redis服务,不提供读写服务,主要用来监控redis实例节点.2) ...

  3. docker、docker-compose搭建redis哨兵,集群。

    目录 注意坑: Redisson整合哨兵模式的坑: 1. 安装docker和docker-compose 2. docker-compose搭建redis哨兵模式(一主二从二哨兵) 2.1启动redi ...

  4. SpringBoot集成Docker下的Redis哨兵(一主两从一哨兵)

    目录 前言 一.Redis 配置 配置简介 redis.conf sentinel.conf 最终文件结构 启动 docker启动一主俩从 docker启动redis哨兵 二.SpringBoot配置 ...

  5. redis哨兵模式(docker)

    搭建一主两从三哨兵 一.下载redis配置文件以及环境准备 # 挂载目录前的准备,需要redis.conf文件 可以直接下载配置文件,下载路径到/usr/local wget http://downl ...

  6. redis哨兵集群部署 docker单机模拟-六节点

    目录 如果有不懂得,私信我就行,十分欢迎交流 一.准备工作 二.拉取redis镜像 三.部署多个容器 四.脚本讲解 1.脚本循环 2.配置文件 3.创建docker容器 五.从节点加入主节点 一.加入 ...

  7. Docker中搭建redis分片集群,搭建redis哨兵结构,实现springboot中对redis分片集群、哨兵结构的访问,Redis缓存雪崩、缓存击穿处理(非关系型数据库技术课程 第十二周)

    文章目录 一.要求: 二.知识总结 缓存雪崩 解决方案 docker中redis分片集群搭建 配置好配置文件 redis-6380.conf redis-6381.conf redis-6382.co ...

  8. docker swarm搭建Redis哨兵(Sentinel)模式

    前言 本篇文章介绍docker swarm搭建三主三从Redis哨兵(Sentinel)模式 环境 IP HOSTNAME label redis port sentinel port 172.16. ...

  9. 解锁新技能《docker如何搭建Redis哨兵模式》

    在我们搭建Redis哨兵架构时我们先了解一些Redis及其相关的一些知识: 问题:Redis是什么? Redis是C语言开发的一个开源的,遵从BSD协议的高性能键值对(key-value)内存数据库, ...

最新文章

  1. ZigBee TI ZStack CC2530 2.4 IAR软件版本
  2. Mysql 死锁过程及案例详解之插入意向锁与自增锁备份锁日志锁Insert Intention Lock Auto-increment Lock Backup Lock Log Lock
  3. (转)双系统卸载Ubuntu
  4. plsql只提交存储过程里的事务_plsql 存储过程 事务 | 学步园
  5. 第四十五篇、UITableViewCell高度计算
  6. jquery用ajax,超简单的jquery的AJAX用法
  7. 一个对话让你明白架构师是做什么的?
  8. 小数化分数 (思维)
  9. python3数据可视化软件_Python数据可视化工具Plotly
  10. 快递查询工具,如何查看单号在每个时间段的具体信息
  11. 【自动驾驶-感知-红绿灯】红绿灯识别知识点
  12. 中国人工智能发展主要存在哪些制约因素,有哪些好的建议
  13. 数据挖掘学习之路:Python去极值方法(极值滤波)
  14. PHP刹车助力,自动刹车辅助都是骗人玩意儿?
  15. CoffeeScript入门实践
  16. html页面变成黑白,修改CSS样式实现网页变灰色/黑白代码的几个方法整理
  17. Win11如何将右下角图标全部显示?Win11将右下角图标全部显示
  18. jquery中ajax的分页,利用jQuery中的ajax分页实现代码
  19. ThinkPad X390拆机
  20. 微信支付-java实现微信支付-后端篇

热门文章

  1. 天猫精灵 python_树莓派通过snowboy唤醒引擎(Python2、Python3的都可以),自定义唤醒词、关键字,达到小爱同学、天猫精灵一样的唤醒方式的全套教程...
  2. 2022年22届西安交通大学MBA提前面试时间
  3. 充电暖手两用芯片-DLTAP602SD
  4. 我的爬虫都爬出了些什么?
  5. 书生笔记-clickhouse单机安装
  6. 软件测试师具备的素质_软件测试工程师的素质
  7. 【RPA】Word 文档生成器(Python 篇)
  8. 尚硅谷Redis6基础教程-秒杀案例中库存遗留问题
  9. python随机选取字符串_Python random模块sample、randint、shuffle、choice随机函数概念和应用...
  10. robotframework 内置库BuiltIn学习笔记