linux集群系列(3) --- LVS之負載均衡集群DR實例
一、簡介
1.1. 負載均衡集群:LB (load balancing)
LVS 是linux virtual server 的簡寫,即linux的虛擬服務器,是一個虛擬的服務器集群系統。ip負載均衡技術是在負載調度器的實現技術中效率最高的,在已有的ip負載均衡技術中,主要通過網絡地址轉換(即VS/NAT 技術)、由於使用nat技術會使分派器成為瓶頸,所以提出了通過ip隧道實現虛擬服務器(即VS/TUN)和通過直接路由實現虛擬服務器的方法即VS/DR。 VS/NAT、VS/TUN和VS/DR技術是LVS集群中實現的三種IP負載均衡技術。
LVS采用ip負載均衡技術和基於內容的請求分發技術。調度器具有很好的吞吐率,將請求均衡的轉移到不同的服務器上執行,且調度器自動屏蔽故障的服務器,從而將一組服務器構成了一個高性能、高可用的虛擬服務器 。整個服務器對客戶完全透明,而且無需修改客戶端和服務器端的程序,所以有透明性、可伸縮性、高可用性和易管理性優點。
1.2. DR模型:
如下圖所,外網用戶直接訪問Director上的VIP地址,Director將請求分派給real server 進行處理,當realserver處理後不再經過Director,而是直接送至客戶端,繞開了Director,使Director不再成為瓶頸。可以有大量的節點,最多可以有100個節點
1.3. LVS調度算法:
當Director收到請求後,需要將請求發送給Real server 進行處理,但是發送哪一臺服務器就要按照算法進行分派。
共有十總調度算法:
固定算法4個:按照設置的算法執行,不考慮現實狀況
RR:Round-robin 輪循調度算法
WRR:Weighted round-robin 加權的輪循調度算法,權值越大越優先
DH:Destination hashing 目標hash 同一個ip用戶的請求都發送給其中的一臺固定的realserver
SH:Source hashing 來源hash
當director的前端同時連接有兩臺或多臺路由器或防火墻時,用戶通過通過其中一臺路由器(或防火墻)將請求發送給director,realserver處理完後將仍然從進入的路由器(或防火墻)的接口出去
動態算法6個:
LC:least-connection :最小連接
查看每個節點的active 和inactive數量,根據active* 256+inactive,哪個結果小,就將請求發送給該服務器
WLC:weighted least-connection 加權的最小連接,
計算方法:(active* 256+inactive)/權值,哪個結果小,就將請求發送給該服務器
SED:最少的期望權值
只考慮active激活的連接,不考慮inactive連接。計算方法:(active+1)*256
NQ:永不排隊
若果某個節點未處於活動連接,就將下一個請求發送給該節點進行處理
LBLC:
當director和服務器之間有squid或varnish緩存服務器時,需要在director上設置,使請求分別到兩臺緩存服務器上
LBLCR:在LBLC的基礎上,
若其中一部分人不在訪問網站,則下面一臺緩存服務器將不會有頁面緩存,這時候該緩存服務器將會空閑下來。該算法使下面一臺緩存服務器自動復制上面一臺緩存服務器的網頁,用於提供緩存。
二、DR類型手動配制示例
2.1. 實驗環境:
VIP=10.109.134.210
DIP=10.109.134.201
RIP1=10.109.134.202
RIP2=10.109.134.203
2.2. Director的配置示例:
[[email protected]~]# ifconfig eth0:0 10.109.134.210/22
[[email protected]~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:50:56:B9:0B:B9
inet addr:10.109.134.201 Bcast:10.109.135.255 Mask:255.255.252.0
eth0:0 Link encap:Ethernet HWaddr 00:50:56:B9:0B:B9
inet addr:10.109.134.210 Bcast:10.109.135.255 Mask:255.255.252.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[[email protected]~]# route add -host 10.109.134.210 dev eth0:0
2.2.1. 測試遠程訪問網頁是否正常:
[[email protected]~]# curl http://10.109.134.202
test202
[[email protected]~]# curl http://10.109.134.203
<html><h1>test203</h1></html>
2.2.2. 配制LVS
[[email protected]]# ipvsadm -C
[[email protected]]# ipvsadm -A -t 10.109.134.210:80 -s wlc
[[email protected]]# ipvsadm -a -t 10.109.134.210:80 -r 10.109.134.202 -g -w 2
[[email protected]]# ipvsadm -a -t 10.109.134.210:80 -r 10.109.134.203 -g -w 1
[[email protected]]# ipvsadm -L -n
IPVirtual Server version 1.2.1 (size=4096)
ProtLocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.109.134.210:80 wlc
-> 10.109.134.203:80 Route 1 0 0
-> 10.109.134.202:80 Local 2 0 0
2.3. RealServer1的配置示例:
[[email protected]~]# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:50:56:B9:76:D1
inet addr:10.109.134.202 Bcast:10.109.135.255 Mask:255.255.252.0
[[email protected]~]# cd /proc/sys/net/ipv4/conf
[[email protected]]# echo 1 > lo/arp_ignore
[[email protected]]# echo 1 > all/arp_ignore
[[email protected]]# echo 2 > all/arp_announce
[[email protected]]# echo 2 > lo/arp_announce
[[email protected]]# cat lo/arp_announce
2
[[email protected]]# ifconfig lo:0 down
[[email protected]]# ifconfig lo:0 10.109.134.210 broadcast 10.109.134.210 netmask255.255.255.255 up
[[email protected]]# iptables –F
2.4. RealServer2的配置示例:
[[email protected]~]# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:50:56:B9:42:5D
inet addr:10.109.134.203 Bcast:10.109.135.255 Mask:255.255.252.0
[[email protected]~]# cd /proc/sys/net/ipv4/conf
[[email protected]]# echo 1 > lo/arp_ignore
[[email protected]]# echo 1 > all/arp_ignore
[[email protected]]# echo 2 > all/arp_announce
[[email protected]]# echo 2 > lo/arp_announce
[[email protected]]# cat lo/arp_announce
2
[[email protected]]# ifconfig lo:0 down
[[email protected]]# ifconfig lo:0 10.109.134.210 broadcast 10.109.134.210 netmask255.255.255.255 up
[[email protected]]# iptables -F
2.5. 實例截圖:
三、DR類型服務自動配制示例
3.1. Director的配置腳本:
#!/bin/bash
#
# LVS script for VS/DR
# chkconfig: - 90 10
#
. /etc/rc.d/init.d/functions
#
VIP=10.109.134.210
DIP=10.109.134.201
RIP1=10.109.134.202
RIP2=10.109.134.203
PORT=80
RSWEIGHT1=2
RSWEIGHT2=5
#
case "$1" in
start)
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/routeadd -host $VIP dev eth0:0
# Since this is the Director we must be able toforward packets
echo 1 >/proc/sys/net/ipv4/ip_forward
# Clear all iptables rules.
/sbin/iptables -F
# Reset iptables counters.
/sbin/iptables -Z
# Clear all ipvsadm rules/services.
/sbin/ipvsadm-C
# Add an IP virtual service for VIP 192.168.0.219port 80
# In this recipe, we will use the round-robinscheduling method.
# In production, however, you should use a weighted,dynamic scheduling method.
/sbin/ipvsadm-A -t $VIP:80 -s wlc
# Now direct packets for this VIP to
# the real server IP (RIP) inside the cluster
/sbin/ipvsadm-a -t $VIP:80 -r $RIP1 -g -w $RSWEIGHT1
/sbin/ipvsadm-a -t $VIP:80 -r $RIP2 -g -w $RSWEIGHT2
/bin/touch/var/lock/subsys/ipvsadm &> /dev/null
;;
stop)
# Stop forwarding packets
echo 0 >/proc/sys/net/ipv4/ip_forward
# Reset ipvsadm
/sbin/ipvsadm-C
# Bring down the VIP interface
/sbin/ifconfig eth0:0 down
/sbin/routedel $VIP
/bin/rm -f/var/lock/subsys/ipvsadm
echo"ipvs is stopped..."
;;
status)
if [ ! -e/var/lock/subsys/ipvsadm ]; then
echo"ipvsadm is stopped ..."
else
echo"ipvs is running ..."
ipvsadm -L-n
fi
;;
*)
echo"Usage: $0 {start|stop|status}"
;;
esac
3.2. RealServer的配置腳本:
#!/bin/bash
#
# Script to start LVS DR real server.
# chkconfig: - 90 10
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=10.109.134.210
host=`/bin/hostname`
case "$1" in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig lo down
/sbin/ifconfig lo up
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
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# StopLVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
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
;;
status)
#Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ !"$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
#not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
#Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
---END---
本文出自 “風過無痕” 博客,請務必保留此出處http://wangfx.blog.51cto.com/1697877/1956851
linux集群系列(3) --- LVS之負載均衡集群DR實例