设为首页 收藏本站
查看: 1262|回复: 0

[经验分享] 基于keepalived、redis sentinel的高可用redis集群【修改版】

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2015-9-4 11:34:47 | 显示全部楼层 |阅读模式
  原方案地址原方案


  硬件



机器名IP作用
master192.168.0.2redis的master服务器
slave1192.168.0.3redis的slave服务器
slave2192.168.0.4redis的slave服务器
route1192.168.0.5【虚拟IP:192.168.0.7】keepalived和redis sentinel服务器,承载写redis的VIP【虚拟ip】,做写的双机热备的主master指定
route2192.168.0.6【虚拟IP:192.168.0.8】keepalived和redis sentinel服务器,承载读redis的VIP,做读的负载均衡和写的双机热备的master备份路由指定

详细的keepalived配置,route1
! Configuration File for keepalived
global_defs {
   notification_email {
     邮箱
   }
   notification_email_from 邮箱@bitauto.com
   smtp_server 邮箱服务器地址
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 100
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }   
    virtual_ipaddress {
        192.168.0.7
    }     
}
vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 101
    priority 101
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.0.8
    }
}
virtual_server 192.168.0.7 6379 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    persistence_timeout 15
    protocol TCP
    real_server 192.168.0.4 6379 {
        weight 4
        notify_up /home/wind/redis_up.sh
        MISC_CHECK {
        misc_path "/home/wind/redischeck.py 192.168.0.4 6379"
        misc_timeout 5
        }
    }
    real_server 192.168.0.2 6379 {
        weight 3      
notify_up /home/wind/redis_up.sh
        MISC_CHECK {
        misc_path "/home/wind/redischeck.py 192.168.0.2 6379"
        misc_timeout 5
        }
    }
    real_server 192.168.0.3 6379 {        
weight 3      
notify_up /home/wind/redis_up.sh
        MISC_CHECK {
        misc_path "/home/wind/redischeck.py 192.168.0.2 6379"
        misc_timeout 5
        }
    }
}
virtual_server 192.168.0.8 6379 {
    delay_loop 3
    lb_algo wrr
    lb_kind DR
    persistence_timeout 30
    protocol TCP
    real_server 192.168.0.2 6379 {
        weight 8
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 6379
        }
    }
    real_server 192.168.0.3 6379 {
        weight 2
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 6379
        }
    }
    real_server 192.168.0.4 6379 {
        weight 2
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 6379
        }
    }
}


route2的配置文件
! Configuration File for keepalived
global_defs {
   notification_email {
    # xieqj@bitauto.com
    #shanghq@bitauto.com
   }
   notification_email_from xieqj@bitauto.com
   smtp_server mail.bitauto.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }   
    virtual_ipaddress {
        192.168.87.89
    }
    #notify_master "/home/wind/redis.sh"     
}
vrrp_instance VI_2 {
    state MASTER
    interface eth1
    virtual_router_id 101
    priority 151
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.87.90
    }
}
virtual_server 192.168.87.89 6379 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    persistence_timeout 15
    protocol TCP
    real_server 192.168.87.103 6379 {
        weight 4        
notify_up /home/wind/redis_up.sh
       MISC_CHECK {
        misc_path "/home/wind/redischeck.py 192.168.87.103 6379"
        misc_timeout 5
        }
    }
    real_server 192.168.87.104 6379 {      
weight 4        
notify_up /home/wind/redis_up.sh
       MISC_CHECK {
        misc_path "/home/wind/redischeck.py 192.168.87.103 6379"
        misc_timeout 5
        }
    }
    real_server 192.168.87.105 6379 {      
weight 4        
notify_up /home/wind/redis_up.sh
       MISC_CHECK {
        misc_path "/home/wind/redischeck.py 192.168.87.103 6379"
        misc_timeout 5
        }
    }
}
virtual_server 192.168.87.90 6379 {
    delay_loop 3
    lb_algo wrr
    lb_kind DR
    persistence_timeout 30
    protocol TCP
    real_server 192.168.87.104 6379 {
        weight 8
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 6379
        }
    }
    real_server 192.168.87.105 6379 {
        weight 2
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 6379
        }
    }
}

  keepalived的master的MISCH_CHECK监测脚本
  

#!/usr/bin/python
import sys,commands
cmd="/usr/local/bin/redis-cli -h "+sys.argv[1]+" -p "+sys.argv[2]+" info"  #sys.argv是输入的参数,sys.argv[0]是需要执行的命令,以后才是参数。1是ip,2是端口
str=commands.getoutput(cmd)
ismaster=-100
ismaster=str.count("role:master")#原来是使用index方法,但是找不到字符串时会报错
zero=0
if ismaster>zero:
sys.exit(0)    #返回0在keepalived表示健康
else:
sys.exit(1)    #返回1表示keepalived检测端口不健康

更多MISC_CHECK参考见http://bbs.ywlm.net/thread-845-1-1.html


  
  redis维护脚本redis_up.sh用在检测到服务启动时

#!/usr/bin/env bash
/usr/local/bin/redis-cli -h $argv[1] -p 6379 config set appendonly no

  redis设置与sentinel设置无变化
  需要特别注意的配置,如果没有这个配置就会发生keepalived不转发的的问题,而且是如果redis和keepalive是同一台机器,会转发,但是如果相互间独立,则realserver收不到转发包
  配置master

vim /etc/sysctl.conf,添加内容如下

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
执行命令
sysctl -p
ip addr add 192.168.0.7/32 dev lo
ip addr add 192.168.0.8/32 dev loip add list
lo环上出现了指定的ip即可  配置slave1


  vim /etc/sysctl.conf,添加内容如下

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
执行命令
sysctl -p
ip addr add 192.168.0.7/32 dev lo
ip addr add 192.168.0.8/32 dev loip add list  配置slave2


  vim /etc/sysctl.conf,添加内容如下

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
执行命令

sysctl -p
ip addr add 192.168.0.7/32 dev lo
ip addr add 192.168.0.8/32 dev loip add list

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-109333-1-1.html 上篇帖子: Redis主从配置及通过Keepalived实现Redis自动切换高可用 下篇帖子: 高可用开源方案 Keepalived VS Heartbeat对比
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表