l LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。
l 目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);
十种调度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。
l Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
l 在DR 模式的集群中,LVS 负载调度器作为群集的访问入口,但不作为网关使用,服务池中所有节点都各自接入Internet, 发送给客户机的web响应数据包不需要经过lvs负载调度器。
系统环境(配置好yum源):
[root@host1 ~]# cat /etc/redhat-release
CentOS release 6.2 (Final)
[root@host1 ~]# uname -a
Linux localhost.localdomain 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux
通过部署Apache应用构建Web服务器测试效果,通过部属iSCSI做为存储服务器。
实验目标:
实现Web服务器的备份和冗余,其中任意借点出现故障都会自动切
换,保证应用正常运行。
[root@host1 ~]# modprobe ip_vs
[root@host1 ~]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@host1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.10.100
NETMASK=255.255.255.0
[root@host1 ~]# /etc/init.d/network restart
[root@host1 ~]# ifconfig eth0:0
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:46:D9:DE
inet addr:192.168.10.100 Bcast:192.168.10.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@host1 ~]# vim /etc/sysctl.conf #避免网络内的ARP解析出现异常,应该关闭Linux内核的重定向参数响应
#在最后添加
net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0 |
[root@host1 ~]# sysctl -p
[root@host1 ~]# yum -y install ipvsadm #ipvsadm是负载调度器上使用LVS群集管理工具,通过调用ip_vs模块来添加、删除服务器节点,以及查看群集的运行状态
[root@host1 ~]# ipvsadm -v
ipvsadm v1.25 2008/5/15 (compiled with popt and IPVS v1.2.1)
[root@host1 ~]# mkdir /etc/sysconfig/ipvsadm
[root@host1 ~]# /etc/init.d/ipvsadm restart
[root@host1 ~]# chkconfig ipvsadm on
[root@host1 ~]# yum -y install gcc kernel-devel openssl-devel popt-devel #安装支持库
[root@host1 ~]# tar -zxf keepalived-1.2.2.tar.gz
[root@host1 ~]# cd keepalived-1.2.2
[root@host1 ~]# vi keepalived/libipvs-2.6/ip_vs.h
将#include <sys/types.h>放到#include <linux/types.h>的上面
[root@host1 keepalived-1.2.2]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686/
[root@host1 keepalived-1.2.2]# make
[root@host1 keepalived-1.2.2]# make install
[root@host1 keepalived-1.2.2]# chkconfig --add keepalived
[root@host1 keepalived-1.2.2]# chkconfig keepalived on
[root@host1 keepalived-1.2.2]# cd /etc/keepalived/
[root@host1 keepalived]# cp keepalived.conf keepalived.conf.bak
[root@host1 keepalived]# vim keepalived.conf #文件里面所有内容
! Configuration File for keepalived
global_defs { router_id LVS_MASTER #从调度器上改为router_id LVS_SLAVE }
vrrp_instance VI_1 { state MASTER #从调度器上改为state SLAVE interface eth0 virtual_router_id 51 priority 100 #从调度器上改为priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.100 } }
virtual_server 192.168.10.100 80 { delay_loop 6 lb_algo rr lb_kind DR nat_make 255.255.255.0 persistence_timeout 50 protocol TCP
real_server 192.168.10.3 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 } }
real_server 192.168.10.4 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 } } }
|
配置负载分配策略
# /etc/init.d/ipvsadm stop
# ipvsadm –A –t 192.168.10.100:80 –s rr
# ipvsadm –a –t 192.168.10.100:80 –r 192.168.10.3 –g –w 1
# ipvsadm –a –t 192.168.10.100:80 –r 192.168.10.4 –g –w 1
# /etc/init.d/ipvsadm save
# chkconfig ipvsadm on
ipvsadm
从调度器上同上所有配置,先安装相关软件包,在安装keepalived,最后配置/etc/keepalived.conf,只需将注释的地方修改即可
(1)分别在2台RealServer(节点)上面编写脚本
[root@host3 ~]# vim /etc/init.d/realserver.sh
#!/bin/bash VIP=192.168.10.100 startrs() { echo "start LVS of REALServer" /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 } stoprs() { /sbin/ifconfig lo:0 down echo "close LVS Directorserver" echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce } # ============ Main =========== case $1 in "start") startrs;; "stop") stoprs;; "*") echo "Usage $0 {start|stop}" exit 1 esac
|
[root@host3 ~]# chmod +x /etc/init.d/realserver.sh
[root@host3 ~]#/etc/init.d/realserver.sh start
(2)在2台RealServer(节点)中分别配置HTTP并启动realserver.sh
[root@host3 ~]# yum -y install httpd
[root@host3 ~]# cd /var/www/html/
[root@host3 html]# echo "192.168.10.3" > index.html
[root@host3 html]# cat index.html
192.168.10.3
[root@host4 html]# cat index.html #两台节点的配置除了网页不一样(为了更明显的看到测试效果),其他都一样
192.168.10.4
[root@host3 ~]# /etc/init.d/realserver.sh start
[root@host3 ~]# echo "/etc/init.d/realserver.sh start" >> /etc/rc.local
[root@host3 html]# /etc/init.d/httpd start
[root@host3 html]# chkconfig httpd on
(3)给2台节点添加一条路由记录,将访问VIP的数据限制在本地以避免通信紊乱
[root@host4 ~]# route add -host 192.168.10.100 dev lo:0
[root@host4 ~]# echo "/sbin/route add -host 192.168.10.100 dev lo:0" >> /etc/rc.local
另一台的配置过程一样,过程略
在两台调度器上分别启动Keepalived
[root@host1 ~]# /etc/init.d/keepalived restart
[root@host1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.100:80 rr persistent 50
-> 192.168.10.3:80 Route 1 0 0
-> 192.168.10.4:80 Route 1 0 0
使用客户机访问<http://192.168.10.100>,使主调度器挂掉看是否能够访问
使用客户机访问<http://192.168.10.100>,使一个节点挂掉看是否能够访问