前言
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隔离,进而提供租户和网络隔离,下图展示了我们如何进行配置和部署:
第一篇文章会略长,我们将聚焦于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的结构,使用如下命令:
- # ovs-vsctl show
- 7ec51567-ab42-49e8-906d-b854309c9edf
- Bridge br-int
- Port br-int
- Interface br-int
- type: internal
- Port "int-br-eth2"
- Interface "int-br-eth2"
- Bridge "br-eth2"
- Port "br-eth2"
- Interface "br-eth2"
- type: internal
- Port "eth2"
- Interface "eth2"
- Port "phy-br-eth2"
- Interface "phy-br-eth2"
- 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结构:
- # ovs-vsctl show
- efd98c87-dc62-422d-8f73-a68c2a14e73d
- Bridge br-int
- Port "int-br-eth2"
- Interface "int-br-eth2"
- Port br-int
- Interface br-int
- type: internal
- Port "qvocb64ea96-9f"
- tag: 1
- Interface "qvocb64ea96-9f"
- Bridge "br-eth2"
- Port "phy-br-eth2"
- Interface "phy-br-eth2"
- Port "br-eth2"
- Interface "br-eth2"
- type: internal
- Port "eth2"
- Interface "eth2"
- ovs_version: "1.11.0"
”br-int“网桥现在有了一个新的port"qvocb64ea96-9f" 连接VM,并且被标记为vlan1。虚拟机的每个网卡都需要对应在"br-int”网桥上创建一个port。
OVS中另一个有用的命令是dump-flows,以下为例子:
- # ovs-ofctl dump-flows br-int
- NXST_FLOW reply (xid=0x4):
- 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
- 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
- cookie=0x0, duration=76681.36s, table=0, n_packets=68, n_bytes=7950,idle_age=17, hard_age=65534, priority=1 actions=NORMAL
Network Namespaces (netns)
网络namespace是linux上一个很cool的特性,它的用途很多。在openstack网络中被广泛使用。网络namespace是拥有独立的网络配置隔离容器,并且该网络不能被其他名字空间看到。网络名字空间可以被用于封装特殊的网络功能或者在对网络服务隔离的同时完成一个复杂的网络设置。在Oracle OpenStack Tech Preview中我们使用最新的R3企业版内核,该内核提供给了对netns的完整支持。
通过如下例子我们展示如何使用netns命令控制网络namespaces。定义一个新的namespace:
- # ip netns add my-ns
- # ip netns list
- my-ns
我们说过namespace是一个隔离的容器,我们可以在namspace中进行各种操作,比如ifconfig命令。
- # ip netns exec my-ns ifconfig -a
- lo Link encap:Local Loopback
- LOOPBACK MTU:16436 Metric:1
- RX packets:0 errors:0 dropped:0 overruns:0 frame:0
- TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
- RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
- # ip netns exec my-ns ifconfig -a
- lo Link encap:Local Loopback
- LOOPBACK MTU:65536 Metric:1
- RX packets:0 errors:0 dropped:0 overruns:0 frame:0
- TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
- RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
- my-port Link encap:Ethernet HWaddr 22:04:45:E2:85:21
- BROADCAST MTU:1500 Metric:1
- RX packets:0 errors:0 dropped:0 overruns:0 frame:0
- TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
- RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
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对,注意我们定义了两端的名字:
- # ip link add veth0 type veth peer name veth1
- # ifconfig -a
- .
- .
- veth0 Link encap:Ethernet HWaddr 5E:2C:E6:03:D0:17
- BROADCAST MULTICAST MTU:1500 Metric:1
- RX packets:0 errors:0 dropped:0 overruns:0 frame:0
- TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
- veth1 Link encap:Ethernet HWaddr E6:B6:E2:6D:42:B8
- BROADCAST MULTICAST MTU:1500 Metric:1
- RX packets:0 errors:0 dropped:0 overruns:0 frame:0
- TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
- .
- .
- veth0 => veth1 =>br-eth3 => eth3 ======> eth2 on another Linux server
- # brctl addbr br-eth3
- # brctl addif br-eth3 eth3
- # brctl addif br-eth3 veth1
- # brctl show
- bridge name bridge id STP enabled interfaces
- br-eth3 8000.00505682e7f6 no eth3
- veth1
- # ifconfig veth0 50.50.50.50
- # ping -I veth0 50.50.50.51
- PING 50.50.50.51 (50.50.50.51) from 50.50.50.50 veth0: 56(84) bytes of data.
- 64 bytes from 50.50.50.51: icmp_seq=1 ttl=64 time=0.454 ms
- 64 bytes from 50.50.50.51: icmp_seq=2 ttl=64 time=0.298 ms
- # ethtool -S veth1
- NIC statistics:
- peer_ifindex: 12
- # ip link
- .
-
- .
- 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如下:
- 创建网络——我们创建网络时,发生了什么。如何创建多个隔离的网络。
- 创建虚拟机——一旦我们有了网络,我们可以创建虚拟机并将其接入网络。
- 虚拟机的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:
- # neutron net-create net1
- Created a new network:
- +---------------------------+--------------------------------------+
- | Field | Value |
- +---------------------------+--------------------------------------+
- | admin_state_up | True |
- | id | 5f833617-6179-4797-b7c0-7d420d84040c |
- | name | net1 |
- | provider:network_type | vlan |
- | provider:physical_network | default |
- | provider:segmentation_id | 1000 |
- | shared | False |
- | status | ACTIVE |
- | subnets | |
- | tenant_id | 9796e5145ee546508939cd49ad59d51f |
- +---------------------------+--------------------------------------+
为这个network创建subnet::
- # neutron subnet-create net1 10.10.10.0/24
- Created a new subnet:
- +------------------+------------------------------------------------+
- | Field | Value |
- +------------------+------------------------------------------------+
- | allocation_pools | {"start": "10.10.10.2", "end": "10.10.10.254"} |
- | cidr | 10.10.10.0/24 |
- | dns_nameservers | |
- | enable_dhcp | True |
- | gateway_ip | 10.10.10.1 |
- | host_routes | |
- | id | 2d7a0a58-0674-439a-ad23-d6471aaae9bc |
- | ip_version | 4 |
- | name | |
- | network_id | 5f833617-6179-4797-b7c0-7d420d84040c |
- | tenant_id | 9796e5145ee546508939cd49ad59d51f |
- +------------------+------------------------------------------------+
现在我们有了一个network和subnet,网络拓扑像这样:
现在让我们深入看下到底发生了什么?在控制节点,我们一个新的namespace被创建:
- # ip netns list
- qdhcp-5f833617-6179-4797-b7c0-7d420d84040c
这个namespace的名字是qdhcp- (参见上边),让我们深入namespace中看看有什么?
- # ip netns exec qdhcp-5f833617-6179-4797-b7c0-7d420d84040c ip addr
- 1: lo: mtu 65536 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 12: tap26c9b807-7c: mtu 1500 qdisc noqueue state UNKNOWN
- link/ether fa:16:3e:1d:5c:81 brd ff:ff:ff:ff:ff:ff
- inet 10.10.10.3/24 brd 10.10.10.255 scope global tap26c9b807-7c
- inet6 fe80::f816:3eff:fe1d:5c81/64 scope link
- valid_lft forever preferred_lft forever
我们发下在namespace下有两个网络接口,一个是loop设备,另一个叫“tap26c9b807-7c”。这个接口设置了IP地址10.10.10.3,他会接收dhcp请求(后边会讲)。接下来我们来跟踪下“tap26c9b807-7c”的网络连接性。我们从OVS上看下这个接口所连接的OVS网桥"br-int"。
- # ovs-vsctl show
- 8a069c7c-ea05-4375-93e2-b9fc9e4b3ca1
- Bridge "br-eth2"
- Port "br-eth2"
- Interface "br-eth2"
- type: internal
- Port "eth2"
- Interface "eth2"
- Port "phy-br-eth2"
- Interface "phy-br-eth2"
- Bridge br-ex
- Port br-ex
- Interface br-ex
- type: internal
- Bridge br-int
- Port "int-br-eth2"
- Interface "int-br-eth2"
- Port "tap26c9b807-7c"
- tag: 1
- Interface "tap26c9b807-7c"
- type: internal
- Port br-int
- Interface br-int
- type: internal
- ovs_version: "1.11.0"
由上可知,veth pair的两端“int-br-eth2” 和 "phy-br-eth2",这个veth pari连接两个OVS网桥"br-eth2"和"br-int"。上一篇文章中,我们解释过如何通过ethtool命令查看veth pairs的两端。就如下边的例子:
- # ethtool -S int-br-eth2
- NIC statistics:
- peer_ifindex: 10
- .
- .
- #ip link
- .
- .
- 10: phy-br-eth2: mtu 1500 qdisc pfifo_fast state UP qlen 1000
- .
- .
注意“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标签的修改:
- # ovs-ofctl dump-flows br-eth2
- NXST_FLOW reply (xid=0x4):
- 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
- 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
- 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标签的修改如下:
- # ovs-ofctl dump-flows br-int
- NXST_FLOW reply (xid=0x4):
- 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
- 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
- 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创建的例子:
挂载网络并启动虚拟机:
一旦虚拟机启动并运行,我们发下nova支持给虚拟机绑定IP:
- # nova list
- +--------------------------------------+--------------+--------+------------+-------------+-----------------+
- | ID | Name | Status | Task State | Power State | Networks |
- +--------------------------------------+--------------+--------+------------+-------------+-----------------+
- | 3707ac87-4f5d-4349-b7ed-3a673f55e5e1 | Oracle Linux | ACTIVE | None | Running | net1=10.10.10.2 |
- +--------------------------------------+--------------+--------+------------+-------------+-----------------+
nova list命令显示虚拟机在运行中,并被分配了IP 10.10.10.2。我们通过虚拟机定义文件,查看下虚拟机与虚拟机网络之间的连接性。虚拟机的配置文件在目录/var/lib/nova/instances//下可以找到。通过查看虚拟机定义文件,libvirt.xml,我们可以看到虚拟机连接到网络接口“tap53903a95-82”,这个网络接口连接到了Linux网桥 “qbr53903a95-82”:
- <interface type="bridge">
- <mac address="fa:16:3e:fe:c7:87"/>
- <source bridge="qbr53903a95-82"/>
- <target dev="tap53903a95-82"/>
- </interface>
- # brctl show
- bridge name bridge id STP enabled interfaces
- qbr53903a95-82 8000.7e7f3282b836 no qvb53903a95-82
- tap53903a95-82
网桥有两个网络接口,一个连接到虚拟机(“tap53903a95-82 “),另一个( “qvb53903a95-82”)连接到OVS网桥”br-int"。
- # ovs-vsctl show
- 3c42f80-77e9-46c8-8560-7697d76de51c
- Bridge "br-eth2"
- Port "br-eth2"
- Interface "br-eth2"
- type: internal
- Port "eth2"
- Interface "eth2"
- Port "phy-br-eth2"
- Interface "phy-br-eth2"
- Bridge br-int
- Port br-int
- Interface br-int
- type: internal
- Port "int-br-eth2"
- Interface "int-br-eth2"
- Port "qvb53903a95-82"
- tag: 3
- Interface "qvb53903a95-82"
- ovs_version: "1.11.0"
我们之前看过,OVS网桥“br-int"连接到"br-eth2",通过veth pair(int-br-eth2,phy-br-eth2 ),br-eth2连接到物理网卡eth2。整个流入如下:
- 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.
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:
namespace与虚拟机之间连通,并且可以互相ping通,对于定位问题非常有用。我们可以从虚拟机ping通namespace,可以使用tcpdump或其他工具定位网络中断问题。
为了响应虚拟机的dhcp请求,Neutron使用了”dnsmasq“的Linux工具,这个工具是一个轻量的DNS、DHCP服务,更多的信息请查看(http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html)。我们可以在控制节点通过PS命令看到:
- 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文件我们可以看到:
- # cat /var/lib/neutron/dhcp/5f833617-6179-4797-b7c0-7d420d84040c/host
- fa:16:3e:fe:c7:87,host-10-10-10-2.openstacklocal,10.10.10.2
- # ip netns exec qdhcp-5f833617-6179-4797-b7c0-7d420d84040c tcpdump -n
- 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
- 19:27:12.191666 IP 10.10.10.3.bootps > 10.10.10.2.bootpc: BOOTP/DHCP, Reply, length 325
总结
本文,我们基于之前讲解的各种网络组件,分析了三种use case下网络如何连通的。这些use cases对了解整个网络栈以及了解虚拟机/计算节点/DHCP namespace直接如何连通很有帮助。
根据我们的分析,我们确信启动虚拟机、虚拟机发出DHCP请求、虚拟机收到正确的IP后这个网络按照我们预想的工作。我们看到一个包经过一长串路径最终到达目的地,如果这一切成功,意味着这些组件功能正常。
下一篇文章中,我们会学习更复杂的neutron服务并分析他们如何工作。
前文中,我们学习了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上看到的网络拓扑图:
进一步探索,我们会在openstack网络节点上看到另一个namespace,这个namespace用于为新创建的网络提供服务。现在我们有两个namespace,每个network一个。
- # ip netns list
- qdhcp-63b7fcf2-e921-4011-8da9-5fc2444b42dd
- qdhcp-5f833617-6179-4797-b7c0-7d420d84040c
可以通过nova net-list查看network的ID信息,或者使用UI查看网络信息。
- # nova net-list
- +--------------------------------------+-------+------+
- | ID | Label | CIDR |
- +--------------------------------------+-------+------+
- | 5f833617-6179-4797-b7c0-7d420d84040c | net1 | None |
- | 63b7fcf2-e921-4011-8da9-5fc2444b42dd | net2 | None |
- +--------------------------------------+-------+------+
我们新创建的network,net2有自己的namespace,这个namespace与net1是分离的。在namespace中,我们可以看到两个网络接口,一个local,一个是用于DHCP服务。
- # ip netns exec qdhcp-63b7fcf2-e921-4011-8da9-5fc2444b42dd ip addr
- 1: lo: mtu 65536 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 19: tap16630347-45: mtu 1500 qdisc noqueue state UNKNOWN
- link/ether fa:16:3e:bd:94:42 brd ff:ff:ff:ff:ff:ff
- inet 20.20.20.3/24 brd 20.20.20.255 scope global tap16630347-45
- inet6 fe80::f816:3eff:febd:9442/64 scope link
- valid_lft forever preferred_lft forever
net1和net2两个network没有被联通,我们需要创建一个router,通过router将两个network联通。Openstack Neutron向用户提供了创建router并将两个或多个network连接的能力。router其实只是一个额外的namespace。使用Neutron创建router可以通过GUI或者命令行操作:
- # neutron router-create my-router
- Created a new router:
- +-----------------------+--------------------------------------+
- | Field | Value |
- +-----------------------+--------------------------------------+
- | admin_state_up | True |
- | external_gateway_info | |
- | id | fce64ebe-47f0-4846-b3af-9cf764f1ff11 |
- | name | my-router |
- | status | ACTIVE |
- | tenant_id | 9796e5145ee546508939cd49ad59d51f |
- +-----------------------+--------------------------------------+
现在我们将两个netwrok通过router连接:
查看subnet的ID:
- # neutron subnet-list
- +--------------------------------------+------+---------------+------------------------------------------------+
- | id | name | cidr | allocation_pools |
- +--------------------------------------+------+---------------+------------------------------------------------+
- | 2d7a0a58-0674-439a-ad23-d6471aaae9bc | | 10.10.10.0/24 | {"start": "10.10.10.2", "end": "10.10.10.254"} |
- | 4a176b4e-a9b2-4bd8-a2e3-2dbe1aeaf890 | | 20.20.20.0/24 | {"start": "20.20.20.2", "end": "20.20.20.254"} |
- +--------------------------------------+------+---------------+------------------------------------------------+
将subnet 10.10.10.0/24添加到router:
- # neutron router-interface-add fce64ebe-47f0-4846-b3af-9cf764f1ff11 subnet=2d7a0a58-0674-439a-ad23-d6471aaae9bc
- Added interface 0b7b0b40-f952-41dd-ad74-2c15a063243a to router fce64ebe-47f0-4846-b3af-9cf764f1ff11.
将subnet 20.20.20.0/24添加到router:
- # neutron router-interface-add fce64ebe-47f0-4846-b3af-9cf764f1ff11 subnet=4a176b4e-a9b2-4bd8-a2e3-2dbe1aeaf890
- Added interface dc290da0-0aa4-4d96-9085-1f894cf5b160 to router fce64ebe-47f0-4846-b3af-9cf764f1ff11.
此时,我们在查看网络拓扑会发现两个网络被router打通:
我们还可以发现两个网络接口连接到router,作为各自subnet的gateway。
我们可以看到为router创建的namespace。
- # ip netns list
- qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11
- qdhcp-63b7fcf2-e921-4011-8da9-5fc2444b42dd
- qdhcp-5f833617-6179-4797-b7c0-7d420d84040c
我们进入namespace内部可以看到:
- # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 ip addr
- 1: lo: mtu 65536 qdisc noqueue state UNKNOWN
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 20: qr-0b7b0b40-f9: mtu 1500 qdisc noqueue state UNKNOWN
- link/ether fa:16:3e:82:47:a6 brd ff:ff:ff:ff:ff:ff
- inet 10.10.10.1/24 brd 10.10.10.255 scope global qr-0b7b0b40-f9
- inet6 fe80::f816:3eff:fe82:47a6/64 scope link
- valid_lft forever preferred_lft forever
- 21: qr-dc290da0-0a: mtu 1500 qdisc noqueue state UNKNOWN
- link/ether fa:16:3e:c7:7c:9c brd ff:ff:ff:ff:ff:ff
- inet 20.20.20.1/24 brd 20.20.20.255 scope global qr-dc290da0-0a
- inet6 fe80::f816:3eff:fec7:7c9c/64 scope link
- valid_lft forever preferred_lft forever
我们看到两个网络接口,“qr-dc290da0-0a“ 和 “qr-0b7b0b40-f9。这两个网络接口连接到OVS上,使用两个network/subnet的gateway IP。
- # ovs-vsctl show
- 8a069c7c-ea05-4375-93e2-b9fc9e4b3ca1
- Bridge "br-eth2"
- Port "br-eth2"
- Interface "br-eth2"
- type: internal
- Port "eth2"
- Interface "eth2"
- Port "phy-br-eth2"
- Interface "phy-br-eth2"
- Bridge br-ex
- Port br-ex
- Interface br-ex
- type: internal
- Bridge br-int
- Port "int-br-eth2"
- Interface "int-br-eth2"
- Port "qr-dc290da0-0a"
- tag: 2
- Interface "qr-dc290da0-0a"
- type: internal
- Port "tap26c9b807-7c"
- tag: 1
- Interface "tap26c9b807-7c"
- type: internal
- Port br-int
- Interface br-int
- type: internal
- Port "tap16630347-45"
- tag: 2
- Interface "tap16630347-45"
- type: internal
- Port "qr-0b7b0b40-f9"
- tag: 1
- Interface "qr-0b7b0b40-f9"
- type: internal
- ovs_version: "1.11.0"
我们可以看到,这些接口连接到”br-int",并打上了所在network对应的VLAN标签。这里我们可以通过gateway地址(20.20.20.1)成功的ping通router namespace:
我们还可以看到IP地址为20.20.20.2可以ping通IP地址为10.10.10.2的虚拟机:
两个subnet通过namespace中的网络接口互相连通。在namespace中,Neutron将系统参数net.ipv4.ip_forward设置为1。命令查看如下:
- # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 sysctl net.ipv4.ip_forward
- 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”。
- # neutron net-create my-public --router:external=True
- Created a new network:
- +---------------------------+--------------------------------------+
- | Field | Value |
- +---------------------------+--------------------------------------+
- | admin_state_up | True |
- | id | 5eb99ac3-905b-4f0e-9c0f-708ce1fd2303 |
- | name | my-public |
- | provider:network_type | vlan |
- | provider:physical_network | default |
- | provider:segmentation_id | 1002 |
- | router:external | True |
- | shared | False |
- | status | ACTIVE |
- | subnets | |
- | tenant_id | 9796e5145ee546508939cd49ad59d51f |
- +---------------------------+--------------------------------------+
在我们的环境中,控制节点的eth3是一个没有绑定IP的网卡。我们使用它接入外部public network。因此我们将eth3加入OVS网桥"br-ex",Neutron会将虚拟机向外部网络的发送的网络包路由到这个bridge。
- # ovs-vsctl add-port br-ex eth3
- # ovs-vsctl show
- 8a069c7c-ea05-4375-93e2-b9fc9e4b3ca1
- .
- .
- .
- Bridge br-ex
- Port br-ex
- Interface br-ex
- type: internal
- Port "eth3"
- Interface "eth3"
- .
- .
- .
我们在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。
- # 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
- Created a new subnet:
- +------------------+------------------------------------------------------+
- | Field | Value |
- +------------------+------------------------------------------------------+
- | allocation_pools | {"start": "180.180.180.2", "end": "180.180.180.100"} |
- | cidr | 180.180.180.0/24 |
- | dns_nameservers | |
- | enable_dhcp | False |
- | gateway_ip | 180.180.180.1 |
- | host_routes | |
- | id | ecadf103-0b3b-46e8-8492-4c5f4b3ea4cd |
- | ip_version | 4 |
- | name | public_subnet |
- | network_id | 5eb99ac3-905b-4f0e-9c0f-708ce1fd2303 |
- | tenant_id | 9796e5145ee546508939cd49ad59d51f |
- +------------------+------------------------------------------------------+
然后,我们需要将router接入我们新创建的public network,使用下列命令创建:
- # neutron router-gateway-set my-router my-public
- 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的namespace中,我们看到其中增加了一个180.180.180.0/24网段IP的网络接口,IP为180.180.180.2:
- # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 ip addr
- .
- .
- 22: qg-c08b8179-3b: mtu 1500 qdisc noqueue state UNKNOWN
- link/ether fa:16:3e:a4:58:40 brd ff:ff:ff:ff:ff:ff
- inet 180.180.180.2/24 brd 180.180.180.255 scope global qg-c08b8179-3b
- inet6 2606:b400:400:3441:f816:3eff:fea4:5840/64 scope global dynamic
- valid_lft 2591998sec preferred_lft 604798sec
- inet6 fe80::f816:3eff:fea4:5840/64 scope link
- valid_lft forever preferred_lft forever
- .
- .
在这里router的gateway地址180.180.180.2与虚拟机是联通的,虚拟机可以ping到它。我们也能从虚拟机ping到外部网络的gateway180.180.180.1以及这个gateway所连的网络。如果我们查看router namespace,发现iptables的NAT talbe中有以下两行规则。
- # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 iptables-save
- .
- .
- -A neutron-l3-agent-snat -s 20.20.20.0/24 -j SNAT --to-source 180.180.180.2
- -A neutron-l3-agent-snat -s 10.10.10.0/24 -j SNAT --to-source 180.180.180.2
- .
- .
因此,从net1或net2向外网发出的网络包,其源IP地址会被修改为180.180.180.2。我们可以在虚拟机中ping外网的某个地址,看下请求包的IP地址是否是这个IP地址。
namespace中的路由表会把所有外部流量路由到外网的gateway(180.180.180.1)。
- # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 route -n
- Kernel IP routing table
- Destination Gateway Genmask Flags Metric Ref Use Iface
- 0.0.0.0 180.180.180.1 0.0.0.0 UG 0 0 0 qg-c08b8179-3b
- 10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 qr-15ea2dd1-65
- 20.20.20.0 0.0.0.0 255.255.255.0 U 0 0 0 qr-dc290da0-0a
- 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功能是启动的。
- # ip netns exec qrouter-fce64ebe-47f0-4846-b3af-9cf764f1ff11 sysctl net.ipv4.ip_forward
- 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。
- # neutron floatingip-create public
- Created a new floatingip:
- +---------------------+--------------------------------------+
- | Field | Value |
- +---------------------+--------------------------------------+
- | fixed_ip_address | |
- | floating_ip_address | 180.180.180.3 |
- | floating_network_id | 5eb99ac3-905b-4f0e-9c0f-708ce1fd2303 |
- | id | 25facce9-c840-4607-83f5-d477eaceba61 |
- | port_id | |
- | router_id | |
- | tenant_id | 9796e5145ee546508939cd49ad59d51f |
- +---------------------+--------------------------------------+
在router namespace中我们可以看到,新增加了3跳iptabales规则:
- -A neutron-l3-agent-OUTPUT -d 180.180.180.3/32 -j DNAT --to-destination 20.20.20.2
- -A neutron-l3-agent-PREROUTING -d 180.180.180.3/32 -j DNAT --to-destination 20.20.20.2
- -A neutron-l3-agent-float-snat -s 20.20.20.2/32 -j SNAT --to-source 180.180.180.3
绑定Floating IP后,我们可以连接到虚拟机。需要确认安全组规则已经被设置,从而允许这样连接:
- nova secgroup-add-rule default icmp -1 -1 0.0.0.0/0
- nova secgroup-add-rule default tcp 22 22 0.0.0.0/0
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