四種LVS工作模型,十種調(diào)度算法
四種工作模型:
1送巡、lvs-nat:修改請求報文的目標(biāo)IP,多目標(biāo)IP的DNAT
請求報文:CIP-->路由器-->LVS-->RIP
回應(yīng)報文:RIP-->LVS-->路由器-->CIP
2旷祸、lvs-dr(直接路由):操縱封裝新的MAC地址
發(fā)送報文
①CIP-->VIP:SRC=CIP/CIPMAC,DST=VIP/VIPMAC
②DIP-->RIP:SRC=CIP/DIPMAC住拭,DST=VIP/LVSMAC
③LVS-->RS:SRC=CIP/LVSMAC撞反,DST=VIP/RS1MAC
回應(yīng)報文
④RS-->DIP:SRC=VIP/RS1MAC链瓦,DST=CIP/DIPMAC
⑤VIP-->CIP:SRC=VIP/VIPMAC蒲凶,DST=CIP/CIPMAC
3、lvs-tun(隧道):在原請求IP報文之外新加一個IP首部
請求報文通過LVS服務(wù)器杯道,但響應(yīng)卻直接回應(yīng)給客戶端匪煌,不通過LVS责蝠,可以跨路由
4、lvs-fullnat:修改請求報文的源和目標(biāo)IP
①CIP-->VIP
②DIP-->RIP
③RIP-->DIP
④VIP-->CIP
默認(rèn)kernel不支持
十種調(diào)度算法:
靜態(tài)方法:僅根據(jù)算法本身進(jìn)行調(diào)度
1萎庭、 RR:roundrobin霜医,輪詢
2、 WRR:Weighted RR驳规,加權(quán)輪詢
3肴敛、 SH:Source Hashing,實(shí)現(xiàn)session sticky吗购,源IP地址hash医男;將來自于同一個IP地址的請求始終發(fā)往第一次挑中的RS,從而實(shí)現(xiàn)會話綁定
4捻勉、 DH:Destination Hashing镀梭;目標(biāo)地址哈希,第一次輪詢調(diào)度至RS踱启,后續(xù)將發(fā)往同一個目標(biāo)地址的請求始終轉(zhuǎn)發(fā)至第一次挑中的RS报账,典型使用場景是正向代理緩存場景中的負(fù)載均衡,如:寬帶運(yùn)營商
動態(tài)方法:主要根據(jù)每RS當(dāng)前的負(fù)載狀態(tài)及調(diào)度算法進(jìn)行調(diào)度Overhead=value較小的RS將被調(diào)度
1埠偿、 LC:least connections 適用于長連接應(yīng)用
Overhead=activeconns256+inactiveconns
2透罢、 WLC:Weighted LC,默認(rèn)調(diào)度方法
Overhead=(activeconns256+inactiveconns)/weight
3冠蒋、 SED:Shortest Expection Delay,初始連接高權(quán)重優(yōu)先
Overhead=(activeconns+1)*256/weight
4羽圃、 NQ:Never Queue,第一輪均勻分配抖剿,后續(xù)SED
5朽寞、 LBLC:Locality-Based LC,動態(tài)的DH算法斩郎,使用場景:根據(jù)負(fù)載狀態(tài)實(shí)現(xiàn)正向代理
6愁憔、 LBLCR:LBLC with Replication,帶復(fù)制功能的LBLC孽拷,解決LBLC負(fù)載不均衡問題,從負(fù)載重的復(fù)制到負(fù)載輕的RS
基于NAT模型的LVS
#環(huán)境:5臺機(jī)器 client:172.16.0.6 LVS:172.16.0.7半抱、192.168.37.7 RS1:192.168.37.17 RS2:192.168.37.27 mysql:192.168.37.17
#網(wǎng)卡:A與B橋接脓恕,B與C、D窿侈、E是NAT
#mysql
nmcli connection modify eth0 ipv4.addresses 192.168.37.37/24 ipv4.gateway 192.168.37.7 ipv4.method manual
nmcli connection up eth0
yum install mariadb-server
systemctl start mariadb
mysql -e "create database wordpress;grant all on wordpress.* to wordpress@'192.168.37.%' identified by 'centos'"
mysql -e "flush privileges;"
#RS1
nmcli connection modify eth0 ipv4.addresses 192.168.37.17/24 ipv4.gateway 192.168.37.7 ipv4.method manual
nmcli connection up eth0
yum install httpd mod_ssl php-fpm php-mysql -y
echo RS1 > /var/www/html/index.html
vim /etc/httpd/conf.d/fcgi.conf
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.php index.html
tar -xf wordpress-5.1.1-zh_CN.tar.gz -C /var/www/html/
cd /var/www/html/wordpress/
cp wp-config-sample.php wp-config.php
vim wp-config.php
/** WordPress數(shù)據(jù)庫的名稱 */
define( 'DB_NAME', 'wordpress' );
/** MySQL數(shù)據(jù)庫用戶名 */
define( 'DB_USER', 'wordpress' );
/** MySQL數(shù)據(jù)庫密碼 */
define( 'DB_PASSWORD', 'centos' );
/** MySQL主機(jī) */
define( 'DB_HOST', '192.168.37.37' );
[root@rs1 ~]# systemctl start httpd php-fpm
#此時使用瀏覽器訪問http://192.168.37.17/wordpress,進(jìn)行安裝
[root@rs1 wordpress]# cd ..
[root@rs1 html]# scp -r wordpress/ 192.168.37.27:/var/www/html
[root@rs1 html]# scp /etc/httpd/conf/httpd.conf 192.168.37.27:/etc/httpd/conf/
[root@rs1 html]# scp /etc/httpd/conf.d/fcgi.conf 192.168.37.27:/etc/httpd/conf.d/
#RS2
nmcli connection modify eth0 ipv4.addresses 192.168.37.27/24 ipv4.gateway 192.168.37.7 ipv4.method manual
nmcli connection up eth0
yum install httpd mod_ssl php-fpm php-mysql -y
echo RS2 > /var/www/html/index.html
systemctl start httpd php-fpm
#LVS,初始化關(guān)閉防火墻炼幔、selinux、時間同步
vim /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.37.7
PREFIX=24
NAME=eth0
DEVICE=eth0
ONBOOT=yes
vim /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
IPADDR=172.16.0.7
PREFIX=24
NAME=eth1
DEVICE=eth1
ONBOOT=yes
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p
yum install ipvsadm
ipvsadm -A -t 172.16.0.7:80 -s rr #-s 調(diào)度輪詢史简,-p 持續(xù)連接(默認(rèn)360s)
ipvsadm -a -t 172.16.0.7:80 -r 192.168.37.17 -m #-m nat模型
ipvsadm -a -t 172.16.0.7:80 -r 192.168.37.27 -m
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.0.7:80 rr
-> 192.168.37.17:80 Masq 1 0 0
-> 192.168.37.27:80 Masq 1 0 0
ipvsadm -A -t 172.16.0.7:443 -s rr
ipvsadm -a -t 172.16.0.7:443 -r 192.168.37.27 -m
ipvsadm -a -t 172.16.0.7:443 -r 192.168.37.17 -m
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.0.7:80 rr
-> 192.168.37.17:80 Masq 1 0 0
-> 192.168.37.27:80 Masq 1 0 0
TCP 172.16.0.7:443 rr
-> 192.168.37.17:443 Masq 1 0 0
-> 192.168.37.27:443 Masq 1 0 0
#client
vim /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE=eth2
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
NAME="eth2"
IPADDR=172.16.0.6
PREFIX=24
[root@centos6 ~]$ while true;do curl 172.16.0.7;sleep 0.5;done
[root@centos6 ~]$ while true;do curl -k https://172.16.0.7;sleep 0.5;done
[root@centos6 ~]$ http://172.16.0.7/wordpress
使用firewall mark實(shí)現(xiàn)統(tǒng)一集群調(diào)度http乃秀,https
#LVS
ipvsadm -C #清除所有
iptables -t mangle -A PREROUTING -d 172.16.0.7 -p tcp -m multiport --dport 80,443 -j MARK --set-mark 10
ipvsadm -A -f 10 -s rr
ipvsadm -a -f 10 -r 192.168.37.17 -m
ipvsadm -a -f 10 -r 192.168.37.27 -m
ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
FWM 10 rr
-> 192.168.37.17:0 Masq 1 0 0
-> 192.168.37.27:0 Masq 1 0 0
#client
[root@centos6 ~]$ while true;do curl 172.16.0.7;sleep 0.5;done
[root@centos6 ~]$ while true;do curl -k https://172.16.0.7;sleep 0.5;done
[root@centos6 ~]$ http://172.16.0.7/wordpress
基于DR模型的LVS
#跨網(wǎng)段LVS-DR
#環(huán)境:5臺機(jī)器,Client:172.16.0.6 Router:172.16.0.7、192.168.37.7跺讯、10.0.0.200 RS1:192.168.37.17枢贿、10.0.0.100 RS2:192.168.37.27、10.0.0.100 LVS:192.168.37.37刀脏、10.0.0.100
#router 2塊網(wǎng)卡局荚,橋接與nat模式,基本配置跟nat模型一樣
#原LVS當(dāng)路由,router
yum remove ipvsadm
iptables -t mangle -F
nmcli connection modify eth0 +ipv4.addresses 10.0.0.200/8
nmcli connection up eth0
#RS1、RS2
ip address add 10.0.0.100/32 dev lo label lo:0
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
#原mysql當(dāng)LVS
ip address add 10.0.0.100/24 dev eth0 label eth0:0
yum install ipvsadm
iptables -t mangle -A PREROUTING -d 10.0.0.100 -p tcp -m multiport --dport 80,443 -j MARK --set-mark 10
ipvsadm -A -f 10 -s wrr
ipvsadm -a -f 10 -r 192.168.37.17 -g -w 1
ipvsadm -a -f 10 -r 192.168.37.27 -g -w 1
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
FWM 10 wrr
-> 192.168.37.17:0 Route 1 0 0
-> 192.168.37.27:0 Route 1 0 0
#client
[root@centos6 ~]$ vim /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE=eth2
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
NAME="eth2"
IPADDR=172.16.0.6
PREFIX=24
GATEWAY=172.16.0.7
[root@centos6 ~]$ service network restart
[root@centos6 ~]$ while true; do curl http://10.0.0.100/index.html;sleep 0.5;done
監(jiān)控LVS
#ldirectord基本被keepalived替代了
#環(huán)境跟上面一樣
#LVS
ipvsadm -C
yum install http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-7/x86_64/ldirectord-3.9.6-0rc1.1.2.x86_64.rpm
cp /usr/share/doc/ldirectord-3.9.6/ldirectord.cf /etc/ha.d/
vim /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=no #down時yes權(quán)重為0愈污,no為刪除
virtual=10.0.0.100:80 #指定VS的FWM 或 IP:PORT
real=192.168.37.17:80 gate #DR模型
real=192.168.37.27:80 gate
fallback=127.0.0.1:80 gate #sorry server
service=http
scheduler=wrr
#persistent=600 #持續(xù)連接
protocol=tcp
checktype=negotiate
checkport=80
request="index.html"
#receive="Test Page"
#virtualhost=www.x.y.z
yum install httpd
echo "Error Server" > /var/www/html/index.html
systemctl start httpd ldirectord
#RS1耀态、RS2
ip address add 10.0.0.100/32 dev lo label lo:0
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
#client
while true; do curl http://10.0.0.100/index.html;sleep 0.5;done
#RS1俺猿、RS2停掉服務(wù),客戶端查看情況
systemctl stop httpd