当前位置:七道奇文章资讯系统安全Linux安全
日期:2011-03-05 03:40:00  来源:本站整理

<b>Lvs+keepalived实现负载均衡、弊端剔除(DR情势)</b>[Linux安全]

赞助商链接



  本文“<b>Lvs+keepalived实现负载均衡、弊端剔除(DR情势)</b>[Linux安全]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:


Node1: 192.168.1.253
Node2: 192.168.1.254
VIP: 192.168.1.250
 
环境 centos5.2(我这里是用两台机实现的)
1、安装ipvsadm
yum -y install ipvsadm
 
2、安装keepalived
[root@node1 ~]# wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
[root@node1 ~]# tar zxvf keepalived-1.1.15.tar.gz
[root@node1 ~]# cd keepalived-1.1.15
[root@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.15
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes    #支持lvs
IPVS sync daemon support : Yes 
Use VRRP Framework       : Yes
Use LinkWatch            : No
Use Debug flags          : No
 
[root@node1 keepalived-1.1.15]# make && make install
到此,lvs+keepalived安装完成.但是还不能利用lvs功效,接下来以dr情势配置lvs+keepalived
 
3、[root@node1 ~]# cd /etc/keepalived/
[root@node1 keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_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 
[root@node1 ~]# /etc/init.d/keepalived start
假如要随系统启动,履行chkconfig --add keepalived
                                       chkconfig keepalived  on
 
4、接着配置真实服务器,这里通过脚本来实现吧(这个脚本只在WEB机上履行,我目前是用两台机.服务器既充当lvs、也充当web机)
 [root@node1 keepalived]# vim realserver
#!/bin/bash
#description : start realserver
VIP=192.168.1.250
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
[root@node1 keepalived]# chmod +x realserver
[root@node1 keepalived]# ./realserver start
 
5、启动keepalived (附启动日记)
[root@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 added
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.253 added
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Registering Kernel netlink reflector
Oct 15 13:59:36 vmtest Keepalived_healthcheckers: Registering Kernel netlink command channel
Oct 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 Bytes
Oct 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=28419
Oct 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 added
Oct 15 13:59:36 vmtest Keepalived: Starting VRRP child process, pid=28420
Oct 15 13:59:36 vmtest Keepalived_vrrp: Netlink reflector reports IP 10.0.0.253 added
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering Kernel netlink reflector
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering Kernel netlink command channel
Oct 15 13:59:36 vmtest Keepalived_vrrp: Registering gratutious ARP shared channel
Oct 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 Bytes
Oct 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 STATE
Oct 15 13:59:38 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 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.250
Oct 15 13:59:38 vmtest Keepalived_healthcheckers: Netlink reflector reports IP 192.168.1.250 added
Oct 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 added
Oct 15 13:59:43 vmtest Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.250
 
[root@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=11157
Oct 15 15:40:26 node1 Keepalived: Starting VRRP child process, pid=11159
Oct 15 15:40:26 node1 Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering Kernel netlink reflector
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering Kernel netlink command channel
Oct 15 15:40:26 node1 Keepalived_vrrp: Registering gratutious ARP shared channel
Oct 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 Bytes
Oct 15 15:40:26 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Oct 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 reflector
Oct 15 15:40:26 node1 Keepalived_healthcheckers: Registering Kernel netlink command channel
Oct 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 Bytes
Oct 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主从互备功效,当主lvs的keepalived启动后.会自动切换回来
[root@node1 ~]# tali -f /var/log/messages
Oct 15 15:43:18 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Oct 15 15:43:19 node1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Oct 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.250
Oct 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,呈现以下现像

绿线画的是lvs正常,当我终止httpd的时刻,再看lvs时,192.168.1.253这个IP已不在lvs表中(画红线处),因为我停掉了httpd服务.
我再启动httpd服务时,再查看lvs,192.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.51cto.com/936097/214975   以上是“<b>Lvs+keepalived实现负载均衡、弊端剔除(DR情势)</b>[Linux安全]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .