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

[经验分享] Lvs+keepalived实现负载均衡、故障剔除(DR模式)

[复制链接]

尚未签到

发表于 2015-11-19 15:45:40 | 显示全部楼层 |阅读模式
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://antsw.blog.iyunv.com/936097/214975 DSC0000.jpg Node1: 192.168.1.253Node2: 192.168.1.254VIP: 192.168.1.250 环境 centos5.2(我这里是用两台机实现的)1、安装ipvsadmyum -y install ipvsadm 2、安装keepalived[iyunv@node1 ~]# wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz[iyunv@node1 ~]# tar zxvf keepalived-1.1.15.tar.gz[iyunv@node1 ~]# cd keepalived-1.1.15[iyunv@node1 keepalived-1.1.15]# ./configure --prefix=/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.18-92.el5-i686/configure后会输入这结果为正确Keepalived configuration------------------------Keepalived version       : 1.1.15Compiler                 : gccCompiler flags           : -g -O2Extra Lib                : -lpopt -lssl -lcryptoUse IPVS Framework       : Yes    #支持lvsIPVS sync daemon support : Yes  Use VRRP Framework       : YesUse LinkWatch            : NoUse Debug flags          : No [iyunv@node1 keepalived-1.1.15]# make && make install到此,lvs+keepalived安装完成。但是还不能使用lvs功能,接下来以dr模式配置lvs+keepalived 3、[iyunv@node1 ~]# cd /etc/keepalived/[iyunv@node1 keepalived]# vim keepalived.conf! Configuration File for keepalivedglobal_defs {   notification_email {    shanswei@gmail.com   }   notification_email_from  shanswei@gmail.com   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL} vrrp_instance VI_1 {    state MASTER      ##lvs设置成master    interface eth0    virtual_router_id 51    priority 102     ##lvs设置成 99    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {       192.168.1.250 ## 如多个vip继续换行填写。    }}virtual_server 192.168.1.250 80 {    delay_loop 6    lb_algo wlc    lb_kind DR    persistence_timeout 50    protocol TCP     real_server 192.168.1.253 80 {        weight 3        TCP_CHECK {        connect_port 80        connect_timeout 30        }    }    real_server 192.168.1.254 80 {        weight 1        TCP_CHECK {        connect_port 80        connect_timeout 30        }    } 好了,keepalived配置完成了。启动keepalived  
[iyunv@node1 ~]# /etc/init.d/keepalived start
如果要随系统启动,执行chkconfig --add keepalived
                                       chkconfig keepalived  on
4、接着配置真实服务器,这里通过脚本来实现吧(这个脚本只在WEB机上执行,我现在是用两台机。服务器既充当lvs、也充当web) [iyunv@node1 keepalived]# vim realserver#!/bin/bash#description : start realserverVIP=192.168.1.250/etc/rc.d/init.d/functionscase "$1" instart)echo " start LVS of REALServer"/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 upecho "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announce;;stop)/sbin/ifconfig lo:0 downecho "close LVS Directorserver"echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "0" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/all/arp_announce;;*)echo "Usage: $0 {start|stop}"exit 1esac[iyunv@node1 keepalived]# chmod +x realserver[iyunv@node1 keepalived]# ./realserver start 5、启动keepalived (附启动日志)[iyunv@node1 ~]# /etc/init.d/keepalived start  (LVS)Oct 15 13:59:36 vmtest Keepalived: Starting Keepalived v1.1.15 (10/15,2009)Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Using MII-BMSR NIC polling thread...Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.253 addedOct 15 13:59:36 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.253 addedOct 15 13:59:36 vmtest Keepalived_healthcheckers: Registering Kernel netlink reflectorOct 15 13:59:36 vmtest Keepalived_healthcheckers: Registering Kernel netlink command channelOct 15 13:59:36 vmtest Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Configuration is using : 11891 BytesOct 15 13:59:36 vmtest Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.253:80]Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.254:80]Oct 15 13:59:36 vmtest Keepalived: Starting Healthcheck child process, pid=28419Oct 15 13:59:36 vmtest Keepalived_vrrp: Using MII-BMSR NIC polling thread...Oct 15 13:59:36 vmtest Keepalived_vrrp: Netlink reflector reports IP 192.168.1.253 addedOct 15 13:59:36 vmtest Keepalived: Starting VRRP child process, pid=28420Oct 15 13:59:36 vmtest Keepalived_vrrp: Netlink reflector reports IP 10.0.0.253 addedOct 15 13:59:36 vmtest Keepalived_vrrp: Registering Kernel netlink reflectorOct 15 13:59:36 vmtest Keepalived_vrrp: Registering Kernel netlink command channelOct 15 13:59:36 vmtest Keepalived_vrrp: Registering gratutious ARP shared channelOct 15 13:59:36 vmtest Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.Oct 15 13:59:36 vmtest Keepalived_vrrp: Configuration is using : 36278 BytesOct 15 13:59:36 vmtest Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]Oct 15 13:59:37 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATEOct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATEOct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.Oct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250Oct 15 13:59:38 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.250 addedOct 15 13:59:38 vmtest avahi-daemon[2580]: Registering new address record for 192.168.1.250 on eth0.Oct 15 13:59:38 vmtest Keepalived_vrrp: Netlink reflector reports IP 192.168.1.250 addedOct 15 13:59:43 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250 [iyunv@node1 ~]# /etc/init.d/keepalived start  (LVS)Oct 15 15:40:26 node1 Keepalived: Starting Keepalived v1.1.15 (10/12,2009)Oct 15 15:40:26 node1 Keepalived_healthcheckers: Using MII-BMSR NIC polling thread...Oct 15 15:40:26 node1 Keepalived: Starting Healthcheck child process, pid=11157Oct 15 15:40:26 node1 Keepalived: Starting VRRP child process, pid=11159Oct 15 15:40:26 node1 Keepalived_vrrp: Using MII-BMSR NIC polling thread...Oct 15 15:40:26 node1 Keepalived_vrrp: Registering Kernel netlink reflectorOct 15 15:40:26 node1 Keepalived_vrrp: Registering Kernel netlink command channelOct 15 15:40:26 node1 Keepalived_vrrp: Registering gratutious ARP shared channelOct 15 15:40:26 node1 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.Oct 15 15:40:26 node1 Keepalived_vrrp: Configuration is using : 36712 BytesOct 15 15:40:26 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATEOct 15 15:40:26 node1 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]Oct 15 15:40:26 node1 Keepalived_healthcheckers: Registering Kernel netlink reflectorOct 15 15:40:26 node1 Keepalived_healthcheckers: Registering Kernel netlink command channelOct 15 15:40:26 node1 Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.Oct 15 15:40:26 node1 Keepalived_healthcheckers: Configuration is using : 12417 BytesOct 15 15:40:26 node1 Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.253:80]Oct 15 15:40:26 node1 Keepalived_healthcheckers: Activating healtchecker for service [192.168.1.254:80]绿色字体表示启动成功 6、测试,我现在停掉主lvs上面的keepalived,从lvs会出现以下日志,表示从lvs能正常接管主的lvs,从而实现lvs主从互备功能,当主lvskeepalived启动后。会自动切换回来[iyunv@node1 ~]# tali -f /var/log/messagesOct 15 15:43:18 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATEOct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATEOct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.Oct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250Oct 15 15:43:19 node1 avahi-daemon[3087]: Registering new address record for 192.168.1.250 on eth0.Oct 15 15:43:24 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250 7、测试down机,比如我这机跑的是web服务,我现在停止apache,出现以下现像 DSC0001.jpg 绿线画的是lvs正常,当我停止httpd的时候,再看lvs时,192.168.1.253这个IP已不在lvs表中(画红线处),因为我停掉了httpd服务。我再启动httpd服务时,再查看lvs192.168.1.253又自动加进表中(画蓝线处),此时已实现了负载均衡、故障剔除功能。 8、心得 这个配置只需要两个软件就可以了,ipvsadm、keepalived,再加上一个脚本,这个脚本只在
web机上执行
还有keepalived的配置文件,在一台机上配置好了之后,直接拷到另一台机去,拷到另一台
机只需要改动两个地方
vrrp_instanceVI_1{
    stateMASTER       ##主lvs设置成master
    interfaceeth0
    virtual_router_id51
    priority102   ##从lvs设置成 99
    stateMASTER 如果是从lvs,则改成BACKUP,priority102 改为小于100.
  本文出自 “山捻子” 博客,请务必保留此出处http://antsw.blog.iyunv.com/936097/214975

运维网声明 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-141272-1-1.html 上篇帖子: lvs+keepalived简单配置 下篇帖子: 在CentOS 6.3 下安装 ipvsadm 1.26.1 & keepalived 1.2.5
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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