一. Sentinel 高可用环境准备

  1.1 Sentinel 集群环境

环境 说明
操作系统版本 CentOS  7.4.1708 
IP地址 172.168.18.200
网关Gateway 172.168.18.1
DNS 172.168.16.11
三个sentinel服务端口 26379,26380,26381
Sentinel密码 无 不设置
是否RDB持久化 不支持
是否 AOF持久化 不支持

  1.2 Redis主库库环境,主从库搭建在(redis 系列22 复制Replication 下)

主库ip 172.168.18.201 6379
从库ip 172.168.18.203 6379,  172.168.18.200 6379

二.  Sentinel 配置说明

  2.1 启动Sentinel服务方法

    对于启动Sentinel服务有二种方法:

    (1)是使用redis-sentinel程序来启动 redis-sentinel  sentinel.conf。

    (2)是使用redis-server 程序来启动一个运行在Sentinel模式下的Redis服务器 redis-server  sentinel.conf  --sentinel。

    启动 Sentinel 实例必须指定相应的配置文件, 系统会使用配置文件来保存 Sentinel 的当前状态, 并在 Sentinel 重启时通过载入配置文件来进行状态还原。查看redis-sentinel程序,只是一个软链接,如下所示:

    lrwxrwxrwx.  root root       12月  : redis-sentinel -> redis-server     

  2.2  sentinel.conf 参数说明

    下面解说sentinel.conf文件中,所需的至少配置参数描述:

    -- 监控主库, 名称:mymaster可以自定义, IP端口: 127.0.0.1 ,判断主库客观下线需要2个Sentinel 同意
sentinel monitor mymaster 127.0.0.1 -- 认为主库已经下线所需的毫秒数,例如下线状态超过60000则判定已经下线。
sentinel down-after-milliseconds mymaster -- 指定故障转移超时时间,以毫秒为单位,配置所有slaves指向新的master所需的最大时间
sentinel failover-timeout mymaster -- 在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步,这个值设为 来保证每次只有一个slave 处于不能处理命令请求的状态。如果这个数字越大,就意味着越 多的slave因为replication而不可用。
parallel-syncs mymaster --设置连接master的密码。
sentinel auth-pass mymaster

三.  Sentinel高可用搭建

  只使用单个Sentinel进程来监控redis集群是不可靠的,当单个Sentinel进程down后,整个集群系统将无法按照预期的方式运行。所以有必要将sentinel集群,在IP 200的电脑上将启动三个Sentinel进程,实现集群。

  3.1  添加3个Sentinel.conf文件

    在ip 为200的sentinel集群服务器上,在redis运行目录下,增加3个配置文件,名称分别为:Sentinel_26379.conf, Sentinel_26380.conf, Sentinel_26381.conf。相关脚本如下:

    --  Sentinel_26379.conf文件配置参数
protected-mode no
port
sentinel monitor mymaster 172.168.18.201
sentinel auth-pass mymaster
daemonize yes
logfile "/usr/local/redis/bin/sentinel_26379.log"
sentinel down-after-milliseconds mymaster
sentinel parallel-syncs mymaster
sentinel failover-timeout mymaster -- Sentinel_26380.conf文件配置参数如下,其它参数与Sentinel_26379文件一样
port
logfile "/usr/local/redis/bin/sentinel_26380.log" -- Sentinel_26381.conf文件配置参数如下,其它参数与Sentinel_26379文件一样
port
logfile "/usr/local/redis/bin/sentinel_26381.log"

  --增加后文件目录如下:

[root@localhost bin]# pwd
/usr/local/redis/bin
[root@localhost bin]# ls -l
总用量
-rw-r--r--. root root 12月 : dump.rdb
-rw-r--r--. root root 12月 : redis_bak.conf
-rwxr-xr-x. root root 12月 : redis-benchmark
-rwxr-xr-x. root root 12月 : redis-check-aof
-rwxr-xr-x. root root 12月 : redis-check-rdb
-rwxr-xr-x. root root 12月 : redis-cli
-rw-r--r--. root root 12月 : redis.conf
lrwxrwxrwx. root root 12月 : redis-sentinel -> redis-server
-rwxr-xr-x. root root 12月 : redis-server
-rw-r--r--. root root 12月 : sentinel_26379.conf
-rw-r--r--. root root 12月 : sentinel_26379.log
-rw-r--r--. root root 12月 : sentinel_26380.conf
-rw-r--r--. root root 12月 : sentinel_26380.log
-rw-r--r--. root root 12月 : sentinel_26381.conf
-rw-r--r--. root root 12月 : sentinel_26381.log
-rw-r--r--. root root 12月 : sentinel.conf

  

  3.2 启动三个sentinel服务

     [root@localhost bin]# pwd
/usr/local/redis/bin
[root@localhost bin]# ./redis-sentinel ./sentinel_26379.conf
[root@localhost bin]# ./redis-sentinel ./sentinel_26380.conf
[root@localhost bin]# ./redis-sentinel ./sentinel_26381.conf

  (1)查看进程信息

[root@localhost bin]# ps -ef | grep redis-sentinel
root : ? :: ./redis-sentinel *: [sentinel]
root : ? :: ./redis-sentinel *: [sentinel]
root : ? :: ./redis-sentinel *: [sentinel]

  (2)查看主库与sentinel关联信息(连接一个sentinel客户端)

[root@localhost bin]# ./redis-cli -h 172.168.18.200 -p
172.168.18.200:> sentinel master mymaster
) "name"
) "mymaster"
) "ip"
) "172.168.18.201"
) "port"
) ""
) "runid"
) "26cd40ba173490e2ceac61433211af7dc7716dda"
) "flags"
) "master"
) "link-pending-commands"
) ""
) "link-refcount"
) ""
) "last-ping-sent"
) ""
) "last-ok-ping-reply"
) ""
) "last-ping-reply"
) ""
) "down-after-milliseconds"
) ""
) "info-refresh"
) ""
) "role-reported"
) "master"
) "role-reported-time"
) ""
) "config-epoch"
) ""
) "num-slaves"
) ""
) "num-other-sentinels"
) ""
) "quorum"
) ""
) "failover-timeout"
) ""
) "parallel-syncs"
) ""

  (3)sentinel客户端查看群集信息,可以看到此时主库ip为201

    172.168.18.200:> info sentinel
# Sentinel
sentinel_masters:
sentinel_tilt:
sentinel_running_scripts:
sentinel_scripts_queue_length:
sentinel_simulate_failure_flags:
master0:name=mymaster,status=ok,address=172.168.18.201:,slaves=,sentinels=

 

四.Sentinel高可用测试

  4.1 测试主从同步

    -- 主库写入一个键值对
[root@hsr bin]# ./redis-cli -h 172.168.18.201 -p -a
172.168.18.201:> set mysentinel "hello"
OK
-- 从库203 读取了该键
[root@xuegod64 redis-4.0.]# redis-cli -h 172.168.18.203 -p -a
172.168.18.203:> get mysentinel
"hello"
-- 从库200 读取了该键
[root@localhost bin]# ./redis-cli -h 172.168.18.200 -p -a
172.168.18.200:> get mysentinel
"hello"

  4.2 测试故障转移

    (1)  首先把主库201的down掉

    172.168.18.201:> shutdown
not connected>

    (2) 在sentinel客户端查看群集信息,发现此时已经实现了故障转移,已经将从库 200 升级成为了新主库

    172.168.18.200:> info sentinel
# Sentinel
sentinel_masters:
sentinel_tilt:
sentinel_running_scripts:
sentinel_scripts_queue_length:
sentinel_simulate_failure_flags:
  master0:name=mymaster,status=ok,address=172.168.18.200:,slaves=,sentinels=

    (3) 在redis客户端,查看ip 200的复制信息,角色已成了为master

    172.168.18.200:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.168.18.203,port=6379,state=online,offset=204170,lag=0
master_replid:7464817ee3337cc8f2b508577287b0f0c385a859
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:204170
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:190908
repl_backlog_histlen:13263

    (4)此时ip200 的redis服务,由之前的只读,变成了可读写。 

    172.168.18.200:> set mastername  "ip200"
OK
--此时只有203一个从库,成功读取了该键
172.168.18.203:> get mastername
"ip200"

    (5)查看其中的一个sentinel日志,下面是关于故障转移的相关信息:

[root@localhost bin]# cat sentinel_26379.log
:X Dec ::31.394 # +sdown master mymaster 172.168.18.201
:X Dec ::31.496 # +new-epoch
:X Dec ::31.499 # +vote-for-leader 300fd3d5b5673885c17942c465ec7a09f8f8e2ad
:X Dec ::32.271 # +config-update-from sentinel 300fd3d5b5673885c17942c465ec7a09f8f8e2ad 172.168.18.200 @ mymaster 172.168.18.201
:X Dec ::32.272 # +switch-master mymaster 172.168.18.201 172.168.18.200
:X Dec ::32.272 * +slave slave 172.168.18.203: 172.168.18.203 @ mymaster 172.168.18.200
:X Dec ::32.272 * +slave slave 172.168.18.201: 172.168.18.201 @ mymaster 172.168.18.200
:X Dec ::02.323 # +sdown slave 172.168.18.201: 172.168.18.201 @ mymaster 172.168.18.200

  总结:sentinel高可用是基于复制来实现的。在sentinel实现过程中:首先要先搭建好复制架构,并确保数据同步正常运行;最后在复制基础上,再搭建sentinel群集服务架构,并测试好故障转移切换。

  

redis 系列25 哨兵Sentinel (高可用演示 下)的更多相关文章

  1. redis 系列23 哨兵Sentinel (上)

    一.概述 Sentinel(哨岗或哨兵)是Redis的高可用解决方案:由一个或多个Sentinel实例(instance)组成的Sentinel系统(system)可以监视任意多个主服务器,以及这些主 ...

  2. redis 系列24 哨兵Sentinel (中)

    四. 检测下线状态 对于Redis的Sentinel中关于下线有两个不同的概念:(1)主观下线(Subjectively Down, 简称 Sdown) 指的是单个 Sentinel 实例对服务器做出 ...

  3. Redis Sentinel高可用架构

    Redis目前高可用的架构非常多,比如keepalived+redis,redis cluster,twemproxy,codis,这些架构各有优劣,今天暂且不说这些架构,今天主要说说redis se ...

  4. Redis Sentinel 高可用服务搭建

    阅读目录: 关于 Redis 的概念 关于 Redis Sentinel 的概念 搭建 Redis Server(master) 搭建 Redis Server(slave) 搭建 Redis Sen ...

  5. redis sentinel 高可用(HA)方案部署,及python应用示例

    redis sentinel(哨兵)高可用集群的部署方法,并通过 python 程序实例讲解如何使用 redis sentinel 简介 介绍 redis sentinel(哨兵)集群的部署,配置一主 ...

  6. 【转载】Redis Sentinel 高可用服务架构搭建

    作者:田园里的蟋蟀 出处:http://www.cnblogs.com/xishuai/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接. 阅读 ...

  7. Redis Sentinel 高可用部署实践集群

    一.Redis Sentinel 介绍    1.Sentinel     数据库环境搭建,从单机版到主备.再到多数据库集群,我们需要一个高可用的监控:比如Mysql中,我们可能会采用MHA来搭建我们 ...

  8. Redis哨兵模式高可用解决方案

    一.序言 Redis高可用有两种模式:哨兵模式和集群模式,本文基于哨兵模式搭建一主两从三哨兵Redis高可用服务. 1.目标与收获 一主两从三哨兵Redis服务,基本能够满足中小型项目的高可用要求,使 ...

  9. Redis Sentinel 高可用实现说明

    背景:      前面介绍了Redis 复制.Sentinel的搭建和原理说明,通过这篇文章大致能了解Sentinel的原理和实现方法以及相关的搭建.这篇文章就针对Redis Sentinel的搭建做 ...

随机推荐

  1. Python制作微信小助手

    网址: https://mp.weixin.qq.com/s/uWSgeD5FyzXV3LsMNus01Q

  2. 精读《Function VS Class 组件》

    1. 引言 为什么要了解 Function 写法的组件呢?因为它正在变得越来越重要. 那么 React 中 Function Component 与 Class Component 有何不同? how ...

  3. Dancing Links 学习笔记

    Dancing Links 本周的AI引论作业布置了一道数独 加了奇怪剪枝仍然TLE的Candy?不得不去学了dlx dlxnb! Exact cover 设全集X,X的若干子集的集合为S.精确覆盖是 ...

  4. History Api使用演示

    h5新增的一个特性即在history对象上 新增了pushState 和 replaceState 接口 配合在window对象上新增的popState事件使用 为什么要用它:因为通过historya ...

  5. [LeetCode] Lemonade Change 买柠檬找零

    At a lemonade stand, each lemonade costs $5.  Customers are standing in a queue to buy from you, and ...

  6. RaspberryPi上建立wordpress

    准备工作: 1.RaspberryPi 3代 B型 2.可用内存卡 3.读卡器 4.DiskGenius 5.Win32 Disk Imager 6.可用局域网 7.Xshell 和 Xftp 8.官 ...

  7. js与jq基础记录

    1.在js传递参数中含加号(+)的处理方式: 只需要把js中传过去的+号替换成base64 编码 %2B:encodeURI(str).replace(/\+/g,'%2B'). 2.随机产生8位随机 ...

  8. d3.js,初遇

    接触d3完全是由兴趣所致,废话不多说看代码: var dataArray = [23, 13, 21, 14, 37, 15, 18, 34, 30];这是这个图所需要的数据,其实这个柱状图最初不长这 ...

  9. C语言复习1_变量与数据类型

    变量命名规则: 1.变量名的首字母或下划线(不能是其他特殊符号) 2.变量名的其他字母包含下划线.数字 和字母 3.不能使用关键字 基本数据类型 分为数值型和非数值型,其中数值型分为整型和非整型 整型 ...

  10. JavaWeb学习路线

    一.三大组件介绍 javaweb在开发中有三大组件分别提供不同的功能,这三大组件为servlet,filter,listener 1.servlet 简单来说就是客户端请求服务器和接受服务器的响应,狭 ...