一. 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. idea+scala sdk + scala插件

    0X01 前言 我的主语言是python,说起java,想起了大二(三年前)上课时教过,课程设计的时候曾经做过个俄罗斯方块,后面其他设计copy代码读懂代码(再后面的课设就用python了). 本次涉 ...

  2. centos docker-ce安装

    懂得自然懂 https://yeasy.gitbooks.io/docker_practice/content/install/centos.html

  3. 【转廖大神】package.json 包安装

    现在我们遇到第一个问题:koa这个包怎么装,app.js才能正常导入它? 方法一:可以用npm命令直接安装koa.先打开命令提示符,务必把当前目录切换到hello-koa这个目录,然后执行命令: C: ...

  4. Akka.net 性能测试兼使用小技巧

    最近想研究一下分布式开发,先拿了akka.net 跑一下性能 参考自己写个网络实现,一般在本机通讯,300M每秒的传输率,作为参考 嗯,先说结果,用Akka.net直接发bytearray,最后也只有 ...

  5. hadoop fs -put 报错

    [hadoop@master ~]$ ll total -rw-rw-r-- hadoop hadoop Apr : aaa drwxr-xr-x hadoop hadoop Jun Desktop ...

  6. date函数的属性

    date () a: "am"或是"pm" A: "AM"或是"PM" d: 几日,两位数字,若不足则补零:从" ...

  7. C# WinForm:DataTable中数据复制粘贴操作的实现

    1. 需要实现类似于Excel的功能,就是在任意位置选中鼠标起点和终点所连对角线所在的矩形,进行复制粘贴. 2. 要实现这个功能,首先需要获取鼠标起点和终点点击的位置. 3. 所以通过GridView ...

  8. vue变异方法

    push()  往数组最后面添加一个元素,成功返回当前数组的长度    pop()  删除数组的最后一个元素,成功返回删除元素的值    shift()  删除数组的第一个元素,成功返回删除元素的值u ...

  9. PDF转换成Word,ppt转换成word

    pdf与word我没找到直接转换的方式,不过可以用间接方式嘛! pdf ==>picture ==>word!ppt转word的原理也是先把ppt转成图片,再把图片插入word! 先准备好 ...

  10. ASP.NET Core 2.1对GDPR的支持

    欧盟的<通用数据保护条例>(General Data Protection Regulation,以下简称 GDPR)已经于 2018 年 5 月 25 日正式施行.GDPR 涵盖了包括数 ...