推荐一篇关于LVS的好文:
https://www.cnblogs.com/gaoxu387/p/7941381.html
一、原博主要内容:
1、概述
IP负载均衡:四层负载,是基于IP+端口的负载均衡,主要代表是LVS(Linux Virtual Server)
LVS工作原理:LVS的IP负载均衡技术是通过IPVS模块实现的,IPVS模块工作在内核空间。
2、LVS的工作模式
A、DR模式
通过 MAC 地址改写机制实现转发,集群局限于局域网内,需要设置lo接口的VIP不能响应本地网络内的arp请求。
B、TUN模式
通过再封装一层IP报文转发,集群可在公网上。
C、NAT模式
通过修改IP报文转发,集群局限在局域网内。
3、LVS的调度算法
- 轮叫调度(Round-Robin Scheduling)
- 加权轮叫调度(Weighted Round-Robin Scheduling)
- 最小连接调度(Least-Connection Scheduling)
- 加权最小连接调度(Weighted Least-Connection Scheduling)
- 基于局部性的最少链接(Locality-Based Least Connections Scheduling)
- 带复制的基于局部性最少链接(Locality-Based Least Connections with Replication Scheduling)
- 目标地址散列调度(Destination Hashing Scheduling)
- 源地址散列调度(Source Hashing Scheduling)
固定调度算法:rr,wrr,dh,sh
动态调度算法:wlc,lc,lblc,lblcr
二、DR模式的配置
这里介绍一下DR模式的配置。
1、负载均衡服务器:一般对外的负载均衡器须具有灾备能力,在负载均衡服务器上安装keepalived,并完成keepalived的配置,配置如下:
! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_EPG { state MASTER //主用设置为MASTER,备用设置为BACKUP interface bond0 virtual_router_id 64 priority 100 //主用优先级要比备用优先级高 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 111.11.11.10 //对外VIP } } virtual_server 111.11.11.10 6600 { delay_loop 6 lb_algo rr //调度算法RR,也可以设置其他算法 lb_kind DR //负载均衡模式为DR persistence_timeout 20 protocol TCP real_server 111.11.11.21 6600 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 111.11.11.22 6600 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 111.11.11.23 6600 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
2、RS节点服务器:需要设置lo接口的VIP,且设置其不能响应本地网络内的arp请求,可执行脚本:
1 #!/bin/bash 2 # description: Config realserver lo and apply noarp 3 VIP=111.11.11.10 4 5 . /etc/rc.d/init.d/functions 6 7 case "$1" in 8 start) 9 echo "1" >/proc/sys/net/ipv4/conf/bond0/arp_ignore 10 echo "2" >/proc/sys/net/ipv4/conf/bond0/arp_announce 11 echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore 12 echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 13 sysctl -p >/dev/null 2>&1 14 ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP 15 /sbin/route add -host $VIP dev lo:0 16 echo "RealServer Start OK" 17 ;; 18 stop) 19 ifconfig lo:0 down 20 route del $VIP >/dev/null 2>&1 21 echo "0" >/proc/sys/net/ipv4/conf/bond0/arp_ignore 22 echo "0" >/proc/sys/net/ipv4/conf/bond0/arp_announce 23 echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore 24 echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce 25 echo "RealServer Stoped" 26 ;; 27 status) 28 # Status of LVS-DR real server. 29 islothere=`/sbin/ifconfig lo:0 | grep $VIP` 30 isrothere=`netstat -rn | grep "lo:0" | grep $VIP` 31 if [ ! "$islothere" -o ! "isrothere" ];then 32 # Either the route or the lo:0 device 33 # not found. 34 echo "LVS-DR real server Stopped." 35 else 36 echo "LVS-DR Running." 37 fi 38 ;; 39 *) 40 # Invalid entry. 41 echo "$0: Usage: $0 {start|status|stop}" 42 exit 1 43 ;; 44 esac 45 exit 0
注意:此脚本最好设置成开机执行,如脚本名称为realserver.sh,放置在/usr/bin/路径下,赋可执行权限,在/etc/rc.local的末尾添加一行:/usr/bin/realserver.sh start