基于redhat linux虚拟服务器的web负载均衡集群

硬件环境

分发机

LB1192.168.0.129

LB2192.168.0.130

真实节点主机

NODE1:192.168.0.131
NODE2:192.168.0.132

试验在VMWARE下完成测试成功

 

初始化配置

LB1上编辑hosts文件,并且设置 root ssh key 这样有利于节点之间相互访问

 

 
  
  1. [root@localhost ha]# cat /etc/hosts 
  2.  
  3. # Do not remove the following line, or various programs 
  4.  
  5. # that require network functionality will fail. 
  6.  
  7. 127.0.0.1          localhost.localdomain localhost 
  8.  
  9. ::1              localhost6.localdomain6 localhost6 
  10.  
  11. 192.168.0.129 lb1.test.com lb1 
  12.  
  13. 192.168.0.130 lb2.test.com lb2 
  14.  
  15. 192.168.0.131 node1.test.com node1 
  16.  
  17. 192.168.0.132 node2.test.com node2 

 

 

 
  
  1. [root@localhost ha]# ssh-keygen 
  2.  
  3. Generating public/private rsa key pair. 
  4.  
  5. Enter file in which to save the key (/root/.ssh/id_rsa): 
  6.  
  7. /root/.ssh/id_rsa already exists. 
  8.  
  9. Overwrite (y/n)? 
  10.  
  11. [root@localhost ha]# 

 

LB1/root/.ssh/etc/hosts复制到其他节点中

 

 
  
  1. For hostname in lb2 node1 node2 ; do scp –r /root/.ssh $hostname:/root ;done 
  2.  
  3. For hostname in lb2 node1 node2 ; do scp /etc/hosts $hostname:/etc/hosts ;done 

 

LB1上安装ipvasdm piranha httpd 软件包

 

 
  
  1. yum install -y ipvsadm piranha httpd 
  2.  
  3. Loaded plugins: fastestmirror 
  4.  
  5. Loading mirror speeds from cached hostfile 
  6.  
  7. Setting up Install Process 
  8.  
  9. Package ipvsadm-1.24-10.x86_64 already installed and latest version 
  10.  
  11. Package piranha-0.8.4-16.el5.x86_64 already installed and latest version 
  12.  
  13. Package httpd-2.2.3-43.el5.centos.x86_64 already installed and latest version 
  14.  
  15. Nothing to do 

LB2上安装ipvasdm piranha 包!

 

 
  
  1. yum install -y ipvsadm piranha 
  2.  
  3. Loaded plugins: fastestmirror 
  4.  
  5. Loading mirror speeds from cached hostfile 
  6.  
  7. Setting up Install Process 
  8.  
  9. Package ipvsadm-1.24-10.x86_64 already installed and latest version 
  10.  
  11. Package piranha-0.8.4-16.el5.x86_64 already installed and latest version 
  12.  
  13. Nothing to do 

node1 node2上安装httpd arptables_jf

 

 
  
  1. [root@localhost ~]# yum install httpd arptables_jf 
  2.  
  3. Loaded plugins: fastestmirror 
  4.  
  5. Loading mirror speeds from cached hostfile 
  6.  
  7. Setting up Install Process 
  8.  
  9. Package httpd-2.2.3-43.el5.centos.x86_64 already installed and latest version 
  10.  
  11. Package arptables_jf-0.0.8-8.x86_64 already installed and latest version 
  12.  
  13. Nothing to do 

[root@localhost ~]#

登陆 LB1 配置piranha登陆设置密码

 

 
  
  1. [root@localhost ha]# piranha-passwd 
  2.  
  3. New Password: 
  4.  
  5. Verify: 
  6.  
  7. Updating password for user piranha 

启动piranha-gui服务

 
  
  1. [root@localhost ha]# /etc/init.d/piranha-gui start 

Starting piranha-gui:                                      [确定]

[root@localhost ha]#

浏览器中打开http://192.168.0.129:3636,输入用户名密码登陆!

 

设置primary server public ip 192.168.0.129 点击accept生效

选择REDUNDANCY 然后点击ENABLE,设置Redundant server public ip 192.168.0.130,单击accept

 

选择VIRTUAL SERVERS 然后点击ADD

 

编辑VIRTUAL SERVER编辑,如图!

 

添加,编辑REAL SERVER 为两个真实节点,编辑完后别忘了,保持,并且active 

 

选择MONITORING SCRIPTS , sending Program设为/bin/testlinks %h ,

Expect 设置 ok 。点击ACCEPT保存。点击VIRTUAL SERVERS回到上级菜单

点击ACTIVETE激活!

最后回到CONTROL/MONITORING点击update information now 更新

配置文件保存在/etc/sysconfig/ha/lvs.cf

 

 
  
  1. [root@localhost ha]# cat /etc/sysconfig/ha/lvs.cf 
  2.  
  3. serial_no = 58 
  4.  
  5. primary = 192.168.0.129 
  6.  
  7. service = lvs 
  8.  
  9. backup_active = 1 
  10.  
  11. backup = 192.168.0.130 
  12.  
  13. heartbeat = 1 
  14.  
  15. heartbeat_port = 539 
  16.  
  17. keepalive = 6 
  18.  
  19. deadtime = 18 
  20.  
  21. network = direct 
  22.  
  23. debug_level = NONE 
  24.  
  25. monitor_links = 0 
  26.  
  27. syncdaemon = 0 
  28.  
  29. virtual apache { 
  30.  
  31.      active = 1 
  32.  
  33.      address = 192.168.0.250 eth0:1 
  34.  
  35.      vip_nmask = 255.255.255.255 
  36.  
  37.      port = 80 
  38.  
  39.      send = "GET / HTTP/1.0\r\n\r\n" 
  40.  
  41.      expect = "ok" 
  42.  
  43.      use_regex = 0 
  44.  
  45.      send_program = "/bin/testlinks %h" 
  46.  
  47.      load_monitor = none 
  48.  
  49.      scheduler = wlc 
  50.  
  51.      protocol = tcp 
  52.  
  53.      timeout = 6 
  54.  
  55.      reentry = 15 
  56.  
  57.      quiesce_server = 0 
  58.  
  59.      server node1 { 
  60.  
  61.          address = 192.168.0.131 
  62.  
  63.          active = 1 
  64.  
  65.          weight = 1 
  66.  
  67.      } 
  68.  
  69.      server node2 { 
  70.  
  71.          address = 192.168.0.132 
  72.  
  73.          active = 1 
  74.  
  75.          weight = 1 
  76.  
  77.      } 
  78.  
  79.  
  80. [root@localhost ha]# 

将这个文件cpLB2上!

 
  
  1. [root@localhost ha]# scp  /etc/sysconfig/ha/lvs.cf lb2:/etc/sysconfig/ha/lvs.cf 

LB1上编辑服务器测试程序/bin/testlink,并且加上执行权限!

                                                                               

 

 
  
  1. [root@localhost ha]# cat /bin/testlink 
  2.  
  3. #!/bin/bash 
  4.  
  5. /usr/bin/links -dump 1 $1 >/dev/null 2>&1 
  6.  
  7. if [ 0 -eq $? ] ; then 
  8.  
  9. echo ok 
  10.  
  11. else 
  12.  
  13. echo fail 
  14.  
  15. fi 
  16.  
  17. [root@localhost ha]#chmod a+x /bin/testlink  

然后讲、bin/testlink文件复制一份到LB2上!

 

node1上编辑arptables脚本,并且运行它

 

 
  
  1. [root@localhost ~]# cat /arp_arptables.sh 
  2.  
  3. #!/bin/bash 
  4.  
  5. VIP=192.168.0.250 
  6.  
  7. RIP=192.168.0.131 
  8.  
  9. arptables -F 
  10.  
  11. arptables -A IN -d $VIP -j DROP 
  12.  
  13. arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP 
  14.  
  15. /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up 
  16.  
  17. /sbin/route add -host $VIP dev eth0:1 
  18.  
  19. [root@localhost ~]# 

放入测试页面,并且启动httpd

 

 

 
  
  1. [root@localhost ~]# echo "node1" > /var/www/html/index.html 
  2.  
  3. [root@localhost ~]# service httpd start 

启动 httpd

 

 

node2上编辑arptables脚本,并且运行它

 

 
  
  1. [root@localhost ~]# cat /arp_arptables.sh 
  2.  
  3. #!/bin/bash 
  4.  
  5. VIP=192.168.0.250 
  6.  
  7. RIP=192.168.0.132 
  8.  
  9. arptables -F 
  10.  
  11. arptables -A IN -d $VIP -j DROP 
  12.  
  13. arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP 
  14.  
  15. /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up 
  16.  
  17. /sbin/route add -host $VIP dev eth0:1 
  18.  
  19. [root@localhost ~]# 

放入测试页面,并且启动httpd

 

 

 
  
  1. [root@localhost ~]# echo "node2" > /var/www/html/index.html 
  2.  
  3. [root@localhost ~]# service httpd start 

启动 httpd

 

LB1 LB2上启动PULSE服务,查看ipvsadm结果

 

 
  
  1. /etc/init.d/pulse start 
  2.  
  3. Ipvsadm  -L –n 

 

如果前面的配置正确,这里可以在lb1上看到正确的回显。

当我们将lib1的网线拔除后,分发服务将自动切换到lib2上。

当我们将node1的网线拔除后,分发器将把node1从分发列表中剔出。当我们将

node1的网线插回后,分发器又将把node1恢复到分发列表中去。

无论你如何测试,请使用额外的机器访问VIP,这也有利于测试分发器是否正确的保持

session