Redis高可用-Sentinel(哨兵)

Sentinel介绍

Sentinel 机制可以解决 master 和 slave 角色的切换问题

Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:

  • 监控 sentinel会不断地检查你的主服务器和从服务器是否运作正常。
  • 提醒 当被监控的某个redis服务器出现问题时,Sentinel可以通过API向管理员或者其他应用程序发送通知。
  • 自动故障迁移 当一个主服务器不能正常工作时,Sentinel会开始一次自动故障迁移操作,它会将失效的主服务器的其中一个从服务器升级为新的主服务器。当客户端试图链接失效的主服务器时,集群会向客户端返回新主服务器的地址。

Sentinel的前提是已经手动实现了一个redis的主从复制环境

配置文件 Sentinel.conf

每个节点有配置哨兵服务。

Ansible的模板文件 sentinel.conf.j2

复制代码
[root@ansible do_redis]# grep -E '^[a-Z]' /data/playbooks/do_redis/roles/redis/templates/sentinel.conf.j2 
bind 127.0.0.1 {{ ansible_default_ipv4['address'] }}
daemonize yes
port 26379
dir /usr/local/redis
pidfile "redis-sentinel.pid
logfile "sentinel-26379.log
sentinel monitor mymaster {{ master_ip }}  6379 2
sentinel auth-pass mymaster {{ redis_password }}
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

服务脚本

复制代码
[root@ansible do_redis]# cat roles/redis/templates/sentinel.service.j2 
[Unit]
Description=Redis sentinel Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/redis/redis-sentinel-26379.pid
User=redis
Group=redis
LimitNOFILE=100000
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/sentinel.conf --sentinel
ExecStop=/usr/local/redis/bin/redis-cli -p 26379 shutdown
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动脚本,查看状态

复制代码
systemctl start sentienl

redis-cli -redis-cli -a 123456 info replication

[root@node37 redis]# redis-cli -a 123456 -p 26379 -h 192.168.37.17  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=192.168.37.17:6379,slaves=2,sentinels=3
相关推荐
虹科网络安全10 小时前
艾体宝产品|深度解读 Redis 8.4 新增功能:原子化 Slot 迁移(上)
数据库·redis·bootstrap
虹科网络安全13 小时前
艾体宝新闻|Redis 月度更新速览:2026 年 3 月
数据库·redis·缓存
y = xⁿ15 小时前
Redis八股学习日记:布隆过滤器
数据库·redis·学习
凯瑟琳.奥古斯特17 小时前
Bootstrap快速上手指南
开发语言·前端·css·bootstrap·html
IT界的老黄牛18 小时前
停电后 Redis 集群两节点起不来:fix 完还报 Bad file format?多部分 AOF 修复的正确姿势
运维·redis·缓存
YaBingSec19 小时前
玄机网络安全靶场:Hadoop YARN ResourceManager 未授权 RCE WP
大数据·数据库·hadoop·redis·笔记·分布式·web安全
qq_40999093?19 小时前
NoSQL数据库解析:Redis
数据库·redis·nosql
空中海20 小时前
第六篇:可靠性篇 — Sentinel 熔断限流与 Seata 分布式事务
分布式·sentinel
KeyonY20 小时前
车联网规则引擎设计之热更新与版本管理
redis·golang·车联网
代码中介商20 小时前
Linux多线程编程完全指南(下):线程同步与互斥锁
linux·redis·线程·互斥锁