因工作需要搭建

LVS(VIP: 10.0.32.224)
10.0.32.59LVS-Master
10.0.32.65LVS-Backup
10.0.32.84(LVS-tomcat3)  
10.0.32.101(LVS-tomcat4)


1、安装LVS軟件

1.1.查看是否有IPVS模块

[root@Master~]# modprobe -l | grepipvs 
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko


1.2.安装IPVS管理工具

[root@Master~]# yum-y install ipvsadm


2、安装keepalived(分别在LVS-Master和LVS-Backup中)

2.1.安装keepalived并配置。

[root@Master~]# yum -yinstall keepalived(yum安装keepalived)
[root@Master~]# vi /etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs{
        notification_email {
            acassen@firewall.loc
            failover@firewall.loc 
            sysadmin@firewall.loc 
        }
        notification_email_fromAlexandre.Cassen@firewall.loc
        smtp_server 192.168.200.1  #设置SMTP Server地址
        smtp_connect_timeout 30
        router_id LVS_DEVEL
}
vrrp_sync_groupLVS_GV1 {
        group {
            LVS_qingsuan   #写集群的名字‘LVS_“名字”’,如:清算平台服务器
        }
}
vrrp_instanceLVS_qingsuan{
        state MASTER #指定Keepalived的角色,MASTER为主,BACKUP为备
        interface eth0 #绑定真实的物理网卡
        virtual_router_id 51 #虚拟路由编号,主备要一致
        priority 100 #定义优先级,数字越大,优先级越高,主DR必须大于备用DR
        advert_int 1 #检查间隔,默认为1s
        authentication {
            auth_type PASS 
            auth_pass qingsuan 
        }
        virtual_ipaddress {
            10.0.32.224      #定义虚拟IP(VIP)为可多设,每行一个
        }
}
 
virtual_server10.0.32.224443 { #定义对外提供服务的LVS的VIP以及port
        delay_loop 6 #设置健康检查时间,单位是秒
        lb_algo wlc #设置负载调度的算法为wlc
        lb_kind DR #设置LVS实现负载的机制,有NAT、TUN、DR三个模式,设DR模式
        persistence_timeout 50 #会话保持时间,单位为秒
        protocol TCP #指定转发协议类型,有TCP和UDP两种
        sorry_server 127.0.0.1 80
 
        real_server 10.0.32.84 443 { #指定real server1的IP地址
            weight 1 #配置节点权值,数字越大
            TCP_CHECK {
                connect_timeout 3 #表示3秒无响应,则超时
                tcp_port 443
            }
        }
 
        real_server10.0.32.101 443 { #指定real server2的IP地址
            weight 1 #配置节点权值,数字越大
            TCP_CHECK {
                connect_timeout 3 #表示3秒无响应,则超时
            }
        }
}


2.2.启动keepalived、设置keepalived服务自启动,检查是否生效。

[root@Master~]# /etc/init.d/keepalived start
[root@Master~]# chkconfigkeepalived on


3、在LVS-Backup上安装LVS、keepalived并配置

3.1.安装LVS、keepalived

[root@Backup~]# yum-y install ipvsadm
[root@Backup~]# yum-y install keepalived


3.2.配置LVS-Backup的keepalived

先从LVS-Master把修改过的keepalived配置文件复制过来。

[root@Mastekeepalived]#scpkeepalived.conf root@10.0.32.65:/etc/keepalived/
[root@Backup~]# vi /etc/keepalived/keepalived.conf
只修改state为BACKUP和priority为90,MASTER为主机服务器,BACKUP为备用服务器。
stateBACKUP
priority90


 

3.3.启动LVS-Backup的keeplived服务,并检查是否生效。

[root@Backup~]# /etc/init.d/keepalived start
[root@Backup~]# chkconfigkeepalived on


 

5、配置WEB服务器,即real server节点1和2

由于采用的是DR方式调度,Real_Server会以LVS的VIP来直接回复Client,所以需要在Real_Server的lo上开启LVS的VIP来与Client建立通信

配置web

[root@web~]# vi /etc/init.d/lvs-server#编辑脚本放到/etc/init.d/下
#!/bin/bash
#chkconfig: 35 85 15
VIP=10.0.32.223
chmod755 /etc/rc.d/init.d/functions
 
./etc/rc.d/init.d/functions
case"$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIPnetmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
;;
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@web~]# chmod 755 /etc/init.d/lvs-server
[root@web~]# /etc/init.d/lvs-server start