参考链接:http://www.uml.org.cn/zjjs/201211124.asp
Lvs:(linux virtual server)是一款网络模型四层的负载软件,即IP+端口的转发模式,
Lvs提供三种负载方式:
VS/NAT:支持tcp/ip协议,负载均衡器需要一个合法的IP地址。所以的请求包和应答包都经过负载均衡器。扩展有限,
VS/TUN:传输基于IP(IP Encapsulation)模式,负载均衡器值负责将请求包转发到物理服务器,而物理服务器将请求包直接发送给用户,可以支持比较高的吞吐量。如果负载调度器的网卡是全双工100M的网卡。能为超过100台的物理服务器服务。不过需要所有得服务器支持IP协议。
VS/DR:通过TCP/ip协议传输,直接路由模式,负载调度器只负责请求转发,这种发送不需要隧道模式,应答包通过路由的方法返回给客户端,需要负载器必须与real server在同一物理网卡上面。
环境如下:centos6.5
Lvs主调度器:192.168.65.128 vip:192.168.65.200
真实服务器:192.168.65.150 vip:192.168.65.200
真实服务器:192.168.65.151 vip:192.168.65.200
特别注意的是:虚拟地址由keepalived提供
修改系统内核文件
vim /etc/sysctl.conf 修改默认值0为1,开启内核路由转发模式
net.ipv4.ip_forward = 1
安装Lvs集群文件
#yum install ipvsadm*
#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
#tar -zxvf ipvsadm-1.26.tar.gz
#make && make install
设置lvs的轮询到后台服务器
添加虚拟IP地址,wrr表示给予权重的轮询。rr表示轮询
#ipvsadm -A -t 192.168.65.200:80 -s wrr
制定后台和轮询的IP地址分别是150和151这两台机器 -g表示路由模式,-w表示权重
ipvsadm -a -t 192.168.65.200:80 -r 192.168.65.150:80 -g -w 5
ipvsadm -a -t 192.168.65.200:80 -r 192.168.65.151:80 -g -w 5
service ipvsadm save
编辑lvs的配置文件
vim /etc/sysconfig/ipvsadm
-A -t 192.168.65.200:80 -s wrr
-a -t 192.168.65.200:80 -r 192.168.65.150:80 -g -w 5
-a -t 192.168.65.200:80 -r 192.168.65.151:80 -g -w 5
开机启动脚本 vim/etc/init.d/ipvsadm
#!/bin/bash
#
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server
# config: /etc/sysconfig/ipvsadm
#
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
# available server built on a cluster of real servers, with the load
# balancer running on Linux.
### END INIT INFO
# Source function library
. /etc/rc.d/init.d/functions
IPVSADM=ipvsadm
IPVSADMRESTORE=${IPVSADM}-restore
IPVSADMSAVE=${IPVSADM}-save
# Saved IPVS data
IPVSADM_DATA=/etc/sysconfig/$IPVSADM
# Configuration
IPVSADM_CONFIG=/etc/sysconfig/${IPVSADM}-config
IPVS=ip_vs
PROC_IPVS=/proc/net/$IPVS
VAR_SUBSYS_IPVSADM=/var/lock/subsys/$IPVSADM
if [ ! -x /sbin/$IPVSADM ]; then
echo -n $"${IPVSADM}: /sbin/$IPVSADM does not exist."; warning; echo
exit 5
fi
# Old or new modutils
/sbin/modprobe --version 2>&1 | grep -q module-init-tools \
&& NEW_MODUTILS=1 \
|| NEW_MODUTILS=0
# Default IPVSADM configuration:
IPVS_MODULES_UNLOAD="yes"
IPVS_SAVE_ON_STOP="no"
IPVS_SAVE_ON_RESTART="no"
IPVS_STATUS_NUMERIC="yes"
# Load IPVSADM configuration.
[ -f "$IPVSADM_CONFIG" ] && . "$IPVSADM_CONFIG"
rmmod_r() {
# Unload module with all referring modules.
# At first all referring modules will be unloaded, then the module itself.
local mod=$1
local ret=0
local ref=
# Get referring modules.
# New modutils have another output format.
[ $NEW_MODUTILS = 1 ] \
&& ref=$(lsmod | awk "/^${mod}[[:space:]]/ { print \$4; }" | tr ',' ' ') \
|| ref=$(lsmod | grep ^${mod} | cut -d "[" -s -f 2 | cut -d "]" -s -f 1)
# recursive call for all referring modules
for i in $ref; do
rmmod_r $i
let ret+=$?;
done
# Unload module.
# The extra test is for 2.6: The module might have autocleaned,
# after all referring modules are unloaded.
if grep -q "^${mod}" /proc/modules ; then
modprobe -r $mod > /dev/null 2>&1
res=$?
[ $res -eq 0 ] || echo -n " $mod"
let ret+=$res;
fi
return $ret
}
start() {
# Do not start if there is no config file.
[ ! -f "$IPVSADM_DATA" ] && return 6
# check if ipvs module load is deactivated
if grep -qIsE "^install[[:space:]]+${IPVS}[[:space:]]+/bin/(true|false)" /etc/modprobe.conf /etc/modprobe.d/* ; then
echo $"${IPVSADM}: ${IPVS} is disabled."
return 150
fi
# If we don't clear these first, we might be adding to pre-existing rules.
action $"${IPVSADM}: Clearing the current IPVS table:" $IPVSADM -C
echo -n $"${IPVSADM}: Applying IPVS configuration: "
$IPVSADMRESTORE < ${IPVSADM_DATA}
if [ $? -eq 0 ];then success; echo; else failure; echo; return 1;fi
touch $VAR_SUBSYS_IPVSADM
}
stop() {
# Do not stop if ipvs module is not loaded.
[ ! -e "$PROC_IPVS" ] && return 0
action $"${IPVSADM}: Clearing the current IPVS table:" $IPVSADM -C
ret=0
if [ "x$IPVS_MODULES_UNLOAD" = "xyes" ]; then
action $"${IPVSADM}: Unloading modules:" rmmod_r $IPVS
[ $? -ne 0 ] && ret=1
fi
rm -f $VAR_SUBSYS_IPVSADM
return $ret
}
status() {
# Do not print status if lockfile is missing and ipvs modules are not
# loaded.
if [ ! -f "$VAR_SUBSYS_IPVSADM" -a ! -e "$PROC_IPVS" ]; then
echo $"${IPVSADM}: IPVS is not running."
return 3
fi
# Do show status if ipvs module is not loaded.
if [ ! -e "$PROC_IPVS" ];then
echo $"${IPVSADM}: IPVS module is not loaded."
return 3
fi
NUM=""
[ "x$IPVS_STATUS_NUMERIC" = "xyes" ] && NUM="-n"
$IPVSADM -L $NUM && echo
}
save() {
# Check if module is loaded
[ ! -e "$PROC_IPVS" ] && return 0
echo -n $"${IPVSADM}: Saving IPVS table to ${IPVSADM_DATA}: "
$IPVSADMSAVE -n > ${IPVSADM_DATA} 2>/dev/null
if [ $? -eq 0 ];then success; echo; else failure; echo; return 1;fi
return 0
}
restart() {
[ "x$IPVS_SAVE_ON_RESTART" = "xyes" ] && save
stop
start
}
# See how we were called.
case "$1" in
start)
[ -f "$VAR_SUBSYS_IPVSADM" ] && exit 0
# If we have no configuration, save the current one
[ -f ${IPVSADM_DATA} ] || save
start
RETVAL=$?
;;
stop)
[ "x$IPVS_SAVE_ON_STOP" = "xyes" ] && save
stop
RETVAL=$?
;;
restart|force-reload)
restart
RETVAL=$?
;;
reload)
# Start will flush everything, so it counts as a reload
start
RETVAL=$?
;;
status)
status
RETVAL=$?
;;
save)
save
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|restart|force-reload|reload|status|save}"
RETVAL=2
esac
exit $RETVA
添加ipvsadmd的规则配置文件,保持规则一直在。service ipvsadm reload
vim /etc/sysconfig/ipvsadm
-A -t 192.168.65.200:80 -s wrr
-a -t 192.168.65.200:80 -r 192.168.65.154:80 -g -w 5
-a -t 192.168.65.200:80 -r 192.168.65.155:80 -g -w 5
配置real server服务器,两台服务器的配置一样
service iptables stop
iptables -F
vim /etc/sysctl.conf 在文件的末尾添加如下内容
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
立即生效配置,禁止arp相应的请求
sysctl -p
设置虚拟网卡,添加如下内容
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
BOOTPROTO=static
IPADDR=192.168.65.200
NETMASK=255.255.255.255
ONBOOT=yes
添加路由: route add -host 192.168.65.200 dev lo:0
在lvs调度器查看
ipvsadm -Ln
查看你当前ipvsadm的轮询时间,为了使实验有比较好的效果,设置时间为1秒,可以查看转换效果
查看当前的的轮询时间
ipvsadm -L –timeout
设置时间为1秒
ipvsadm –set 1 1 1
在浏览器查看并且过一秒刷新页面地址变化则说明lvs轮询成功
刷新页面之后。可以目前的当前链接的后端服务器轮询
ipvsadm -Lcn
LVS的三种工作模式:
基于四层的网络模型,及IP+端口的转发模式
LVS-NAT:网络地址转换 Network address translation
LVS-DR:直接路由 Direct routing
LVS-TUN:IP隧道 IP tunneling
ipvsadm 参数详解
用法:
ipvsadm -A|E -t|u VIP:PORT [-s scheduler] [-p [timeout]] [-M netmask]]
-A:定义新的集群服务
-E:修改已有的集群服务
-D:删除某集群服务
-s:指定调度算法,可选项:rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq ,默认的调度算法是: wlc.
-C:清空
-S:等同ipvsadm-save 保存虚拟服务器规则,输出为-R 选项可读的格式
-L|l:列出当前已定义的集群服务和real server
–stats:显示统计信息 与-L同时使用
–rate:显示入站响应速率 与-L同时使用
-c:显示LVS 目前的连接 与-L同时使用
-n:不进行反向解析 与-L同时使用
-Z:清空计数器
-t –tcp-service service-address 说明虚拟服务器提供的是tcp 的服务
-u –udp-service service-address 说明虚拟服务器提供的是udp 的服务
-f –fwmark-service fwmark 说明是经过iptables 标记过的服务类型。
-p –persistent [timeout] 持久稳固的服务。这个选项的意思是来自同一个客户的多次请求,将被同一台真实的服务器处理。timeout 的默认值为300 秒。
ipvsadm -a|e -t|u VIP:PORY -r RIP [-g|i|m] [-w weight]
-a:添加real server
-e:修改real server
-g:指定LVS 的工作模式为直接路由模式(也是LVS 默认的模式)
-i:指定LVS 的工作模式为隧道模式
-m:指定LVS 的工作模式为NAT 模式
-w:真实服务器的权值
ipvsad的调度算法介绍
rr:轮叫调度,不管每个服务器的实际情况
wrr :加权轮叫调度。
lc:最少链接调度
wlc:加权最少链接调度
报错:collect2: ld returned 1 exit status
make: * [ipvsadm] Error 1
yum install install kernel-headers popt-static
报错:unexpected argument 192.168.65.130:80
使用上面ipvsad的脚本,添加/etc/sysconfig/ipvsadm
里面的规则,重启ipvsadm服务
报错:
eloading ipvsadm configuration (via systemctl): Failed to issue method call: Job type reload is not applicable for unit ipvsadm.service.
解决办法
添加:/sys/fs/cgroup/systemd/system.slice/ipvsadm.service