因为没有其他机器来搭建集群环境,我只能在我的小本上安装虚拟机来模拟集群环境了,先介绍下网络环境:
虚拟机1:192.168.1.6 eth0 MASTER CentOS 6.2
虚拟机2:192.168.1.8 eth2 BACKUP CentOS 6.2
VIP:192.168.1.10
1、分别在两台虚拟机上安装keepalived、ipvsadm、apache安装细节就不说了,网上都有。
2、 在MASTER虚拟机上添加可执行文件lvs_dr,BACKUP稍有区别,内容如下
VIP= 192.168 . 1.10 #Virtual IP
RIP1= 192.168 . 1.6 #Real Server IP
RIP2= 192.168 . 1.8 #Real Server IP
/etc/rc.d/init.d/functions
case " $1 " in
start)
echo " start LVS of DR "
/sbin/ifconfig eth0: 0 $VIP broadcast $VIP netmask 255.255 . 255.255 up #注意子网掩码,BACKUP是eth2
/sbin/route add -host $VIP dev eth0: 0 #BACKUP是eth2
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t $VIP: 80 -s rr
/sbin/ipvsadm -a -t $VIP: 80 -r $RIP1: 80 -g
/sbin/ipvsadm -a -t $VIP: 80 -r $RIP2: 80 -g
/sbin/ipvsadm
;;
stop)
echo " close LVS of DR "
/sbin/ifconfig eth0: 0 down #BACKUP是eth2
/sbin/ipvsadm -C
;;
*)
echo " Usage:$0{start|stop} "
exit 1
esac
2、在MASTER、BACKUP两台虚拟机添加可执行文件realserver,内容如下
VIP=
192.168
.
1.10
/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
3、在MASTER修改配置文件keepalived.conf文件,BAKUP上稍有不同,如下
global_defs {
notification_email {
<to email address> #该处为提醒邮箱地址
}
notification_email_from <from email address> #该处是提醒邮箱发送地址
smtp_server <smtp server> #邮件服务器
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #BACKUP是BACKUP
interface eth0 #BACKUP是eth2
virtual_router_id 51
priority 100 #BACKUP是99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168 . 1.10
}
}
virtual_server 192.168 . 1.10 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168 . 1.6 80 {
weight 3
TCP_CHECK {
connect_port 80
connect_timeout 30
}
}
real_server 192.168 . 1.8 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 30
}
} }
4、在MASTER上,编辑/var/www/html/readme.html
this is 192.168 . 1.6
在BACKUP上,编辑/var/www/html/readme.html
this is 192.168 . 1.8
以示区别
5、在MASTER、BACKUP开启Apache、keealived等
# /root/realserver start
# /root/lvs-dr start
6、在宿主机上ping通192.168.1.10,但无法访问http://192.168.1.10/readme.html ,后来发现是防火墙问题,开启后,就可以了。可以分别关闭两台虚拟机在验证集群效果。