一、LVS介紹
1、介紹
LVS是Linux Virtual Server的簡寫,是linux虛擬的服務器集群系統,可以在unix/linux平台下實現負載均衡集群功能,由章文嵩博士組織成立,是國內出現最早的自由軟件之一。
LVS項目介紹 http://www.linuxvirtualserver.org/zh/lvs1.html LVS集群的體系結構 http://www.linuxvirtualserver.org/zh/lvs2.html LVS集群中的IP負載均衡技術 http://www.linuxvirtualserver.org/zh/lvs3.html LVS集群的負載調度 http://www.linuxvirtualserver.org/zh/lvs4.html
2、LVS特性
①真正實現負載調度的工具是IPVS,工作在linux內核層面。
②LVS自帶的IPVS管理工具是ipvsadm。
③keepalived實現管理IPVS及對負載均衡器的高可用。
3、LVS——DR模式工作原理

4、LVS集群其它模式
①DR直接路由模式(重點掌握)
②NAT
③TUN隧道模式
④FULLNAT
5、LVS應用場景
日PV1000-2000W或者並發請求1W以下的都可以使用Nginx,超過的話使用LVS,大型門戶網站,電商網站需要用到
二、手工配置LVS
1、環境
[root@lb01 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@lb01 ~]# uname -r 3.10.0-327.el7.x86_64 [root@lb01 ~]# getenforce Disabled [root@lb01 ~]# systemctl status firewalld.service ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) [root@lb01 ~]# hostname -I 10.0.0.5 172.16.1.5
2、安裝ipvsadm管理工具
①檢測是否安裝
[root@lb01 ~]# lsmod |grep ip_vs
②安裝ipvsadm
yum -y install ipvsadm
③檢查並激活lvs
[root@lb01 ~]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@lb01 ~]# lsmod |grep ip_vs ip_vs 140944 0 nf_conntrack 105745 1 ip_vs libcrc32c 12644 2 xfs,ip_vs
3、配置LVS負載均衡(lb01操作)
ip addr add 10.0.0.3/24 dev eth0 #在eth0網卡綁定VIP地址 ipvsadm -C #清除當前所有LVS規則 ipvsadm --set 30 5 60 #設置tcp、tcpfin、udp鏈接超時時間 ipvsadm -A -t 10.0.0.3:80 -s wrr -p 20 #添加虛擬服務(-A) ipvsadm -a -t 10.0.0.3:80 -r 10.0.0.7:80 -g -w 1 #將虛擬服務關聯到真實服務上(-a) ipvsadm -a -t 10.0.0.3:80 -r 10.0.0.8:80 -g -w 1 #將虛擬服務關聯到真實服務上(-a) ipvsadm -ln #查看配置結果
-C 清空整個表 -A 添加一個虛擬服務 -t 指定一個地址,一定是ip+端口 -s 調度算法 -a 添加一個real server -m NAT模式 -g DR模式,默認 -d 刪除一個real server -p 會話保持功能 -w 權重 -i tunnel模式
4、web服務器操作(web01、web02)
①在lo網卡綁定VIP地址
ip addr add 10.0.0.3/32 dev lo
②修改內核參數抑制ARP響應
cat >>/etc/sysctl.conf<<EOF net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 EOF sysctl -p
5、在lb02上面測試
[root@lb02 ~]# curl 10.0.0.3 web02 [root@lb02 ~]# curl 10.0.0.3 web02 [root@lb02 ~]# curl 10.0.0.3 web02 [root@lb02 ~]# curl 10.0.0.3 web02 [root@lb02 ~]# curl 10.0.0.3 web02 [root@lb02 ~]# curl 10.0.0.3 web01 [root@lb02 ~]# curl 10.0.0.3 web01
三、Keepalived配合LVS實現高可用負載均衡
1、安裝Keepalived
yum -y install keepalived
2、配置keepalived管理LVS
| global_defs { router_id LVS_01 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3/24 } } virtual_server 10.0.0.3 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.0.0.7 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }
real_server 10.0.0.8 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } |
global_defs { router_id LVS_02 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3/24 } } virtual_server 10.0.0.3 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.0.0.7 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }
real_server 10.0.0.8 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } |
3、測試keepalived高可用,故障轉移
①停掉lb01的keepalvied
[root@lb01 ~]# systemctl stop keepalived.service [root@lb01 ~]# curl 10.0.0.3 web01
②停掉lb02的keepalvied
[root@lb02 ~]# systemctl stop keepalived.service [root@lb02 ~]# curl 10.0.0.3 web01
4、測試Keepalived對后端節點的健康檢查功能
①web后端正常時狀態
[root@lb01 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.0.0.3:80 wrr persistent 50 -> 10.0.0.7:80 Route 1 0 0 -> 10.0.0.8:80 Route 1 0 0
②web后端節點宕機或者服務關閉時(此處關閉了web01)
[root@lb01 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.0.0.3:80 wrr persistent 50 -> 10.0.0.7:80 Route 1 0 0
5、Keepalived+LVS多實例配置
①lb01
global_defs { router_id LVS_01 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3/24 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 10.0.0.4/24 } } virtual_server 10.0.0.3 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.0.0.7 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.8 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } virtual_server 10.0.0.4 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.0.0.7 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.8 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
②lb02
global_defs { router_id LVS_02 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3/24 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 10.0.0.4/24 } } virtual_server 10.0.0.3 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.0.0.7 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.8 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } virtual_server 10.0.0.4 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.0.0.7 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.8 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
