实现redis哨兵,模拟master故障场景
由于主从架构无法实现master和slave角色的自动切换,所以在发送master节点宕机时,redis主从复制无法实现自动的故障转移,即将slave 自动提升为新的master。因此,需要配置哨兵来"盯"着它们干活,一旦发现master节点宕机,会快速的将slave节点提升为新master节点。

一、实现主从复制架构
1.1 安装redis
#在所有节点都安装redis
yum install redis -y
1.2 修改配置文件
#所有节点都修改如下:
[root@localhost ~]#vim /etc/redis.conf
# replicaof <masterip> <masterport>
replicaof 10.0.0.8 6379 #指定master的IP和端口号
....省略
# masterauth <master-password>
masterauth 123456
...省略
1.3 启动redis
# systemctl enable --now redis
# ss -ntl
ss -State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:*
...省略...
1.4 查看主从复制状态
#在master节点查看到如下内容:
[root@localhost ~]#redis-cli -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.18,port=6379,state=online,offset=6398,lag=0
slave1:ip=10.0.0.28,port=6379,state=online,offset=6398,lag=1
master_failover_state:no-failover
master_replid:1d5939408b470ad048e8e397b2c258836c6caa73
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6398
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:6398
二、配置哨兵
2.1 实现哨兵
Sentinel实际上是一个特殊的redis服务器,有些redis指令支持,但很多指令并不支持.默认监听在26379/tcp端口。
##每个节点配置相同
# cp /etc/redis-sentinel.conf{,.bak}
# cat > /etc/redis-sentinel.conf<<EOF
bind 0.0.0.0
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/sentinel_26379.log"
dir "/tmp"
sentinel monitor mymaster 10.0.0.8 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
EOF
# mdkri /var/log/redis #创建log目录
# systemctl enable --now redis-sentinel.service
# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 511 0.0.0.0:26379 0.0.0.0:*
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 511 [::1]:6379 [::]:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
2.2检查哨兵状态
[root@localhost ~]#redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.0.8:6379,slaves=2,sentinels=1
#查看日志,观察启动过程
[root@localhost ~]#tail -f /var/log/redis/sentinel.log
6206:X 07 Feb 2022 23:30:54.262 # Configuration loaded
6206:X 07 Feb 2022 23:30:54.263 * Increased maximum number of open files to 10032 (it was originally set to 1024).
6206:X 07 Feb 2022 23:30:54.263 * monotonic clock: POSIX clock_gettime
6206:X 07 Feb 2022 23:30:54.264 * Running mode=sentinel, port=26379.
6206:X 07 Feb 2022 23:30:54.266 # Sentinel ID is 9fa9b4b370f49ca201d9d52bb28bb7d41c95ff65
6206:X 07 Feb 2022 23:30:54.266 # +monitor master mymaster 10.0.0.8 6379 quorum 2
6206:X 07 Feb 2022 23:30:54.267 * +slave slave 10.0.0.18:6379 10.0.0.18 6379 @ mymaster 10.0.0.8 6379
6206:X 07 Feb 2022 23:30:54.268 * +slave slave 10.0.0.28:6379 10.0.0.28 6379 @ mymaster 10.0.0.8 6379
6206:X 07 Feb 2022 23:30:55.548 * +sentinel sentinel ca0733135183c8aebf666f068e9134e3adffc362 10.0.0.8 26379 @ mymaster 10.0.0.8 6379
6206:X 07 Feb 2022 23:31:10.040 * +sentinel sentinel 48b97abdf2a0628bb425291e968c25a7ecc6e19d 10.0.0.28 26379 @ mymaster 10.0.0.8 6379
三、模拟master故障场景
3.1 模拟故障,停掉master
停掉身为master的主机10.0.0.8
# systemctl stop redis
3.2 查看日志

3.3 故障恢复
# systemctl start redis
3.4 观察变化

可以发现,当master恢复重新加入回来后,并没有重新获得master,而是以slave身份存在。
实现redis哨兵,模拟master故障场景的更多相关文章
- Redis 哨兵模式实现主从故障互切换
200 ? "200px" : this.width)!important;} --> 介绍 Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 S ...
- Redis Sentinel 模拟故障迁移
什么是redis sentinel 参考文档:https://redis.io/topics/sentinel 简单的来说,就是Redis Sentinel 为redis 提供高可用性,主要体现在下面 ...
- redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用
目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...
- 国内外三个不同领域巨头分享的Redis实战经验及使用场景
Redis不是比较成熟的memcache或者Mysql的替代品,是对于大型互联网类应用在架构上很好的补充.现在有越来越多的应用也在纷纷基于Redis做架构的改造.首先简单公布一下Redis平台实际情况 ...
- (转)国内外三个不同领域巨头分享的Redis实战经验及使用场景
随着应用对高性能需求的增加,NoSQL逐渐在各大名企的系统架构中生根发芽.这里我们将为大家分享社交巨头新浪微博.传媒巨头Viacom及图片分享领域佼佼者Pinterest带来的Redis实践,首先我们 ...
- redis哨兵架构的基础知识及部署和管理
一.前言 1.哨兵的介绍 sentinal,中文名是哨兵 哨兵是redis集群架构中非常重要的一个组件,主要功能如下 ()集群监控,负责监控redis master和slave进程是否正常工作 ()消 ...
- Redis实战经验及使用场景
随着应用对高性能需求的增加,NoSQL逐渐在各大名企的系统架构中生根发芽.这里我们将为大家分享社交巨头新浪微博.传媒巨头Viacom及图片分享领域佼佼者Pinterest带来的Redis实践,首先我们 ...
- Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)
Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...
- Redis哨兵(sentinel)模式搭建
一.Sentinel介绍 之前骚了一波Redis的简介及应用场景,今天试了下他的哨兵模式: Sentinel是Redis的高可用性(HA)解决方案,由一个或多个Sentinel实例组成的Sentine ...
随机推荐
- 探索 dotnet core 为何在 Windows7 系统需要补丁的原因
在一些 Windows 7 系统上,根据 dotnet 官方文档,需要安装上 KB2533623 补丁,才能运行 dotnet core 或 .NET 5 等应用.尽管非所有的设备都需要安装此,但这也 ...
- JAVA并发-AQS知识笔记
概述 AQS是AbstractQueuedSynchronizer的缩写,翻译成中文就是抽象队列同步器,AbstractQueuedSynchronizer这个类也是在java.util.concur ...
- JVM调优2-远程监控
监控远程JVM VisualJVM不仅是可以监控本地jvm进程,还可以监控远程的jvm进程,需要借助于JMX技术实现. 什么是JMX JMX(Java Management Extensions,即J ...
- Spring系列2:Spring容器基本概念和使用
本文内容 简单回顾IoC和DI概念 Spring容器的概念 的xml配置和初始化 容器的基本使用 bean的定义和初始化配置 简单理解IoC和DI概念 什么是IoC控制反转? 通俗地但不严谨地讲,以前 ...
- Solon 开发,七、自定义注解开发汇总
Solon 开发 一.注入或手动获取配置 二.注入或手动获取Bean 三.构建一个Bean的三种方式 四.Bean 扫描的三种方式 五.切面与环绕拦截 六.提取Bean的函数进行定制开发 七.自定义注 ...
- RHCSA 第六天
一. 创建下列用户.组和组成员资格: 1.创建名为 sysmgrs 的组 2.创建用户 natasha 同时指定sysmgrs作为natasha的附加组 3.创建用户 harry 同时指定 sysm ...
- GIS :元宇宙未来发展的有力技术支撑
摘要:元宇宙是描述未来互联网迭代发展的一个概念,是一个将现实世界和虚拟世界相互融合的一个可感知的持久.共享的3D虚拟空间组成的世界. 本文分享自华为云社区<[云驻共创]元宇宙漫游指南-新一代GI ...
- 【记录一个问题】libtask无法在android下编译通过
源码来自:https://github.com/msteinert/libtask 首先是asm.S无法编译通过. 其次,编译context.c出现这些错误: .//context.c:124:19: ...
- 使用Xamarin开发移动应用示例——数独游戏(一)项目的创建与调试
最近项目中需要移动客户端,由于团队基本上使用.Net产品线,所以决定使用Xmarin进行开发,这样技术路线统一,便于后期维护.官网上是这样介绍的" Xamarin 允许你使用 .NET 代码 ...
- 定位new
常规的new是分配内存,然后调用相应的构造函数,而定位new是在已经分配内存的上面调用构造函数: // ConsoleApplication7.cpp : 定义控制台应用程序的入口点. #includ ...