目錄
一席里、負(fù)載均衡集群介紹
二、LVS介紹
三拢驾、LVS調(diào)度算法
四奖磁、LVS NAT模式搭建
一、負(fù)載均衡集群介紹
負(fù)載均衡集群不難理解繁疤,簡(jiǎn)單說(shuō)就是讓多臺(tái)服務(wù)器均衡地去承載壓力咖为。
實(shí)現(xiàn)負(fù)載均衡集群的開源軟件有LVS、keepalived稠腊、haproxy躁染、Nginx等。
其中LVS屬于4層(網(wǎng)絡(luò)OSI 7層模型傳輸層)架忌,nginx屬于7層(應(yīng)用層)吞彤,haproxy既可以認(rèn)為是4層,也可以當(dāng)做7層使用叹放。
keepalived的負(fù)載均衡功能其實(shí)就是lvs饰恕。
lvs這種4層的負(fù)載均衡是可以分發(fā)除80外的其他端口通信的,比如MySQL的许昨,而nginx僅僅支持http懂盐,https,mail糕档,haproxy也支持MySQL這種莉恼。
相比較來(lái)說(shuō),LVS這種4層的更穩(wěn)定速那,能承受更多的請(qǐng)求俐银,而nginx這種7層的更加靈活,能實(shí)現(xiàn)更多的個(gè)性化需求端仰。
二捶惜、LVS介紹
LVS(Linux Virtual Server)是由國(guó)內(nèi)大牛章文嵩開發(fā)的,這款軟件的流行度不亞于Apache的httpd荔烧,它是一款四層的負(fù)載均衡軟件吱七,是針對(duì)TCP/IP做的轉(zhuǎn)發(fā)和路由,所以穩(wěn)定性和效率相當(dāng)高鹤竭。
LVS架構(gòu)中有一個(gè)核心角色叫做分發(fā)器(Load balance)踊餐,它用來(lái)分發(fā)用戶的請(qǐng)求,還有諸多處理用戶請(qǐng)求的服務(wù)器(Real Server臀稚,簡(jiǎn)稱rs)吝岭。
LVS根據(jù)實(shí)現(xiàn)方式的不同,主要分為三種類型:NAT模式、IP Tunnel(IP隧道)模式窜管、DR模式散劫。
- LVS三種模式介紹:
- NAT模式
- 這種模式借助iptables的nat表來(lái)實(shí)現(xiàn)
- 用戶的請(qǐng)求到分發(fā)器后,通過(guò)預(yù)設(shè)的iptables規(guī)則幕帆,把請(qǐng)求的數(shù)據(jù)包轉(zhuǎn)發(fā)到后端的rs上去
- rs需要設(shè)定網(wǎng)關(guān)為分發(fā)器的內(nèi)網(wǎng)ip
- 用戶請(qǐng)求的數(shù)據(jù)包和返回給用戶的數(shù)據(jù)包全部經(jīng)過(guò)分發(fā)器获搏,分發(fā)器的性能和網(wǎng)絡(luò)的帶寬、質(zhì)量會(huì)成為整個(gè)集群的瓶頸
- NAT模式下處理請(qǐng)求的機(jī)器數(shù)量不能太多蜓肆,相對(duì)其他模式能處理的請(qǐng)求數(shù)較少
- 在nat模式中颜凯,只需要分發(fā)器有公網(wǎng)ip即可,所以比較節(jié)省公網(wǎng)ip資源
- IP Tunnel模式
- 這種模式仗扬,需要有一個(gè)公共的IP配置在分發(fā)器和所有rs上症概,我們把它叫做vip
- 客戶端請(qǐng)求的目標(biāo)IP為vip,分發(fā)器接收到請(qǐng)求數(shù)據(jù)包后早芭,會(huì)對(duì)數(shù)據(jù)包做一個(gè)加工彼城,會(huì)把目標(biāo)IP改為rs的IP,這樣數(shù)據(jù)包就到了rs上
- rs接收數(shù)據(jù)包后退个,會(huì)還原原始數(shù)據(jù)包募壕,這樣目標(biāo)IP為vip,因?yàn)樗衦s上配置了這個(gè)vip语盈,所以它會(huì)認(rèn)為是它自己
- rs處理完請(qǐng)求后根據(jù)路由表將響應(yīng)報(bào)文直接返回給客戶端舱馅,不再經(jīng)過(guò)分發(fā)器,此時(shí)分發(fā)器不再是集群性能的瓶頸
- 這種模式下刀荒,需要給分發(fā)器和所有的rs全部分配公網(wǎng)IP代嗤,所以比較浪費(fèi)公網(wǎng)IP
- DR模式
- 這種模式,也需要有一個(gè)公共的IP配置在分發(fā)器和所有rs上缠借,也就是vip
- 和IP Tunnel不同的是干毅,這種模式下不會(huì)封裝IP,而是將數(shù)據(jù)幀的MAC地址改為rs的MAC地址
- rs接收數(shù)據(jù)包后泼返,會(huì)還原原始數(shù)據(jù)包硝逢,這樣目標(biāo)IP為vip,因?yàn)樗衦s上配置了這個(gè)vip绅喉,所以它會(huì)認(rèn)為是它自己
三渠鸽、LVS調(diào)度算法
- 輪詢 Round-Robin rr //均衡的將用戶請(qǐng)求分發(fā)到每一個(gè)rs上
- 加權(quán)輪詢 Weight Round-Robin wrr //帶權(quán)重的輪詢,依照指定的權(quán)重比來(lái)分發(fā)請(qǐng)求
- 最小連接 Least-Connection lc //將請(qǐng)求分發(fā)到連接數(shù)比較少的rs上
- 加權(quán)最小連接 Weight Least-Connection wlc //帶權(quán)重的最小連接
- 基于局部性的最小連接 Locality-Based Least Connections lblc
- 帶復(fù)制的基于局部性最小連接 Locality-Based Least Connections with
Replication lblcr- 目標(biāo)地址散列調(diào)度 Destination Hashing dh
- 源地址散列調(diào)度 Source Hashing sh
- 常用的是前4種
四柴罐、LVS NAT模式搭建
- 實(shí)驗(yàn)環(huán)境
? 需要3臺(tái)虛擬機(jī)拱绑,1臺(tái)作調(diào)度器dir,2臺(tái)作真實(shí)服務(wù)器rs丽蝎。
? NAT模式下,調(diào)度器需要有兩個(gè)IP,一個(gè)公網(wǎng)IP一個(gè)內(nèi)網(wǎng)IP屠阻,真實(shí)服務(wù)器只需要內(nèi)網(wǎng)IP红省。
? 三臺(tái)機(jī)器都關(guān)閉firewalld服務(wù),并安裝啟動(dòng)iptables-serives国觉,清空iptables規(guī)則吧恃。
? 三臺(tái)機(jī)器都關(guān)閉SElinux。
? 分發(fā)器dir
主機(jī)名:minglinux-03
內(nèi)網(wǎng)IP 192.168.162.128
公網(wǎng)IP 192.168.150.123 vmware僅主機(jī)網(wǎng)絡(luò)模式
VMware設(shè)置內(nèi)網(wǎng)的網(wǎng)卡設(shè)為NAT模式麻诀,連接外網(wǎng)的網(wǎng)卡設(shè)為僅主機(jī)模式
? 真實(shí)服務(wù)器rs1
主機(jī)名:minglinux-01
內(nèi)網(wǎng)IP 192.168.162.130
? 真實(shí)服務(wù)器rs2
主機(jī)名:minglinux-02
內(nèi)網(wǎng)IP 192.168.162.132
- 分發(fā)器虛擬機(jī)網(wǎng)絡(luò)設(shè)置
[root@minglinux-03 ~] vim /etc/sysconfig/network-scripts/ifcfg-ens33 //ifcfg-ens33網(wǎng)卡連接內(nèi)網(wǎng)
[root@minglinux-03 ~] vim /etc/sysconfig/network-scripts/ifcfg-ens37
//ifcfg-ens37網(wǎng)卡連接公網(wǎng)痕寓,網(wǎng)段為VMware僅主機(jī)網(wǎng)絡(luò)模式子網(wǎng)網(wǎng)段
[root@minglinux-03 ~] ifup ens37
[root@minglinux-03 ~] ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:7a:0f:05 brd ff:ff:ff:ff:ff:ff
inet 192.168.162.128/24 brd 192.168.162.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe7a:f05/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:7a:0f:0f brd ff:ff:ff:ff:ff:ff
inet 192.168.150.123/24 brd 192.168.150.255 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe7a:f0f/64 scope link
valid_lft forever preferred_lft forever
- rs虛擬機(jī)網(wǎng)絡(luò)設(shè)置
? 兩臺(tái)rs均設(shè)置內(nèi)網(wǎng)網(wǎng)關(guān)為dir的內(nèi)網(wǎng)IP(192.168.162.128)
//rs1網(wǎng)關(guān)設(shè)置
[root@minglinux-01 ~] vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@minglinux-01 ~] ifdown ens33 && ifup ens33
[root@minglinux-01 ~] route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.162.128 0.0.0.0 UG 0 0 0 ens33
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33
192.168.162.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
//rs2網(wǎng)關(guān)設(shè)置
[root@minglinux-02 ~] vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@minglinux-02 ~] ifdown ens33 && ifup ens33
[root@minglinux-02 ~] route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.162.128 0.0.0.0 UG 0 0 0 ens33
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33
192.168.162.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
- 3臺(tái)機(jī)器防火墻設(shè)置
? 3臺(tái)機(jī)器都執(zhí)行如下命令:
systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services //安裝iptables服務(wù)
systemctl start iptables //啟動(dòng)服務(wù)
systemctl enable iptables //開機(jī)啟動(dòng)
iptables -F
service iptables save
setenforce 0 //臨時(shí)關(guān)閉SElinux,修改selinux配置文件/etc/selinux/config可永久生效
//此時(shí)兩臺(tái)rs機(jī)器由于網(wǎng)關(guān)修改無(wú)法上網(wǎng)蝇闭,安裝iptables-services操作需先將網(wǎng)關(guān)改回
? epel源下載安裝速度慢的話可以臨時(shí)將epel源取消呻率,方法如下:
[root@minglinux-01 ~] cd /etc/yum.repos.d
[root@minglinux-01 /etc/yum.repos.d] ls
CentOS7-Base-163.repo CentOS-fasttrack.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-Media.repo epel.repo
CentOS-Debuginfo.repo CentOS-Sources.repo epel-testing.repo
[root@minglinux-01 /etc/yum.repos.d] mv epel.repo epel.repo.1
[root@minglinux-01 /etc/yum.repos.d] yum list |grep iptables-service
iptables-services.x86_64 1.4.21-24.1.el7_5 @updates
iptables-services.x86_64 1.4.21-28.el7 base
[root@minglinux-01 /etc/yum.repos.d] yum install -y iptables-services
- 在dir上安裝ipvsadm和配置腳本
? ipvsadm是實(shí)現(xiàn)LVS的核心工具
[root@minglinux-03 ~] yum install -y ipvsadm
[root@minglinux-03 ~] vim /usr/local/sbin/lvs_nat.sh //新建一個(gè)腳本
//寫入以下內(nèi)容
#! /bin/bash
# director 調(diào)整linux系統(tǒng)內(nèi)核參數(shù),開啟路由轉(zhuǎn)發(fā)功能呻引,讓數(shù)據(jù)包能轉(zhuǎn)發(fā)到后端rs上
echo 1 > /proc/sys/net/ipv4/ip_forward
# 關(guān)閉icmp的重定向
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
# 注意區(qū)分網(wǎng)卡名字礼仗,兩個(gè)網(wǎng)卡分別為ens33和ens37
echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects
# director 設(shè)置nat防火墻
iptables -t nat -F //清空nat表的規(guī)則
iptables -t nat -X //清空nat表中所有的鏈
iptables -t nat -A POSTROUTING -s 192.168.162.0/24 -j MASQUERADE
//讓同網(wǎng)段的內(nèi)網(wǎng)可以上網(wǎng)
# director設(shè)置ipvsadm
IPVSADM='/usr/sbin/ipvsadm'
$IPVSADM -C //ipvsadm的-C選項(xiàng)清空規(guī)則
$IPVSADM -A -t 192.168.150.123:80 -s lc -p 3
//-A增加Virtual Server,-t為TCP逻悠,-s選項(xiàng)指定調(diào)度算法元践,lc為最小連接算法。
//-p指定超時(shí)時(shí)間童谒,即在一段時(shí)間內(nèi)(3s)单旁,來(lái)自同一客戶端的請(qǐng)求會(huì)被分發(fā)到相同的rs上
$IPVSADM -a -t 192.168.150.123:80 -r 192.168.162.130:80 -m -w 1
$IPVSADM -a -t 192.168.150.123:80 -r 192.168.162.132:80 -m -w 1
//-a增加rs,-r指定rs的IP饥伊,-m表示LVS的模式為NAT(masquerad)
//如果是-g表示LVS模式為DR象浑,-i表示LVS模式為IP Tunnel,-w指定權(quán)重撵渡。
[root@minglinux-03 ~] sh !$ //執(zhí)行腳本
sh /usr/local/sbin/lvs_nat.sh
- NAT模式效果測(cè)試
兩臺(tái)rs需安裝有nginx并啟動(dòng)nginx
? 設(shè)置兩臺(tái)rs不同的主頁(yè)融柬,做一個(gè)區(qū)分
[root@minglinux-01 ~] echo "This is rs1" > /data/wwwroot/default/index.html
[root@minglinux-01 ~] curl localhost
This is rs1
[root@minglinux-02 ~] echo "This is rs2" > /usr/share/nginx/html/index.html
[root@minglinux-02 ~] curl localhost
This is rs2
? windows瀏覽器訪問(wèn)測(cè)試
腳本中設(shè)置了3s的長(zhǎng)久連接,此時(shí)連續(xù)刷新頁(yè)面并不改變
? 修改lvs_nat.sh腳本去掉指定超時(shí)連接的設(shè)定
[root@minglinux-03 ~] vim /usr/local/sbin/lvs_nat.sh
[root@minglinux-03 ~] sh !$
sh /usr/local/sbin/lvs_nat.sh
[root@minglinux-03 ~] ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.150.123:80 lc
-> 192.168.162.130:80 Masq 1 0 0
-> 192.168.162.132:80 Masq 1 0 0
? 修改lvs_nat.sh腳本中使用的算法為rr
在dir上使用curl測(cè)試:
//請(qǐng)求均衡地發(fā)送到后端的rs上
[root@minglinux-03 ~] vim /usr/local/sbin/lvs_nat.sh
[root@minglinux-03 ~] sh /usr/local/sbin/lvs_nat.sh
[root@minglinux-03 ~] curl 192.168.150.123
This is rs2
[root@minglinux-03 ~] curl 192.168.150.123
This is rs1
[root@minglinux-03 ~] curl 192.168.150.123
This is rs2
[root@minglinux-03 ~] curl 192.168.150.123
This is rs1
擴(kuò)展
lvs 三種模式詳解 http://www.it165.net/admin/html/201401/2248.html
lvs幾種算法 http://www.aminglinux.com/bbs/thread-7407-1-1.html
關(guān)于arp_ignore和 arp_announce http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
lvs原理相關(guān)的 http://blog.csdn.net/pi9nc/article/details/23380589