配置LVS-NAT模式
阿新 • • 發佈:2018-12-14
1、環境
三臺伺服器,一臺作為 director,兩臺作為 real server,director 有一個外網網絡卡(192.168.0.186) 和一個內網ip(172.16.0.8),兩個 real server 上只有內網 ip (172.16.0.20) 和 (172.16.0.21),並且需要把兩個 real server 的內網閘道器設定為 director 的內網 ip(172.16.0.8)。
注意,切記一定要在兩臺 RS 上設定閘道器的 IP 為 director 的內網 IP。
vmware下新增網絡卡步驟
1、
後邊的real server伺服器也選擇同一個虛擬網路
2、配置ip地址
編輯後的ifcfg-ens37
重啟網絡卡
2、安裝軟體
兩個 real server 上都安裝 httpd 服務
# yum install -y httpd
Director 上安裝 ipvsadm
# yum install -y ipvsadm
3、配置Director上配置路由轉發功能
[[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward
0
[[email protected] ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward=1 #新增這一行
[[email protected] ~]#sysctl -p
[[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward
1
如上驗證成功
4、配置nat模式實現
[[email protected] ~]# ipvsadm -A -t 192.168.0.186:80 -s rr #新增一臺新的虛擬伺服器,並以rr的演算法排程
[[email protected] ~]# ipvsadm -a -t 192.168.0.186:80 -r 172.16.0.20:80 -m -w 1 #向虛擬服務上新增一臺真實伺服器並指向80埠
[[email protected] ~]# ipvsadm -a -t 192.168.0.186:80 -r 172.16.0.21:80 -m -w 1
[[email protected] ~]# ipvsadm -L #檢視ipvsadm設定的規則
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http rr
-> 172.16.0.20:http Masq 1 0 1
-> 172.16.0.21:http Masq 1 0 1
[[email protected] ~]# ipvsadm -S >/etc/sysconfig/ipvsadm #永久儲存ipvs規則,如果不儲存,重啟後就沒有了
[[email protected] ~]# cat /etc/sysconfig/ipvsadm
-A -t localhost.localdomain:http -s rr
-a -t localhost.localdomain:http -r 172.16.0.20:http -m -w 1
-a -t localhost.localdomain:http -r 172.16.0.21:http -m -w 1
5、測試LVS的效果
通過瀏覽器測試2臺機器上的web內容 http://192.168.0.186 。為了區分開,可以把httpd的預設頁修改一下:
分別對應在 RS1、RS2 上執行
# echo "this is node1" >/var/www/html/index.html
# echo "this is node2" >/var/www/html/index.html
測試
[[email protected] ~]# while true;do curl http://192.168.0.186;sleep 2;done
<h1>this is node2</h1>
<h1>this is node1</h1>
<h1>this is node2</h1>
<h1>this is node1</h1>
<h1>this is node2</h1>
<h1>this is node1</h1>
<h1>this is node2</h1>
測試成功!