LVS(Linux Virtual Server)是一个第三方的开源负载均衡软件,应用层发出的请求他可以接收,然后再转发给后端的RealServer,即能够处理请求的服务器。那么转发后的请求处理后的数据,如何返回给请求发起方呢?LVS提供了两套响应方案:一套是由各Slave节点响应请求,并直接将数据返回给请求发起方,LVS相当于纯工作转派,这种就是DR模式;第二套方案也得Slave节点响应请求,不过在返回数据时,Slave不会将数据直接返回给请求的发起方,而是反馈给LVS,由LVS统一返回到请求发起方,这种即是NAT模式,相当于LVS是统一入口,出入都得从它走。
现在LVS已经是 Linux标准内核的一部分,在Linux2.4内核以前,使用LVS时必须要重新编译内核以支持LVS功能模块,但是从Linux2.4内核以后,已经完全内置了LVS的各个功能模块,无需给内核打任何补丁,可以直接使用LVS提供的各种功能。不过,我们还需要一个管理工具来执行常规的管理操作,这就需要用到ipvsadm软件包。
##检查kernel是否已经支持LVS的ipvs模块:
[root@data-1-1 tmp]# uname -r
2.6.32-573.12.1.el6.i686
[root@data-1-1 tmp]# modprobe -l |grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko
##如果有类似上面的输出,表明系统内核已经默认支持了IPVS模块。接着就可以安装IPVS管理软件了。
##安装IPVS管理软件
# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
[root@data-1-1 tmp]# ll ipvsadm-1.24.tar.gz
-rw-r--r-- 1 root root 36598 12月 11 2005 ipvsadm-1.24.tar.gz
[root@data-1-1 tmp]# tar zxf ipvsadm-1.24.tar.gz
[root@data-1-1 tmp]# cd ipvsadm-1.24
##注意:在make时可能会出现错误编译信息,这是由于编译程序找不到对应内核的原因,按照如下操作就可以正常编译:
[root@data-1-1 ipvsadm-1.24]# ln -s /usr/src/kernels/2.6.32-573.12.1.el6.i686/ /usr/src/linux
[root@data-1-1 ipvsadm-1.24]# make
[root@data-1-1 ipvsadm-1.24]# make install
##也可以下载rpm安装包,通过rpm方式进行安装:rpm –ivh ipvsadm-1.24-6.1.i386.rpm
[root@data-1-1 ipvsadm-1.24]# ipvsadm --help
##如果看到帮助提示,表明IPVS已经成功安装。
##ipvsadm 参数注释:
[root@data-1-1 ~]# ipvsadm --help
ipvsadm v1.24 2005/12/10 (compiled with getopt_long and IPVS v1.2.1)
Usage:
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask]
ipvsadm -D -t|u|f service-address
ipvsadm -C
ipvsadm -R
ipvsadm -S [-n]
ipvsadm -a|e -t|u|f service-address -r server-address [options]
ipvsadm -d -t|u|f service-address -r server-address
ipvsadm -L|l [options]
ipvsadm -Z [-t|u|f service-address]
ipvsadm --set tcp tcpfin udp
ipvsadm --start-daemon state [--mcast-interface interface] [--syncid sid]
ipvsadm --stop-daemon state
ipvsadm -h
Commands:
Either long or short options are allowed.
--add-service -A add virtual service with options ##在内核的虚拟服务器列表中添加一条新的虚拟IP记录。也就是增加一台新的虚拟服务器。虚拟IP也就是虚拟服务器的IP地址。
--edit-service -E edit virtual service with options ##编辑内核虚拟服务器列表中的一条虚拟服务器记录
--delete-service -D delete virtual service ##删除内核虚拟服务器列表中的一条虚拟服务器记录
--clear -C clear the whole table ##清除内核虚拟服务器列表中的所有记录
--restore -R restore rules from stdin ##恢复虚拟服务器规则
--save -S save rules to stdout ##保存虚拟服务器规则,输出为-R 选项可读的格式
--add-server -a add real server with options ##在内核虚拟服务器列表的一条记录里添加一条新的Real Server记录。也就是在一个虚拟服务器中增加一台新的Real Server
--edit-server -e edit real server with options ##编辑一条虚拟服务器记录中的某条Real Server记录
--delete-server -d delete real server ##删除一条虚拟服务器记录中的某条Real Server记录
--list -L|-l list the table ##显示内核中虚拟服务器列表
--zero -Z zero counters in a service or all services ##虚拟服务器列表计数器清零(清空当前的连接数量等)
--set tcp tcpfin udp set connection timeout values ##设置连接超时值
--start-daemon start connection sync daemon
--stop-daemon stop connection sync daemon
--help -h display this help message
Options:
--tcp-service -t service-address service-address is host[:port] ##说明虚拟服务器提供的是tcp服务
--udp-service -u service-address service-address is host[:port] ##说明虚拟服务器提供的是udp服务
--fwmark-service -f fwmark fwmark is an integer greater than zero ##说明是经过iptables标记过的服务类型
--scheduler -s scheduler one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,the default scheduler is wlc. ##指定调度算法
##rr:调度器通过“轮叫”调度算法将外部用户请求按顺序1:1的分配到集群中的每个Real Server上,这种算法平等地对待每一台Real Server,而不管服务器上实际的负载状况和连接状态。
##wrr:“加权轮叫”调度算法是根据Real Server的不同处理能力来调度访问请求。可以对每台Real Server设置不同的调度权值。
##lc:“最少连接”调度算法动态地将网络请求调度到已建立的链接数最少的服务器上。(此调度算法可以较好地均衡负载)
##wlc:“加权最少链接调度”是“最少连接调度”的超集,每个服务节点可以用相应的权值表示其处理能力。
--persistent -p [timeout] persistent service ##在某个Real Server上持续的服务时间。也就是说来自同一个用户的多次请求,将被同一个Real Server处理。
--netmask -M netmask persistent granularity mask
--real-server -r server-address server-address is host (and port) ##指定Real Server的IP地址
--gatewaying -g gatewaying (direct routing) (default) ##指定LVS 的工作模式为DR模式(此模式是LVS 默认工作模式,一般用DR模式即可)
--ipip -i ipip encapsulation (tunneling) ##指定LVS 的工作模式为TUN模式
--masquerading -m masquerading (NAT) ##指定LVS 的工作模式为NAT模式
--weight -w weight capacity of real server ##指定Real Server的权值
--u-threshold -x uthreshold upper threshold of connections
--l-threshold -y lthreshold lower threshold of connections
--mcast-interface interface multicast interface for connection sync
--syncid sid syncid for connection sync (default=255)
--connection -c output of current IPVS connections
--timeout output of timeout (tcp tcpfin udp)
--daemon output of daemon information
--stats output of statistics information
--rate output of rate information
--exact expand numbers (display exact values)
--thresholds output of thresholds information
--persistent-conn output of persistent connection info
--sort sorting output of service/server entries
--numeric -n numeric output of addresses and ports
##举例
# ipvsadm -A -t 192.168.60.200:80 -s rr -p 600
##在内核的虚拟服务器列表中添加一条192.168.60.200的虚拟服务器记录,并且指定此虚拟服务器的服务端口为80,然后指定此虚拟服务器的调度策略为轮询调度,并且在每个real server上的持续服务时间为600秒,即10分钟。
# ipvsadm -A -t 192.168.60.188:21 -s wlc
##在内核的虚拟服务器列表中又添加了一条192.168.60.188的虚拟服务器,此虚拟服务器的服务端口为21,即FTP服务。使用的调度策略为wlc,即加权最少链接算法。
# ipvsadm -a -t 192.168.60.200:80 -r 192.168.60.132:80 –g
# ipvsadm -a -t 192.168.60.200:80 -r 192.168.60.144:80 –g
##在虚拟服务器192.168.60.200中添加两条新的Real Server记录,两个Real Server的IP分别为192.168.60.132和192.168.60.144,参数“-g”指定了虚拟服务器的工作模式为直接路由模式,即DR模式。
##这样设置完毕后,当用户访问192.168.60.200的80服务时,LVS会根据设置的调度策略和路由模式将请求分配到192.168.60.132以及192.168.60.144的80端口。
##实战LVS实现WWW服务的负载均衡
data-1-1 (Director Server)
66.0.96.95 (物理IP)
66.0.96.91 (VIP)
data-1-2 (Real server)
66.0.96.96 (物理IP)
66.0.96.91 (VIP)
data-1-3 (Real server)
66.0.96.97 (物理IP)
66.0.96.91 (VIP)
##eth0网卡一定要选“桥接”方式,因为是虚拟机测试,我的本机(66.0.96.250)作为客户端,最后来访问。
##首先在66.0.96.95 上面做以下配置(Director Server)
##开始配置ipvs,执行如下操作:
# ipvsadm -C
# ipvsadm -A -t 66.0.96.91:80 -s rr
# ipvsadm -a -t 66.0.96.91:80 -r 66.0.96.96:80 -g
# ipvsadm -a -t 66.0.96.91:80 -r 66.0.96.97:80 -g
# ipvsadm -L -n
##绑定一个虚拟IP(也叫VIP),此IP用于对外提供服务,执行如下命令:
# ifconfig eth0:0 66.0.96.91
##在lvs的DR和TUn模式下,用户的访问请求到达真实服务器后,是直接返回给用户的,而不再经过前端的Director Server,因此,就需要在每个Real server节点上增加虚拟的VIP地址,这样数据才能直接返回给用户
##然后在66.0.96.96 和66.0.96.97上面做以下配置,即Real server端。(Real server)
# ifconfig lo:0 66.0.96.91 broadcast 66.0.96.91 netmask 255.255.255.255
# service httpd start
##由于VIP地址,是Director Server和所有的Real server共享的,如果有ARP请求VIP地址时,Director Server与所有Real server都做应答的话,就出现问题了,因此,需要禁止Real server响应ARP请求。
# 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;
##测试LVS
##在虚拟机的宿主机器(66.0.96.250)上面通过浏览器访问:http://66.0.96.91/index.html
##多试几次,需要关闭浏览器,重复访问,可以看见访问的其实是96和97的www服务。
##关闭96上面的www服务
# service httpd stop
##再重复关闭浏览器,访问:http://66.0.96.91/index.html。这次是有规律的一次访问失败,一次访问97的www服务。至此,可以看见负载均衡是成功了!
##如果在在66.0.96.96 和66.0.96.97上面访问:http://66.0.96.91/index.html。则分别返回各自的www服务,如果在66.0.96.95上面访问,则链接不上,一定要在以上三台机器外访问才可以看出效果。
lvs 高可用工具
猜你喜欢
转载自blog.csdn.net/zq9017197/article/details/50471365
今日推荐
周排行