zoukankan      html  css  js  c++  java
  • (转)深入浅出Redis-redis哨兵集群

    背景:对于后台开发,需要了解与集群相关的工作。

    深入浅出Redis-redis哨兵集群

    Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器。

    Redis Sentinel(译为“哨兵”)是 Redis 官方推荐的高可用性(HA)解决方案,当用 Redis 做 Master-slave 的高可用方案时,假如 master 宕机了,Redis 本身(包括它的很多客户端)都没有实现自动进行主备切换,而 Redis-sentinel 本身也是一个独立运行的进程,它能监控多个 master-slave 集群,发现 master 宕机后能进行自动切换。

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

    • 监控(Monitoring):Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
    • 提醒(Notification):当被监控的某个 Redis 服务器出现问题时,Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
    • 自动故障迁移(Automatic failover):当一个主服务器不能正常工作时,Sentinel 会开始一次自动故障迁移操作,它会将失效主服务器的其中一个从服务器升级为新的主服务器,并让失效主服务器的其他从服务器改为复制新的主服务器;当客户端试图连接失效的主服务器时,集群也会向客户端返回新主服务器的地址,使得集群可以使用新主服务器代替失效服务器。

    Redis Sentinel 是一个分布式系统,你可以在一个架构中运行多个 Sentinel 进程(progress),这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息,并使用投票协议(agreement protocols)来决定是否执行自动故障迁移,以及选择哪个从服务器作为新的主服务器。

    一个 Sentinel 进程可以与其他多个 Sentinel 进程进行连接,每个 Sentinel 进程之间可以互相检查对方的可用性,并进行信息交换。

    主从分离(读写分离)配置

    我们有两种方法配置从服务

      1、在配置文件中配置从服务

      2、在服务启动后设置

    上面两种配置的区别在于,当salve 断线重连之后,

       如果我们是修改类配置文件,重连之后会自己链接上去master,并且同步master 上面的数据,

       如果我们是手动连接上去的主服务器,重连之后,从服务器会读取自己本地的 rdb恢复数据,而不会去自动链接主服务

    Sentinel 哨兵配置

    1、配置端口

        在sentinel.conf 配置文件中, 我们可以找到port 属性,这里是用来设置sentinel 的端口,一般情况下,至少会需要三个哨兵对redis 进行监控,我们可以通过修改端口启动多个sentinel 服务。

    2、配置主服务器的ip 和端口

    # sentinel monitor <master-name> <ip> <redis-port> <quorum>
    #
    # Tells Sentinel to monitor this master, and to consider it in O_DOWN
    # (Objectively Down) state only if at least <quorum> sentinels agree.
    #
    # Note that whatever is the ODOWN quorum, a Sentinel will require to
    # be elected by the majority of the known Sentinels in order to
    # start a failover, so no failover can be performed in minority.
    #
    # Slaves are auto-discovered, so you don't need to specify slaves in
    # any way. Sentinel itself will rewrite this configuration file adding
    # the slaves using additional configuration options.
    # Also note that the configuration file is rewritten when a
    # slave is promoted to master.
    #
    # Note: master name should not include special characters or spaces.
    # The valid charset is A-z 0-9 and the three characters ".-_".
    sentinel monitor mymaster 127.0.0.1 6380 2

    4、Sentinel 总结

    、Sentinel的作用:

    A、Master 状态监测

    B、如果Master 异常,则会进行Master-slave 转换,将其中一个Slave作为Master,将之前的Master作为Slave 

    C、Master-Slave切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换 

    、Sentinel的工作方式:

    1):每个Sentinel以每秒钟一次的频率向它所知的Master,Slave以及其他 Sentinel 实例发送一个 PING 命令 
    2):如果一个实例(instance)距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被 Sentinel 标记为主观下线。 
    3):如果一个Master被标记为主观下线,则正在监视这个Master的所有 Sentinel 要以每秒一次的频率确认Master的确进入了主观下线状态。 
    4):当有足够数量的 Sentinel(大于等于配置文件指定的值)在指定的时间范围内确认Master的确进入了主观下线状态, 则Master会被标记为客观下线 
    5):在一般情况下, 每个 Sentinel 会以每 10 秒一次的频率向它已知的所有Master,Slave发送 INFO 命令 
    6):当Master被 Sentinel 标记为客观下线时,Sentinel 向下线的 Master 的所有 Slave 发送 INFO 命令的频率会从 10 秒一次改为每秒一次 
    7):若没有足够数量的 Sentinel 同意 Master 已经下线, Master 的客观下线状态就会被移除。 
    若 Master 重新向 Sentinel 的 PING 命令返回有效回复, Master 的主观下线状态就会被移除

    Redis Sentinel 高可用服务架构搭建

  • 相关阅读:
    PHP
    PHP
    PHP
    PHP
    PHP
    MySQL
    PHP
    PHP
    PHP
    linux 用户及用户组管理
  • 原文地址:https://www.cnblogs.com/lixuwu/p/10823358.html
Copyright © 2011-2022 走看看