实验信息和拓扑:
备注:Centos 6.5 selinux –disabled iptables off
ServerName | Ipaddress information |
LVSMaster | 172.16.100.10/24 |
LVSBackup | 172.16.100.11/24 |
VIP | 172.16100.200/24 |
RealServer1 | 172.16.100.100/24 |
RealServer2 | 172.16.100.101/24 |
- 配置lvs Master & lvs Backup
# yum install popt popt-devel popt-static libnl-devel libnl–y
# mkdir -p mkdir /usr/local/src/lvs
# cd /usr/local/src/lvs/
安装ipvsadm
# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
# tar zxvf ipvsadm-1.26.tar.gz
# make && make install && echo"install LVS ok"
安装keepalived
# wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
# tar zxvf keepalived-1.2.13.tar.gz
# cd keepalived-1.2.13
# ./configure && make && make install && echo "installkeepalived ok" || echo "install keepalived is failed"
配置keepalived为启动脚本:
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/sbin/keepalived /usr/sbin/
LVS Backup 配置同上
Master keepalived.conf配置文件:
!Configuration File for keepalived
global_defs{
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_instanceVI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1q2w3e4r5t6y
}
virtual_ipaddress {
172.16.100.200
}
}
virtual_server172.16.100.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 172.16.100.100 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 172.16.100.101 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
Lvs Backup配置文件:
! Configuration File for keepalived
global_defs{
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_instanceVI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1q2w3e4r5t6y
}
virtual_ipaddress {
172.16.100.200
}
}
virtual_server172.16.100.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 172.16.100.100 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 172.16.100.101 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
两台realserver配置脚本如下:
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=172.16.100.200
host=`/bin/hostname`
case "$1" in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig lo down
/sbin/ifconfig lo 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
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
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
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ ! "$islothere" -o !"isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
# sh realserver.sh start
- 访问验证:
1.任意关闭一台web服务器,服务都可以正常访问
2.任意关闭一台lvs,服务都可以正常访问
本文出自 “Sword Slave” 博客,请务必保留此出处http://diudiu.blog.51cto.com/6371183/1568003