1、LVS负载均衡

实现LVS负载均衡转发方式有三种,分别为NAT、DR、TUN模式,LVS均衡算法包括:RR(round-robin)、LC(least_connection)、W(weight)RR、WLC模式等(RR为轮询模式,LC为最少连接模式)。

LVS NAT原理:用户请求LVS VIP到达director,director将请求的报文的目标IP地址改成后端的realserver IP地址,同时将报文的目标端口也改成后端选定的realserver相应端口,最后将报文发送到realserver,realserver将数据返给director,director再把数据发送给用户。(两次请求都经过director,所以访问大的话,director会成为瓶颈)

LVS DR原理:用户请求LVS VIP到达director,director将请求的报文的目标MAC地址改成后端的realserver MAC地址,目标IP为VIP(不变),源IP为用户IP地址(保持不变),然后Director将报文发送到realserver,realserver检测到目标为自己本地VIP,如果在同一个网段,然后将请求直接返给用户。如果用户跟realserver不在一个网段,则通过网关返回用户

LVS TUN原理:用户请求LVS到达director,director通过IP-TUN加密技术将请求的报文的目标MAC地址改成后端的realserver MAC地址,目标IP为VIP(不变),源IP为用户IP地址(保持不变),然后Director将报文发送到realserver,realserver基于IP-TUN解密,然后检测到目标为自己本地VIP,如果在同一个网段,然后将请求直接返给用户。如果用户跟realserver不在一个网段,则通过网关返回用户

2、LVS负载均衡配置

1)Ipvsadm编译安装方法如下

wget  -c  
http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
ln -s /usr/src/kernels/2.6.*  /usr/src/linux
yum install -y libnl* popt*
tar xzvf ipvsadm
-1.24.tar.gz cd ipvsadm-1.24 make make install

2)Ipvsadm软件安装完毕后,需要进行配置,主要配置方法有三步:添加虚拟服务器IP,添加realserver后端服务及启动LVS服务器VIP地址,配置代码如下:

ipvsadm  -A  -t  192.168.0.188:80  -s  rr

ipvsadm  -a  -t  192.168.0.188:80  -r  192.168.0.112  -g  -w 2

ipvsadm  -a  -t  192.168.0.188:80  -r  192.168.0.113  -g  -w 2

3)shell脚本部署LVS相关软件

#!/bin/bash
SNS_VIP=$2
SNS_RIP1=$3
SNS_RIP2=$4
if [ "$1" == "stop" -a -z "$2" ];then
    echo "------------------------------------------"
    echo -e "\033[32mPlease Enter $0 stop LVS_VIP\n\nEXample:$0 stop 192.168.1.111\033[0m"
    echo
    exit
else
    if [ -z "$2" -a -z "$3" -a -z "$4" ];then
        echo "----------------------------------------"
        echo -e "\033[32mPlease Enter Input $0 start VIP REALSERVER1  REALSERVER2\n\nEXample:$0 start/stop 192.168.1.111 192.168.1.2 192.168.1.3\033[0m"
        echo
        exit 0
    fi
fi
. /etc/rc.d/init.d/functions
logger $0 called with $1
function IPVSADM(){
/sbin/ipvsadm --set  30 5 60
/sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
/sbin/route add -host $SNS_VIP dev eth0:0
/sbin/ipvsadm -A -t $SNS_VIP:80 -s wlc -p 120
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
}
case "$1" in
start)
IPVSADM
echo "-----------------------------------------------------"
/sbin/ipvsadm -Ln
touch /var/lock/subsys/ipvsadm > /dev/null 2>&1
;;
stop)
/sbin/ipvsadm -C
/sbin/ipvsadm -Z
ifconfig eth0:0 down >>/dev/null 2>&1
route del $SNS_VIP >>/dev/null 2>&1
rm -rf /var/lock/subsys/ipvsadm > /dev/null 2>&1
echo "ipvsadm stopped!"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ]
then
echo "ipvsadm stopped!"
exit 1
else
echo "ipvsadm started!"
fi
;;
*)
echo "Usage: $0 {start | stop | status}"
exit 1
esac
exit 0

4)LVS服务器绑定VIP地址,命令如下

VIP=192.168.0.190
ifconfig    eth0:0  $VIP netmask  255.255.255.255  broadcast  $VIP
/sbin/route  add  -host  $VIP  dev  eth0:0

5)LVS ipvsadm配置参数说明

-A                        增加一台虚拟服务器VIP地址;
-t                        虚拟服务器提供的是tcp服务;
-s                        使用的调度算法;
-a                        在虚拟服务器中增加一台后端真实服务器;
-r                        指定真实服务器地址;
-w                        后端真实服务器的权重;
-m                        设置当前转发方式为NAT模式;-g为直接路由模式;-i  模式为隧道模式。

6)查看LVS转发列表命令为:ipvsadm -Ln

7)Nginx客户端realserver配置VIP脚本

#!/bin/sh
#LVS Client Server
VIP=192.168.0.188
case  $1  in                                                                                                                                                                                                                                                    
start)                                                                                                                                                                                                                                            
    ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
    /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 >/dev/null 2>&1
    echo "RealServer Start OK"
    exit 0
;;                                                                                                                                                                                                                                         
stop)
    ifconfig lo:0 down
    route del $VIP >/dev/null 2>&1
    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
    echo "RealServer Stoped OK"
    exit 1
;;
*)
    echo "Usage: $0 {start|stop}"
;;
esac