由于主从架构无法实现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. oceanbase数据库比赛总结

    前言 ob数据库大赛由蚂蚁金服的oceanbase团队组织,今年是第一届,宣传很广,比赛十月份开始,但早在上半年就看见大量的宣传了,比赛也是相当的卷.我们进了复赛之后感觉要卷进决赛需要付出的时间精力都 ...

  2. 初识python: 字符串常用操作

    直接上代码示例: #!/user/bin env python # author:Simple-Sir # time:20180914 # 字符串常用操作 name = 'lzh lyh' print ...

  3. 工厂模式(python)

    以字符串作为传递参数 以类名作为传递参数 来自为知笔记(Wiz)

  4. Cause: org.postgresql.util.PSQLException: 栏位索引超过许可范围:13,栏位数:12

    Cause: org.postgresql.util.PSQLException: 栏位索引超过许可范围:13,栏位数:12. 这个报错的原因是在mapper文件中的sql语书写错误 <inse ...

  5. django中使用支付宝

    一.注册 https://auth.alipay.com/login/ant_sso_index.htm?goto=https%3A%2F%2Fopenhome.alipay.com%2Fplatfo ...

  6. c# - 按引用内存地址传参 和 按输出传参 的具体使用

    1.前言 传递参数,不需要返回值,对懒人很舒服哟,缺点是不好定位数据 2.操作 using System; namespace ConsoleApp1.letVlaueGo { public clas ...

  7. IDEA导入Web项目配置Tomcat启动

    1.导入项目 2.配置project 3.导入项目模块 配置Models 4.配置Libraries 5. 6. 7.配置tomcat

  8. mysql表设计原则

    0.三大范式及反范式 ◆ 第一范式(1NF):强调的是列的原子性,即列不能够再分成其他几列.  ◆ 第二范式(2NF):首先是 1NF,另外包含两部分内容,一是表必须有一个主键:二是没有包含在主键中的 ...

  9. python文档2-unittest单元测试之mock.patch

    介绍mock里面另一种实现方式,patch装饰器的使用,patch() 作为函数装饰器,为您创建模拟并将其传递到装饰函数 patch简介 1.unittest.mock.patch(target,ne ...

  10. Django db使用MySQL连接池

    Django db使用MySQL连接池 Sep 25 2016 Django db模块本身不支持MySQL连接池,只有一个配置CONN_MAX_AGE连接最大存活时间,如果WSGI服务器使用了线程池技 ...