blog:http://blog.vfocus.net
测试环境: 硬件 两台dell1950 (单硬盘) 系统 FC7 软件 ipvsadm-1.24 heartbeat-2.1.2 说明 一台172.16.3.75 (lvsdr)作为主director服务器 172.16.3.73 (lvsdrbak)作为热备director服务器 虚拟ip为172.16.3.76 采用基于DR的模式进行负载均衡
算法采用的是轮叫调度(Round-Robin Scheduling) 即ipvsadm –rr模式
注1:(本次测试为节约机器,将备份节点也作为一台realserver) 注2:算法模式一共有7种分别是 加权轮叫调度(Weighted Round-Robin Scheduling) 最小连接调度(Least-Connection Scheduling) 加权最小连接调度(Weighted Least-Connection Scheduling) 基于局部性的最少链接(Locality-Based Least Connections Scheduling) 带复制的基于局部性最少链接(Locality-Based Least Connections with Replication Scheduling) 目标地址散列调度(Destination Hashing Scheduling) 源地址散列调度(Source Hashing Scheduling) 只需要改变ipvsadm后面的参数 ipvsadm -rr 论叫调度 rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq
一,配置基于隧道(DR)模式的LVS集群
所需软件 ipvsadm-1.24 http://www.linuxvirtualserver.org/software/ 需要建立一个软链接把内核指向/usr/src/linux
ln -s /usr/src/kernels/2.6.9-42.EL-i686/ /usr/src/linux tar zxvf ipvsadm-1.24.tar.gz cd ipvsadm-1.2.4 make;make install
1)在lvsdr上部署(备份节点做完全一样的部署)
$ vi /etc/rc.d/init.d/lvsDR
#!/bin/bash VIP=172.16.3.76 RIP1=172.16.3.73 RIP2=172.16.3.235 . /etc/rc.d/init.d/functions case "$1" in start) echo "start LVS of DirectorServer" #Set the Virtual IP Address /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 #Clear IPVS Table /sbin/ipvsadm -C #Set Lvs /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 #Run Lvs /sbin/ipvsadm #end ;; stop) echo "close LVS Directorserver" /sbin/ipvsadm -C ;; *) echo "Usage: $0 {start|stop}" exit 1 esac
2)在realserver上部署
$ vi /etc/rc.d/init.d/realserverDR
#!/bin/sh VIP=172.16.3.76 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 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 sysctl -p ~
二,安装heartbeat
所需软件 e2fsprogs-1.40.4.cfs1-0redhat.i386.rpm 光盘中有 libnet http://www.packetfactory.net/libnet heartbeat-2.1.2.tar.gz http://linux-ha.org/download/
安装软件 安装e2fsprogs
rpm -ivh e2fsprogs-1.35-7.1.i386.rpm
安装libnet
tar zxvf libnet.tar.gz cd libnet ./configure make;make install
安装heartbeat
tar zxvf heartbeat-2.0.2.tar.gz cd heartbeat-2.0.2 ./Configureme configure make;make install cp doc/ha.cf doc/haresources doc/authkeys /etc/ha.d/ cp ldirectord/ldirectord.cf /etc/ha.d/ groupadd -g 694 haclient useradd -u 694 -g haclient hacluster
修改配置
重点三个配置文件 ha.cf haresources authkeys
1, ha.cf
[root@lvsdrbak src]# awk '/^[^$]/&&/^[^#]/' /etc/ha.d/ha.cf debugfile /var/log/ha-debug logfile /var/log/ha-log logfacility local0 keepalive 2 deadtime 30 warntime 10 initdead 120 udpport 694 bcast eth0 # Linux auto_failback on node lvsdr node lvsdrbak hopfudge 1
2, haresources
这个文件其实就只有一行 lvsdr IPaddr::172.16.3.76 ldirectord lvsDR
3, authkeys
auth 1 1 sha1 key-for-sha1-any-text-you-want chmod -R 600 autykeys 权限必须是600
以上三个文件 主从服务器上一致
修改hosts
$ vi /etc/hosts
127.0.0.1 localhost.localdomain localhost 172.16.3.75 lvsdr 10.1.1.3 HA01 10.1.1.2 HA02 172.16.3.73 lvsdrbak
其中10.1.1.3与10.1.1.2是在eth0上绑的另外ip作为检测heartbeat用
三,配置ldirectord
Ldirectord的作用是监测Real Server,当Real Server失效时,把它从Load Balancer列表中删除,恢复时重新添加,在安装heartbeat时已经安装了Ldirectord。
但是在fc7中启动ldirectord会报缺少Mail/Send.pm或者LWP/useragant模块,需要安装perl模块
所需模块 lwp/useagant http://search.cpan.org/CPAN/authors/id/G/GA/GAAS/libwww-perl-5.808.tar.gz mailsend http://search.cpan.org/CPAN/authors/id/M/MA/MARKOV/MailTools-2.02.tar.gz
生成makefile: perl Makefile.PL
建立模块 make
测试模块 make test
如果测试结果报告“all test ok”,您就可以放心地安装编译好的模块了。安装模块前,先要 make install
配置 ldirectord 配置文件在/etc/ha.d/ldirectord.cf
[root@lvsdr lvs]# awk '/^[^$]/&&/^[^#]/' /etc/ha.d/ldirectord.cf
checktimeout=3 checkinterval=1 autoreload=yes logfile="/var/log/ldirectord.log" quiescent=yes virtual=172.16.3.76:80
real=172.16.3.73:80 gate real=172.16.3.235:80 gate service=http request="test.html" receive="Test Page" scheduler=rr protocol=tcp #checktype=negotiate #checkport=80 #request="index.html" #receive="Test Page" #virtualhost=www.x.y.z测试 ldirectord是否可以起来
现在可以在主节点172.16.3.75启动heartbeat
/etc/init.d/heartbeat start
起来正常的结果应该是这样
[root@lvsdr src]# netstat -npl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 :::22 :::* LISTEN 2095/sshd udp 0 0 0.0.0.0:32784 0.0.0.0:* 28530/heartbeat: wr udp 0 0 0.0.0.0:694 0.0.0.0:* 28530/heartbeat: wr
[root@lvsdr src]# ifconfig eth0 Link encap:Ethernet HWaddr 00:15:C5:EF:E0:D9 inet addr:172.16.3.75 Bcast:172.16.3.255 Mask:255.255.255.0 inet6 addr: fe80::215:c5ff:feef:e0d9/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:58138 errors:0 dropped:0 overruns:0 frame:0 TX packets:17891 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:8955674 (8.5 MiB) TX bytes:3217775 (3.0 MiB) Interrupt:16 Memory:f8000000-f8012100
eth0:0 Link encap:Ethernet HWaddr 00:15:C5:EF:E0:D9 inet addr:172.16.3.76 Bcast:172.16.3.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:16 Memory:f8000000-f8012100
eth0:1 Link encap:Ethernet HWaddr 00:15:C5:EF:E0:D9 inet addr:10.1.1.3 Bcast:10.1.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:16 Memory:f8000000-f8012100
lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:2493 errors:0 dropped:0 overruns:0 frame:0 TX packets:2493 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1339237 (1.2 MiB) TX bytes:1339237 (1.2 MiB)
tunl0 Link encap:IPIP Tunnel HWaddr inet addr:172.16.3.76 Mask:255.255.255.255 UP RUNNING NOARP MTU:1480 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
测试:关闭主节点,备份节点将自动接管directorserver服务。
四,把备份节点也作为realserver
1. 在上述DR节点做的以外,备份节点还需要也添加realserver脚本
另外需要添加关闭备用节点的脚本
[root@lvsdr lvs]# vi /etc/rc.d/init.d/closeDR VIP=172.16.3.76 . /etc/rc.d/init.d/functions case "$1" in start) echo "start director server and close dr" ifconfig lo:0 down echo 1 > /proc/sys/net/ipv4/ip_forward echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce ;; stop) echo "start Real Server" ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up /sbin/route add -host $VIP dev lo:0 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 sysctl –p
;; *) echo "Usage: lvs {start|stop}" exit 1 esac
这个一个关闭备份节点的脚本,可以放到heartbeat里面去,在启动备用节点的时候自动关闭备份节点脚本
2, 修改备份节点的haresources
只需添加一项
[root@lvsdrbak lvs]# awk '/^[^$]/&&/^[^#]/' /etc/ha.d/haresources lvsdr closedr IPaddr::172.16.3.76 ldirectord lvsDR
确定closedr脚本在 启动lvsDR之前就可以了。
五,测试
director服务器是 172.16.3.75 热备机是172.16.3.73 虚拟ip是172.16.3.76 realserver为 172.16.3.73 172.16.3.235 在主DR上启动服务
/etc/rc.d/init.d/heartbeat start
在ie里面输入http://172.16.3.76可以得到这两个页面 172.16.3.235下面有个index1.html 显示172.16.3.235 172.16.3.73下面有个index2.html 显示172.16.3.73 可以通过http://172.16.3.76/index1.html http://172.16.3.76/index2.html
访问
拔掉172.16.3.75的网线,服务器切到172.16.3.73上。也能正常显示。
|