Redis(五)Redis的高可用方案【哨兵】

时间:2021-08-04
本文章向大家介绍Redis(五)Redis的高可用方案【哨兵】,主要包括Redis(五)Redis的高可用方案【哨兵】使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、哨兵架构

架构图

  • sentinel哨兵是特殊的redis服务,不提供读写服务,主要用来监控redis实例节点。
  • 哨兵架构下客户端第一次需要从哨兵找出redis的主节点
  • 后续就直接访问redis的主节点,不会每次都通过 sentinel代理访问redis的主节点
  • 当redis的主节点发生变化,哨兵会第一时间感知到,并且将新的redis主节点通知给客户端

  PS:这里面redis的客户端一般都实现了订阅功能,订阅sentinel发布的节点变动消息

构建步骤

1、创建一个新的目录,模拟用【从根目录开始演示】
cd zhTools/redis-5.0.3/zhRedisDemo
mkdir sentinel #创建主从模式的目录
cd sentinel 
mkdir 26380 #用于存放6380端口的数据【哨兵1】
mkdir 26381 #用于存放6381端口的数据【哨兵2】
mkdir 26382 #用于存放6381端口的数据【哨兵3】

2、把redis的哨兵文件复制3份分别放到对应的目录下【从根目录开始演示】
cd zhTools/redis-5.0.3
cp sentinel.conf zhRedisDemo/sentinel/26380/sentinel-26380.conf
cp sentinel.conf zhRedisDemo/sentinel/26381/sentinel-26381.conf 
cp sentinel.conf zhRedisDemo/sentinel/26382/sentinel-26382.conf 

3、进入26380目录,并修改sentinel-26380.conf【另外两台配置类似】
port 26380 #端口
daemonize yes #守护线程方式启动【后台运行】
pidfile "/var/run/redis‐sentinel‐26380.pid" #把pid进程号写入pidfile配置的文件
logfile "26380.log" #日志文件存放在当前目录
dir /root/zhTools/redis-5.0.3/zhRedisDemo/sentinel/26380/data # 指定数据存放目录
sentinel monitor mymaster 120.24.58.161 26380 2 #配置监控的主机ip,mymaster这个名字随便取,客户端访问时会用到。
PS:最后的2表示明当有多少个sentinel认为一个master失效时,master才算真正失效(值一般为:sentinel总数/2 + 1) sentinel auth-pass mymaster xxxxxxx #如果主节点设置了密码,需要设置这个属性
PS:sentinel不能分别为master和slave设置不同的密码,因此master和slave的密码应该设置相同。
4、使用配置文件分别启动3个哨兵 redis‐sentinel sentinel‐26380.conf redis‐sentinel sentinel‐26381.conf redis‐sentinel sentinel‐26382.conf 5、连接上哨兵,即可查看集群信息是否搭建完成 redis‐cli ‐p 26380 127.0.0.1:26380>info

原文地址:https://www.cnblogs.com/riches/p/15095445.html