看到标题,你就知道我做什么,所以这里直接贴安装和配置步骤。
VIP: 192.168.1.98
LVS主:192.168.1.216 (redhat5-2.6.18-194.el5-x86_64)
LVS备:192.168.1.217 (redhat5-2.6.18-194.el5-x86_64)
real_server1:192.168.1.220 (suse11)
real_server2:192.168.1.226 (redhat5)
一、LVS主、备上都安装ipvsadm + keepalived
安装ipvsadm-1.24
1, wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
2,创建一个连接文件:ln -sv /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux
3,解压:tar zxvf ipvsadm-1.24.tar.gz
4,安装:cd ipvsadm-1.24; make;make install
5,执行ipvsadm,检验ipvsadm 是否被正确安装
安装keepalived-1.1.20 (当前最新是1.2.2,发现安装不上,就选了1.1.*的最新一个版本)
1,wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
2,解压:tar zxvf keepalived-1.1.20.tar.gz
3,安装:cd keepalived-1.1.20; ./configure –prefix=/usr/local/keepalive; make;make install
安装完成生成目录:/usr/local/keepalived
4,配置keepalived.conf :
vi /usr/local/keepalive/etc/keepalived/keepalived.conf
LVS主:
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL_1
}
vrrp_sync_group VGM {
group {
VI_CACHE
}
}
vrrp_instance VI_CACHE {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 216
priority 150
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.98
}
}
virtual_server 192.168.1.98 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
# persistence_timeout 50 (这里可以让访问的IP在一段时间转发到相同的real_server)
protocol TCP
real_server 192.168.1.226 80 {
weight 100
TCP_CHECK {
connect_timeout 5
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.220 80 {
weight 100
TCP_CHECK {
connect_timeout 5
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
LVS备的配置多和LVS主的配置一致,需要修改以下几段:
global_defs {
router_id LVS_DEVEL_2 #随便取,不重复就行
}
vrrp_instance VI_CACHE {
state BACKUP #
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 217 #随便取,不重复就行
priority 100 #备比主的值小就行
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.98
}
}
配置完成。
LVS主/备启动keepalived。因为我的配置文件的路径不是/etc/keepalived/keepalived.conf
所以必须带上-f或--use-file
/usr/local/keepalive/sbin/keepalived --use-file /usr/local/keepalive/etc/keepalived/keepalived.conf
5,开启net.ipv4.ip_forward = 1 在 /etc/sysctl.conf 中,并使生效:sysctl -p
二、real_server都增加以下脚本并启动
#!/bin/bash
#description : start realserver
VIP=192.168.1.98
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIP 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 "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
启动:/usr/local/bin/lvs_real start
在局域网中的其它电脑上ping 192.168.1.98 ,如果不通请先检查防火墙相关设置。
正在 Ping 192.168.1.98 具有 32 字节的数据:
来自 192.168.1.98 的回复: 字节=32 时间=6ms TTL=64
来自 192.168.1.98 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.1.98 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.1.98 的回复: 字节=32 时间=1ms TTL=64
配置成功。