由于主从架构无法实现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故障场景的更多相关文章

  1. Redis 哨兵模式实现主从故障互切换

    200 ? "200px" : this.width)!important;} --> 介绍 Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 S ...

  2. Redis Sentinel 模拟故障迁移

    什么是redis sentinel 参考文档:https://redis.io/topics/sentinel 简单的来说,就是Redis Sentinel 为redis 提供高可用性,主要体现在下面 ...

  3. redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用

    目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...

  4. 国内外三个不同领域巨头分享的Redis实战经验及使用场景

    Redis不是比较成熟的memcache或者Mysql的替代品,是对于大型互联网类应用在架构上很好的补充.现在有越来越多的应用也在纷纷基于Redis做架构的改造.首先简单公布一下Redis平台实际情况 ...

  5. (转)国内外三个不同领域巨头分享的Redis实战经验及使用场景

    随着应用对高性能需求的增加,NoSQL逐渐在各大名企的系统架构中生根发芽.这里我们将为大家分享社交巨头新浪微博.传媒巨头Viacom及图片分享领域佼佼者Pinterest带来的Redis实践,首先我们 ...

  6. redis哨兵架构的基础知识及部署和管理

    一.前言 1.哨兵的介绍 sentinal,中文名是哨兵 哨兵是redis集群架构中非常重要的一个组件,主要功能如下 ()集群监控,负责监控redis master和slave进程是否正常工作 ()消 ...

  7. Redis实战经验及使用场景

    随着应用对高性能需求的增加,NoSQL逐渐在各大名企的系统架构中生根发芽.这里我们将为大家分享社交巨头新浪微博.传媒巨头Viacom及图片分享领域佼佼者Pinterest带来的Redis实践,首先我们 ...

  8. Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)

    Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...

  9. Redis哨兵(sentinel)模式搭建

    一.Sentinel介绍 之前骚了一波Redis的简介及应用场景,今天试了下他的哨兵模式: Sentinel是Redis的高可用性(HA)解决方案,由一个或多个Sentinel实例组成的Sentine ...

随机推荐

  1. 【优雅代码】07-spring下的优秀工具类

    [优雅代码]07-spring下的优秀工具类 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮助到你的话请顺手点 ...

  2. Springboot项目引入druid安装部署使用

    一.maven引入依赖,数据库驱动根据项目需求自行引入 <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot ...

  3. UI自动化,通过Xpath结合父类、同级元素,查找到唯一的元素定位

    UI自动化,通过Xpath定位的总结 当页面展示的内容的html布局格式一样,只能第几个控件的绝对定位来区分时,如果后面有变更控件的顺序,就会导致找不到这个元素,维护成本较高,可以尝试用其他的方式,比 ...

  4. android studio 配置 Lombok 插件 -具体步骤

    1.前言 idea 用惯了 Lombok 插件了,好用的很,可是开发安卓 却没有,即便在 android studio 安装了插件,但是仍然无法使用 因为需要配置 2.解决 (1)进入设置,找到插件设 ...

  5. Linux系统管理学习实训任务书

    1.<Linux系统管理实训任务一之搭建实验基础环境> https://www.toutiao.com/i6763578305091207694/ 2.<Linux系统管理实训任务一 ...

  6. 灵雀云发布云原生制品仓库Harbor企业版(Alauda Registry Service for Harbor)

      灵雀云发布云原生制品仓库Harbor企业版(Alauda Registry Service for Harbor) 近日,国内领先的云原生全栈私有云提供商灵雀云宣布,推出企业版云原生制品仓库Ala ...

  7. Jekyll + NexT + GitHub Pages 主题深度优化

    前言 笔者在用 Jekyll 搭建个人博客时踩了很多的坑,最后发现了一款不错的主题 jekyll-theme-next,但网上关于 Jekyll 版的 Next 主题优化教程少之又少,于是就决定自己写 ...

  8. xray与burp联动被动扫描

    最近也是刚实习了几天,看见带我的那位老哥在用xray,而且贼溜,所以我想写几篇关于xray的使用的文章 0x00 xray建立监听 在实际测试过程中,除了被动扫描,也时常需要手工测试.这里使用 Bur ...

  9. kali linux2020 虚拟机改root密码

    kali在2020版的更新中,好多小伙伴登不进root账号,这里来教大家怎样改root账户的密码 1.当我们打开虚拟机看到这个界面的时候,按e进入编辑模式 2.在编辑模式中,"quite s ...

  10. 若依(ruoyi)代码生成树表结构的那些坑

    若依(RuoYI)代码生成树表结构的那些坑 相信许多做后端开发的同学,一定用过若依这款框架,这款框架易上手,适合用来做后台管理系统,但是其中也存在一些坑,稍不注意就会中招(大佬可以忽略...) 今天, ...