轉載出處:http://blog.csdn.net/starean/article/details/16860819
html
1. 簡介neutron-l3-agentlinux
OpenStack neutron-l3-agent 主要負責實現網絡三層協議,爲虛擬機完成SNAT,DNAT等地址的轉換與假裝,提供安全彈性隔離的雲網絡環境,安全
下面詳細敘述了OpenStack如何使用iptables鏈與規則完成複雜的neutron-l3-agent 的網絡地址轉換(NAT)功能,虛擬機floating ip與fixed ip綁定的工做原理。網絡
2. iptables 簡介
ssh
2.1 iptables 鏈拓撲結構ide
2.2 iptables 表結構wordpress
Table filter: post
Chain INPUT測試
Chain FORWARDChain OUTPUTui
filter 表用於通常的信息包過濾,它包含 INPUT 、 OUTPUT 和 FORWARD 鏈。
Table nat:
Chain PREROUTING
Chain OUTPUT
Chain POSTROUTING
PREROUTING 鏈由指定信息包一到達防火牆就改變它們的規則所組成,而 POSTROUTING 鏈由指定正當信息包打算離開防火牆時改變它們的規則所組成。
3. iptables command
# 添加一條規則到 INPUT 鏈的末尾,ACCEPT 來自源地址 10.9.1.141 的包
#容許protocol爲TCP 、 UDP 、 ICMP 的包經過
# 從INPUT鏈中刪除掉規則「Drop 到端口80的包」
# 將 INPUT 鏈的缺省規則指定爲 DROP
[root@xianghui-10-9-1-141 ~]# iptables -P INPUT DROP
# 建立一個新鏈new-chain
# 刪除Table filter 中的全部規則
[root@xianghui-10-9-1-141 ~]# iptables -F
# 列出INPUT鏈中的全部規則
[root@xianghui-10-9-1-141 ~]# neutron router-create router1
+--------------------------------------+---------+-----------------------+
| id | name | external_gateway_info |
+--------------------------------------+---------+-----------------------+
|c36b384e-b1f5-45e5-bb4f-c3ed32885142 | router1 | null |
+--------------------------------------+---------+-----------------------+
[root@xianghui-10-9-1-141 ~]# vi /etc/neutron/l3_agent.ini
interface_driver = neutron.agent.linux.interface.OVSInterfaceDriver
# OS is RHEL6.4, not support namespace
use_namespaces = False
# This is done by setting the specific router_id.
router_id = c36b384e-b1f5-45e5-bb4f-c3ed32885142
# Name of bridge used for external network traffic. This should be set to
# empty value for the linux bridge
external_network_bridge = br-eth1
[root@xianghui-10-9-1-141 ~]# service neutron-l3-agent restart
啓用轉發功能
6. neutron floating ip 與 fixed ip 的轉換
源地址轉換(SNAT)
[root@xianghui-10-9-1-141 ~]# iptables -t nat -Aneutron-l3-agent-float-snat -s 70.0.0.6/32-j SNAT --to-source 192.168.12.100
目的地址轉換(DNAT)
[root@xianghui-10-9-1-141 ~]# iptables -t nat -Aneutron-l3-agent-PREROUTING -d 192.168.12.100/32-j DNAT --to-destination 70.0.0.6
測試:(從guest 70.0.0.11上ping 192.168.12.100, 結果被轉發到70.0.0.6的guest上)
[root@xianghui-10-9-1-141 ~]# ssh ec2-user@70.0.0.11
[ec2-user@wordpress-test-wikidatabase-jevfsmkbakch ~]$ ping 192.168.12.100
PING 192.168.12.100 (192.168.12.100) 56(84) bytes of data.
64 bytes from 70.0.0.6: icmp_req=1 ttl=64 time=3.09 ms
64 bytes from 70.0.0.6: icmp_req=2 ttl=64 time=0.281 ms
64 bytes from 70.0.0.6: icmp_req=3 ttl=64 time=0.151 ms
將規則neutron-l3-agent-float-snat加到POSTROUTING規則以後,從70.0.0.6發出的包被假裝成來自192.168.12.16,藉此掩蓋源地址
[root@xianghui-10-9-1-141 ~]# iptables -t nat -A POSTROUTING -j neutron-l3-agent-float-snat
[ec2-user@wordpress-test-wikidatabase-jevfsmkbakch ~]$ ping 192.168.12.100
PING 192.168.12.100 (192.168.12.100) 56(84) bytes of data.
64 bytes from 192.168.12.100: icmp_req=1 ttl=63 time=2.47 ms
64 bytes from 192.168.12.100: icmp_req=2 ttl=63 time=0.199 ms
64 bytes from 192.168.12.100: icmp_req=3 ttl=63 time=0.251 ms
7. 實例分析(ALL-IN-ONE)
7.1 虛擬機的網絡拓撲
7.2 虛擬機之間用floating ip ping通
# ping 192.168.12.100(70.0.0.6) from 70.0.0.11
# s:70.0.0.11 d:70.0.0.6
# prerouting -> forward -> postrouting
[root@xianghui-10-9-1-141 ~]# iptables -A neutron-l3-agent-FORWARD -d 70.0.0.11/32 -j ACCEPT
[root@xianghui-10-9-1-141 ~]# iptables -A neutron-l3-agent-FORWARD -d 70.0.0.6/32 -j ACCEPT
[root@xianghui-10-9-1-141 ~]# iptables -t nat -A neutron-l3-agent-PREROUTING -d 192.168.12.100/32 -j DNAT --to-destination 70.0.0.6
7.3 虛擬機主機ping通虛擬機的floating ip
-A OUTPUT -j neutron-l3-agent-OUTPUT
[root@xianghui-10-9-1-141 ~]# iptables -A neutron-l3-agent-OUTPUT -d 192.168.12.100/32 -j DNAT --to-destination 70.0.0.6