一、 网卡配置:
client:windows
192.168.2.26
director:rhel5.6 x64单网卡
网卡配置:eth0
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0C:29:C4:0D:1F
ONBOOT=yes
IPADDR=192.168.2.230
NETMASK=255.255.255.0
GATEWAY=192.168.2.254
网卡配置:eth0:0 --对外服务的IP
DEVICE=eth0:0
BOOTPROTO=static
HWADDR=00:0C:29:C4:0D:1F
ONBOOT=yes
IPADDR=192.168.2.220
NETMASK=255.255.255.255
realserver1:rhel5.6 x64,apache
网卡配置:eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
HWADDR=00:0c:29:9f:69:74
IPADDR=192.168.2.232
NETMASK=255.255.255.0
GATEWAY=192.168.2.254
网卡配置:lo:0 --director发包到此服务器,此服务器确认是否是发给自己的
DEVICE=lo:0
IPADDR=192.168.2.220
NETMASK=255.255.255.255
ONBOOT=yes
realserver2:rhel5.6 x64,apache
网卡配置:eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
HWADDR=00:0C:29:52:AE:FA
IPADDR=192.168.2.233
NETMASK=255.255.255.0
GATEWAY=192.168.2.254
网卡配置:lo:0 --director发包到此服务器,此服务器确认是否是发给自己的
DEVICE=lo:0
IPADDR=192.168.2.220
NETMASK=255.255.255.255
ONBOOT=yes
二、director配置
1、检查环境是否支持ip_vs,出现以下结果,则表明系统支持
[root@localhost ~]# modprobe -l | grep ipvs
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_dh.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_ftp.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_lblc.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_lblcr.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_lc.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_nq.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_rr.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_sed.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_sh.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_wlc.ko
/lib/modules/2.6.18-238.el5/kernel/net/ipv4/ipvs/ip_vs_wrr.ko
2、安装ipvsadm配置工具,yum安装即可
yum -y install ipvsadm
3、开启转发功能
修改/etc/sysctl.conf,确保有以下两行:
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1 此行默认值为1,检查一下
改动生效:
sysctl -p
4、配置lvs转发规则:
[root@localhost ~]# vim /lvs.sh
ipvsadm -C
ipvsadm -A -t 192.168.2.220:80 -s rr
ipvsadm -a -t 192.168.2.220:80 -r 192.168.2.232:80 -g
ipvsadm -a -t 192.168.2.220:80 -r 192.168.2.233:80 -g
注:
1、最好在真实服务器性能相差不大的情况下使用rr转发方式也可以shell下一条一条的输入,这里为了方便,管理使用脚本;
2、虚拟服务器IP一定是对外可见的公网IP!!!
持久链接脚本:
[root@localhost ~]# vim /lvs.sh
ipvsadm -C
ipvsadm -A -t 192.168.2.220:80 -s rr -p
ipvsadm -a -t 192.168.2.220:80 -r 192.168.2.232:80 -g
ipvsadm -a -t 192.168.2.220:80 -r 192.168.2.233:80 -g
注:-p默认为300秒,如果到了300秒,客户端还处理链接状态,则lvs服务器更新持久链接模板,再续300秒,还可以自定义时间值,单位(秒): -p 3600
5、启动
sh /lvs.sh --此时已经可以提供服务
service ipvsadm save --保存一下否则service ipvsadm stop(重启机器)后使用chkconfig自启动规则会丢失
service ipvsadm start
停止:service ipvsadm stop
重启:service ipvsadm restart
查看启动后的状态:
[root@localhost ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.220:http rr
-> 192.168.2.232:http Route 1 0 0
-> 192.168.2.233:http Route 1 0 0
6、添加开机自启动
[root@localhost ~]# chkconfig --level 3 ipvsadm on //推荐,需要service ipvsadm save
或
[root@localhost ~]# echo "/bin/sh /lvs.sh" >> /etc/rc.local
三、realserver配置(两台一样)
添加启动脚本(并添加到开机启动)
#!/bin/bash VIP0=192.168.2.220 . /etc/rc.d/init.d/functions case "$1" in start) echo " Start Realserver." /sbin/ifconfig lo:0 $VIP0 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 " Stop Realserver." 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 88 ;; esac
四:测试
客户端访问192.168.2.220,会看到不同服务器上的内容。
director查看连接状态:
[root@localhost ~]# ipvsadm -lcn
IPVS connection entries
pro expire state source virtual destination
TCP 14:56 ESTABLISHED 192.168.2.26:62460 192.168.2.220:80 192.168.2.233:80
TCP 01:47 FIN_WAIT 192.168.2.26:62459 192.168.2.220:80 192.168.2.232:80
补充:配置完成后,LVS开启WEB服务的话,访问2.220的流量被转发,访问2.230不受影响。