公司購買的一批雲服務器只帶內網,配置了一個負載均衡器(lb),這批服務器通過lb可以對外提供服務,但是這批服務器不能主動連接外網,例如使用wget下載文件,或者curl訪問ttlsa.com站點。
額外購買一台帶外網的雲服務器,將這台服務器配置成網關服務器,其他服務器網關改為這台服務器,問題得到解決。方法很簡單
網關服務器IP:172.16.0.1
客戶端IP:172.16.0.0/24
1. 開啟forward
1
2
|
# cat /etc/sysctl.conf | grep forw
net.ipv4.ip_forward = 1
|
2. 配置轉發
1
|
# iptables -t nat -A POSTROUTING -s 172.16.0.0/24 -j MASQUERADE
|
轉發網段172.16.0.0/24傳過來的包,你也可以指定特定的ip地址。例如:
1
|
# iptables -t nat -A POSTROUTING -s 172.16.0.10 -j MASQUERADE
|
3. 重啟iptables
1
|
# service iptables restart
|
4. 修改網關
以上三步都在網關服務器上操作,這步在需要連接外網的客戶端操作
1
2
|
# cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep GATEWAY
GATEWAY=172.16.0.1
|
5. 測試
1
2
3
4
5
6
7
8
9
10
|
# ping www.ttlsa.com -c 4
PING www.ttlsa.com (114.215.173.139) 56(84) bytes of data.
64 bytes from ip215.hichina.com (114.215.173.139): icmp_seq=1 ttl=46 time=28.1 ms
64 bytes from ip215.hichina.com (114.215.173.139): icmp_seq=2 ttl=46 time=27.9 ms
64 bytes from ip215.hichina.com (114.215.173.139): icmp_seq=3 ttl=46 time=27.8 ms
64 bytes from ip215.hichina.com (114.215.173.139): icmp_seq=4 ttl=46 time=27.8 ms
--- www.ttlsa.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3032ms
rtt min/avg/max/mdev = 27.815/27.951/28.193/0.220 ms
|
6. 結束
是不是很簡單?搭建一個虛擬機網關IP 172.16.0.1,客戶端IP172.16.0.2。按照上面的步驟完成你的實驗。