基于redhat linux虚拟服务器的web负载均衡集群
硬件环境
分发机
LB1:192.168.0.129
LB2:192.168.0.130
真实节点主机
NODE1:192.168.0.131
NODE2:192.168.0.132
试验在VMWARE下完成测试成功
初始化配置
在LB1上编辑hosts文件,并且设置 root ssh key 这样有利于节点之间相互访问
- [root@localhost ha]# cat /etc/hosts
- # Do not remove the following line, or various programs
- # that require network functionality will fail.
- 127.0.0.1 localhost.localdomain localhost
- ::1 localhost6.localdomain6 localhost6
- 192.168.0.129 lb1.test.com lb1
- 192.168.0.130 lb2.test.com lb2
- 192.168.0.131 node1.test.com node1
- 192.168.0.132 node2.test.com node2
- [root@localhost ha]# ssh-keygen
- Generating public/private rsa key pair.
- Enter file in which to save the key (/root/.ssh/id_rsa):
- /root/.ssh/id_rsa already exists.
- Overwrite (y/n)?
- [root@localhost ha]#
将LB1的/root/.ssh和/etc/hosts复制到其他节点中
- For hostname in lb2 node1 node2 ; do scp –r /root/.ssh $hostname:/root ;done
- For hostname in lb2 node1 node2 ; do scp /etc/hosts $hostname:/etc/hosts ;done
在LB1上安装ipvasdm piranha httpd 软件包
- yum install -y ipvsadm piranha httpd
- Loaded plugins: fastestmirror
- Loading mirror speeds from cached hostfile
- Setting up Install Process
- Package ipvsadm-1.24-10.x86_64 already installed and latest version
- Package piranha-0.8.4-16.el5.x86_64 already installed and latest version
- Package httpd-2.2.3-43.el5.centos.x86_64 already installed and latest version
- Nothing to do
再LB2上安装ipvasdm piranha 包!
- yum install -y ipvsadm piranha
- Loaded plugins: fastestmirror
- Loading mirror speeds from cached hostfile
- Setting up Install Process
- Package ipvsadm-1.24-10.x86_64 already installed and latest version
- Package piranha-0.8.4-16.el5.x86_64 already installed and latest version
- Nothing to do
在node1 node2上安装httpd arptables_jf
- [root@localhost ~]# yum install httpd arptables_jf
- Loaded plugins: fastestmirror
- Loading mirror speeds from cached hostfile
- Setting up Install Process
- Package httpd-2.2.3-43.el5.centos.x86_64 already installed and latest version
- Package arptables_jf-0.0.8-8.x86_64 already installed and latest version
- Nothing to do
[root@localhost ~]#
登陆 LB1 配置piranha登陆设置密码
- [root@localhost ha]# piranha-passwd
- New Password:
- Verify:
- Updating password for user piranha
启动piranha-gui服务
- [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
- [root@localhost ha]# cat /etc/sysconfig/ha/lvs.cf
- serial_no = 58
- primary = 192.168.0.129
- service = lvs
- backup_active = 1
- backup = 192.168.0.130
- heartbeat = 1
- heartbeat_port = 539
- keepalive = 6
- deadtime = 18
- network = direct
- debug_level = NONE
- monitor_links = 0
- syncdaemon = 0
- virtual apache {
- active = 1
- address = 192.168.0.250 eth0:1
- vip_nmask = 255.255.255.255
- port = 80
- send = "GET / HTTP/1.0\r\n\r\n"
- expect = "ok"
- use_regex = 0
- send_program = "/bin/testlinks %h"
- load_monitor = none
- scheduler = wlc
- protocol = tcp
- timeout = 6
- reentry = 15
- quiesce_server = 0
- server node1 {
- address = 192.168.0.131
- active = 1
- weight = 1
- }
- server node2 {
- address = 192.168.0.132
- active = 1
- weight = 1
- }
- }
- [root@localhost ha]#
将这个文件cp到LB2上!
- [root@localhost ha]# scp /etc/sysconfig/ha/lvs.cf lb2:/etc/sysconfig/ha/lvs.cf
在LB1上编辑服务器测试程序/bin/testlink,并且加上执行权限!
- [root@localhost ha]# cat /bin/testlink
- #!/bin/bash
- /usr/bin/links -dump 1 $1 >/dev/null 2>&1
- if [ 0 -eq $? ] ; then
- echo ok
- else
- echo fail
- fi
- [root@localhost ha]#chmod a+x /bin/testlink
然后讲、bin/testlink文件复制一份到LB2上!
在node1上编辑arptables脚本,并且运行它
- [root@localhost ~]# cat /arp_arptables.sh
- #!/bin/bash
- VIP=192.168.0.250
- RIP=192.168.0.131
- arptables -F
- arptables -A IN -d $VIP -j DROP
- arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP
- /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
- /sbin/route add -host $VIP dev eth0:1
- [root@localhost ~]#
放入测试页面,并且启动httpd
- [root@localhost ~]# echo "node1" > /var/www/html/index.html
- [root@localhost ~]# service httpd start
启动 httpd:
在node2上编辑arptables脚本,并且运行它
- [root@localhost ~]# cat /arp_arptables.sh
- #!/bin/bash
- VIP=192.168.0.250
- RIP=192.168.0.132
- arptables -F
- arptables -A IN -d $VIP -j DROP
- arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP
- /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
- /sbin/route add -host $VIP dev eth0:1
- [root@localhost ~]#
放入测试页面,并且启动httpd
- [root@localhost ~]# echo "node2" > /var/www/html/index.html
- [root@localhost ~]# service httpd start
启动 httpd:
LB1 LB2上启动PULSE服务,查看ipvsadm结果
- /etc/init.d/pulse start
- Ipvsadm -L –n
如果前面的配置正确,这里可以在lb1上看到正确的回显。
当我们将lib1的网线拔除后,分发服务将自动切换到lib2上。
当我们将node1的网线拔除后,分发器将把node1从分发列表中剔出。当我们将
node1的网线插回后,分发器又将把node1恢复到分发列表中去。
无论你如何测试,请使用额外的机器访问VIP,这也有利于测试分发器是否正确的保持
了session。
转载于:https://blog.51cto.com/fghjk/772688