安裝包組-ipvsadm
生成文件解析:
Unit File: ipvsadm.service
主程序:/usr/sbin/ipvsadm
規則保存工具:/usr/sbin/ipvsadm-save
規則重載工具:/usr/sbin/ipvsadm-restore
配置文件:/etc/sysconfig/ipvsadm-config
基本命令介紹:
基本命令:
增加服務:
ipvsadm -A -t|u|f service-address [-s 算法] [-p 超時時長]
具體使用:
ipvsadm -A -t 172.20.23.32:80 -s rr
修改服務:
ipvsadm -E -t|u|f service-address [-s 算法] [-p 超時時長]
刪除服務:
ipvsadm -D -t|u|f service-address
service-address格式:
-t:TCP協議的端口 VIP:TCP_PORT
-u:UDP協議端口 VIP:UDP_PORT
-f:firewall MARK 標記 一個
管理集羣上的RS:
ipvsadm -a|e -t|u|f [集羣服務IP地址:port] -r [後端主機] [-g|i|m] [-w weight]
具體使用:
ipvsadm -a -t 172.20.23.32:80 -r 192.168.23.33 -m
ipvsadm -a -t 172.20.23.32:80 -r 192.168.23.34 -m
lvs類型指定:
-g: gateway, dr類型,默認
-i: ipip, tun類型
-m: masquerade, nat類型
service-address格式:
RIP:port 省略端口,就不做端口映射
-w [n]:指定權重
清空定義的所有內容:ipvsadm –C
清空計數器:ipvsadm -Z [-t|u|f service-address]
查看:ipvsadm -L|l [options]
--numeric, -n:以數字形式輸出地址和端口號
--exact:擴展信息,精確值
--connection,-c:當前IPVS連接輸出
--stats:統計信息
--rate :輸出速率信息
NAT模型應用:
注意:
1 製作集羣時首要原則要保證集羣內各個節點的時時間要保持一致!
2 NAT模型需要開啓端口轉發功能:
一次性開啓:
echo 1 > /proc/sys/net/ipv4/ip_forward
永久有效方式:
編輯配置文件:
/etc/sysctl.conf
net.ipv4.ip_forward=1
環境描述:
客戶端主機:172.20.23.20
LVS服務器:
VIP--172.20.23.34
DIP--192.168.23.34
後端服務器:apache
RS1 --192.168.23.36
RS2 --192.168.23.37
配置LVS規則:
[root@KEEP213:21:49~]#cat /proc/sys/net/ipv4/ip_forward
1
[root@KEEP213:22:30~]#ipvsadm -A -t 172.20.23.34:80 -s rr
[root@KEEP213:22:37~]#ipvsadm -a -t 172.20.23.34:80 -r 192.168.23.36 -m
[root@KEEP213:23:05~]#ipvsadm -a -t 172.20.23.34:80 -r 192.168.23.37 -m
[root@KEEP213:23:09~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.20.23.20:80 rr
-> 192.168.23.36:80 Masq 1 0 0
-> 192.168.23.37:80 Masq 1 0 0
配置後端服務器網關指向LVS服務器:
route add default gw 192.168.23.34
客戶端測試:
網關指向LVS服務器!當然實際中會有路由器隔開,那就
把網關指向最近路由器接口即可!
測試結果:
[root@www13:30:57network-scripts]#for i in {1..1000};do curl 172.20.23.34; sleep 1 ; done
hello linux
hello world
更改調度規則實現權重:
[root@KEEP213:30:50~]#ipvsadm -E -t 172.20.23.34:80 -s wrr
[root@KEEP213:34:49~]#ipvsadm -e -t 172.20.23.34:80 -r 192.168.23.37 -m -w 3
[root@KEEP213:35:17~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.20.23.34:80 wrr
-> 192.168.23.36:80 Masq 1 0 0
-> 192.168.23.37:80
客戶端測試:
[root@www13:32:36network-scripts]#for i in {1..1000};do curl 172.20.23.34; sleep 1 ; done
hello linux
hello world
hello world
hello world
https集羣調度方式:
LVS服務器配置:
[root@KEEP213:35:23~]#ipvsadm -A -t 172.20.23.34:443 -s rr
[root@KEEP213:37:41~]#ipvsadm -a -t 172.20.23.34:443 -r 192.168.23.36 -m
[root@KEEP213:37:58~]#ipvsadm -a -t 172.20.23.34:443 -r 192.168.23.37 -m
[root@KEEP213:38:02~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.20.23.34:80 wrr
-> 192.168.23.36:80 Masq 1 0 0
-> 192.168.23.37:80 Masq 3 0 0
TCP 172.20.23.34:443 rr
-> 192.168.23.36:443 Masq 1 0 0
-> 192.168.23.37:443
測試方式:
[root@www13:41:14network-scripts]#for i in {1..1000};do curl -k https://172.20.23.34:443; sleep 1 ; done
hello linux
hello world
-k:忽略證書檢測
注意:
1 這裏僅僅爲了測試沒有申請證書,實際中後端服務器
需要配置ssl證書加密!
實現根據原IP綁定會話:sh
[root@KEEP213:48:26~]#ipvsadm -A -t 172.20.23.34:80 -s sh
[root@KEEP213:48:33~]#ipvsadm -a -t 172.20.23.34:80 -r 192.168.23.36 -m
[root@KEEP213:48:46~]#ipvsadm -a -t 172.20.23.34:80 -r 192.168.23.37 -m
[root@KEEP213:48:50~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.20.23.34:80 sh
-> 192.168.23.36:80 Masq 1 0 0
-> 192.168.23.37:80 Masq 1 0 0
客戶端測試:
[root@www13:47:16network-scripts]#for i in {1..1000};do curl http://172.20.23.34; sleep 1 ; done
hello world
hello world
刪除和保存策略:
刪除LVS策略:
ipvsadm -D -t 172.20.23.32:443
.......................................
清空加保存:
保存策略:
ipvsadm-save -n > LVS.backup
清空策略:
ipvsadm -C
恢復策略:
ipvsadm-restore < LVS.backup
保存的特殊配置文件:可做unix腳本啓動服務
/etc/sysconfig/ipvsadm
手動將使用ipvsadm-save命令保存到這個文件!
這樣下次啓動的時候可以使用systemctl start ipvsadm 命令加載
這個文件中的LVS策略!
可設置開機自動啓動:
[root@www10:39:14~]#systemctl enable ipvsadm
Created symlink from /etc/systemd/system/multi-user.target.wants/ipvsadm.service to /usr/lib/systemd/system/ipvsadm.service.
實現跨網段NAT:
環境描述:
客戶端:172.20.23.20/16
LVS服務器:
VIP:172.20.23.34/16
DIP:10.0.0.100/24
路由器:
eth0 --10.0.0.1/24
eth1 --192.168.23.10/24
後端服務器:
RS1 --192.168.23.36
RS2 --192.168.23.37
網關及路由設置:
客戶端:
[root@www15:34:35certs]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.20.23.34 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@KEEP215:31:54network-scripts]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
172.20.0.0 0.0.0.0 255.255.0.0 U 101 0 0 eth1
192.168.23.0 10.0.0.1 255.255.255.0 UG 0 0 0 eth0
路由器:
[root@www15:35:29~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.23.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.20.0.0 10.0.0.100 255.255.0.0 UG 0 0 0 eth0
後端服務器:
[root@apache115:35:58network-scripts]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.23.10 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
修改LVS服務器規則:
[root@KEEP215:31:29network-scripts]#ipvsadm -E -t 172.20.23.34:80 -s wrr
[root@KEEP215:31:35network-scripts]#ipvsadm -e -t 172.20.23.34:80 -r 192.168.23.36 -m
[root@KEEP215:31:50network-scripts]#ipvsadm -e -t 172.20.23.34:80 -r 192.168.23.37 -m
客戶端測試:
[root@www15:36:16certs]#for i in {1..1000};do curl http://172.20.23.34; sleep 1 ; done
hello world
hello linux