实验拓扑如下:
二、服务器ip配置信息
1. ip_lb 主机(master) :
2.ip_lb_2 主机 (backup)
3.web1 主机
4.web2 主
三、在master和backup分别查看是否有ipvsadm软件
1. 检查ipvsadm是否安装
rpm -qa ipvsadm*
rpm -vih ipvsadm*
yum install -y ipvsadm*
yum 配置文件
[maweibing]
named=maweibing
baseurl=file:///mnt/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
2. 检查模版是否加入内核
四、分别在web1和web2上面安装apache服务.
# 安装apache服务不说明了. 开放的端口是:80
1. keepalived-1.2.1.tar.gz # 解压这个文件并且安装 (tar -zxvf keepalived-1.2.1.tar.gz)
2. ./configure --prefix=/home/keepalived/ --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
3.make
4.make install
5.配置文件的路径
cp /home/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /home/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /home/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /home/keepalived/sbin/keepalived /sbin/
6.配置keepalived.conf文件.( ip_lb 主机<master> )
vim /etc/keepalived/keepalived.conf
#global define
global_defs {
router_id LVS_T1
}
vrrp_sync_group bl_group {
group {
bl_one
}
}
vrrp_instance bl_one {
state MASTER
interface eth0
lvs_sync_daemon_interface eth0
virtual_router_id 38
priority 150
advert_int 3
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.100
}
}
virtual_server 192.168.96.100 8099 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.96.33 8099 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8099
}
}
real_server 192.168.96.4 8099 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8099
}
}
}
#global define
global_defs {
router_id LVS_T1
}
vrrp_sync_group bl_group {
group {
bl_one
}
}
vrrp_instance bl_one {
state MASTER
interface eth0
lvs_sync_daemon_interface eth0
virtual_router_id 38
priority 150
advert_int 3
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.100
}
}
virtual_server 192.168.96.100 8099 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.96.33 8099 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8099
}
}
real_server 192.168.96.4 8099 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8099
}
}
}
7、配置keepalived.conf文件.(ip_lb_2 主机 <backup> )
vim /etc/keepalived/keepalived.conf
#global define
global_defs {
router_id LVS_T2
}
vrrp_sync_group bl_group {
group {
bl_one
}
}
vrrp_instance bl_one {
state BACKUP
interface eth0
lvs_sync_daemon_interface eth0
virtual_router_id 38
priority 120
advert_int 3
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.100
}
}
virtual_server 192.168.96.100 8099 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.96.33 8099 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8099
}
}
real_server 192.168.96.4 8099 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8099
}
}
}
#global define
global_defs {
router_id LVS_T2
}
vrrp_sync_group bl_group {
group {
bl_one
}
}
vrrp_instance bl_one {
state BACKUP
interface eth0
lvs_sync_daemon_interface eth0
virtual_router_id 38
priority 120
advert_int 3
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.100
}
}
virtual_server 192.168.96.100 8099 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 1
protocol TCP
real_server 192.168.96.33 8099 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8099
}
}
real_server 192.168.96.4 8099 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 8099
}
}
}
8. 分别在主备上启动keepalived
[root@ip_lb home]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@ip_lb home]# tail -f /var/log/message
![1339870735_8872.jpg](https://img-my.csdn.net/uploads/201206/17/1339870735_8872.jpg)
# PS:这里说的LVS.并不是用ipvsadm软件配置.而是直接用keepalived的virtual_server配置项控制的.
# 安装ipvsadm只是可以看到负载状况.其实只需要keepalived也可以实现负载均衡集群.
六、在web1和web2主机上配置LVS-DR的相关配置.
1. vim /etc/init.d/lvsDR_node (web1主机)
#! /bin/sh
# descript : start real server DR
VIP=192.168.96.100
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS of RealServer DR"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
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 of RealServer DR"
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
#! /bin/sh
# descript : start real server DR
VIP=192.168.96.100
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo "start LVS of RealServer DR"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
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 of RealServer DR"
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
2. 把这个文件复制到web2主机上.
3. 在web1和web2上分别启动这个脚本:
/etc/init.d/lvsDR_node start
七、web1和web2主机上开启httpd服务.
/etc/init.d/httpd start
# ================= TEST =======================================
八、测试是否可以负载均衡.
编写测试脚本:
#! /bin/sh
for((i=1;i<=100;i++));do
curl http://192.168.96.100:8099 >> /tmp/q;
done
#! /bin/sh
for((i=1;i<=100;i++));do
curl http://192.168.96.100:8099 >> /tmp/q;
done
结果:
![1339875821_8932.jpg](https://img-my.csdn.net/uploads/201206/17/1339875821_8932.jpg)
九、测试断开一个web服务或加入一个web服务
现在关闭 web2主机(192.168.96.4) 的web 服务..
查看日志可以看到:
![1339876121_7366.jpg](https://img-my.csdn.net/uploads/201206/17/1339876121_7366.jpg)
现在再把 web2主机(192.168.96.4)的web 服务加入进来:
查看日志可以看到:
![1339876287_8715.jpg](https://img-my.csdn.net/uploads/201206/17/1339876287_8715.jpg)
十、 测试HA
现在模拟主(master)的keepalived主机死掉.
备机的日志文件:
![1339877433_1304.jpg](https://img-my.csdn.net/uploads/201206/17/1339877433_1304.jpg)
可以看到备机的keepalived已经开始工作....
转载于:https://blog.51cto.com/skyking116/1230899