实验拓扑如下:

1339841646_9963.jpg


二、服务器ip配置信息

1. ip_lb 主机(master) :

1339868802_2154.jpg

2.ip_lb_2 主机 (backup)

1339868877_2839.jpg

3.web1 主机

1339843488_1431.jpg

4.web2 主

1339843535_4842.jpg


三、在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. 检查模版是否加入内核

[root@localhost ~]# lsmod |grep ip_vs
ip_vs 78081 0 

四、分别在web1和web2上面安装apache服务.
# 安装apache服务不说明了. 开放的端口是:80
 
五、在master主机(192.168.96.2)和backup主机(192.168.96.22)上安装keepalived
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 主机 )
vim /etc/keepalived/keepalived.conf
[plain]view plaincopyprint?
  1. #global define 

  2. global_defs { 

  3. router_id LVS_T1 

  4. vrrp_sync_group bl_group { 

  5. group { 

  6. bl_one 

  7. vrrp_instance bl_one { 

  8. state MASTER 

  9. interface eth0 

  10. lvs_sync_daemon_interface eth0 

  11. virtual_router_id 38 

  12. priority 150 

  13. advert_int 3 

  14. authentication { 

  15. auth_type PASS 

  16. auth_pass 1111 

  17. virtual_ipaddress { 

  18. 192.168.96.100 

  19. virtual_server 192.168.96.100 8099 { 

  20. delay_loop 3 

  21. lb_algo rr 

  22. lb_kind DR 

  23. persistence_timeout 1 

  24. protocol TCP 

  25. real_server 192.168.96.33 8099 { 

  26. weight 1 

  27. TCP_CHECK { 

  28. connect_timeout 10 

  29. nb_get_retry 3 

  30. delay_before_retry 3 

  31. connect_port 8099 

  32. real_server 192.168.96.4 8099 { 

  33. weight 1 

  34. TCP_CHECK { 

  35. connect_timeout 10 

  36. nb_get_retry 3 

  37. delay_before_retry 3 

  38. connect_port 8099 

#global define
global_defs {
router_id LVS_T1
}vrrp_sync_group bl_group {
group {bl_one
}
}vrrp_instance bl_one {state MASTERinterface eth0lvs_sync_daemon_interface eth0virtual_router_id 38priority 150advert_int 3authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.96.100}
}virtual_server 192.168.96.100 8099 {delay_loop 3lb_algo rrlb_kind DRpersistence_timeout 1protocol TCPreal_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 主机  )
vim /etc/keepalived/keepalived.conf
[plain]view plaincopyprint?
  1. #global define 

  2. global_defs { 

  3. router_id LVS_T2 

  4. vrrp_sync_group bl_group { 

  5. group { 

  6. bl_one 

  7. vrrp_instance bl_one { 

  8. state BACKUP 

  9. interface eth0 

  10. lvs_sync_daemon_interface eth0 

  11. virtual_router_id 38 

  12. priority 120 

  13. advert_int 3 

  14. authentication { 

  15. auth_type PASS 

  16. auth_pass 1111 

  17. virtual_ipaddress { 

  18. 192.168.96.100 

  19. virtual_server 192.168.96.100 8099 { 

  20. delay_loop 3 

  21. lb_algo rr 

  22. lb_kind DR 

  23. persistence_timeout 1 

  24. protocol TCP 

  25. real_server 192.168.96.33 8099 { 

  26. weight 1 

  27. TCP_CHECK { 

  28. connect_timeout 10 

  29. nb_get_retry 3 

  30. delay_before_retry 3 

  31. connect_port 8099 

  32. real_server 192.168.96.4 8099 { 

  33. weight 1 

  34. TCP_CHECK { 

  35. connect_timeout 10 

  36. nb_get_retry 3 

  37. delay_before_retry 3 

  38. connect_port 8099 

#global define
global_defs {
router_id LVS_T2
}vrrp_sync_group bl_group {
group {bl_one
}
}vrrp_instance bl_one {state BACKUPinterface eth0lvs_sync_daemon_interface eth0virtual_router_id 38priority 120advert_int 3authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.96.100}
}virtual_server 192.168.96.100 8099 {delay_loop 3lb_algo rrlb_kind DRpersistence_timeout 1protocol TCPreal_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

# PS:这里说的LVS.并不是用ipvsadm软件配置.而是直接用keepalived的virtual_server配置项控制的.
# 安装ipvsadm只是可以看到负载状况.其实只需要keepalived也可以实现负载均衡集群.

六、在web1和web2主机上配置LVS-DR的相关配置.
1. vim /etc/init.d/lvsDR_node (web1主机)
[plain]view plaincopyprint?
  1. #! /bin/sh 

  2. # descript : start real server DR 

  3. VIP=192.168.96.100 

  4. . /etc/rc.d/init.d/functions 

  5. case "$1" in 

  6. start) 

  7. echo "start LVS of RealServer DR" 

  8. /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up 

  9. echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore 

  10. echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce 

  11. ;; 

  12. stop) 

  13. /sbin/ifconfig lo:0 down 

  14. echo "close LVS of RealServer DR" 

  15. echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore 

  16. echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce 

  17. ;; 

  18. *) 

  19. echo "Usage : $0 {start|stop}" 

  20. exit 1 

  21. esac 

#! /bin/sh
# descript : start real server DR
VIP=192.168.96.100
. /etc/rc.d/init.d/functions
case "$1" instart)echo "start LVS of RealServer DR"/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 upecho "1" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/all/arp_announce;;stop)/sbin/ifconfig lo:0 downecho "close LVS of RealServer DR"echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "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 =======================================

八、测试是否可以负载均衡.
编写测试脚本:
[plain]view plaincopyprint?
  1. #! /bin/sh 

  2. for((i&#61;1;i<&#61;100;i&#43;&#43;));do 

  3. curl http://192.168.96.100:8099 >> /tmp/q; 

  4. done 

#! /bin/sh
for((i&#61;1;i<&#61;100;i&#43;&#43;));do
curl http://192.168.96.100:8099 >> /tmp/q;
done


结果:
1339875821_8932.jpg

九、测试断开一个web服务或加入一个web服务
现在关闭 web2主机(192.168.96.4) 的web 服务..
查看日志可以看到:
1339876121_7366.jpg

现在再把 web2主机(192.168.96.4)的web 服务加入进来:
查看日志可以看到:
1339876287_8715.jpg

十、 测试HA
现在模拟主(master)的keepalived主机死掉. 
备机的日志文件:
1339877433_1304.jpg

可以看到备机的keepalived已经开始工作....