在 lvs+keepalived 环境里面,lvs 主要的工作是提供调度算法,把客户端请求按照需求调度在 real 服务器,keepalived 主要的工作是提供 lvs 控制器的一个冗余,并且对 real 服务器做健康检查,发现不健康的 real 服务器,就把它从 lvs 集群中剔除,real 服务器只负责提供服务。
LVS/TUN
在原有的 IP 报文外再次封装多一层 IP 首部,内部 IP 首部 (源地址为 CIP,目标 IIP 为 VIP),外层 IP 首部 (源地址为 DIP,目标 IP 为 RIP)
(1) 当用户请求到达 Director Server,此时请求的数据报文会先到内核空间的 PREROUTING 链。 此时报文的源 IP 为 CIP,目标 IP 为 VIP 。 (2) PREROUTING 检查发现数据包的目标 IP 是本机,将数据包送至 INPUT 链 (3) IPVS 比对数据包请求的服务是否为集群服务,若是,在请求报文的首部再次封装一层 IP 报文,封装源 IP 为 DIP,目标 IP 为 RIP。然后发至 POSTROUTING 链。 此时源 IP 为 DIP,目标 IP 为 RIP (4) POSTROUTING 链根据最新封装的 IP 报文,将数据包发至 RS(因为在外层封装多了一层 IP 首部,所以可以理解为此时通过隧道传输)。 此时源 IP 为 DIP,目标 IP 为 RIP (5) RS 接收到报文后发现是自己的 IP 地址,就将报文接收下来,拆除掉最外层的 IP 后,会发现里面还有一层 IP 首部,而且目标是自己的 lo 接口 VIP,那么此时 RS 开始处理此请求,处理完成之后,通过 lo 接口送给 eth0 网卡,然后向外传递。 此时的源 IP 地址为 VIP,目标 IP 为 CIP (6) 响应报文最终送达至客户端
LVS/TUN 模型特性
RIP、VIP、DIP 全是公网地址
RS 的网关不会也不可能指向 DIP
所有的请求报文经由 Director Server,但响应报文必须不能进过 Director Server
不支持端口映射
RS 的系统必须支持隧道
其实企业中最常用的是 DR 实现方式,而 NAT 配置上比较简单和方便,后边实践中会总结 DR 和 NAT 具体使用配置过程。
TUN(virtual server via ip tunneling IP 隧道)调度器把请求的报文通过 IP 隧道转发到真实的服务器。真实的服务器将响应处理后的数据直接返回给客户端。这样调度器就只处理请求入站报文。此转发方式不修改请求报文的 IP 首部(源 IP 为 CIP,目标 IP 为 VIP),而在原 IP 报文之外再封装一个 IP 首部(源 IP 是 DIP,目标 IP 是 RIP),将报文发往挑选出的目标 RS;RS 直接响应给客户端(源 IP 是 VIP,目标 IP 是 CIP),由于一般网络服务应答数据比请求报文大很多,采用 lvs-tun 模式后,集群系统的最大吞吐量可以提高 10 倍
# write script, recommand manage it by keepalived.conf #!/bin/sh # Startup script handle the initialisation of LVS # chkconfig: - 28 72 # description: Initialise the Linux Virtual Server for TUN # ### 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. # description: start LVS of TUN LOCK=/var/lock/lvs-tun.lock VIP=10.10.36.11 RIP1=10.10.36.4 RIP2=10.10.36.7 . /etc/rc.d/init.d/functions
then echo"The LVS-TUN Server is already running !" else #Load the tun mod /sbin/modprobe tun /sbin/modprobe ipip #Set the tun Virtual IP Address /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev tunl0 #Clear IPVS Table /sbin/ipvsadm -C #The icmp recruit setting echo"0" >/proc/sys/net/ipv4/ip_forward echo"0" >/proc/sys/net/ipv4/conf/all/send_redirects echo"0" >/proc/sys/net/ipv4/conf/default/send_redirects #Set Lvs /sbin/ipvsadm -At $VIP:80 -s rr /sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -i -w 1 /sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -i -w 1 /bin/touch $LOCK #Run Lvs echo"starting LVS-TUN-DIR Server is ok !" fi }
stop() { #stop Lvs server /sbin/ipvsadm -C /sbin/ifconfig tunl0 down >/dev/null #Remove the tun mod /sbin/modprobe -r tun /sbin/modprobe -r ipip rm -rf $LOCK echo"stopping LVS-TUN-DIR server is ok !" }
status() { if [ -e $LOCK ]; then echo"The LVS-TUN Server is already running !" else echo"The LVS-TUN Server is not running !" fi }
#!/bin/sh # # Startup script handle the initialisation of LVS # chkconfig: - 28 72 # description: Initialise the Linux Virtual Server for TUN # ### 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. # description: start LVS of TUN-RIP LOCK=/var/lock/ipvsadm.lock VIP=10.10.36.11 . /etc/rc.d/init.d/functions start() { PID=`ifconfig | grep tunl0 | wc -l` if [ $PID -ne 0 ]; then echo"The LVS-TUN-RIP Server is already running !" else #Load the tun mod /sbin/modprobe tun /sbin/modprobe ipip #Set the tun Virtual IP Address /sbin/ifconfig tunl0 $VIP netmask 255.255.255.255 broadcast $VIP up /sbin/route add -host $VIP dev tunl0 echo"1" >/proc/sys/net/ipv4/conf/tunl0/arp_ignore echo"2" >/proc/sys/net/ipv4/conf/tunl0/arp_announce echo"1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo"2" >/proc/sys/net/ipv4/conf/all/arp_announce echo"0" > /proc/sys/net/ipv4/conf/tunl0/rp_filter echo"0" > /proc/sys/net/ipv4/conf/all/rp_filter /bin/touch $LOCK echo"starting LVS-TUN-RIP server is ok !" fi }
stop() { /sbin/ifconfig tunl0 down echo"0" >/proc/sys/net/ipv4/conf/tunl0/arp_ignore echo"0" >/proc/sys/net/ipv4/conf/tunl0/arp_announce echo"0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo"0" >/proc/sys/net/ipv4/conf/all/arp_announce #Remove the tun mod /sbin/modprobe -r tun /sbin/modprobe -r ipip rm -rf $LOCK echo"stopping LVS-TUN-RIP server is ok !" }
status() { if [ -e $LOCK ]; then echo"The LVS-TUN-RIP Server is already running !" else echo"The LVS-TUN-RIP Server is not running !" fi }
# 关于 3 中模式的参数 [packet-forwarding-method] -g, --gatewaying Use gatewaying (direct routing). This is the default. -i, --ipip Use ipip encapsulation (tunneling). -m, --masquerading Use masquerading (network access translation, or NAT). Note: Regardless of the packet-forwarding mechanism specified, real servers for addresses forwhich there are interfaces on the local node will be use the local forwarding method, then packets for the servers will be passed to upper layer on the local node. This cannot be specified by ipvsadm, rather it set by the kernel as real servers are added or modified.