1. 簡介neutron-l3-agent
OpenStack neutron-l3-agent 主要負責實作網絡三層協定,為虛拟機完成SNAT,DNAT等位址的轉換與僞裝,提供安全彈性隔離的雲網絡環境,
下面詳細叙述了OpenStack如何使用iptables鍊與規則完成複雜的neutron-l3-agent 的網絡位址轉換(NAT)功能,虛拟機floating ip與fixed ip綁定的工作原理。
2. iptables 簡介
2.1 iptables 鍊拓撲結構
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2Lc1TPnJGaW12YoJ1MjZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zN4EjMxQTMwEjNwITMzEDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
2.2 iptables 表結構
Table filter:
Chain INPUT
Chain FORWARD
Chain OUTPUT
filter 表用于一般的資訊包過濾,它包含 INPUT 、 OUTPUT 和 FORWARD 鍊。
Table nat:
Chain PREROUTING
Chain OUTPUT
Chain POSTROUTING
PREROUTING 鍊由指定資訊包一到達防火牆就改變它們的規則所組成,而 POSTROUTING 鍊由指定正當資訊包打算離開防火牆時改變它們的規則所組成。
3. iptables command
# 添加一條規則到 INPUT 鍊的末尾,ACCEPT 來自源位址 10.9.1.141 的包
[[email protected] ~]# iptables -A INPUT -s 10.9.1.141 -j ACCEPT
#允許protocol為TCP 、 UDP 、 ICMP 的包通過
[[email protected] ~]# iptables -A INPUT -p TCP, UDP
# 從INPUT鍊中删除掉規則“Drop 到端口80的包”
[[email protected] ~]# iptables -D INPUT --dport 80 -j DROP
# 将 INPUT 鍊的預設規則指定為 DROP
[[email protected] ~]# iptables -P INPUT DROP
# 建立一個新鍊new-chain
[[email protected] ~]# iptables -N new-chain
# 删除Table filter 中的所有規則
[[email protected] ~]# iptables -F
# 列出INPUT鍊中的所有規則
[[email protected] ~]# iptables -L INPUT
# 删除鍊
[[email protected] ~]# iptables -X
4. 配置neutron-l3-agent
[[email protected] ~]# neutron router-create router1
+--------------------------------------+---------+-----------------------+
| id | name | external_gateway_info |
+--------------------------------------+---------+-----------------------+
|c36b384e-b1f5-45e5-bb4f-c3ed32885142 | router1 | null |
+--------------------------------------+---------+-----------------------+
[[email protected] ~]# 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
[[email protected] ~]# service neutron-l3-agent restart
啟用轉發功能
[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
5. neutron 利用iptables 實作 NAT 原理
iptables 中neutron l3 agent自定義的鍊:
neutron-l3-agent-PREROUTING
neutron-l3-agent-OUTPUT
neutron-l3-agent-POSTROUTING
建立外部網絡(配置設定floatingip)
[[email protected] ~]# neutron net-create ext_net --router:external=True
+---------------------------+--------------------------------------+
| Field | Value |
+---------------------------+--------------------------------------+
| admin_state_up | True |
| id | 2d72d81b-cf09-459e-87fb-a50fa0e8730a |
| name | ext_net |
| provider:network_type | vlan |
| provider:physical_network | physnet1 |
| provider:segmentation_id | 1000 |
| router:external | True |
| shared | False |
| status | ACTIVE |
| subnets | e1932e73-1e4b-4f87-9ebf-758a757e20ef |
| tenant_id | b21a96e16c3c438caab4a27a1f58a5b8 |
+---------------------------+--------------------------------------+
[[email protected] cfn]# subnet-create ext_net --allocation-pool start=192.168.12.10,end=192.168.12.50 --gateway 192.168.12.1 192.168.12.0/24 --enable_dhcp=False
+------------------+----------------------------------------------------+
| Field | Value |
+------------------+----------------------------------------------------+
| allocation_pools | {"start": "192.168.12.10", "end": "192.168.12.50"} |
| cidr | 192.168.12.0/24 |
| dns_nameservers | |
| enable_dhcp | False |
| gateway_ip | 192.168.12.1 |
| host_routes | |
| id | e1932e73-1e4b-4f87-9ebf-758a757e20ef |
| ip_version | 4 |
| name | |
| network_id | 2d72d81b-cf09-459e-87fb-a50fa0e8730a |
| tenant_id | b21a96e16c3c438caab4a27a1f58a5b8 |
+------------------+----------------------------------------------------+
建立内部網絡(配置設定fixedip)
[[email protected] cfn]# neutron net-create vlan-70 --provider:network_type vlan --provider:physical_network physnet1 --provider:segmentation_id 16
+---------------------------+--------------------------------------+
| Field | Value |
+---------------------------+--------------------------------------+
| admin_state_up | True |
| id | 793a95b7-cf1f-4bde-b7b8-5a9a2e552fae |
| name | vlan-70 |
| provider:network_type | vlan |
| provider:physical_network | physnet1 |
| provider:segmentation_id | 16 |
| router:external | False |
| shared | False |
| status | ACTIVE |
| subnets | f542941d-5d53-45e4-85d0-944e030c2bcc |
| tenant_id | b21a96e16c3c438caab4a27a1f58a5b8 |
+---------------------------+--------------------------------------+
[[email protected] cfn]# neutron subnet-create vlan-70 70.0.0.0/24
+------------------+--------------------------------------------+
| Field | Value |
+------------------+--------------------------------------------+
| allocation_pools | {"start": "70.0.0.2", "end": "70.0.0.254"} |
| cidr | 70.0.0.0/24 |
| dns_nameservers | |
| enable_dhcp | True |
| gateway_ip | 70.0.0.1 |
| host_routes | |
| id | f542941d-5d53-45e4-85d0-944e030c2bcc |
| ip_version | 4 |
| name | |
| network_id | 793a95b7-cf1f-4bde-b7b8-5a9a2e552fae |
| tenant_id | b21a96e16c3c438caab4a27a1f58a5b8 |
+------------------+--------------------------------------------+
[[email protected] cfn]# neutron net-list
+--------------------------------------+---------+------------------------------------------------------+
| id | name | subnets |
+--------------------------------------+---------+------------------------------------------------------+
| 2d72d81b-cf09-459e-87fb-a50fa0e8730a | ext_net | e1932e73-1e4b-4f87-9ebf-758a757e20ef 192.168.12.0/24 |
| 793a95b7-cf1f-4bde-b7b8-5a9a2e552fae | vlan-70 | f542941d-5d53-45e4-85d0-944e030c2bcc 70.0.0.0/24 |
+--------------------------------------+---------+------------------------------------------------------+
綁定内外網到router1
# neutron router-gateway-set $ROUTER_ID $EXTERNAL_NETWORK_ID
[[email protected] cfn]# neutron router-gateway-set 06d85a01-fc42-4cde-a0f1-377f2f394a64 2d72d81b-cf09-459e-87fb-a50fa0e8730a
# neutron router-interface-add $ROUTER_ID $SUBNET_ID
[[email protected] cfn]# neutron router-interface-add 06d85a01-fc42-4cde-a0f1-377f2f394a64 f542941d-5d53-45e4-85d0-944e030c2bcc
經過上面的步驟後neutron-l3-agent會加入下列規則到iptables:
-A PREROUTING -j neutron-l3-agent-PREROUTING
-A POSTROUTING -j neutron-l3-agent-POSTROUTING
-A POSTROUTING -j neutron-postrouting-bottom
-A OUTPUT -j neutron-l3-agent-OUTPUT
-A neutron-l3-agent-snat -j neutron-l3-agent-float-snat
-A neutron-l3-agent-snat -s 70.0.0.0/24 -j SNAT --to-source 192.168.12.10
-A neutron-postrouting-bottom -j neutron-l3-agent-snat
建立floating ip(192.168.12.11)并綁定到vm的fixed ip(選擇70.0.0.3):
[[email protected] ~]# neutron floatingip-create 2d72d81b-cf09-459e-87fb-a50fa0e8730a
Created a new floatingip:
+---------------------+--------------------------------------+
| Field | Value |
+---------------------+--------------------------------------+
| fixed_ip_address | |
| floating_ip_address | 192.168.12.11 |
| floating_network_id | 2d72d81b-cf09-459e-87fb-a50fa0e8730a |
| id | f8b48ab7-ea51-4f29-bc84-0ab179808dbb |
| port_id | |
| router_id | |
| tenant_id | adc4e7a4effa44ffa3c6e48dd5a8555a |
+---------------------+--------------------------------------+
找出想要被綁定的fixed ip 的port id
[[email protected] ~]# neutron port-list
+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+
| id | name | mac_address | fixed_ips |
+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+
| 0d06055b-2f31-4d8e-b8da-e048d76a07cc | | fa:16:3e:d7:f4:19 | {"subnet_id": "5c62752f-27ba-4d38-9702-2ca17ec2741d", "ip_address": "70.0.0.3"} |
+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+
[[email protected] ~]# neutron floatingip-associate f8b48ab7-ea51-4f29-bc84-0ab179808dbb0d06055b-2f31-4d8e-b8da-e048d76a07cc
Associated floatingip f8b48ab7-ea51-4f29-bc84-0ab179808dbb
[[email protected] ~]# neutron floatingip-list
+--------------------------------------+------------------+---------------------+--------------------------------------+
| id | fixed_ip_address | floating_ip_address | port_id |
+--------------------------------------+------------------+---------------------+--------------------------------------+
| f8b48ab7-ea51-4f29-bc84-0ab179808dbb | 70.0.0.3 | 192.168.12.11 | b0797fe6-b799-41ea-86d0-9d9bfa0b2eb9 |
+--------------------------------------+------------------+---------------------+--------------------------------------+
經過前面步驟後,iptables會多出下面的規則, 所有目标ip是192.168.12.11的包都會被轉發到ip 70.0.0.3的guest上
-A neutron-l3-agent-OUTPUT -d 192.168.12.11/32 -j DNAT --to-destination 70.0.0.3
-A neutron-l3-agent-PREROUTING -d 192.168.12.11/32 -j DNAT --to-destination 70.0.0.3
-A neutron-l3-agent-float-snat -s 70.0.0.3/32 -j SNAT --to-source 192.168.12.11
6. neutron floating ip 與 fixed ip 的轉換
源位址轉換(SNAT)
[[email protected] ~]# iptables -t nat -Aneutron-l3-agent-float-snat -s 70.0.0.6/32-j SNAT --to-source 192.168.12.100
目的位址轉換(DNAT)
[[email protected] ~]# 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上)
[[email protected] ~]# ssh [email protected]
[[email protected] ~]$ 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,借此掩蓋源位址
[[email protected] ~]# iptables -t nat -A POSTROUTING -j neutron-l3-agent-float-snat
[[email protected] ~]$ 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
[[email protected] ~]# iptables -A neutron-l3-agent-FORWARD -d 70.0.0.11/32 -j ACCEPT
[[email protected] ~]# iptables -A neutron-l3-agent-FORWARD -d 70.0.0.6/32 -j ACCEPT
[[email protected] ~]# 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
[[email protected] ~]# iptables -A neutron-l3-agent-OUTPUT -d 192.168.12.100/32 -j DNAT --to-destination 70.0.0.6