zoukankan      html  css  js  c++  java
  • redis集群sentinel哨兵模式的搭建与实际应用

    参考资料:https://blog.csdn.net/men_wen/article/details/72724406

    之前环境使用的keepalived+redis vip集群模式,现在我们服务切换到阿里云,尝试着使用一下哨兵的集群模式,期间遇到了很多坑,查了很多资料总算成功搭建完毕。

    后期使用的稳定性还有待确认,暂时记录一下流程做备忘。

       

    •  环境描述:阿里云主机3台(专有网络,开放6379端口,实现内网高可用性)搭建此环境至少三台服务器并且台数是基数,因为涉及到选举问题,我使用的云服务直接yum安装redis 和 redis-sentinel服务
    1. 192.168.1.66 (redis主)服务部署    redis主服务 redis-sentinel哨兵服务
    2. 192.168.1.67 (redis从)服务部署    redis主服务 redis-sentinel哨兵服务
    3. 192.168.1.68 (redis从)服务部署    redis主服务 redis-sentinel哨兵服务

    准备工作:安装软件~

    yum install redis redis-sentinel  
    cp /etc/redis-sentinel.conf /etc/back_redis-sentinel.conf
    cp /etc/redis.conf /etc/back_redis.conf
    

      

     一、配置Redis主服务

    • redis主配置文件:(/etc/redis.conf)
    bind 0.0.0.0
    protected-mode no
    port 6379
    pidfile "/var/run/redis/redis.pid"
    loglevel notice
    logfile "/tmp/log/redis/redis.log"
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    dbfilename "dump.rdb"
    dir "/tmp/lib/redis"
    masterauth "11111111"
    requirepass "11111111"
    

      

    • redis从配置文件
    bind 0.0.0.0
    protected-mode no
    port 6379
    pidfile "/var/run/redis/redis.pid"
    loglevel notice
    logfile "/tmp/log/redis/redis.log"
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    dbfilename "dump.rdb"
    dir "/tmp/lib/redis"
    masterauth "11111111"
    requirepass "11111111"
    slaveof 192.168.1.66 6379
    

    配置完毕后,启动服务测试是否能正常通信。如有报错请先处理不通的问题,部分错误可以见下方错误摘记 

    [root@itvcloud-yunwei Icloud]# redis-cli -h 172.17.189.87    #主备相互测试畅通,Pong为正常
    172.17.189.87:6379> 
    172.17.189.87:6379> 
    172.17.189.87:6379> auth mypasswd
    OK
    172.17.189.87:6379> ping
    PONG
    

      

    二、配置哨兵模式

    • redis-sentinel配置文件
    // Sentinel节点的端口
    port 26379  
    dir /var/redis/data/
    logfile "26379.log"
    
    // 当前Sentinel节点监控 192.168.1.66:6379 这个主节点
    // 2代表判断主节点失败至少需要2个Sentinel节点节点同意
    // mymaster是主节点的别名
    sentinel monitor mymaster 192.168.1.66 6379 2
    
    //每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过30000毫秒且没有回复,则判定不可达
    sentinel down-after-milliseconds mymaster 30000
    
    //当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1
    sentinel parallel-syncs mymaster 1
    
    //故障转移超时时间为180000毫秒
    sentinel failover-timeout mymaster 180000
    
    • 启动服务后redis-sentinel配置文件会变更为
    port 26379
    protected-mode no
    bind 0.0.0.0
    sentinel myid 2c3a6184def1f66f8e97f864e17257aa29c8bbea
    sentinel auth-pass mymaster 11111111
    sentinel config-epoch mymaster 229
    sentinel leader-epoch mymaster 229
    logfile "/var/log/redis/sentinel.log"
    daemonize yes
    pidfile "/var/run/redis/sentinel.pid"
    # Generated by CONFIG REWRITE
    dir "/tmp"
    

      

    有几点需要注意:

    1.遇到的问题是哨兵模式检测主服务一直是odown状态,出现故障后也无法自动切换,检测不到主服务状态

    解决办法:redis-sentinel.conf 配置protected-mode no,此选项会影响哨兵模式之间的通信,关闭安全模式即可,或者增加bind 0.0.0.0

    2.密码添加注意需要在redis.conf配置密码,在redis-sentinel.conf理也需要配置密码,否则会影响切换

     3.(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error. 这个错误是没有给redis存储dump文件目录的权限, chown -R redis.redis /var/lib/redis

    粗略记录仅供自己做备忘。

    4.相互之间端口都互通,ping状态为pong,客户端状态为master_link_status:down ,此状态下数据无法著丛复制,查看master 报错 Can't save in background: fork: Cannot allocate memory,

    解决办法: vi /etc/sysctl.conf 添加 vm.overcommit_memory = 1 ; sysctl -p使配置生效  原因可以参考:https://blog.csdn.net/zqz_zqz/article/details/53384854

  • 相关阅读:
    YAR 并行RPC框架研究
    Yar
    Monolog
    laravel controller:make
    eclipse自动补全的设置(自动提示)
    如何在 PHP 中处理 Protocol Buffers 数据
    JAVA printWriter中write()和println()区别
    eclipse中启动tomcat
    Ajax简介
    div
  • 原文地址:https://www.cnblogs.com/liuquan/p/8892235.html
Copyright © 2011-2022 走看看