首页 > 代码库 > LVS+keepalived 实战

LVS+keepalived 实战

一、分别在backup lvs和master lvs上安装LVS

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
uname -rln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linuxtar zxvf ipvsadm-1.24.tar.gzcd ipvsadm-1.24make && make install

二、分别在backup lvs和master lvs上安装keepalived 

wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gztar zxvf keepalived-1.1.19.tar.gzcd keepalived-1.1.19./configure --prefix=/usr/local/keepalivedmake make installcp /usr/local/keepalived/sbin/keepalived /usr/sbin/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/mkdir /etc/keepalived

三、修改keepalived相关配置

vi /etc/sysconfig/keepalivedKEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"
vi /etc/init.d/keepalived#更新. /etc/sysconfig/keepalivedPATH="$PATH:/usr/sbin"export PATH
vi /usr/local/keepalived/etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {   notification_email {     50625185@qq.com   }   notification_email_from 50625185@qq.com   smtp_server localhost   smtp_connect_timeout 30   router_id NodeA}vrrp_instance VI_1 {    state MASTER    interface ens160    virtual_router_id 51    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {         192.168.20.229 //虚拟IP    }}virtual_server 192.168.20.229 80 {    //定义虚拟服务器    delay_loop 6                  //健康检查时间,单位是秒    lb_algo rr              //负载调度算法,这里设置为rr,即轮询算法    lb_kind DR              //LVS实现负载均衡的机制,可以有NAT、TUN和DR三个模式可选    persistence_timeout 1        //会话保持时间,单位是秒(可以适当延长时间以保持session)    protocol TCP                  //转发协议类型,有tcp和udp两种    real_server 192.168.20.200 8151 {  //定义WEB服务器       weight 1                  //权重       TCP_CHECK {                //通过tcpcheck判断RealServer的健康状态           connect_timeout 5      //连接超时时间           nb_get_retry 3        //重连次数           delay_before_retry 3  //重连间隔时间           connect_port 8151        //检测端口       }    }    real_server 192.168.20.201 8151 {  //定义WEB服务器       weight 2                 //权重       TCP_CHECK {                //通过tcpcheck判断RealServer的健康状态           connect_timeout 5      //连接超时时间           nb_get_retry 3        //重连次数           delay_before_retry 3  //重连间隔时间           connect_port 8151        //检测端口       }    }}

虚拟VIP:192.168.20.229 

分流WEB:192.168.20.200、192.168.20.201

四、启动keepalived 

/etc/init.d/keepalived start

五、测试

ipvsadmIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  docker223:80 rr persistent 1  -> 192.168.20.200:8151          Route   1      0          0    

浏览器访问 http://192.168.20.229 分流 http://192.168.20.200:8151 WEB服务

 

LVS+keepalived 实战