LVS的工作机制以及其调度算法等一些初步了解在我之前的文章里面已有记录。请看这里
- #!/bin/bash
- #
- # LVS script for VS/DR
- #
- . /etc/rc.d/init.d/functions
- #
- VIP=172.23.136.150
- RIP1=172.23.136.149
- RIP2=172.23.136.148
- PORT=80
- #
- case "$1" in
- start)
- /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
- /sbin/route add -host $VIP dev eth0:1
- # Since this is the Director we must be able to forward 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 172.23.136.150 port 80
- # In this recipe, we will use the round-robin scheduling 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 1
- /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 2
- /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:1 down
- /sbin/route del $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
(二)、RealServer端配置
在各个realserver端分别运行ipvsclient脚本,脚本内容如下:
- #!/bin/bash
- #
- # Script to start LVS DR real server.
- # description: LVS DR real server
- #
- . /etc/rc.d/init.d/functions
- VIP=172.23.136.150
- 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)
- # Stop LVS-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
(三)、测试
访问172.23.136.150,可以发现负载均衡已正常工作
现在我在后端两台realserver上都配置了phpmyadmin,并且两数据库的账号密码均相同。现在访问172.23.136.150/phpmyadmin 你会发现一个很有意思的现象就是始终无法登陆上(在使用ipvsadm定义规则时没有定义权重)
因此Directory还需要基于“连接追踪”实现将同一个客户端的请求始终发往其第一次被分配到的realserver,ipvs会在自己的内部维护一个hash表,表中保存着不同的客户端第一次请求时所分发的后端realserver,以及保存该条目的时间,当该段时间消耗完之后,连接还未断开,那么这段时间会自动延迟你所定义的持久连接时间。当下一个请求达到时,就会去这个表中对比,将请求分发给条目中所对应的realserver用来保证整个请求的完整性。
lvs的持久连接类型分以下几种:
1.pcc:持久客户端连接,在指定规则时,使用0端口代表所有的端口,即所有到达VIP的请求全部按照调度算法负载至后端的realserver
2.ppc:持久端口连接,明确指定请求VIP的哪个端口的请求分发至后端的realserver
3.Netfilter marked packets:防火墙标记的持久连接,主要用于多端口协议间的关联,例如在电子商务网站上,在80端口挑选了商品后,当付款的时候就会跳转至443端口。
4.FTP持久连接,用于主动连接和被动连接,很少用到。
(一)、pcc
任何类型的持久连接均只需要在Directory上配置,realserver则不需要进行额外配置,因为这些只涉及到Directory的请求分发方法。
ipvsadm -C
ipvsadm -A -t 172.23.136.150:0 -p 360
ipvsadm -a -t 172.23.136.150:0 -r 172.23.136.148
ipvsadm -a -t 172.23.136.150:0 -r 172.23.136.149
在之前的配置基础上执行以上配置即可。
(二)、ppc
ipvsadm -C
ipvsadm -A -t 172.23.136.150:80 -p 360
ipvsadm -a -t 172.23.136.150:80 -r 172.23.136.148
ipvsadm -a -t 172.23.136.150:80 -r 172.23.136.149
(三)、持久防火墙标记
持久防火墙标记需要结合iptables来使用
iptables -t mangle -A PREROUTING -i eth0 -p tcp -d 172.23.136.150 -m multiport --dport 80,443 -j MARK --set-mark 1
###把来自eth0所有目的地址为172.23.136.150的80和443端口的请求绑定在一起,标签为1
ipvsadm -C
ipvsadm -A -f 1 -p 360
ipvsadm -a -f 1 -r 172.23.136.148
ipvsadm -a -f 1 -r 172.23.136.149
(四)、FTP持久连接
首先要了解FTP的工作模式:21控制端口 20数据端口
被动连接 是随机从1024---65000内选出一个 作为回应端口号,所以我们要限制被动连接回应端口的范围。
编辑你所用的FTP软件,vsftpd或者pure-ftpd,设置其端口范围结合iptables打标签
iptables -t mangle -A PREROUTING -i eth0 -p tcp -d 192.168.2.100 --dport 21 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -i eth0 -p tcp -d 192.168.2.100 --dport 10000:12000 -j MARK --set-mark 1
ipvsadm -C
ipvsadm -A -f 1 -p 360
ipvsadm -a -f 1 -r 172.23.136.148
ipvsadm -a -f 1 -r 172.23.136.149
小结:
访问172.23.136.150/phpmyadmin,现在已经可以正常登录进去,因为此次的连接被持久分发到后端的同一台realserver上。整个请求是完整的。
查看其连接分配状态,可以发现同一个客户端的请求都被定向至后端的同一个realserver。
ipvsadm -lcn
IPVS connection entries
pro expire state source virtual destination
TCP 01:55 FIN_WAIT 172.23.136.93:56944 172.23.136.150:80 172.23.136.149:80
TCP 01:56 FIN_WAIT 172.23.136.93:56947 172.23.136.150:80 172.23.136.149:80
TCP 01:39 FIN_WAIT 172.23.136.93:56928 172.23.136.150:80 172.23.136.149:80
TCP 01:56 FIN_WAIT 172.23.136.93:56946 172.23.136.150:80 172.23.136.149:80
TCP 01:39 FIN_WAIT 172.23.136.93:56930 172.23.136.150:80 172.23.136.149:80
TCP 01:55 FIN_WAIT 172.23.136.93:56943 172.23.136.150:80 172.23.136.149:80
TCP 01:55 FIN_WAIT 172.23.136.93:56945 172.23.136.150:80 172.23.136.149:80
TCP 01:47 FIN_WAIT 172.23.136.93:56933 172.23.136.150:80 172.23.136.149:80
TCP 01:39 FIN_WAIT 172.23.136.93:56931 172.23.136.150:80 172.23.136.149:80
TCP 14:57 ESTABLISHED 172.23.136.93:56948 172.23.136.150:80 172.23.136.149:80
TCP 05:50 NONE 172.23.136.93:0 172.23.136.150:80 172.23.136.149:80
TCP 01:39 FIN_WAIT 172.23.136.93:56932 172.23.136.150:80 172.23.136.149:80