linux LVS (keepalived+ipvsadm)负载均衡搭建

ipsvadm 配置
一:安装配置 LVS+DR+Keepalived
client 访问的地址 VIP 192.168.133.100
       负载服务器master真实IP        192.168.133.128
       负载服务器backup真实IP    192.168.133.150
       负载服务器虚拟IP              192.168.133.100
       后端WEB服务器IP                 192.168.133.128
       后端WEB服务器IP                 192.168.133.150
系统:Redhat 5.4 x86 内核:2.6.18-164.el5
1.下载所需要的软件:ipvsadm keepalived
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz    我这里是直接安装的RHEL4.0自带的ipvsadm-1.24-6.i386.rpm
wget http://www.keepalived.org/software/keepalived-1.1.18.tar.gz
2.安装ipvsadm(负载均衡器都要配置master和backup)
  从Linux内核版本2.6起,ip_vs code已经被整合进了内核中,因此,只要在编译内核的时候选择了ipvs的功能,您的Linux即能支持LVS。Linux 2.4.23以后的内核版本也整合了ip_vs code,但如   果是更旧的内核版本,您得自己手动将ip_vs code整合进内核原码中,并重新编译内核方可使用lvs。
   我使用的RHEL4.0  这个版本支持ipvsadm,所以无需更改内核配置  rpm -ivh ipvsadm-1.24-6.i386.rpm
   然后使用ipvsadm -v
   ipvsadm v1.24 2005/12/10 (compiled with popt and IPVS v1.2.1)
   则Ipvsadm安装成功                                                          
3.安装keepalived负载均衡器都要配置master和backup)  以下建议完全参照,否则会有一些问题
tar    zxvf keepalived-1.1.18tar.gz
cd keepalived-1.1.18
./configure --prefix=/usr/local/keepalived
make 
make install
 cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
 cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
 mkdir /etc/keepalived
 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
 cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
启动keepalived
service keepalived start|stop|restart

4.配置master和backup
A.开启ip_forward 转发功能:
echo 1 >/proc/sys/net/ipv4/ip_forward   默认是0,关闭ip转发;这里需要开启,所以设置值为1
B.配置keepalived.conf 配置文件 (backup 192.168.133.150)
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.133.128
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {         #配置负载均衡器的 主和备 ,并做相关设置
    state BACKUP             #备份服务器上将MASTER改为BACKUP 
    interface eth0           #HA监测网络接口
    virtual_router_id 51     #主、备机的virtual_router_id必须相同
    priority 100             #主、备机取不同的优先级,主机值较大,备份机值较小
    advert_int 1             #VRRP Multicast广播周期秒数
    authentication {
        auth_type PASS       #VRRP认证方式
        auth_pass 1111       #VRRP口令字
    }
    virtual_ipaddress {
        192.168.133.100      #(如果有多个VIP,继续换行填写.)
        
    }
}

virtual_server 192.168.133.100 80 {      VIP设置
    delay_loop 2                         #每隔2秒查询realserver状态
    lb_algo rr                           #lvs 算法
    lb_kind DR                           #Direct Route
    nat_mask 255.255.255.0               
    persistence_timeout 50               #同一IP的连接60秒内被分配到同一台realserver
    protocol TCP                         #用TCP协议检查realserver状态

    real_server 192.168.133.128 80 {     真实主机1配置
        weight 100                       #设置后台主机的权重
        TCP_CHECK {                      #TCP协议检查
            connect_timeout 3            #
            nb_get_retry 3               #
            delay_before_retry 3         #
        }
    }
    real_server 192.168.133.150 80  {    真实主机2配置
        weight 1                         #设置后台主机的权重
        TCP_CHECK {                      #TCP协议检查
            connect_timeout 3            #
            nb_get_retry 3               #
            delay_before_retry 3         #
        }
    }
}
配置keepalived.conf 配置文件 (master 192.168.133.128)
! Configuration File for keepalived
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.133.128
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.133.100
    }
}

virtual_server 192.168.133.100 80 {
    delay_loop 2
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.133.128 80 {
        weight 100
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.133.150 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

5.配置 VIP:   master 配置一下就可以
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.133.100
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
重启网卡,启动keepalived 显示如下成功!
   service network restart
   service keepalived restart

业务热线电话:(+86)021-52765253 021-61172807      上海哲涛网络科技有限公司版权所有 © 2005-2018       沪ICP备06058430-1号

沪公网安备 31011302000898号

校睿宝,培训机构学员管理软件,教师工资计算软件,消课管理软件!

手机智慧课堂答题,短信答题,微信答题!

数据备份软件,文件数据备份软件,文件备份软件,B/S架构备份软件,分布式计算机备份软件!