LVS NAT模型演示
這里我們刻意將兩個RS的web頁面文件,提供為不同的內容(實際應用中,每個RS頁面的內容要完全相同)
基本配置
[root@web1 ~]# route add default gw 192.168.110.130 --> web1服務器中,默認網關要指向DIP [root@web2 ~]# route add default gw 192.168.110.130 --> web2服務器中,默認網關要指向DIP [root@Directory ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 //開啟路由轉發功能 [root@Directory ~]# sysctl -p //以上修改立即生效
directory配置
[root@Directory ~]# ipvsadm -A -t 10.1.249.43:80:80 -s rr -->添加一個集群服務,調度算法為rr [root@Directory ~]# ipvsadm -L -n -->查看添加的集群服務 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.1.249.43:80 rr --> 成功添加 調度算法為rr [root@Directory ~]# ipvsadm -a -t 10.1.249.43:80 -r 192.168.110.129 -m -->向集群中添加RS [root@Directory ~]# ipvsadm -a -t 10.1.249.43:80 -r 192.168.110.128 -m -->將web1和web2添加到集群中 [root@Directory ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.1.249.43:80 rr -> 192.168.110.128:80 Masq 1 0 0 -->成功添加 -> 192.168.110.129:80 Masq 1 0 0 --> 權重默認為1 masq-->masquerade-->地址偽裝
測試:瀏覽器內輸入10.1.249.43(VIP的地址)
刷新
默認每刷新一次,會出現不同的頁面
因為我們此時的算法為rr 即在web1和web2之間輪詢
命令查看狀態
[root@Directory ~]# ipvsadm -L -nc TCP 00:17 TIME_WAIT 10.1.250.160:55181 10.1.249.43:80 192.168.110.128:80 -->明顯看出在輪詢 TCP 01:55 TIME_WAIT 10.1.250.160:55253 10.1.249.43:80 192.168.110.129:80 TCP 01:55 TIME_WAIT 10.1.250.160:55254 10.1.249.43:80 192.168.110.128:80 TCP 01:55 TIME_WAIT 10.1.250.160:55251 10.1.249.43:80 192.168.110.129:80 ... [root@Directory ~]# ipvsadm -L -n --stats IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 10.1.249.43:80 18 88 87 12095 8353 -> 192.168.110.128:80 9 43 42 5768 4048 --> 匹配到的報文數幾乎一樣 -> 192.168.110.129:80 9 45 45 6327 4305
修改調度算法為wrr測試
[root@Directory ~]# ipvsadm -Z 統計數據清零 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 10.1.249.43:80 0 0 0 0 0 --> 全部清零 -> 192.168.110.128:80 0 0 0 0 0 -> 192.168.110.129:80 0 0 0 0 0 [root@Directory ~]# ipvsadm -E -t 10.1.249.43:80 -s wrr --> 修改調度算法 [root@Directory ~]# ipvsadm -e -t 10.1.249.43:80 -r 192.168.110.129 -m -w 3 --> 將web1的權重修改為3 [root@Directory ~]# ipvsadm -L -n -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.1.249.43:80 wrr -> 192.168.110.128:80 Masq 1 0 1 -> 192.168.110.129:80 Masq 3 0 0 --> 此時web1的權重為3
測試訪問 一定要訪問VIP的地址 此時,平均出現3次web1會出現1次web2
統計數據查看
[root@Directory ~]# ipvsadm -L -n --stats IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 10.1.249.43:80 24 120 117 16393 9859 -> 192.168.110.128:80 6 30 30 4218 2958 -> 192.168.110.129:80 18 90 87 12175 6901 --> 數據幾乎為上面的3倍
查看訪問日志
此時查看web1或者web2中httpd的訪問日志時,一定是CIP [root@web2 ~]# tail /var/log/httpd/access_log --> 查看web2的httpd的訪問日志 10.1.250.160 - - [03/Sep/2016:15:23:43 +0800] "GET / HTTP/1.1" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36" 10.1.250.160 - - [03/Sep/2016:15:23:43 +0800] "GET / HTTP/1.1" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36" 10.1.250.160 - - [03/Sep/2016:15:23:44 +0800] "GET / HTTP/1.1" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36" === 10.1.250.160為我們測試訪問的客戶端地址 ===
原創文章,作者:sixijie,如若轉載,請注明出處:http://www.www58058.com/55735