OpenStack neutron floatingips 與 iptables 深入分析

轉載出處:http://blog.csdn.net/starean/article/details/16860819


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 鏈拓撲結構

125958677.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 的包

[root@xianghui-10-9-1-141 ~]# iptables -A INPUT -s 10.9.1.141  -j ACCEPT  


#允許protocol爲TCP 、 UDP 、 ICMP 的包通過

[root@xianghui-10-9-1-141 ~]# iptables -A INPUT -p TCP, UDP  


# 從INPUT鏈中刪除掉規則“Drop 到端口80的包”

[root@xianghui-10-9-1-141 ~]# iptables -D INPUT --dport 80 -j DROP  


# 將 INPUT 鏈的缺省規則指定爲 DROP

[root@xianghui-10-9-1-141 ~]# iptables -P INPUT DROP  


# 創建一個新鏈new-chain

[root@xianghui-10-9-1-141 ~]# iptables -N new-chain  


# 刪除Table filter 中的所有規則

[root@xianghui-10-9-1-141 ~]# iptables -F  


# 列出INPUT鏈中的所有規則

[root@xianghui-10-9-1-141 ~]# iptables -L INPUT  

# 刪除鏈
[root@xianghui-10-9-1-141 ~]# iptables -X  


4. 配置neutron-l3-agent

  1. [root@xianghui-10-9-1-141 ~]# neutron router-create router1  

  2. +--------------------------------------+---------+-----------------------+  

  3. | id                                   | name    | external_gateway_info |  

  4. +--------------------------------------+---------+-----------------------+  

  5. |c36b384e-b1f5-45e5-bb4f-c3ed32885142 | router1 | null |  

  6. +--------------------------------------+---------+-----------------------+  


  1. [root@xianghui-10-9-1-141 ~]# vi /etc/neutron/l3_agent.ini  

  2. interface_driver = neutron.agent.linux.interface.OVSInterfaceDriver  

  3. # OS is RHEL6.4, not support namespace  

  4. use_namespaces = False  

  5. # This is done by setting the specific router_id.  

  6. router_id = c36b384e-b1f5-45e5-bb4f-c3ed32885142  

  7. # Name of bridge used for external network traffic. This should be set to  

  8. # empty value for the linux bridge  

  9. external_network_bridge = br-eth1  


  1. [root@xianghui-10-9-1-141 ~]# service neutron-l3-agent restart  

啓用轉發功能

[root@xianghui-10-9-1-141 ~]#  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)

  1. [root@xianghui-10-9-1-141 ~]#  neutron net-create ext_net --router:external=True  

  2. +---------------------------+--------------------------------------+  

  3. | Field                     | Value                                |  

  4. +---------------------------+--------------------------------------+  

  5. | admin_state_up            | True                                 |  

  6. | id                        | 2d72d81b-cf09-459e-87fb-a50fa0e8730a |  

  7. | name                      | ext_net                              |  

  8. | provider:network_type     | vlan                                 |  

  9. | provider:physical_network | physnet1                             |  

  10. | provider:segmentation_id  | 1000                                 |  

  11. | router:external           | True                                 |  

  12. | shared                    | False                                |  

  13. | status                    | ACTIVE                               |  

  14. | subnets                   | e1932e73-1e4b-4f87-9ebf-758a757e20ef |  

  15. | tenant_id                 | b21a96e16c3c438caab4a27a1f58a5b8     |  

  16. +---------------------------+--------------------------------------+  


  1. [root@oc2603148815 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  

  2. +------------------+----------------------------------------------------+  

  3. | Field            | Value                                              |  

  4. +------------------+----------------------------------------------------+  

  5. | allocation_pools | {"start": "192.168.12.10", "end": "192.168.12.50"} |  

  6. | cidr             | 192.168.12.0/24                                    |  

  7. | dns_nameservers  |                                                    |  

  8. | enable_dhcp      | False                                              |  

  9. | gateway_ip       | 192.168.12.1                                       |  

  10. | host_routes      |                                                    |  

  11. | id               | e1932e73-1e4b-4f87-9ebf-758a757e20ef               |  

  12. | ip_version       | 4                                                  |  

  13. | name             |                                                    |  

  14. | network_id       | 2d72d81b-cf09-459e-87fb-a50fa0e8730a               |  

  15. | tenant_id        | b21a96e16c3c438caab4a27a1f58a5b8                   |  

  16. +------------------+----------------------------------------------------+  

創建內部網絡(分配fixedip)
  1. [root@oc2603148815 cfn]# neutron net-create vlan-70 --provider:network_type vlan --provider:physical_network physnet1 --provider:segmentation_id 16  

  2. +---------------------------+--------------------------------------+  

  3. | Field                     | Value                                |  

  4. +---------------------------+--------------------------------------+  

  5. | admin_state_up            | True                                 |  

  6. | id                        | 793a95b7-cf1f-4bde-b7b8-5a9a2e552fae |  

  7. | name                      | vlan-70                              |  

  8. | provider:network_type     | vlan                                 |  

  9. | provider:physical_network | physnet1                             |  

  10. | provider:segmentation_id  | 16                                   |  

  11. | router:external           | False                                |  

  12. | shared                    | False                                |  

  13. | status                    | ACTIVE                               |  

  14. | subnets                   | f542941d-5d53-45e4-85d0-944e030c2bcc |  

  15. | tenant_id                 | b21a96e16c3c438caab4a27a1f58a5b8     |  

  16. +---------------------------+--------------------------------------+  


  1. [root@oc2603148815 cfn]# neutron subnet-create vlan-70 70.0.0.0/24  

  2. +------------------+--------------------------------------------+  

  3. | Field            | Value                                      |  

  4. +------------------+--------------------------------------------+  

  5. | allocation_pools | {"start": "70.0.0.2", "end": "70.0.0.254"} |  

  6. | cidr             | 70.0.0.0/24                                |  

  7. | dns_nameservers  |                                            |  

  8. | enable_dhcp      | True                                       |  

  9. | gateway_ip       | 70.0.0.1                                   |  

  10. | host_routes      |                                            |  

  11. | id               | f542941d-5d53-45e4-85d0-944e030c2bcc       |  

  12. | ip_version       | 4                                          |  

  13. | name             |                                            |  

  14. | network_id       | 793a95b7-cf1f-4bde-b7b8-5a9a2e552fae       |  

  15. | tenant_id        | b21a96e16c3c438caab4a27a1f58a5b8           |  

  16. +------------------+--------------------------------------------+  


  1. [root@oc2603148815 cfn]# neutron net-list  

  2. +--------------------------------------+---------+------------------------------------------------------+  

  3. | id                                   | name    | subnets                                              |  

  4. +--------------------------------------+---------+------------------------------------------------------+  

  5. | 2d72d81b-cf09-459e-87fb-a50fa0e8730a | ext_net | e1932e73-1e4b-4f87-9ebf-758a757e20ef 192.168.12.0/24 |  

  6. | 793a95b7-cf1f-4bde-b7b8-5a9a2e552fae | vlan-70 | f542941d-5d53-45e4-85d0-944e030c2bcc 70.0.0.0/24     |  

  7. +--------------------------------------+---------+------------------------------------------------------+  

綁定內外網到router1
  1. # neutron router-gateway-set $ROUTER_ID $EXTERNAL_NETWORK_ID  

  2. [root@oc2603148815 cfn]# neutron router-gateway-set 06d85a01-fc42-4cde-a0f1-377f2f394a64 2d72d81b-cf09-459e-87fb-a50fa0e8730a  

  3. # neutron router-interface-add $ROUTER_ID $SUBNET_ID  

  4. [root@oc2603148815 cfn]# neutron router-interface-add 06d85a01-fc42-4cde-a0f1-377f2f394a64 f542941d-5d53-45e4-85d0-944e030c2bcc  

經過上面的步驟後neutron-l3-agent會加入下列規則到iptables:

  1. -A PREROUTING -j neutron-l3-agent-PREROUTING  

  2. -A POSTROUTING -j neutron-l3-agent-POSTROUTING  

  3. -A POSTROUTING -j neutron-postrouting-bottom  

  4. -A OUTPUT -j neutron-l3-agent-OUTPUT  

  5. -A neutron-l3-agent-snat -j neutron-l3-agent-float-snat  

  6. -A neutron-l3-agent-snat -s 70.0.0.0/24 -j SNAT --to-source 192.168.12.10  

  7. -A neutron-postrouting-bottom -j neutron-l3-agent-snat  

創建floating ip(192.168.12.11)並綁定到vm的fixed ip(選擇70.0.0.3):
  1. [root@xianghui-10-9-1-141 ~]# neutron floatingip-create 2d72d81b-cf09-459e-87fb-a50fa0e8730a  

  2. Created a new floatingip:  

  3. +---------------------+--------------------------------------+  

  4. | Field               | Value                                |  

  5. +---------------------+--------------------------------------+  

  6. | fixed_ip_address    |                                      |  

  7. | floating_ip_address | 192.168.12.11                        |  

  8. | floating_network_id | 2d72d81b-cf09-459e-87fb-a50fa0e8730a |  

  9. | id                  | f8b48ab7-ea51-4f29-bc84-0ab179808dbb |  

  10. | port_id             |                                      |  

  11. | router_id           |                                      |  

  12. | tenant_id           | adc4e7a4effa44ffa3c6e48dd5a8555a     |  

  13. +---------------------+--------------------------------------+  

找出想要被綁定的fixed ip 的port id

  1. [root@xianghui-10-9-1-141 ~]# neutron port-list  

  2. +--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+  

  3. | id                                   | name | mac_address       | fixed_ips                                                                            |  

  4. +--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+  

  5. | 0d06055b-2f31-4d8e-b8da-e048d76a07cc |      | fa:16:3e:d7:f4:19 | {"subnet_id": "5c62752f-27ba-4d38-9702-2ca17ec2741d", "ip_address": "70.0.0.3"}      |  

  6. +--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+  


  1. [root@xianghui-10-9-1-141 ~]# neutron floatingip-associate f8b48ab7-ea51-4f29-bc84-0ab179808dbb0d06055b-2f31-4d8e-b8da-e048d76a07cc  

  2. Associated floatingip f8b48ab7-ea51-4f29-bc84-0ab179808dbb  


  1. [root@xianghui-10-9-1-141 ~]# neutron floatingip-list  

  2. +--------------------------------------+------------------+---------------------+--------------------------------------+  

  3. | id                                   | fixed_ip_address | floating_ip_address | port_id                              |  

  4. +--------------------------------------+------------------+---------------------+--------------------------------------+  

  5. | f8b48ab7-ea51-4f29-bc84-0ab179808dbb | 70.0.0.3         | 192.168.12.11       | b0797fe6-b799-41ea-86d0-9d9bfa0b2eb9 |  

  6. +--------------------------------------+------------------+---------------------+--------------------------------------+  

經過前面步驟後,iptables會多出下面的規則, 所有目標ip是192.168.12.11的包都會被轉發到ip 70.0.0.3的guest上

  1. -A neutron-l3-agent-OUTPUT -d 192.168.12.11/32 -j DNAT --to-destination 70.0.0.3  

  2. -A neutron-l3-agent-PREROUTING -d 192.168.12.11/32 -j DNAT --to-destination 70.0.0.3  

  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)

  1. [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)

  1. [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上)


  1. [root@xianghui-10-9-1-141 ~]#  ssh [email protected]  

  2. [ec2-user@wordpress-test-wikidatabase-jevfsmkbakch ~]$ ping 192.168.12.100  

  3. PING 192.168.12.100 (192.168.12.100) 56(84) bytes of data.  

  4. 64 bytes from 70.0.0.6: icmp_req=1 ttl=64 time=3.09 ms  

  5. 64 bytes from 70.0.0.6: icmp_req=2 ttl=64 time=0.281 ms  

  6. 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,藉此掩蓋源地址

  1. [root@xianghui-10-9-1-141 ~]#  iptables -t nat -A POSTROUTING -j neutron-l3-agent-float-snat  

  2. [ec2-user@wordpress-test-wikidatabase-jevfsmkbakch ~]$ ping 192.168.12.100  

  3. PING 192.168.12.100 (192.168.12.100) 56(84) bytes of data.  

  4. 64 bytes from 192.168.12.100: icmp_req=1 ttl=63 time=2.47 ms  

  5. 64 bytes from 192.168.12.100: icmp_req=2 ttl=63 time=0.199 ms  

  6. 64 bytes from 192.168.12.100: icmp_req=3 ttl=63 time=0.251 ms  


7. 實例分析(ALL-IN-ONE)

7.1 虛擬機的網絡拓撲

130819128.png

7.2 虛擬機之間用floating ip ping通

  1. # ping 192.168.12.100(70.0.0.6) from 70.0.0.11  

  2. # s:70.0.0.11 d:70.0.0.6  

  3. # prerouting -> forward -> postrouting  

  4. [root@xianghui-10-9-1-141 ~]#  iptables -A neutron-l3-agent-FORWARD -d 70.0.0.11/32 -j ACCEPT  

  5. [root@xianghui-10-9-1-141 ~]#  iptables -A neutron-l3-agent-FORWARD -d 70.0.0.6/32 -j ACCEPT  

  6. [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

  1. -A OUTPUT -j neutron-l3-agent-OUTPUT  

  2. [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  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章