Ubuntu OS xen虚拟机上测试lvs和keepalived负载均衡
网络拓扑:
虚拟机管理器和xen虚拟机实例:
step1: 安装LVS和keepalived:在director服务器上,分别安装LVS和keepalived软件,使用命令:
$sudo apt-get install ipvsadm
装keepalived,使用命令:
$sudo apt-get installkeepalived
step2:配置keepalived:安装装完之后,在/etc/keepalived目录下建立keepalived.conf文件,写入配置文件如下:
# Global Configuration
global_defs {
lvs_id director1
}
# VRRP Configuration
vrrp_instance LVS {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.122.38
}
# Virtual ServerConfiguration - for WWW service
virtual_server 192.168.122.3880 {
delay_loop 1
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
# Real Server 1 configuration
real_server 192.168.122.36 80{
weight 3
TCP_CHECK {
connection_timeout 10
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.122.37 80{
weight 1
TCP_CHECK {
connection_timeout 10
nb_get_retry 3
delay_before_retry 3
}
}
}
step3:配置LVS
LVS在第一步的时候已经安装过了,现在只需要配置即可。
在director上使用如下命令:
$sudo ifconfig eth0:0192.168.122.38 broadcast 192.168.122.38 netmask 255.255.255.255 up
$sudo route add -host192.168.122.38 dev eth0:0
$sudo echo "1" >/proc/sys/net/ipv4/ip_forward
$sudo ipvsadm -C
$sudo ipvsadm -A -t192.168.122.38:80 -s rr
$sudo ipvsadm -a -t192.168.122.38:80 -r 192.168.122.36:80 -g
$sudo ipvsadm -a -t192.168.122.38:80 -r 192.168.122.37:80 -g
$sudo ipvsadm
$sudo sysctl -p
step4,配置RealServer
在两台www服务器上,分别配置如下命令:
$sudo ifconfig lo:0192.168.122.38 broadcast 192.168.122.38 netmask 255.255.255.255 up
$sudo route add -host192.168.122.38 dev lo:0
$sudo echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
$sudo echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
$sudo echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
$sudo echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
$sudo sysctl -p
step5,验证
在client机器上,访问http://192.168.122.38;可以看出来至于192.168.122.37虚拟机:
把192.168.122.37虚拟机apache关闭:
root@sst-HVM-domU:~#ifconfig eth0
eth0 Link encap:以太网 硬件地址 00:16:3e:00:8d:2b
inet地址:192.168.122.37 广播:192.168.122.255 掩码:255.255.255.0
root@sst-HVM-domU:~#service apache2 stop
* Stopping web serverapache2 apache2: Could not reliably determine the server's fully qualifieddomain name, using 127.0.1.1 for ServerName
...waiting [ OK ]
再来访问http://192.168.122.38