实现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 ...
随机推荐
- nginx之location、inmp架构详解、BBS项目部署
本期内容概要 location lnmp架构 部署BBS项目 内容详细 1.location 使用Nginx Location可以控制访问网站的路径 但一个server可以有多个location配置 ...
- PHP json_encode() 序列化对象、数组、空对象、空数组
$result = [ "object" => ["a" => 1], "array" => [1,2,3,4,5], & ...
- html基础 字符实体 用于html中特殊符号的展示 比如:多个空格、代码展示
结构:&+英文: 常见的字符实体
- python与redis交互(4)
python可以使用redis模块来跟redis交互 redis模块的使用 安装模块: pip3 install redis 导入模块:import redis 连接方式 严格连接模式:r=redis ...
- Linux的六种查找命令
http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html 1. find fi ...
- Go语言系列之标准库os
os包提供了操作系统的系列函数,这些接口不依赖平台.设计为Unix风格的,错误处理是go风格的:调用失败会返回错误值而非错误码.通常错误值里包含更多信息. os包的接口在所有操作系统中都是一致的.非公 ...
- vue中使用两个window.onresize问题解决
在vue开发中,因为引用的父组件和子组件都使用了window.onresize以至于一个window.onresize失效.找了下解决方案,可以采用下面的方式写就可以了. window.onresiz ...
- 【失败的经验】在linux下编译opencv for android
cd /home/ahfu#选择opencv 3.4.6版本来编译wget https://github.com/opencv/opencv/archive/3.4.6.tar.gztar -zxvf ...
- 记录未解决的问题:docker中无法启动mysqld
首先在docker中安装mysql server的包: sudo yum install mysql sudo yum install mariadb-server mariadb /usr/libe ...
- 游戏mod启动器原理
基本原理 游戏程序会按一定顺序读取游戏文件夹根目录的文件. 所以我们制作mod和补丁的时候需要使得我们的文件先读取,从而使得后面读取到重复内容时候,游戏运行的内存中舍弃掉原本的文件. 游戏mod启动器 ...