LVS是4(傳輸)層羣集解決方案NAT,DR,TUN,FULLNAT四種模型本文主要介紹NAT模型html
NAT模型的特性算法
RealServer應該使用私有IP地址,節約IP地址;後端
RealServer的網關應該指向DirectorIP;bash
RIP和DIP應該在同一個網段內;服務器
進出的報文都得通過DirectoryServer,在高負載下,DirectoryServer會成爲系統性能瓶頸,因此可能只適用於中小企業;網絡
支持端口映射;ssh
RealServer能夠使用任意OS:curl
ipvsadm 用法基本介紹tcp
命令選項解釋:
ide
有兩種命令選項格式,長的和短的,具備相同的意思。在實際使用時,兩種均可
以。
-A --add-service 在內核的虛擬服務器表中添加一條新的虛擬服務器記錄。也
就是增長一臺新的虛擬服務器。
-E --edit-service 編輯內核虛擬服務器表中的一條虛擬服務器記錄。
-D --delete-service 刪除內核虛擬服務器表中的一條虛擬服務器記錄。
-C --clear 清除內核虛擬服務器表中的全部記錄。
-R --restore 恢復虛擬服務器規則
-S --save 保存虛擬服務器規則,輸出爲-R 選項可讀的格式
-a --add-server 在內核虛擬服務器表的一條記錄裏添加一條新的真實服務器
記錄。也就是在一個虛擬服務器中增長一臺新的真實服務器
-e --edit-server 編輯一條虛擬服務器記錄中的某條真實服務器記錄
-d --delete-server 刪除一條虛擬服務器記錄中的某條真實服務器記錄
-L|-l --list 顯示內核虛擬服務器表
-Z --zero 虛擬服務表計數器清零(清空當前的鏈接數量等)
--set tcp tcpfin udp 設置鏈接超時值
--start-daemon 啓動同步守護進程。他後面能夠是master 或backup,用來講
明LVS Router 是master 或是backup。在這個功能上也能夠採用keepalived 的
VRRP 功能。
--stop-daemon 中止同步守護進程
-h --help 顯示幫助信息
其餘的選項:
-t --tcp-service service-address 說明虛擬服務器提供的是tcp 的服務
[vip:port] or [real-server-ip:port]
-u --udp-service service-address 說明虛擬服務器提供的是udp 的服務
[vip:port] or [real-server-ip:port]
-f --fwmark-service fwmark 說明是通過iptables 標記過的服務類型。
-s --scheduler scheduler 使用的調度算法,有這樣幾個選項
rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
默認的調度算法是: wlc.
-p --persistent [timeout] 持久穩固的服務。這個選項的意思是來自同一個客
戶的屢次請求,將被同一臺真實的服務器處理。timeout 的默認值爲300 秒。
-M --netmask netmask persistent granularity mask
-r --real-server server-address 真實的服務器[Real-Server:port]
-g --gatewaying 指定LVS 的工做模式爲直接路由模式(也是LVS 默認的模式)
-i --ipip 指定LVS 的工做模式爲隧道模式
-m --masquerading 指定LVS 的工做模式爲NAT 模式
-w --weight weight 真實服務器的權值
--mcast-interface interface 指定組播的同步接口
-c --connection 顯示LVS 目前的鏈接 如:ipvsadm -L -c
--timeout 顯示tcp tcpfin udp 的timeout 值 如:ipvsadm -L --timeout
--daemon 顯示同步守護進程狀態
--stats 顯示統計信息
--rate 顯示速率信息
--sort 對虛擬服務器和真實服務器排序輸出
--numeric -n 輸出IP 地址和端口的數字形式
ipvs採用以下10種調度算法
靜態算法
輪叫調度rr(Round-Robin Scheduling)
加權輪叫調度wrr(Weighted Round-Robin Scheduling)
源地址散列調度sh(Source Hashing Scheduling)
目標地址散列調度dh(Destination Hashing Scheduling)
動態算法
最小鏈接調度lc(Least-Connection Scheduling)
加權最小鏈接調度wlc(Weighted Least-Connection Scheduling)
基於局部性的最少連接lblc(Locality-Based Least Connections Scheduling)
帶複製的基於局部性最少連接lblcr(LocalityBased Least Connections with Replication Scheduling)
最短預期延時調度sed(Shortest Expected Delay Scheduling)
不排隊調度nq(Never Queue Scheduling)
使用方法
[root@www ~]# ipvsadm -A -t VirtualIP:PROT [root@www ~]# ipvsadm -a -t VirtualIP:PROT -r realserverIP -m [root@www ~]# sysctl -w net.ipv4.ip_forward=1 net.ipv4.ip_forward = 1
1)後端RealServer服務器配置
RealServer 1
[root@localhost ~]# ifconfig eth0 172.15.1.2/24 [root@localhost ~]# route add default gw 172.15.1.254
安裝Apache服務
[root@localhost ~]# yum install httpd [root@localhost ~]# cd /var/www/html/ [root@localhost html]# touch index.html [root@localhost html]# echo "ipvsadm 2" > index.html
RealServer 2
[root@localhost ~]# ifconfig eth0 172.15.1.3/24 [root@localhost ~]# route add default gw 172.15.1.254 [root@localhost ~]# yum install httpd
安裝Apache服務
[root@localhost ~]# yum install httpd [root@localhost ~]# cd /var/www/html/ [root@localhost html]# touch index.html [root@localhost html]# echo "ipvsadm 3" > index.html
驗證兩Realserver httpd服務配置成功
[root@CentOS ~]# curl -i 172.15.1.2 HTTP/1.1 200 OK Date: Sun, 16 Nov 2014 10:57:09 GMT Server: Apache/2.2.15 (CentOS) Last-Modified: Sun, 16 Nov 2014 10:45:41 GMT ETag: "6154f-a-507f79036a1f3" Accept-Ranges: bytes Content-Length: 10 Connection: close Content-Type: text/html; charset=UTF-8 ipvsadm 2 [root@CentOS ~]# curl -i 172.15.1.3 HTTP/1.1 200 OK Date: Sun, 16 Nov 2014 10:59:43 GMT Server: Apache/2.2.15 (CentOS) Last-Modified: Sat, 15 Nov 2014 12:05:49 GMT ETag: "2735f-a-507e490f7c682" Accept-Ranges: bytes Content-Length: 10 Connection: close Content-Type: text/html; charset=UTF-8 ipvsadm 3
2)Director Server配置
將VirtualIP配置在eth0的別名上
[root@www ~]# ifconfig eth0:0 192.168.1.250/24 [root@www ~]# ifconfig eth2 172.15.1.254/24
打開網絡地址轉發
[root@www ~]# sysctl -w net.ipv4.ip_forward=1 net.ipv4.ip_forward = 1
3)配置羣集服務
安裝ipvsadm
[root@www ~]# yum install ipvsadm ipvsadm -A -t 192.168.1.250:80 -s rr 若是不指定調度算法默認是wlc ipvsadm -a -t 192.168.1.250:80 -r 172.15.1.2 -m ipvsadm -a -t 192.168.1.250:80 -r 172.15.1.3 -m
4)驗證
[root@www ~]# curl -i 192.168.1.250 HTTP/1.1 200 OK Date: Sun, 16 Nov 2014 11:08:57 GMT Server: Apache/2.2.15 (CentOS) Last-Modified: Sun, 16 Nov 2014 10:45:41 GMT ETag: "6154f-a-507f79036a1f3" Accept-Ranges: bytes Content-Length: 10 Connection: close Content-Type: text/html; charset=UTF-8 ipvsadm 2
5)查看羣集狀態
[root@www ~]# ipvsadm -L -n --stats 查看統計信息 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 192.168.1.250:22 3 983 715 71154 57282 -> 172.15.1.2:22 2 611 433 44165 35533 -> 172.15.1.3:22 1 372 282 26989 21749 TCP 192.168.1.250:80 138 624 418 69800 44372 -> 172.15.1.2:80 69 319 215 35834 22456 -> 172.15.1.3:80 69 305 203 33966 21916 [root@www ~]# ipvsadm -L -n --rate 查看訪問速率 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port CPS InPPS OutPPS InBPS OutBPS -> RemoteAddress:Port TCP 192.168.1.250:22 0 0 0 2 1 -> 172.15.1.2:22 0 0 0 2 1 -> 172.15.1.3:22 0 0 0 0 0 TCP 192.168.1.250:80 0 0 0 0 0 -> 172.15.1.2:80 0 0 0 0 0 -> 172.15.1.3:80 0 0 0 0 0
刪除羣集
[root@www ~]# ipvsadm -D -t 192.168.1.250:22
保存當前配置
[root@www ~]# service ipvsadm save ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ] [root@www ~]# cat /etc/sysconfig/ipvsadm -A -t 192.168.1.250:80 -s rr -a -t 192.168.1.250:80 -r 172.15.1.2:80 -m -w 1 -a -t 192.168.1.250:80 -r 172.15.1.3:80 -m -w 1
將當前配置定義到其餘位置
[root@www ~]# ipvsadm -S > ./ipvsadm.txt [root@www ~]# cat ./ipvsadm.txt -A -t 192.168.1.250:ssh -s wlc -a -t 192.168.1.250:ssh -r 172-15-1-2.lightspeed.stlsmo.sbcglobal.net:ssh -m -w 1 -a -t 192.168.1.250:ssh -r 172-15-1-3.lightspeed.stlsmo.sbcglobal.net:ssh -m -w 1 -A -t 192.168.1.250:http -s rr -a -t 192.168.1.250:http -r 172-15-1-2.lightspeed.stlsmo.sbcglobal.net:http -m -w 1 -a -t 192.168.1.250:http -r 172-15-1-3.lightspeed.stlsmo.sbcglobal.net:http -m -w 1
清除當前全部規則
[root@www ~]# ipvsadm -C [root@www ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
恢復規則
[root@www ~]# ipvsadm -R < ./ipvsadm.txt [root@www ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.250:22 wlc -> 172.15.1.2:22 Masq 1 1 0 -> 172.15.1.3:22 Masq 1 2 0 TCP 192.168.1.250:80 rr -> 172.15.1.2:80 Masq 1 0 0 -> 172.15.1.3:80 Masq 1 0 0
修改調度算法
[root@www ~]# ipvsadm -E -t 192.168.1.250:80 -s rr
NAT模型的LVS有很大的侷限性因此企業裏面應該會用DR模型。