zoukankan      html  css  js  c++  java
  • redis 哨兵

    用主从复制的情况下,Master服务器进行了down机的情况,我们的系统就不能再进行写的操作,所以此时redis在2.6版本引入了哨兵模式,但是并不稳定,2.8版本之后哨兵模式才稳定了起来。
    顾名思义Redis的哨兵模式就是对redis系统进行实时的监控,其主要功能有下面两点
    1.监测主数据库和从数据库是否正常运行。
    2.当我们的主数据库出现故障的时候,可以自动将从数据库转换为主数据库,实现自动的切换。

    3.不能光考虑上面redis自动切换ip,master切换ip正常,但是还需要应用更新配置文件配置成新的master ip ,这不是完全自动化还得需要人工干预,实际生产环境不推荐

    配置哨兵

    [redis@redis02 conf]$ cat redis-sentinel-26379.conf
    port 26379
    daemonize yes
    logfile "/u01/redis/redis-5.0.5/log/26379.log"
    dir "/u01/redis/redis-5.0.5/data"
    #2代表判断主节点失败至少需要2个Sentinel节点同意
    sentinel monitor mymaster 192.168.20.201 7001 2 
    #每个Sentinel节点都要通过定期发送ping命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过了down-after-milliseconds配置的时间且没有有效的回复,则判定节点不可达,<times>(单位为毫秒)就是超时时间 
    sentinel down-after-milliseconds mymaster 30000
    #当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,parallel-syncs就是用来限制在一次故障转移之后,每次向新的主节点发起复制操作的从节点个数
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    

      哨兵至少配置大于3的奇数个好推荐新的master,哨兵要配置在不同的物理主机上

    启动哨兵

    redis-sentinel redis-sentinel-26379.conf
    redis-sentinel redis-sentinel-26380.conf
    

      查看哨兵状态

    [redis@redis02 conf]$ redis-cli -h 192.168.20.201 -p 26379 info sentinel
    # Sentinel
    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    sentinel_simulate_failure_flags:0
    master0:name=mymaster,status=ok,address=192.168.20.201:7001,slaves=2,sentinels=2
    

      kill掉 redis master

    观察日志

    10608:S 16 Sep 2019 16:40:06.568 # Connection with master lost.
    10608:S 16 Sep 2019 16:40:06.569 * Caching the disconnected master state.
    10608:S 16 Sep 2019 16:40:06.652 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:06.652 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:06.652 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:07.659 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:07.659 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:07.660 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:08.664 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:08.664 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:08.665 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:09.675 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:09.676 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:09.676 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:10.684 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:10.684 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:10.684 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:11.692 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:11.692 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:11.693 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:12.696 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:12.696 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:12.696 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:13.717 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:13.717 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:13.717 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:14.724 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:14.724 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:14.724 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:15.735 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:15.735 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:15.736 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:16.793 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:16.794 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:16.794 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:17.845 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:17.845 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:17.846 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:18.885 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:18.885 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:18.885 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:19.901 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:19.901 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:19.901 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:20.911 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:20.912 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:20.912 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:21.923 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:21.924 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:21.924 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:22.931 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:22.931 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:22.932 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:23.937 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:23.937 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:23.938 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:24.942 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:24.942 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:24.943 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:25.948 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:25.948 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:25.949 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:26.956 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:26.956 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:26.956 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:27.967 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:27.967 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:27.967 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:28.993 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:28.993 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:28.993 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:29.999 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:29.999 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:29.999 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:31.007 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:31.008 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:31.008 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:32.112 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:32.112 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:32.112 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:33.137 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:33.137 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:33.137 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:34.147 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:34.147 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:34.147 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:35.155 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:35.155 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:35.156 # Error condition on socket for SYNC: Connection refused
    10608:S 16 Sep 2019 16:40:36.161 * Connecting to MASTER 192.168.20.201:7001
    10608:S 16 Sep 2019 16:40:36.161 * MASTER <-> REPLICA sync started
    10608:S 16 Sep 2019 16:40:36.161 # Error condition on socket for SYNC: Connection refused
    10608:M 16 Sep 2019 16:40:37.018 # Setting secondary replication ID to 15fac3f45a2af5a4421a9906e7e1898abd6713d4, valid up to offset: 374359. New replication ID is e9e7b0c3af955da1330327c91c1ca8f7705fae6d
    10608:M 16 Sep 2019 16:40:37.018 * Discarding previously cached master state.
    10608:M 16 Sep 2019 16:40:37.018 * MASTER MODE enabled (user request from 'id=13 addr=192.168.20.202:33604 fd=13 name=sentinel-8ecf73b5-cmd age=426 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=140 qbuf-free=32628 obl=36 oll=0 omem=0 events=r cmd=exec')
    10608:M 16 Sep 2019 16:40:37.020 # CONFIG REWRITE executed with success.
    10608:M 16 Sep 2019 16:40:38.283 * Replica 127.0.0.1:7004 asks for synchronization
    10608:M 16 Sep 2019 16:40:38.283 * Partial resynchronization request from 127.0.0.1:7004 accepted. Sending 738 bytes of backlog starting from offset 374359.
    

      复制进程从16:40:06发现连接不上MASTER 192.168.20.201:7001,到16:40:37.018 推荐成功新的mater 只用38秒左右这效率很高,在查看哨兵状态

    [redis@redis02 log]$ redis-cli -h 192.168.20.201 -p 26379 info sentinel
    # Sentinel
    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    sentinel_simulate_failure_flags:0
    master0:name=mymaster,status=ok,address=192.168.20.202:7003,slaves=3,sentinels=2
    

      ip改变需要重新配置应用连接redis的新ip还需要人工干预。

  • 相关阅读:
    Linux下修改HOSTNAME
    IBM服务器 IMM日志收集
    X3850 Linux 下DSA日志收集办法
    Linux查看进程内存占用及内存使用情况
    集成开发注意事项(持续更新)
    SOAPUI请求及mockservice 使用
    Linux下su与su -命令的区别
    从XML文件中获取格式化的文本信息
    前端实现下载文件
    动态表单如何对部分表单字段校验
  • 原文地址:https://www.cnblogs.com/omsql/p/11528941.html
Copyright © 2011-2022 走看看