環境搭建:
客戶端:172.20.23.20
路由器:
172.20.23.30/16
192.168.23.30/24
10.0.0.1/16
LVS:
VIP --10.0.0.100/16
DIP --192.168.23.100/24
後端服務器:
RS1 --192.168.23.36
RS2 --192.168.23.37
網絡環境搭建:
客戶端路由設置:
[root@www19:36:15~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.20.23.30 0.0.0.0 UG 100 0 0 eth0
172.20.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0
LVS服務器路由設置:
[root@www19:38:19network-scripts]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0
192.168.23.0 0.0.0.0 255.255.255.0 U 101 0 0 eth1
後端服務器路由設置:
[root@apache119:29:53~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.23.30 0.0.0.0 UG 100 0 0 eth0
192.168.23.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
後端服務器因爲都需要綁定VIP,所以我們不得不進行防止arp廣播處理!
處理方式:調整內核參數
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
也可以寫入配置文件/etc/sysctl.conf
net.ipv4.conf.all.apr_ignore = 1
net.ipv4.conf.all.arp_announce = 2
處理完成之後我們開始綁定VIP,爲了使用穩定我們要選擇lo網卡作爲綁定對象!
綁定方式:
ip a a 10.0.0.100/32 dev lo
ifconfig lo 10.0.0.100/32
想要永久有效需要自行編輯配置文件:
例如:利用網卡別名
[root@apache119:49:07network-scripts]#cat ifcfg-lo:1
DEVICE=lo:1
IPADDR=10.0.0.100
NETMASK=255.255.255.255
NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
LVS調度器規則設定:
[root@www19:39:48network-scripts]#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.100:80 rr
-> 192.168.23.36:80 Route 1 0 3
-> 192.168.23.37:80 Route 1 0 2
https使用時,必須使用同一私鑰和證書:
快速生成測試證書:
/etc/pki/tls/certs目錄下
Makefile文件中修改加密選項
#/usr/bin/openssl genrsa -aes128 $(KEYLEN) > $@
/usr/bin/openssl genrsa $(KEYLEN) > $@
執行make httpd.crt --直接生成私鑰和證書
生成的證書拷貝到/etc/httpd/conf.d/ssl/
然後配置ssl.conf文件
然後複製設個RS主機的所有配置文件到所有RS主機之上!
然後重新生成ipvsadm規則!
測試訪問:
curl -k https://10.0.0.100
注意:
要使用相同的祕鑰和證書
利用iptables規則實現標籤會話:
有標籤的就認爲是同一服務
-f:firewall MARK 標記 一個
利用防火牆打標籤:
iptables -t mangle -A PREROUTING -d VIP -p tcp -m multiport --dports 80,443 -j MARK --set-mark [n]
具體使用:
iptables -t mangle -A PREROUTING -d 10.0.0.100 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 5
防火牆生成策略:
[root@www22:01:50~]#iptables -t mangle -nvL
Chain PREROUTING (policy ACCEPT 54 packets, 3564 bytes)
pkts bytes target prot opt in out source destination
0 0 MARK tcp -- * * 0.0.0.0/0 10.0.0.100 multiport dports 80,443 MARK set 0x5
在lvs服務器中打標:
ipvsadm -A -f [n]
實際應用:
ipvsadm -A -f 5 -s rr
ipvsadm -a -f 5 -r 192.168.23.33 -g
ipvsadm -a -f 5 -r 192.168.23.34 -g
持久連接實現方式
每端口持久(PPC):每個端口對應定義爲一個集羣服務,每集羣服務單獨調度
每防火牆標記持久(PFWMC):基於防火牆標記定義集羣服務;可實現將多個端口上的應用統一調度,即所謂的port Affinity
每客戶端持久(PCC):基於0端口(表示所有服務)定義集羣服務,即將客戶端對所有應用的請求都調度至後端主機,必須定義爲持久模式
持久連接:默認360秒
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]]
-p:後面設置時間
[root@www20:11:11network-scripts]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
FWM 5 rr persistent 360
-> 192.168.23.36:0 Route 1 0 2
-> 192.168.23.37:0 Route 1 0 6
客戶端測試:
[root@www20:10:57~]#for i in {1..1000};do curl -k https://10.0.0.100; sleep 1 ;done
hello world
hello world
hello world
自制後端服務器自制腳本:
[root@www22:28:25~]#cat lvs.sh
#!/bin/bash
while true; do
if curl 192.168.23.33 &> /dev/null; then
true
sleep 1
else
ipvsadm -d -f 5 -r 192.168.23.33
exit 2
fi
if curl 192.168.23.34 &> /dev/null; then
true
sleep 1
else
ipvsadm -d -f 5 -r 192.168.23.34
exit 3
fi
done
LVS健康性檢測工具:
ldirectord工具:
檢測方式:
(a) 網絡層檢測,icmp
(b) 傳輸層檢測,端口探測
(c) 應用層檢測,請求某關鍵資源
RS全不用時:backup server, sorry server
包名:
ldirectord-3.9.6-0rc1.1.1.x86_64.rpm
文件:
/etc/ha.d/ldirectord.cf 主配置文件
/usr/share/doc/ldirectord-3.9.6/ldirectord.cf 配置模版
/usr/lib/systemd/system/ldirectord.service 服務
/usr/sbin/ldirectord 主程序
/var/log/ldirectord.log 日誌
/var/run/ldirectord.ldirectord.pid pid文件
主配置文件初始爲空需要cp模板文件!
配置文件詳解:
checktimeout=3
--後端RS主機3秒之內沒有反應就踢出
fallback=127.0.0.1:80
--所有後端主機全部宕機後啓用
autoreload=yes
--自動加載-更改配置無須重啓
logfile="/var/log/ldirectord.log"
--日誌文件
quiescent=no
--後端主機宕機時的處理辦法yes--調整權重爲0--no爲刪除
配置ipvsadm規則:
virtual=10.0.0.100:80 --VIP
real=192.168.23.33:80 gate 1 --RIP
real=192.168.23.34:80 gate 2 --RIP
service=http --服務
scheduler=wrr --算法
#persistent=600 --持久連接
#netmask=255.255.255.255
protocol=tcp --協議
checktype=negotiate
checkport=80 --檢查端口
request="index.html" --檢測方式-看頁面
receive="hello" --出現字樣
virtualhost=www.x.y.z --虛擬主機
啓動服務:
systemctl start ldirectord
如何實現標籤功能:
1 防火牆標記:
iptables -t mangle -A PREROUTING -d 10.0.0.100 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 10
2 配置文件更改:
virtual=10
real=192.168.23.36 gate
real=192.168.23.37 gate
# real=192.168.6.6:80 gate
fallback=127.0.0.1 gate
service=http
scheduler=rr
#persistent=600
#netmask=255.255.255.255
#protocol=tcp
checktype=negotiate
checkport=80
request="index.html"
receive="hello"
virtualhost=www.x.y.z