zoukankan      html  css  js  c++  java
  • LINUX环境Redis Sentinel集群方案配置方法

    1.测试环境

    master:   127.0.0.1 6479
    
    slave1:    127.0.0.1 6579
    
    slave2:    127.0.0.1 6679
    
    master-sentinel: 127.0.0.1 26479
    
    slave1-sentinel: 127.0.0.1 26579
    
    slave2-sentinel: 127.0.0.1 26679

    2.下载安装redis,我使用的是redis-4.0.14.tar.gz; 百度云提取有效期是30天,如果提示过期,可以下方留言,我看到后会放开权限,并回复!也可自行去官网下载  

    链接:https://pan.baidu.com/s/1ct7s4b5vdR_-bPncv9vqOQ
    提取码:1234

    3.将redis-4.0.14.tar.gz 放到指定的目录 /test_redis_sentinel,也可自行定义安放目录;

    解压:redis-4.0.14.tar.gz

    cd   redis-4.0.14   

    配置redis : 执行命令  

    make install ;也可以指定配置目录:先创建配置目录:

    mkdir  /test_redis_sentinel/redis_install_file;   make install   prefix=/test_redis_sentinel/redis_install_file

    4.配置环境

    cd /test_redis_sentinel

    mkdir redis_cluster

    mkdir redis_cluster/master_6479

    mkdir redis_cluster/slave_6579

    mkdir redis_cluster/slave_6679  

    配置redis

    ----------------------------------------------------------------------
    master:
    cp /test_redis_sentinel/redis-4.0.14/redis.conf    /test_redis_sentinel/redis_cluster/master_6479/

    cp /test_redis_sentinel/redis-4.0.14/sentinel.conf  /test_redis_sentinel/redis_cluster/master_6479/6479-sentinel.conf

    vi ./redis_cluster/master_6479/redis.conf(将对应配置修改成如下)

    #master redis.conf
    #端口
    port 6479
    #授权密码,在安全的环境中可以不设置
    requirepass 1234
    masterauth 1234
    #开启AOF
    appendonly yes
    slave-read-only yes
    #绑定ip,最好不要用127.0.0.1,否则远程java连接时会连接失败,也不要用 0.0.0.0,否则也会远程连接失败;
    bind 192.168.1.15
    #是否是受保护模式protected-mode yes 改成no;否则远程连接失败;
    protected-mode no

    -------------------------------------------------------------
    vi ./redis_cluster/master_6479/6479-sentinel.conf
    #master sentinel.conf
    #sentinel实例之间的通讯端口
    port 26479

    #<quorum>应该小于集群中slave的个数,只有当至少<quorum>个sentinel实例提交"master失效" 才会认为master为ODWON("客观"失效) .
    sentinel monitor mymaster 192.168.1.15 6479 2

    #授权密码,在安全的环境中可以不设置
    sentinel auth-pass mymaster 1234

    #master被当前sentinel实例认定为“失效”(SDOWN)的间隔时间
    sentinel down-after-milliseconds mymaster 30000

    #当新master产生时,同时进行“slaveof”到新master并进行同步复制的slave个数。
    #在salve执行salveof与同步时,将会终止客户端请求。
    #此值较大,意味着“集群”终止客户端请求的时间总和和较大。
    #此值较小,意味着“集群”在故障转移期间,多个salve向客户端提供服务时仍然使用旧数据。
    sentinel parallel-syncs mymaster 1

    #failover过期时间,当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel将会认为此次failoer失败。
    sentinel failover-timeout mymaster 900000

    #是否是受保护模式protected-mode yes 改成no;否则远程连接失败;
    protected-mode no

    -------------------------------------------------------------------------------

    配置redis-slave

    slave1:

    cp /test_redis_sentinel/redis-4.0.14/redis.conf       /test_redis_sentinel/redis_cluster/slave_6579/

    cp /test_redis_sentinel/redis-4.0.14/sentinel.conf        /test_redis_sentinel/redis_cluster/slave_6579/6579-sentinel.conf

    vi ./redis_cluster/slave_6579/redis.conf(将对应配置修改成如下)

    #slave1 redis.conf

    port 6579

    #在有#slaveof <masterip> <masterport>配置的下面添加一行
    slaveof 192.168.1.15 6479

    #其他配置和master redis.conf保持一致,如下:
    #授权密码,在安全的环境中可以不设置
    requirepass 1234
    masterauth 1234


    appendonly yes
    slave-read-only yes
    bind 192.168.1.15
    protected-mode no

    --------------------------------------------------------------------------------
    vi /test_reids_sentinel/redis_cluster/slave_6579/6579-sentinel.conf
    #slave1 sentinel.conf

    port 26579

    #注意,这里要配置主的ip+端口
    sentinel monitor mymaster 192.168.1.15 6479 2

    #其他配置和master的sentinel.conf 保持一致,如下:
    #授权密码,在安全的环境中可以不设置
    sentinel auth-pass mymaster 1234

    sentinel down-after-milliseconds mymaster 30000

    sentinel parallel-syncs mymaster 1

    sentinel failover-timeout mymaster 900000

    protected-mode no
    --------------------------------------------------------------------------------------

    slave2:

    cp /test_redis_sentinel/redis-4.0.14/redis.conf
    /test_redis_sentinel/redis_cluster/slave_6679/

    cp /test_redis_sentinel/redis-4.0.14/sentinel.conf /test_redis_sentinel/redis_cluster/slave_6679/6679-sentinel.conf

    vi ./redis_cluster/slave_6679/redis.conf(将对应配置修改成如下)

    #slave2 redis.conf
    port 6679

    #在有#slaveof <masterip> <masterport>配置的下面添加一行
    slaveof 192.168.1.15 6479

    #其他配置和master redis.conf保持一致,如下:
    #授权密码,在安全的环境中可以不设置
    requirepass 1234
    masterauth 1234


    appendonly yes
    slave-read-only yes
    bind 192.168.1.15
    protected-mode no
    -------------------------------------------------------------------------------------------
    vi /test_reids_sentinel/redis_cluster/slave_6679/6679-sentinel.conf

    -------------------------------------------------------------------------------------------
    #slave2 sentinel.conf
    port 26679

    #注意,这里要配置主的ip+端口
    sentinel monitor mymaster 192.168.1.15 6479 2

    #其他配置和master的sentinel.conf 保持一致,如下:

    #授权密码,在安全的环境中可以不设置
    sentinel auth-pass mymaster 1234

    sentinel down-after-milliseconds mymaster 30000

    sentinel parallel-syncs mymaster 1

    sentinel failover-timeout mymaster 900000

    protected-mode no

    --------------------------------------------------------------------------------

    #查看master:

    info  Replication  

    查看slave的状态:

    redis-cli -h 127.0.0.1 -p 6579

     6.测试

    场景1:slave宕机

    关闭slave1:

    shutdown

    查看slave 6579 sentinel

    redis-cli  -p  6579   info sentinel 

    查看master的Replication信息:

    此时只存在一个slave。

    场景2:slave恢复

    重新开启slave1:

    redis-server /test_redis_sentinel/redis_cluster/slave-6579/redis.conf

    查看sentinel状态:

    sentinel能快速的发现slave加入到集群中:

     

    查看master的Replication信息:

    场景3:master宕机

    master-sentinel作为master 1的leader,会选取一个master 1的slave作为新的master。slave的选取是根据一个判断DNS情况的优先级来得到,优先级相同通过runid的排序得到,但目前优先级设定还没实现,所以直接获取runid排序得到slave 1。

    然后发送命令slaveof no one来取消slave 1的slave状态来转换为master。当其他sentinel观察到该slave成为master后,就知道错误处理例程启动了。sentinel A然后发送给其他slave slaveof new-slave-ip-port 命令,当所有slave都配置完后,sentinel A从监测的masters列表中删除故障master,然后通知其他sentinels。

    关闭master:

    查看sentinel状态:

    6479-sentinel:

    自动将slave2即6579切换成master,原来的master变成slave。

    6579-sentinel:

    显示了failover的过程:

     

    场景4:master恢复

    重新启动原来的master:

    redis-server --include/test_redis_sentinel/redis_cluster/master-6479/redis.conf

    原来的master自动切换成slave,不会自动恢复成master:

    测试完成。

    注意:若在sentinel已选出新主但尚未完成其它实例的reconfigure之前,重启old master,则整个系统会出现无法选出new master的异常。 

     因文件是参考编写,可能存在些许错误,但是测试都是可以的,已经成功部署,有错的地方还请大神指出订正,谢谢

    参考链接:https://my.oschina.net/91jason/blog/480308  很感激大佬的辛苦奉献!

     

  • 相关阅读:
    向日葵、阳光
    laravel还是给我太多惊喜了
    滴滴笔试题——小试牛刀
    剑指offer——二叉搜索树的后序遍历序列
    2019春招美团笔试
    首次实习生招聘会——航天一院
    有趣的数字
    剑指offer——从上往下打印二叉树
    剑指offer——栈的压入、弹出序列
    剑指offer——包含min函数的栈
  • 原文地址:https://www.cnblogs.com/superming/p/15514365.html
Copyright © 2011-2022 走看看