openstack网络架构

来源:互联网 发布:东海村核事故 知乎 编辑:程序博客网 时间:2024/05/29 16:54

深入理解openstack网络架构(1)

前言

openstack网络功能强大同时也相对更复杂。本系列文章通过Oracle OpenStack TechPreview介绍openstack的配置,通过各种场景和例子说明openstack各种不同的网络组件。本文的目的在于提供openstack网络架构的全景图并展示各个模块是如何一起协作的。这对openstack的初学者以及希望理解openstack网络原理的人会非常有帮助。首先,我们先讲解下一些基础并举例说明。   

根据最新的icehouse版用户调查,基于open vswitch插件的Neutron在生产环境和POC环境都被广泛使用,所以在这个系列的文章中我们主要分析这种openstack网络的配置。当然,我们知道openstack网络支持很多种配置,尽管neutron+open vswitch是最常用的配置,但是我们从未说它是最好或者最高效的一种方式。Neutron+open vswitch仅仅是一个例子,对任何希望理解openstack网络的人是一个很好的切入点。即使你打算使用其他类型的网络配置比如使用不同的neutron插件或者根本不使用neutron,这篇文章对你理解openstack网络仍是一个很好的开始。   

我们在例子中使用的配置是Oracle OpenStack Tech Preview所提供的一种配置。安装它非常简单,并且它是一个很好的参考。在这种配置中,我们在所有服务器上使用eth2作为虚拟机的网络,所有虚拟机流量使用这个网卡。Oracle OpenStack Tech Preview使用VLAN进行L2隔离,进而提供租户和网络隔离,下图展示了我们如何进行配置和部署:   

setup2


第一篇文章会略长,我们将聚焦于openstack网络的一些基本概念。我们将讨论open vswitch、network namespaces、linux bridge、veth pairs等几个组件。注意这里不打算全面介绍这些组件,只是为了理解openstack网络架构。可以通过网络上的其他资源进一步了解这些组件。


Open vSwitch (OVS)

在Oracle OpenStack Tech Preview中用于连接虚拟机和物理网口(如上例中的eth2),就像上边部署图所示。OVS包含bridges和ports,OVS bridges不同于与linux bridge(使用brctl命令创建)。让我们先看下OVS的结构,使用如下命令:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ovs-vsctl show  
  2. 7ec51567-ab42-49e8-906d-b854309c9edf  
  3.     Bridge br-int  
  4.         Port br-int  
  5.             Interface br-int  
  6.                 type: internal  
  7.         Port "int-br-eth2"  
  8.             Interface "int-br-eth2"  
  9.     Bridge "br-eth2"  
  10.         Port "br-eth2"  
  11.             Interface "br-eth2"  
  12.                 type: internal  
  13.         Port "eth2"  
  14.             Interface "eth2"  
  15.         Port "phy-br-eth2"  
  16.             Interface "phy-br-eth2"  
  17. ovs_version: "1.11.0"  

我们看到标准的部署在compute node上的OVS,拥有两个网桥,每个有若干相关联的port。上边的例子是在一个没有任何虚拟机的计算节点上。我们可以看到eth2连接到个叫br-eth2的网桥上,我们还看到两个叫“int-br-eth2"和”phy-br-eth2“的port,事实上是一个veth pair,作为虚拟网线连接两个bridages。我们会在后边讨论veth paris。

当我们创建一个虚拟机,br-int网桥上会创建一个port,这个port最终连接到虚拟机(我们会在后边讨论这个连接)。这里是启动一个虚拟机后的OVS结构:  

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ovs-vsctl show  
  2. efd98c87-dc62-422d-8f73-a68c2a14e73d  
  3.     Bridge br-int  
  4.         Port "int-br-eth2"  
  5.             Interface "int-br-eth2"  
  6.         Port br-int  
  7.             Interface br-int  
  8.                 type: internal  
  9.         Port "qvocb64ea96-9f"  
  10.             tag: 1  
  11.             Interface "qvocb64ea96-9f"  
  12.     Bridge "br-eth2"  
  13.         Port "phy-br-eth2"  
  14.             Interface "phy-br-eth2"  
  15.         Port "br-eth2"  
  16.             Interface "br-eth2"  
  17.                 type: internal  
  18.         Port "eth2"  
  19.             Interface "eth2"  
  20. ovs_version: "1.11.0"  
”br-int“网桥现在有了一个新的port"qvocb64ea96-9f" 连接VM,并且被标记为vlan1虚拟机的每个网卡都需要对应在"br-int”网桥上创建一个port。

OVS中另一个有用的命令是dump-flows,以下为例子: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ovs-ofctl dump-flows br-int  
  2. NXST_FLOW reply (xid=0x4):  
  3. cookie=0x0, duration=735.544s, table=0, n_packets=70, n_bytes=9976,idle_age=17, priority=3,in_port=1,dl_vlan=1000 actions=mod_vlan_vid:1,NORMAL  
  4. cookie=0x0, duration=76679.786s, table=0, n_packets=0, n_bytes=0,idle_age=65534, hard_age=65534, priority=2,in_port=1 actions=drop  
  5. cookie=0x0, duration=76681.36s, table=0, n_packets=68, n_bytes=7950,idle_age=17, hard_age=65534, priority=1 actions=NORMAL  
如上所述,VM相连的port使用了Vlan tag 1。然后虚拟机网络(eth2)上的port使用tag1000。OVS会修改VM和物理网口间所有package的vlan。在openstack中,OVS agent 控制open vswitch中的flows,用户不需要进行操作。如果你想了解更多的如何控制open vswitch中的流,可以参考http://openvswitch.org中对ovs-ofctl的描述。  

Network Namespaces (netns)

网络namespace是linux上一个很cool的特性,它的用途很多。在openstack网络中被广泛使用。网络namespace是拥有独立的网络配置隔离容器,并且该网络不能被其他名字空间看到。网络名字空间可以被用于封装特殊的网络功能或者在对网络服务隔离的同时完成一个复杂的网络设置。在Oracle OpenStack Tech Preview中我们使用最新的R3企业版内核,该内核提供给了对netns的完整支持。

通过如下例子我们展示如何使用netns命令控制网络namespaces。定义一个新的namespace:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns add my-ns  
  2. # ip netns list  
  3. my-ns  

我们说过namespace是一个隔离的容器,我们可以在namspace中进行各种操作,比如ifconfig命令。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec my-ns ifconfig -a  
  2. lo        Link encap:Local Loopback  
  3.           LOOPBACK  MTU:16436 Metric:1  
  4.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  5.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
  6. collisions:0 txqueuelen:0  
  7.           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  
我们可以在namespace中运行任何命令,比如对debug非常有用的tcddump命令,我们使用ping、ssh、iptables命令。连接namespace和外部:连接到namespace和namespace直接连接的方式有很多,我们主要聚集在openstack中使用的方法。openstack使用了OVS和网络namespace的组合。OVS定义接口,然后我们将这些接口加入namespace中。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec my-ns ifconfig -a  
  2. lo        Link encap:Local Loopback  
  3.           LOOPBACK  MTU:65536 Metric:1  
  4.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  5.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
  6. collisions:0 txqueuelen:0  
  7.           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  
  8.   
  9. my-port   Link encap:Ethernet HWaddr 22:04:45:E2:85:21  
  10.           BROADCAST  MTU:1500 Metric:1  
  11.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  12.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
  13. collisions:0 txqueuelen:0  
  14.           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  
现在我们可以增加更多的ports到OVS bridge,并且连接到其他namespace或者其他设备比如物理网卡。Neutron使用网络namespace来实现网络服务,如DHCP、routing、gateway、firewall、load balance等。下一篇文章我们会讨论更多细节 。

Linux bridge and veth pairs

Linux bridge用于连接OVS port和虚拟机。ports负责连通OVS bridge和linux bridge或者两者与虚拟机。linux bridage主要用于安全组增强。安全组通过iptables实现,iptables只能用于linux bridage而非OVS bridage。

Veth对在openstack网络中大量使用,也是debug网络问题的很好工具。Veth对是一个简单的虚拟网线,所以一般成对出现。通常Veth对的一端连接到bridge,另一端连接到另一个bridge或者留下在作为一个网口使用。

这个例子中,我们将创建一些veth对,把他们连接到bridge上并测试联通性。这个例子用于通常的Linux服务器而非openstack节点:创建一个veth对,注意我们定义了两端的名字:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip link add veth0 type veth peer name veth1  
  2.   
  3. # ifconfig -a  
  4.   
  5. .  
  6.   
  7. .  
  8.   
  9. veth0     Link encap:Ethernet HWaddr 5E:2C:E6:03:D0:17  
  10.   
  11.           BROADCAST MULTICAST  MTU:1500 Metric:1  
  12.   
  13.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  14.   
  15.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
  16.   
  17. collisions:0 txqueuelen:1000  
  18.   
  19.           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  
  20.   
  21. veth1     Link encap:Ethernet HWaddr E6:B6:E2:6D:42:B8  
  22.   
  23.           BROADCAST MULTICAST  MTU:1500 Metric:1  
  24.   
  25.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  26.   
  27.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
  28.   
  29. collisions:0 txqueuelen:1000  
  30.   
  31.           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  
  32.   
  33. .  
  34.   
  35. .  
为了让例子更有意义,我们将创建如下配置:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. veth0 => veth1 =>br-eth3 => eth3 ======> eth2 on another Linux server  
br-eht3: 一个基本的Linux bridge,连接veth1和eth3。 eth3: 一个没有设定IP的物理网口,该网口连接着私有网络。eth2: 远端Linux服务器上的一个物理网口,连接着私有网络并且被配置了IP(50.50.50.1)一旦我们创建了这个配置,我们将通过veth0 ping 50.50.50.1这个远端IP,从而测试网络联通性:
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # brctl addbr br-eth3  
  2.   
  3. # brctl addif br-eth3 eth3  
  4.   
  5. # brctl addif br-eth3 veth1  
  6.   
  7. # brctl show  
  8.   
  9. bridge name     bridge id               STP enabled     interfaces  
  10.   
  11. br-eth3         8000.00505682e7f6       no              eth3  
  12.   
  13.                                                         veth1  
  14.   
  15. # ifconfig veth0 50.50.50.50  
  16.   
  17. # ping -I veth0 50.50.50.51  
  18.   
  19. PING 50.50.50.51 (50.50.50.51) from 50.50.50.50 veth0: 56(84) bytes of data.  
  20.   
  21. 64 bytes from 50.50.50.51: icmp_seq=1 ttl=64 time=0.454 ms  
  22.   
  23. 64 bytes from 50.50.50.51: icmp_seq=2 ttl=64 time=0.298 ms  
如果命名不像例子中这么显而易见,导致我们无法支持veth设备的两端,我们可以使用ethtool命令查询。ethtool命令返回index号,通过ip link命令查看对应的设备
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ethtool -S veth1  
  2.   
  3. NIC statistics:  
  4.   
  5. peer_ifindex: 12  
  6.   
  7. # ip link  
  8.   
  9. .  

  10. .  
  11.   
  12. 12: veth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000  

总结

文章中,我们快速了解了OVS/网络namespaces/Linux bridges/veth对。这些组件在openstack网络架构中大量使用,理解这些组件有助于我们理解不同的网络场景。下篇文章中,我们会了解虚拟机之间/虚拟机与外部网络之间如何进行通信。


深入理解openstack网络架构(2)----Basic Use Cases

在上一篇文章中,我们了解了几个网络组件,如openvswitch/network namespace/Linux bridges/veth pairs。这篇文章中,我们将用3个简单的use case,展示这些基本网络组件如何以工作从而实现openstack的SDN方案。
在这些use case中,我们会了解整个网络配置和他们如何一起运行。use case如下:

  1. 创建网络——我们创建网络时,发生了什么。如何创建多个隔离的网络。
  2. 创建虚拟机——一旦我们有了网络,我们可以创建虚拟机并将其接入网络。
  3. 虚拟机的DHCP请求——opensack可以自动为虚拟机配置IP。通过openstack neutron控制的DHCP服务完成。我们来了解这个服务如何运行,DHCP请求和回应是什么样子的?

这篇文章中,我们会展示网络连接的原理,我们会了解网络包如何从A到B。我们先了解已经完成的网络配置是什么样子的?然后我们讨论这些网络配置是如何以及何时创建的?我个人认为,通过例子和具体实践看到真实的网络接口如何工作以及如何将他们连接起来是非常有价值的。然后,一切真相大白,我们知道网络连接如何工作,在后边的文章中,我将进一步解释neutron如何配置这些组件,从而提供这样的网络连接能力。

我推荐在你自己的环境上尝试这些例子或者使用Oracle Openstack Tech Preview。完全理解这些网络场景,对我们调查openstack环境中的网络问题非常有帮助。

Use case #1: Create Network

创建network的操作非常简单。我们可以使用GUI或者命令行完成。openstack的网络仅供创建该网络的租户使用。当然如果这个网络是“shared”,它也可以被其他所有租户使用。一个网络可以有多个subnets,但是为了演示目的和简单,我们仅为每一个network创建一个subnet。通过命令行创建network:  

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron net-create net1  
  2.   
  3. Created a new network:  
  4.   
  5. +---------------------------+--------------------------------------+  
  6.   
  7. | Field                     | Value                                |  
  8.   
  9. +---------------------------+--------------------------------------+  
  10.   
  11. | admin_state_up            | True                                 |  
  12.   
  13. | id                        | 5f833617-6179-4797-b7c0-7d420d84040c |  
  14.   
  15. | name                      | net1                                 |  
  16.   
  17. | provider:network_type     | vlan                                 |  
  18.   
  19. | provider:physical_network | default                              |  
  20.   
  21. | provider:segmentation_id  | 1000                                 |  
  22.   
  23. | shared                    | False                                |  
  24.   
  25. | status                    | ACTIVE                               |  
  26.   
  27. | subnets                   |                                      |  
  28.   
  29. | tenant_id                 | 9796e5145ee546508939cd49ad59d51f     |  
  30.   
  31. +---------------------------+--------------------------------------+  
为这个network创建subnet:: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron subnet-create net1 10.10.10.0/24  
  2.   
  3. Created a new subnet:  
  4.   
  5. +------------------+------------------------------------------------+  
  6.   
  7. | Field            | Value                                          |  
  8.   
  9. +------------------+------------------------------------------------+  
  10.   
  11. | allocation_pools | {"start": "10.10.10.2", "end": "10.10.10.254"} |  
  12.   
  13. | cidr             | 10.10.10.0/24                                  |  
  14.   
  15. | dns_nameservers  |                                                |  
  16.   
  17. | enable_dhcp      | True                                           |  
  18.   
  19. | gateway_ip       | 10.10.10.1                                     |  
  20.   
  21. | host_routes      |                                                |  
  22.   
  23. | id               | 2d7a0a58-0674-439a-ad23-d6471aaae9bc           |  
  24.   
  25. | ip_version       | 4                                              |  
  26.   
  27. | name             |                                                |  
  28.   
  29. | network_id       | 5f833617-6179-4797-b7c0-7d420d84040c           |  
  30.   
  31. | tenant_id        | 9796e5145ee546508939cd49ad59d51f               |  
  32.   
  33. +------------------+------------------------------------------------+  

现在我们有了一个network和subnet,网络拓扑像这样:


horizon_network

现在让我们深入看下到底发生了什么?在控制节点,我们一个新的namespace被创建:  

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns list  
  2.   
  3. qdhcp-5f833617-6179-4797-b7c0-7d420d84040c  

这个namespace的名字是qdhcp- (参见上边),让我们深入namespace中看看有什么? 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec qdhcp-5f833617-6179-4797-b7c0-7d420d84040c ip addr  
  2.   
  3. 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN  
  4.   
  5.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
  6.   
  7.     inet 127.0.0.1/8 scope host lo  
  8.   
  9.     inet6 ::1/128 scope host  
  10.   
  11.        valid_lft forever preferred_lft forever  
  12.   
  13. 12: tap26c9b807-7c:  mtu 1500 qdisc noqueue state UNKNOWN  
  14.   
  15.     link/ether fa:16:3e:1d:5c:81 brd ff:ff:ff:ff:ff:ff  
  16.   
  17.     inet 10.10.10.3/24 brd 10.10.10.255 scope global tap26c9b807-7c  
  18.   
  19.     inet6 fe80::f816:3eff:fe1d:5c81/64 scope link  
  20.   
  21.        valid_lft forever preferred_lft forever  

我们发下在namespace下有两个网络接口,一个是loop设备,另一个叫“tap26c9b807-7c”。这个接口设置了IP地址10.10.10.3,他会接收dhcp请求(后边会讲)。接下来我们来跟踪下“tap26c9b807-7c”的网络连接性。我们从OVS上看下这个接口所连接的OVS网桥"br-int"。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ovs-vsctl show  
  2. 8a069c7c-ea05-4375-93e2-b9fc9e4b3ca1  
  3.     Bridge "br-eth2"  
  4.         Port "br-eth2"  
  5.             Interface "br-eth2"  
  6.                 type: internal  
  7.         Port "eth2"  
  8.             Interface "eth2"  
  9.         Port "phy-br-eth2"  
  10.             Interface "phy-br-eth2"  
  11.     Bridge br-ex  
  12.         Port br-ex  
  13.             Interface br-ex  
  14.                 type: internal  
  15.     Bridge br-int  
  16.         Port "int-br-eth2"  
  17.             Interface "int-br-eth2"  
  18.         Port "tap26c9b807-7c"  
  19.             tag: 1  
  20.             Interface "tap26c9b807-7c"  
  21.                 type: internal  
  22.         Port br-int  
  23.             Interface br-int  
  24.                 type: internal  
  25.     ovs_version: "1.11.0"  

由上可知,veth pair的两端“int-br-eth2” 和 "phy-br-eth2",这个veth pari连接两个OVS网桥"br-eth2"和"br-int"。上一篇文章中,我们解释过如何通过ethtool命令查看veth pairs的两端。就如下边的例子: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ethtool -S int-br-eth2  
  2. NIC statistics:  
  3.      peer_ifindex: 10  
  4. .  
  5. .  
  6.    
  7. #ip link  
  8. .  
  9. .  
  10. 10: phy-br-eth2:  mtu 1500 qdisc pfifo_fast state UP qlen 1000  
  11. .  
  12. .  

注意“phy-br-eth2”连接到网桥"br-eth2",这个网桥的一个网口是物理网卡eth2。这意味着我们创建的网络创建了一个连接到了物理网卡eth2的namespace。eth2所在的虚拟机网络会连接所有的虚拟机的。

关于网络隔离:

Openstack支持创建多个隔离的网络,也可以使用多种机制完成网络间的彼此隔离。这些隔离机制包括VLANs/VxLANs/GRE tunnels,这个在我们部署openstack环境时配置。本文中我们选择了VLANs。当使用VLAN标签作为隔离机制,Neutron会从预定义好的VLAN池中选择一个VLAN标签,并分配给一个新创建的network。通过分配VLAN标签给network,Neutron允许在一个物理网卡上创建多个隔离的网络。与其他的平台的最大的区别是,用户不需要负责管理VLAN如何分配给networks。Neutron会负责管理分配VLAN标签,并负责回收。在我们的例子中,net1使用VLAN标签1000,这意味着连接到该网络的虚拟机,发出的包会被打上VLAN标签1000然后发送到物理网络中对namespace也是同样的,如果我们希望namespace连接到某个特定网络,我们需要确保这个namespace发出的/接收的包被正确的打上了标签。

在上边的例子中,namespace中的网络接口“tap26c9b807-7c”被分配了VLAN标签1。如果我们从OVS观察下,会发现VLAN1会被改为VLAN1000,当包进入eth2所在的uxniji网络。反之亦然。我们通过OVS的dump-flows命令可以看到进入虚拟机网络的网络包在br-eth2上进行了VLAN标签的修改

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #  ovs-ofctl dump-flows br-eth2  
  2. NXST_FLOW reply (xid=0x4):  
  3.  cookie=0x0, duration=18669.401s, table=0, n_packets=857, n_bytes=163350, idle_age=25, priority=4,in_port=2,dl_vlan=1 actions=mod_vlan_vid:1000,NORMAL  
  4.  cookie=0x0, duration=165108.226s, table=0, n_packets=14, n_bytes=1000, idle_age=5343, hard_age=65534, priority=2,in_port=2 actions=drop  
  5.  cookie=0x0, duration=165109.813s, table=0, n_packets=1671, n_bytes=213304, idle_age=25, hard_age=65534, priority=1 actions=NORMAL  

从网络接口到namespace我们看到VLAN标签的修改如下: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #  ovs-ofctl dump-flows br-int  
  2. NXST_FLOW reply (xid=0x4):  
  3.  cookie=0x0, duration=18690.876s, table=0, n_packets=1610, n_bytes=210752, idle_age=1, priority=3,in_port=1,dl_vlan=1000 actions=mod_vlan_vid:1,NORMAL  
  4.  cookie=0x0, duration=165130.01s, table=0, n_packets=75, n_bytes=3686, idle_age=4212, hard_age=65534, priority=2,in_port=1 actions=drop  
  5.  cookie=0x0, duration=165131.96s, table=0, n_packets=863, n_bytes=160727, idle_age=1, hard_age=65534, priority=1 actions=NORMAL  

总之,当用户创建network,neutron会创建一个namespace,这个namespace通过OVS连接到虚拟机网络。OVS还负责namespace与虚拟机网络之间VLAN标签的修改。现在,让我们看下创建虚拟机时,发生了什么?虚拟机是怎么连接到虚拟机网络的?

Use case #2: Launch a VM

从Horizon或者命令行创建并启动一个虚拟机,下图是从Horzion创建的例子: 

launch-instance


挂载网络并启动虚拟机:

attach-network
一旦虚拟机启动并运行,我们发下nova支持给虚拟机绑定IP: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # nova list  
  2. +--------------------------------------+--------------+--------+------------+-------------+-----------------+  
  3. | ID                                   | Name         | Status | Task State | Power State | Networks        |  
  4. +--------------------------------------+--------------+--------+------------+-------------+-----------------+  
  5. | 3707ac87-4f5d-4349-b7ed-3a673f55e5e1 | Oracle Linux | ACTIVE | None       | Running     | net1=10.10.10.2 |  
  6. +--------------------------------------+--------------+--------+------------+-------------+-----------------+  

nova list命令显示虚拟机在运行中,并被分配了IP 10.10.10.2。我们通过虚拟机定义文件,查看下虚拟机与虚拟机网络之间的连接性。虚拟机的配置文件在目录/var/lib/nova/instances//下可以找到。通过查看虚拟机定义文件,libvirt.xml,我们可以看到虚拟机连接到网络接口“tap53903a95-82”,这个网络接口连接到了Linux网桥 “qbr53903a95-82”:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <interface type="bridge">  
  2.       <mac address="fa:16:3e:fe:c7:87"/>  
  3.       <source bridge="qbr53903a95-82"/>  
  4.       <target dev="tap53903a95-82"/>  
  5.     </interface>  
通过brctl查看网桥信息如下: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # brctl show  
  2. bridge name     bridge id               STP enabled     interfaces  
  3. qbr53903a95-82          8000.7e7f3282b836       no              qvb53903a95-82  
  4.                                                         tap53903a95-82  

网桥有两个网络接口,一个连接到虚拟机(“tap53903a95-82 “),另一个( “qvb53903a95-82”)连接到OVS网桥”br-int"。 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ovs-vsctl show  
  2. 3c42f80-77e9-46c8-8560-7697d76de51c  
  3.    Bridge "br-eth2"  
  4.        Port "br-eth2"  
  5.            Interface "br-eth2"  
  6.                type: internal  
  7.        Port "eth2"  
  8.            Interface "eth2"  
  9.        Port "phy-br-eth2"  
  10.            Interface "phy-br-eth2"  
  11.    Bridge br-int  
  12.        Port br-int  
  13.            Interface br-int  
  14.                type: internal  
  15.        Port "int-br-eth2"  
  16.            Interface "int-br-eth2"  
  17.        Port "qvb53903a95-82"  
  18.            tag: 3  
  19.            Interface "qvb53903a95-82"  
  20.    ovs_version: "1.11.0"  
我们之前看过,OVS网桥“br-int"连接到"br-eth2",通过veth pair(int-br-eth2,phy-br-eth2 ),br-eth2连接到物理网卡eth2。整个流入如下: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. VM  ->  tap53903a95-82 (virtual interface)  ->  qbr53903a95-82 (Linux bridge)  ->  qvb53903a95-82 (interface connected from Linux bridge to OVS bridge br-int)  ->  int-br-eth2 (veth one end)  ->  phy-br-eth2 (veth the other end)  ->  eth2 physical interface.  
与虚拟机相连的Linux bridage主要用于基于Iptables的安全组设置。安全组用于对虚拟机的网络隔离进行增强,由于iptables不能用于OVS网桥,因此我们使用了Linux网桥。后边我们会看到Linux网桥的规则设置。

VLAN tags:我们在第一个use case中提到过,net1使用VLAN标签1000,通过OVS我们看到qvo41f1ebcf-7c使用VLAN标签3。VLAN标签从3到1000的转换在OVS中完成,通过br-eth2中实现。 总结如下,虚拟机通过一组网络设备连入虚拟机网络。虚拟机和网络之间,VLAN标签被修改。

Use case #3: Serving a DHCP request coming from the virtual machine

之前的use case中,我们看到了一个叫dhcp-的namespace和虚拟机,两者最终连接到物理网络eth2。他们都会被打上VLAN标签1000。我们看到该namespace中的网络接口使用IP 10.10.10.3。因为虚拟机和namespace彼此连接并在相同子网,因此可以ping到对方。如下图,虚拟机中网络接口被分配了IP 10.10.10.2,我们尝试ping namespace中的网络接口的IP:
vm-console

namespace与虚拟机之间连通,并且可以互相ping通,对于定位问题非常有用。我们可以从虚拟机ping通namespace,可以使用tcpdump或其他工具定位网络中断问题。

为了响应虚拟机的dhcp请求,Neutron使用了”dnsmasq“的Linux工具,这个工具是一个轻量的DNS、DHCP服务,更多的信息请查看(http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html)。我们可以在控制节点通过PS命令看到: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. dnsmasq --no-hosts --no-resolv --strict-order --bind-interfaces --interface=tap26c9b807-7c --except-interface=lo --pid-file=/var/lib/neutron/dhcp/5f833617-6179-4797-b7c0-7d420d84040c/pid --dhcp-hostsfile=/var/lib/neutron/dhcp/5f833617-6179-4797-b7c0-7d420d84040c/host --dhcp-optsfile=/var/lib/neutron/dhcp/5f833617-6179-4797-b7c0-7d420d84040c/opts --leasefile-ro --dhcp-range=tag0,10.10.10.0,static,120s --dhcp-lease-max=256 --conf-file= --domain=openstacklocal  

DHCP服务在namespace中连接到了一个tap接口(“--interface=tap26c9b807-7c”),从hosts文件我们可以看到: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # cat  /var/lib/neutron/dhcp/5f833617-6179-4797-b7c0-7d420d84040c/host  
  2. fa:16:3e:fe:c7:87,host-10-10-10-2.openstacklocal,10.10.10.2  
之前的console输出可以看到虚拟机MAC为fa:16:3e:fe:c7:87 。这个mac地址与IP 10.10.10.2 关联,当包含该MAC的DHCP请求到达,dnsmasq返回10.10.10.2。在这个初始过程(可以重启网络服务触发)中从namespace中看,可以看到如下的DHCP请求:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec qdhcp-5f833617-6179-4797-b7c0-7d420d84040c tcpdump -n  
  2. 19:27:12.191280 IP 0.0.0.0.bootpc > 255.255.255.255.bootps: BOOTP/DHCP, Request from fa:16:3e:fe:c7:87, length 310  
  3. 19:27:12.191666 IP 10.10.10.3.bootps > 10.10.10.2.bootpc: BOOTP/DHCP, Reply, length 325  
总之,DHCP服务由dnsmasq提供,这个服务由Neutron配置,监听在DHCP namespace中的网络接口上。Neutron还配置dnsmasq中的MAC/IP映射关系,所以当DHCP请求时会受到分配给它的IP。

总结

本文,我们基于之前讲解的各种网络组件,分析了三种use case下网络如何连通的。这些use cases对了解整个网络栈以及了解虚拟机/计算节点/DHCP namespace直接如何连通很有帮助。
根据我们的分析,我们确信启动虚拟机、虚拟机发出DHCP请求、虚拟机收到正确的IP后这个网络按照我们预想的工作。我们看到一个包经过一长串路径最终到达目的地,如果这一切成功,意味着这些组件功能正常。
下一篇文章中,我们会学习更复杂的neutron服务并分析他们如何工作。


深入理解openstack网络架构(3)-----路由

前文中,我们学习了openstack网络使用的几个基本网络组件,并通过一些简单的use case解释网络如何连通的。本文中,我们会通过一个稍微复杂(其实仍然相当基本)的use case(两个网络间路由)探索网络的设置。 路由使用的组件与连通内部网络相同,使用namespace创建一个隔离的container,允许subnet间的网络包中转。
记住我们在第一篇文章中所说的,这只是使用OVS插件的例子。openstack还有很多插件使用不同的方式,我们提到的只是其中一种。

Use case #4: Routing traffic between two isolated networks

现实中,我们会创建不同的网络用于不同的目的。我们也会需要把这些网络连接起来。因为两个网络在不同的IP段,我们需要router将他们连接起来。为了分析这种设置,我们创建另一个network(net2)并配置一个20.20.20.0/24的subnet。在创建这个network后,我们启动一个Oracle Linux的虚拟机,并连接到net2。下图是从OpenstackGUI上看到的网络拓扑图:

https://blogs.oracle.com/ronen/resource/openstack-routing/two-networks.png

进一步探索,我们会在openstack网络节点上看到另一个namespace,这个namespace用于为新创建的网络提供服务。现在我们有两个namespace,每个network一个。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns list  
  2. qdhcp-63b7fcf2-e921-4011-8da9-5fc2444b42dd  
  3. qdhcp-5f833617-6179-4797-b7c0-7d420d84040c  

可以通过nova net-list查看network的ID信息,或者使用UI查看网络信息。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # nova net-list  
  2. +--------------------------------------+-------+------+  
  3. | ID                                   | Label | CIDR |  
  4. +--------------------------------------+-------+------+  
  5. | 5f833617-6179-4797-b7c0-7d420d84040c | net1  | None |  
  6. | 63b7fcf2-e921-4011-8da9-5fc2444b42dd | net2  | None |  
  7. +--------------------------------------+-------+------+  

我们新创建的network,net2有自己的namespace,这个namespace与net1是分离的。在namespace中,我们可以看到两个网络接口,一个local,一个是用于DHCP服务。 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec qdhcp-63b7fcf2-e921-4011-8da9-5fc2444b42dd ip addr  
  2. 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN  
  3.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
  4.     inet 127.0.0.1/8 scope host lo  
  5.     inet6 ::1/128 scope host  
  6.        valid_lft forever preferred_lft forever  
  7. 19: tap16630347-45:  mtu 1500 qdisc noqueue state UNKNOWN  
  8.     link/ether fa:16:3e:bd:94:42 brd ff:ff:ff:ff:ff:ff  
  9.     inet 20.20.20.3/24 brd 20.20.20.255 scope global tap16630347-45  
  10.     inet6 fe80::f816:3eff:febd:9442/64 scope link  
  11.        valid_lft forever preferred_lft forever  

net1和net2两个network没有被联通,我们需要创建一个router,通过router将两个network联通。Openstack Neutron向用户提供了创建router并将两个或多个network连接的能力。router其实只是一个额外的namespace。使用Neutron创建router可以通过GUI或者命令行操作:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron router-create my-router  
  2. Created a new router:  
  3. +-----------------------+--------------------------------------+  
  4. | Field                 | Value                                |  
  5. +-----------------------+--------------------------------------+  
  6. | admin_state_up        | True                                 |  
  7. | external_gateway_info |                                      |  
  8. | id                    | fce64ebe-47f0-4846-b3af-9cf764f1ff11 |  
  9. | name                  | my-router                            |  
  10. | status                | ACTIVE                               |  
  11. | tenant_id             | 9796e5145ee546508939cd49ad59d51f     |  
  12. +-----------------------+--------------------------------------+  

现在我们将两个netwrok通过router连接:

查看subnet的ID:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron subnet-list  
  2. +--------------------------------------+------+---------------+------------------------------------------------+  
  3. | id                                   | name | cidr          | allocation_pools                               |  
  4. +--------------------------------------+------+---------------+------------------------------------------------+  
  5. | 2d7a0a58-0674-439a-ad23-d6471aaae9bc |      | 10.10.10.0/24 | {"start": "10.10.10.2", "end": "10.10.10.254"} |  
  6. | 4a176b4e-a9b2-4bd8-a2e3-2dbe1aeaf890 |      | 20.20.20.0/24 | {"start": "20.20.20.2", "end": "20.20.20.254"} |  
  7. +--------------------------------------+------+---------------+------------------------------------------------+  

将subnet 10.10.10.0/24添加到router:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron router-interface-add fce64ebe-47f0-4846-b3af-9cf764f1ff11 subnet=2d7a0a58-0674-439a-ad23-d6471aaae9bc  
  2. Added interface 0b7b0b40-f952-41dd-ad74-2c15a063243a to router fce64ebe-47f0-4846-b3af-9cf764f1ff11.  

将subnet 20.20.20.0/24添加到router:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron router-interface-add fce64ebe-47f0-4846-b3af-9cf764f1ff11 subnet=4a176b4e-a9b2-4bd8-a2e3-2dbe1aeaf890  
  2. Added interface dc290da0-0aa4-4d96-9085-1f894cf5b160 to router fce64ebe-47f0-4846-b3af-9cf764f1ff11.  

此时,我们在查看网络拓扑会发现两个网络被router打通:

networks-routed

我们还可以发现两个网络接口连接到router,作为各自subnet的gateway。

我们可以看到为router创建的namespace。 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns list  
  2. qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11  
  3. qdhcp-63b7fcf2-e921-4011-8da9-5fc2444b42dd  
  4. qdhcp-5f833617-6179-4797-b7c0-7d420d84040c  

我们进入namespace内部可以看到:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 ip addr  
  2. 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN  
  3.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
  4.     inet 127.0.0.1/8 scope host lo  
  5.     inet6 ::1/128 scope host  
  6.        valid_lft forever preferred_lft forever  
  7. 20: qr-0b7b0b40-f9:  mtu 1500 qdisc noqueue state UNKNOWN  
  8.     link/ether fa:16:3e:82:47:a6 brd ff:ff:ff:ff:ff:ff  
  9.     inet 10.10.10.1/24 brd 10.10.10.255 scope global qr-0b7b0b40-f9  
  10.     inet6 fe80::f816:3eff:fe82:47a6/64 scope link  
  11.        valid_lft forever preferred_lft forever  
  12. 21: qr-dc290da0-0a:  mtu 1500 qdisc noqueue state UNKNOWN  
  13.     link/ether fa:16:3e:c7:7c:9c brd ff:ff:ff:ff:ff:ff  
  14.     inet 20.20.20.1/24 brd 20.20.20.255 scope global qr-dc290da0-0a  
  15.     inet6 fe80::f816:3eff:fec7:7c9c/64 scope link  
  16.        valid_lft forever preferred_lft forever  

我们看到两个网络接口,“qr-dc290da0-0a“ 和 “qr-0b7b0b40-f9。这两个网络接口连接到OVS上,使用两个network/subnet的gateway IP。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ovs-vsctl show  
  2. 8a069c7c-ea05-4375-93e2-b9fc9e4b3ca1  
  3.     Bridge "br-eth2"  
  4.         Port "br-eth2"  
  5.             Interface "br-eth2"  
  6.                 type: internal  
  7.         Port "eth2"  
  8.             Interface "eth2"  
  9.         Port "phy-br-eth2"  
  10.             Interface "phy-br-eth2"  
  11.     Bridge br-ex  
  12.         Port br-ex  
  13.             Interface br-ex  
  14.                 type: internal  
  15.     Bridge br-int  
  16.         Port "int-br-eth2"  
  17.             Interface "int-br-eth2"  
  18.         Port "qr-dc290da0-0a"  
  19.             tag: 2  
  20.             Interface "qr-dc290da0-0a"  
  21.                 type: internal  
  22.         Port "tap26c9b807-7c"  
  23.             tag: 1  
  24.             Interface "tap26c9b807-7c"  
  25.                 type: internal  
  26.         Port br-int  
  27.             Interface br-int  
  28.                 type: internal  
  29.         Port "tap16630347-45"  
  30.             tag: 2  
  31.             Interface "tap16630347-45"  
  32.                 type: internal  
  33.         Port "qr-0b7b0b40-f9"  
  34.             tag: 1  
  35.             Interface "qr-0b7b0b40-f9"  
  36.                 type: internal  
  37.     ovs_version: "1.11.0"  

我们可以看到,这些接口连接到”br-int",并打上了所在network对应的VLAN标签。这里我们可以通过gateway地址(20.20.20.1)成功的ping通router namespace:

ping-router

我们还可以看到IP地址为20.20.20.2可以ping通IP地址为10.10.10.2的虚拟机:

ping-vm-to-vm

两个subnet通过namespace中的网络接口互相连通。在namespace中,Neutron将系统参数net.ipv4.ip_forward设置为1。命令查看如下: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 sysctl net.ipv4.ip_forward  
  2. net.ipv4.ip_forward = 1  

我们可以看到namespace中的系统参数net.ipv4.ip_forward被设置,这种设置不会对namespace外产生影响。

总结

创建router时,Neutron会创建一个叫qrouter-的namespace。subnets通过OVS的br-int网桥上的网络接口接入router。网络接口被设置了正确的VLAN,从而可以连入它们对应的network。例子中,网络接口qr-0b7b0b40-f9的IP被设置为10.10.10.1,VLAN标签为1,它可以连接到“net1”。通过在namespace中设置系统参数net.ipv4.ip_forward为1,从而允许路由生效。

本文介绍了如何使用network namespace创建一个router。下一篇文章中,我们会探索浮动IP如何使用iptables工作。这也许更复杂但是依然使用这些基本的网络组件。

深入理解openstack网络架构(4)-----连接到public network

在上一篇文章中,我们介绍了openstack中的路由,了解到openstack如何通过namespace实现的router将两个network连通。本文中,我们进一步分析路由功能,说明实现内部internal network和public network的路由(而不仅仅是internal network之间)。
我们还会分析neutron如何将浮动IP配置给虚拟机,从而实现public network与虚拟机的连通。

Use case #5: Connecting VMs to the public network

所谓“public network”,指openstack部署环境以外的网络。这个网络可以是datacenter中的另一个网络、internet、或者一个不被openstack控制的私有网络。

与public network通信,我们需要在openstack中创建一个network并设置为public。这个network用于虚拟机与public network通信。虚拟机不能直接连接到这个新创建的属性为public的network,所有网络流量必须使用openstack创建的router从private network路由到public network。在openstack中创建public network,我们只需要使用neutron net-create 命令,并将router:external设置为True。
在我们的例子中,public newtork叫做“my-public”。 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron net-create my-public --router:external=True  
  2. Created a new network:  
  3. +---------------------------+--------------------------------------+  
  4. | Field                     | Value                                |  
  5. +---------------------------+--------------------------------------+  
  6. | admin_state_up            | True                                 |  
  7. | id                        | 5eb99ac3-905b-4f0e-9c0f-708ce1fd2303 |  
  8. | name                      | my-public                            |  
  9. | provider:network_type     | vlan                                 |  
  10. | provider:physical_network | default                              |  
  11. | provider:segmentation_id  | 1002                                 |  
  12. | router:external           | True                                 |  
  13. | shared                    | False                                |  
  14. | status                    | ACTIVE                               |  
  15. | subnets                   |                                      |  
  16. | tenant_id                 | 9796e5145ee546508939cd49ad59d51f     |  
  17. +---------------------------+--------------------------------------+  

在我们的环境中,控制节点的eth3是一个没有绑定IP的网卡。我们使用它接入外部public network。因此我们将eth3加入OVS网桥"br-ex",Neutron会将虚拟机向外部网络的发送的网络包路由到这个bridge。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ovs-vsctl add-port br-ex eth3  
  2. # ovs-vsctl show  
  3. 8a069c7c-ea05-4375-93e2-b9fc9e4b3ca1  
  4. .  
  5. .  
  6. .  
  7.     Bridge br-ex  
  8.         Port br-ex  
  9.             Interface br-ex  
  10.                 type: internal  
  11.         Port "eth3"  
  12.             Interface "eth3"  
  13. .  
  14. .  
  15. .  

我们在eth3上创建了一个IP范围是180.180.180.0/24的public network。这个public network存在于datacenter中,通过gateway 180.180.180.1可以连接到datacenter网络。为了将这个网络与Openstack环境相连,我们需要在“my-public"这个network,上创建一个有相同IP范围的subnet,并告诉neutron这个network的gateway。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron subnet-create my-public 180.180.180.0/24 --name public_subnet --enable_dhcp=False --allocation-pool start=180.180.180.2,end=180.180.180.100 --gateway=180.180.180.1  
  2. Created a new subnet:  
  3. +------------------+------------------------------------------------------+  
  4. | Field            | Value                                                |  
  5. +------------------+------------------------------------------------------+  
  6. | allocation_pools | {"start": "180.180.180.2", "end": "180.180.180.100"} |  
  7. | cidr             | 180.180.180.0/24                                     |  
  8. | dns_nameservers  |                                                      |  
  9. | enable_dhcp      | False                                                |  
  10. | gateway_ip       | 180.180.180.1                                        |  
  11. | host_routes      |                                                      |  
  12. | id               | ecadf103-0b3b-46e8-8492-4c5f4b3ea4cd                 |  
  13. | ip_version       | 4                                                    |  
  14. | name             | public_subnet                                        |  
  15. | network_id       | 5eb99ac3-905b-4f0e-9c0f-708ce1fd2303                 |  
  16. | tenant_id        | 9796e5145ee546508939cd49ad59d51f                     |  
  17. +------------------+------------------------------------------------------+  

然后,我们需要将router接入我们新创建的public network,使用下列命令创建:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron router-gateway-set my-router my-public  
  2. Set gateway for router my-router  

注意:我们在两种情况下使用术语“public network",一个是datacenter中真实的public network,为了区分我们把它(180.180.180.0/24)叫做"external public network"。另一个是openstack中我们使用的"public network",我们称之为“my-public"的接口网络。我们还涉及两个”gateways“,一个是外部Public network用的gateway(180.180.180.1),另一个是router中的gateway接口(180.180.180.2)。

执行上述的操作后,router上(之前已经拥有两个网络接口,连接两个不同的internal network)增加了第三个网络接口(被称作gateway)。router可以有多个网络接口,连接普通的internal subnet或者作为gateway连入“my-public"网络。一个经常犯的错误是,试图以通常网络接口的方式接入public network,操作可能成功,但是却并不能与外部网络连通。在我们创建一个public network,subnet并接入router,网络拓扑看起来是这样的:

router-public-net

进入router的namespace中,我们看到其中增加了一个180.180.180.0/24网段IP的网络接口,IP为180.180.180.2: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 ip addr  
  2. .  
  3. .  
  4. 22: qg-c08b8179-3b:  mtu 1500 qdisc noqueue state UNKNOWN                                                        
  5.     link/ether fa:16:3e:a4:58:40 brd ff:ff:ff:ff:ff:ff  
  6.     inet 180.180.180.2/24 brd 180.180.180.255 scope global qg-c08b8179-3b  
  7.     inet6 2606:b400:400:3441:f816:3eff:fea4:5840/64 scope global dynamic  
  8.        valid_lft 2591998sec preferred_lft 604798sec  
  9.     inet6 fe80::f816:3eff:fea4:5840/64 scope link  
  10.        valid_lft forever preferred_lft forever  
  11. .  
  12. .  

在这里router的gateway地址180.180.180.2与虚拟机是联通的,虚拟机可以ping到它。我们也能从虚拟机ping到外部网络的gateway180.180.180.1以及这个gateway所连的网络。如果我们查看router namespace,发现iptables的NAT talbe中有以下两行规则。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 iptables-save  
  2. .  
  3. .  
  4. -A neutron-l3-agent-snat -s 20.20.20.0/24 -j SNAT --to-source 180.180.180.2  
  5. -A neutron-l3-agent-snat -s 10.10.10.0/24 -j SNAT --to-source 180.180.180.2  
  6.    
  7. .  
  8. .  

因此,从net1或net2向外网发出的网络包,其源IP地址会被修改为180.180.180.2。我们可以在虚拟机中ping外网的某个地址,看下请求包的IP地址是否是这个IP地址。

namespace中的路由表会把所有外部流量路由到外网的gateway(180.180.180.1)。 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #  ip netns exec  qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 route -n  
  2. Kernel IP routing table  
  3. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  4. 0.0.0.0         180.180.180.1   0.0.0.0         UG    0      0        0 qg-c08b8179-3b  
  5. 10.10.10.0      0.0.0.0         255.255.255.0   U     0      0        0 qr-15ea2dd1-65  
  6. 20.20.20.0      0.0.0.0         255.255.255.0   U     0      0        0 qr-dc290da0-0a  
  7. 180.180.180.0   0.0.0.0         255.255.255.0   U     0      0        0 qg-c08b8179-3b  

虚拟机中发出的流向public network的请求,会被NAT映射为源地址为180.180.180.2,然后发给public network的gateway。同样,我们可以看到在namespace中ip forward功能是启动的。 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 sysctl net.ipv4.ip_forward  
  2. net.ipv4.ip_forward = 1  

Use case #6: Attaching a floating IP to a VM

现在,虚拟机可以访问public network。下一步,我们尝试允许外部客户访问Openstack环境中的虚拟机,通过floating IP可以完成这个功能。 Floating IP由外部网络提供,用户可以将它设置给虚拟机,从而允许外部客户接入虚拟机。

创建Floating IP,第一步是按照上一个usecase的讲解,将虚拟机连入外部网络。第二步时使用命令行,产生一个浮动IP。

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. # neutron floatingip-create public  
  2. Created a new floatingip:  
  3. +---------------------+--------------------------------------+  
  4. | Field               | Value                                |  
  5. +---------------------+--------------------------------------+  
  6. | fixed_ip_address    |                                      |  
  7. | floating_ip_address | 180.180.180.3                        |  
  8. | floating_network_id | 5eb99ac3-905b-4f0e-9c0f-708ce1fd2303 |  
  9. | id                  | 25facce9-c840-4607-83f5-d477eaceba61 |  
  10. | port_id             |                                      |  
  11. | router_id           |                                      |  
  12. | tenant_id           | 9796e5145ee546508939cd49ad59d51f     |  
  13. +---------------------+--------------------------------------+  
根据"my-public" network的能力,用户可以创建很多这样的IP。将浮动IP与虚拟机关联,可以通过命令行或者GUI完成。下图是GUI的例子:

connect-floatingip

在router namespace中我们可以看到,新增加了3跳iptabales规则: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -A neutron-l3-agent-OUTPUT -d 180.180.180.3/32 -j DNAT --to-destination 20.20.20.2  
  2. -A neutron-l3-agent-PREROUTING -d 180.180.180.3/32 -j DNAT --to-destination 20.20.20.2  
  3. -A neutron-l3-agent-float-snat -s 20.20.20.2/32 -j SNAT --to-source 180.180.180.3  
这些规则主要是对Floating IP进行NAT操作。对于router收到的目的地址为180.180.180.3的请求,会被转换成目标地址为20.20.20.2。反之亦然。

绑定Floating IP后,我们可以连接到虚拟机。需要确认安全组规则已经被设置,从而允许这样连接: 

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. nova secgroup-add-rule default icmp -1 -1 0.0.0.0/0  
  2. nova secgroup-add-rule default tcp 22 22 0.0.0.0/0  
这两条规则,允许ping和ssh。

Iptables是一个复杂而强大的工具。如果想更好的理解iptables规则,可以查看iptables的帮助文件。

Summary

本文介绍了如何将openstack环境中的虚拟机与public network连通。通过namespace和routing table,虚拟机不仅能在openstack环境内的不同网络间实现消息路由,还能与外部网络连通。

本文是这个系列文章的最后一篇。网络是opesntack最复杂的部分,是理解openstack的一个关键。阅读这四篇文章,对理解和分析openstack各种网络拓扑是很好的入门。使用我们提到的这些内容,可以更好的理解诸如Firewall as a service、Load Balance as a service、Metadata service这些网络概念。基本的学习方式是,进入namespace中,看究竟是如何利用Linux网络能力实现这些功能的。

我们在最开始说过,这些use case中我们只是使用了openstack众多网络配置方法的一种。我们的例子都是用了open vswitch 插件,可以独立于网络设备使用。通过与这里的例子对比,有助于分析其他的插件和功能。很多情况下,商业插件会使用open vswitch/bridges/namespace以及一些类似的方法和原理。

本系列文章的目的,在于让大多数用户了解oepnstack网络。文章中自下而上,使用一下简单的usecase,试着分析了openstack network 的整个结构以及如何工作的。与网上的其他一些资料不同,我们没有介绍各种openstack网络agent以及他们的功能,而是讲了他们做什么以及如何做的。下一步,你可以查阅这些资料,试着了解不同的agents是如何实现这些功能的。

全文结束。原文:http://blog.csdn.net/halcyonbaby/article/details/41524447

0 0