1. 先了解一下哨兵都 做了什么工作:Redis Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:
  2. * 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
  3. * 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
  4. * 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
  5.    
  6. Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 Sentinel 进程(progress), 这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息, 并使用投票协议(agreement protocols)来决定是否执行自动故障迁移, 以及选择哪个从服务器作为新的主服务器。
  7. Redis Sentinel (哨兵)存在一个单独的可执行文件 redis-sentinel 但实际上它只是一个运行在特殊模式下的 Redis 服务器,通过Redis的哨兵去监控一个redis的集群,如果集群出现故障则自动进行故障迁移。
  8. # 对于Redis Sentinel有两种启动方式,如下: 
  9. 对于 redis-sentinel 程序, 你可以用以下命令来启动 Sentinel 系统:
  10. redis-sentinel /path/to/sentinel.conf
  11.   对于 redis-server 程序, 你可以用以下命令来启动一个运行在 Sentinel 模式下的 Redis 服务器:
  12. redis-server /path/to/sentinel.conf --sentinel

注意: 配置哨兵的前提是主从要先配置完成并运行。

  1. 主机名 IP地址 redis端口 哨兵端口
  2. redis-1 10.10.120.113 6379 26379
  3. redis-2 10.10.116.206 6379 26379
  4. redis-3 10.10.48.62 6379 26379

主从配置与维护:

一、配置redis集群

**** 使用哨兵模式 先要搭建redis主从。****

  1. ### redis 配置文件修改:
  2. ## redis-1 Master 配置文件修改:
  3. #bind 10.10.120.113 # 关闭了保护模式,这行就可以不需要。
  4. daemonize yes # 默认为no 一定要打开
  5. protected-mode no # 默认为yes,这里一定要改成no。关闭保护模式
  6. port 6379
  7. logfile "/data/logs/redis.logs"
  8. databases 16
  9. requirepass 123456 # 从服务密码设置(访问本机数据连接的Auth密码)
  10. masterauth 123456 # 若主服务设置了密码需要加上,在设置哨兵时主从之间连接需要(变更主从需要连接master 的密码.建议主从2个选项都设置上)
  11. maxclients 10000
  12. appendonly yes # 本地最好打开AOF模式,不会因为重启丢失数据。
  13. appendfilename "appendonly.aof"
  14. ## redis-2 slave 配置文件修改:
  15. # config:
  16. daemonize yes
  17. protected-mode no
  18. port 6379
  19. logfile "/data/logs/redis.logs"
  20. slaveof 10.10.120.113 6379 #Redis主节点IP 端口
  21. requirepass 123456 # 客户端连接redis需要用到的密码
  22. masterauth 123456 # 从库连接主库用到的密码,类似mysql的主从同步账号密码。
  23. appendonly yes # 本地最好打开AOF模式,不会因为重启丢失数据。
  24. appendfilename "appendonly.aof"
  25. *** 一个主从有以下信息既可以成功建立:
  26. slaveof 10.10.120.113 6379 #Redis主节点IP 端口
  27. masterauth 123456
  28. ## 验证主从:
  29. [root@h-1 ~]# redis-cli -a 123456
  30. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  31. 127.0.0.1:6379> info Replication
  32. # Replication
  33. role:master
  34. connected_slaves:2
  35. slave0:ip=10.10.116.206,port=6379,state=online,offset=1932,lag=0
  36. slave1:ip=10.10.48.62,port=6379,state=online,offset=1932,lag=0
  37. master_replid:57609f7b3e89bb5351ade82b965a9a9df0453bba
  38. master_replid2:0000000000000000000000000000000000000000
  39. master_repl_offset:1932
  40. second_repl_offset:-1
  41. repl_backlog_active:1
  42. repl_backlog_size:1048576
  43. repl_backlog_first_byte_offset:1
  44. repl_backlog_histlen:1932
  45. 127.0.0.1:6379>

哨兵配置与维护:

如果要做 自动故障转移,建议所有的 redis.conf 都设置 masterauth。因为 自动故障 只会重写 主从关系,即 slaveof,不会自动写入 masterauth。如果 Redis 原本没有设置密码,则可以忽略。

redis-1 sentinel 配置:

  1. ### sentinel 配置文件修改:
  2. # sentinel-1
  3. port 26379
  4. daemonize yes #
  5. pidfile "/var/run/redis-sentinel.pid"
  6. logfile "/data/logs/sentinel.logs"
  7. dir "/tmp"
  8. sentinel myid 300fcc98aae7579f4f5f687454cfcc4787446e74
  9. sentinel deny-scripts-reconfig yes
  10. sentinel monitor mymaster 10.10.120.113 6379 1 # monitor 监控master IP地址和端口,最后的数字1 是有几个哨兵确认即确认主下线。
  11. sentinel auth-pass mymaster 123456 # 重点改这个选项,连接主的密码。
  12. sentinel config-epoch mymaster 24
  13. sentinel leader-epoch mymaster 24
  14. protected-mode no
  15. sentinel down-after-milliseconds mymaster 5000 #修改心跳为5000毫秒
  16. sentinel current-epoch 24

redis-2 sentinel 配置:

  1. # sentinel-2
  2. port 26379
  3. daemonize yes
  4. pidfile "/var/run/redis-sentinel.pid"
  5. logfile "/data/logs/sentinel.logs"
  6. dir "/tmp"
  7. sentinel myid 300fcc98aae7579f4f5f687454cfcc4787446e74
  8. sentinel deny-scripts-reconfig yes
  9. sentinel monitor mymaster 10.10.120.113 6379 1
  10. sentinel auth-pass mymaster 123456
  11. sentinel config-epoch mymaster 24
  12. sentinel leader-epoch mymaster 24
  13. protected-mode no
  14. sentinel down-after-milliseconds mymaster 5000 #修改心跳为5000毫秒
  15. sentinel current-epoch 24

redis-3 sentinel 配置:

  1. # sentinel-3
  2. port 26379
  3. daemonize yes
  4. pidfile "/var/run/redis-sentinel.pid"
  5. logfile "/data/logs/sentinel.logs"
  6. dir "/tmp"
  7. sentinel myid edf0547582d9358fa95c6b4711945265b5ffa8a1
  8. sentinel deny-scripts-reconfig yes
  9. sentinel monitor mymaster 10.10.120.113 6379 1
  10. sentinel auth-pass mymaster 123456
  11. sentinel config-epoch mymaster 24
  12. sentinel leader-epoch mymaster 24
  13. protected-mode no
  14. sentinel current-epoch 24
  15. sentinel down-after-milliseconds mymaster 5000 #修改心跳为5000毫秒

安全性

对于数据比较重要的节点,主节点会通过设置requirepass参数进行密码 验证,这时所有的客户端访问必须使用auth命令实行校验。从节点与主节点 的复制连接是通过一个特殊标识的客户端来完成,因此需要配置从节点的masterauth参数与主节点密码保持一致,这样从节点才可以正确地连接到主 节点并发起复制流程。

验证sentinel 结果:

sentinel info信息:

  1. [root@h-1 ~]# redis-cli -p 26379
  2. 127.0.0.1:26379> info sentinel
  3. # Sentinel
  4. sentinel_masters:1
  5. sentinel_tilt:0
  6. sentinel_running_scripts:0
  7. sentinel_scripts_queue_length:0
  8. sentinel_simulate_failure_flags:0
  9. master0:name=mymaster,status=ok,address=10.10.50.119:6379,slaves=2,sentinels=3
  10. 127.0.0.1:26379>

查看monitor 信息,包含publish ping info 等信息:


  1. [root@h-1 ~]# redis-cli -p 6379 -a 123456
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. 127.0.0.1:6379> monitor
  4. OK
  5. 1575018698.113399 [0 10.10.50.119:46434] "PING"
  6. 1575018698.243075 [0 10.10.50.119:46434] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
  7. 1575018698.243192 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
  8. 1575018698.539274 [0 10.10.50.119:46436] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
  9. 1575018698.539447 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
  10. 1575018698.572941 [0 10.10.50.119:46422] "PING"
  11. 1575018698.572949 [0 10.10.50.119:46422] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"
  12. 1575018698.860119 [0 10.10.50.119:46436] "PING"
  13. 1575018699.127153 [0 10.10.50.119:46434] "PING"
  14. 1575018699.587644 [0 10.10.50.119:46422] "PING"
  15. 1575018699.796480 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"
  16. 1575018699.888122 [0 10.10.50.119:46436] "PING"
  17. 1575018700.175129 [0 10.10.50.119:46434] "PING"
  18. 1575018700.299961 [0 10.10.50.119:46434] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
  19. 1575018700.299989 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
  20. 1575018700.300194 [0 10.10.50.119:46422] "INFO"
  21. 1575018700.569205 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
  22. 1575018700.569235 [0 10.10.50.119:46436] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
  23. 1575018700.622766 [0 10.10.50.119:46422] "PING"
  24. 1575018700.622785 [0 10.10.50.119:46422] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"

Redis 5 配置 Redis sentinel(哨兵模式)的更多相关文章

  1. Redis | 第12章 Sentinel 哨兵模式《Redis设计与实现》

    目录 前言 1. 启动并初始化 Sentinel 2. Sentinel 与服务器间的默认通信 2.1 获取主服务器信息 2.2 获取从服务器信息 2.3 向主服务器和从服务器发送信息 3. 接受来自 ...

  2. Redis笔记-Sentinel哨兵模式

    Redis以主从的模式搭建集群后,如果主节点Master挂掉,虽然可以实现将备用节点Slave切换成主节点,但是Redis本身并没有自动监控机制,需要借助Sentinel哨兵模式,实现监控并实现自动切 ...

  3. Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】

    No cross,no crown . 不经历风雨,怎么见彩虹. Redis哨兵模式,用现在流行的话可以说就是一个"哨兵机器人",给"哨兵机器人"进行相应的配置 ...

  4. (六) Docker 部署 Redis 高可用集群 (sentinel 哨兵模式)

    参考并感谢 官方文档 https://hub.docker.com/_/redis GitHub https://github.com/antirez/redis happyJared https:/ ...

  5. 【Redis】Sentinel 哨兵模式

    Sentinel(哨兵模式) 目录 Sentinel(哨兵模式) 哨兵模式的三个定时任务 Sentinel(哨兵)与Sentinel .主服务器.从服务器之间的连接 检测下线状态 选择领头 Senti ...

  6. 扩展Redis的Jedis客户端,哨兵模式读请求走Slave集群

    原 扩展Redis的Jedis客户端,哨兵模式读请求走Slave集群 2018年12月06日 14:26:45 温故而知新666 阅读数 897   版权声明:本文为博主原创文章,遵循CC 4.0 b ...

  7. Redis sentinel 哨兵模式集群方案配置

    第一个方案是创建 redis cluster,第二种方案就是用哨兵模式来进行主从替换以及故障恢复.兵模式集群方案配置 一.sentinel介绍 Sentinel作用: 1):Master状态检测 2) ...

  8. redis集群主从集群搭建、sentinel(哨兵集群)配置以及Jedis 哨兵模式简要配置

    前端时间项目上为了提高平台性能,为应用添加了redis缓存,为了提高服务的可靠性,redis部署了高可用的主从缓存,主从切换使用的redis自带的sentinel集群.现在权作记录.

  9. SpringBoot进阶教程(三十)整合Redis之Sentinel哨兵模式

    Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案时,假如master宕机了,redis本身(以及其很多客户端)都没有实现自动进行主备切换,而 ...

随机推荐

  1. 第一章、Docker 简介

    笔记内容来自:第一本Docker书 [澳] James Turnbull 著 李兆海 刘斌 巨震 ​ Docker 是一个能够把开发的应用程序自动部署到容器的开源引擎.(由Docker 公司,前dot ...

  2. Tensorflow学习笔记No.11

    图像定位 图像定位是指在图像中将我们需要识别的部分使用定位框进行定位标记,本次主要讲述如何使用tensorflow2.0实现简单的图像定位任务. 我所使用的定位方法是训练神经网络使它输出定位框的四个顶 ...

  3. Angular:路由的配置、传参以及跳转

    ①路由的配置 1.首先用脚手架新建一个项目,在路由配置时选择yes 2.用ng g component创建组件 3.在src/app/app-routing.module.ts中配置路由 import ...

  4. 安卓实用工具箱v4.3几百种小功能

    款多功能实用工具箱.提供了从日常.图片.查询.设备.辅助.提取.优惠券.趣味游戏等多方面的功能,操作简单,即点即用,避免您下载超多应用的难题,且应用体积轻巧,界面简洁.已去除广告! 下载地址:http ...

  5. 主从复制直接转换MGR_5.7验证试验

    环境信息 IP port role info 192.168.188.51 4000 node1 master 192.168.188.52 4000 node2 slave1 192.168.188 ...

  6. MySQL PXC集群安装配置

    1.关闭防火墙 [root@node04 ~]#systemctl disable firewalld [root@node04 ~]#systemctl stop firewalld [root@n ...

  7. 06-flask-文件上传案例

    前端代码 Demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  8. css3选择器归类整理---基本选择器和属性选择器

    css3选择器分类 CSS3选择器分类如下图所示 选择器的语法 1.基本选择器 类型 代码 功能描述 通配选择器 *{ margin: 0; padding: 0; border: none; } 选 ...

  9. python三大流程

    一.三大流程 1. 顺序:按照顺序依次逐行执行代码的过程.自左向右,自上而下 2. 分支:程序按照不同的条件执行不同的处理代码的过程. 分支分为单分支,双分支,多分支 经常用到的分支结构是if语句 i ...

  10. CentOS7下常用安装软件服务rpm方式的介绍

    简介:介绍rpm软件包的管理 rpm安装:安装别人编译好的软件包,rpm即Redhat Package Manager,是Redhat的软件包管理方式   rpm安装优点: 软件已经编译打包,所以传输 ...