LVS+keepalived 实战

Posted CTO老王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS+keepalived 实战相关的知识,希望对你有一定的参考价值。

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

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

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

wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./configure --prefix=/usr/local/keepalived
make 
make install
cp /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/keepalived
KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"
vi /etc/init.d/keepalived
#更新
. /etc/sysconfig/keepalived
PATH="$PATH:/usr/sbin"
export PATH
vi /usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   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

五、测试

ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  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 实战的主要内容,如果未能解决你的问题,请参考以下文章

lvs+keepalived实战

高可用集群之keepalived+lvs实战-技术流ken

项目实战03:Keepalived 实现高可用

LVS+keepalived 实战

LVS+Keepalived效率最高的群集(实战)

LVS+KeepAlived高可用部署实战应用