创建初始网络
在启动实例前,必须先创建必要的网络架构,让实例能连接之,这个架构包含外网(external network)和租户网络(tenant network)。下图为初始网络架构的示意图,展示了网络组件和从实例到外网的网络数据流线路:
External network
external network为实例提供访问Internet的入口。
以下操作在controller节点上完成。
创建external network
- 启动admin证书:
$ source admin-openrc.sh
- 创建网络:
<pre>$ neutron net-create ext-net --router:external True \
--provider:physical_network external --provider:network_type flat
Created a new network: | |
---|---|
Field | Value |
admin_state_up | True |
id | e6f3606d-2bf6-4b01-8fb4-c10d299dbe75 |
name | ext-net |
provider:network_type | flat |
provider:physical_network | external |
provider:segmentation_id | |
router:external | True |
shared | False |
status | ACTIVE |
subnets | |
tenant_id | 4f7806287c9a437e9cd912504ff71727 |
+---------------------------+--------------------------------------+</pre>
跟物理网络一样,虚拟网络也需要指定一个子网(subnet)。外网和network节点上连接外部接口的物理网络共享同一个子网和网关(The external network shares the same subnet and gateway associated with the physical network connected to the external interface on the network node. )。需要给这个子网指定单独的路由和IP地址,以防和外网上的其他设备产生冲突。
创建外网的子网
创建子网:
<pre>$ neutron subnet-create ext-net --name ext-subnet \
</pre>
--allocation-pool start=FLOATING_IP_START,end=FLOATING_IP_END \
--disable-dhcp --gateway EXTERNAL_NETWORK_GATEWAY EXTERNAL_NETWORK_CIDR
FLOATING_IP_START和FLOATING_IP_END分别为预定分配的浮动IP地址范围内的第一个和最后一个的IP地址。EXTERNAL_NETWORK_CIDR替换为物理网络相应的子网(Replace EXTERNAL_NETWORK_CIDR with the subnet associated with the physical network.)。EXTERNAL_NETWORK_GATEWAY替换为物理网络相应的网关,一般是以“.1”结尾的IP地址。关闭子网的DHCP选项,因为实例并不直接连接外网,浮动IP地址需要手动指定(You should disable DHCP on this subnet because instances do not connect directly to the external network and floating IP addresses require manual assignment.)。
在本文的网络配置条件下,上述命令应如下:
<pre>$ neutron subnet-create ext-net --name ext-subnet \
--allocation-pool start=192.168.100.101,end=192.168.100.200 \
--disable-dhcp --gateway 192.168.100.1 192.168.100.0/24
Created a new subnet: | |
---|---|
Field | Value |
allocation_pools | {"start": "192.168.100.101", "end": "192.168.100.200"} |
cidr | 192.168.100.0/24 |
dns_nameservers | |
enable_dhcp | False |
gateway_ip | 192.168.100.1 |
host_routes | |
id | 963754fb-73c4-4a1b-93ee-27d2c2beb97a |
ip_version | 4 |
ipv6_address_mode | |
ipv6_ra_mode | |
name | ext-subnet |
network_id | e6f3606d-2bf6-4b01-8fb4-c10d299dbe75 |
tenant_id | 4f7806287c9a437e9cd912504ff71727 |
+-------------------+--------------------------------------------------------+</pre>
租户网络
租户网络为了实例提供了内部网络之间相互访问的功能。这个架构把各个租户的网络相互隔离了。例如,demo
租户使用这种网络后,只有属于这个租户的实例可以访问这个网络。
以下操作在controller节点上完成。
创建租户网络
- 启动demo证书:
$ source demo-openrc.sh
- 创建网络:
<pre>$ neutron net-create demo-net
Created a new network: | |
---|---|
Field | Value |
admin_state_up | True |
id | 830379a4-cc69-4165-a18f-f9430d999d5f |
name | demo-net |
router:external | False |
shared | False |
status | ACTIVE |
subnets | |
tenant_id | d1f7caccc65840b68258997a759da07f |
+-----------------+--------------------------------------+</pre>
如同外网网络,租户网络也需要专属的子网。不过可以指定任意有效的子网,因为这个架构将各个租户网络隔离开了。默认这个子网会使用DHCP来分配实例的IP地址。
创建租户网络的子网
创建子网:
<pre>$ neutron subnet-create demo-net --name demo-subnet \
</pre>
--gateway TENANT_NETWORK_GATEWAY TENANT_NETWORK_CIDR
将TENANT_NETWORK_CIDR替换为分配给租户网络的子网,TENANT_NETWORK_GATEWAY替换为分配的子网的网关,一般是以“.1”结尾的IP地址。
本文的网络配置下,上述命令如下:
<pre>$ neutron subnet-create demo-net --name demo-subnet \
--gateway 192.162.1.1 192.162.1.0/24
Created a new subnet: | |
---|---|
Field | Value |
allocation_pools | {"start": "192.162.1.2", "end": "192.162.1.254"} |
cidr | 192.162.1.0/24 |
dns_nameservers | |
enable_dhcp | True |
gateway_ip | 192.162.1.1 |
host_routes | |
id | a6593d6d-1992-4207-ae61-7784f0aa5a3c |
ip_version | 4 |
ipv6_address_mode | |
ipv6_ra_mode | |
name | demo-subnet |
network_id | 830379a4-cc69-4165-a18f-f9430d999d5f |
tenant_id | d1f7caccc65840b68258997a759da07f |
+-------------------+--------------------------------------------------+</pre>
一个虚拟路由器可以让两个或多个虚拟网络之间通信。每个路由器需要一个或多个接口(或包括网关)来访问指定网络。在本文,创建一个路由器并把租户网络和外网连接到它上面。
创建路由器并将租户网络和外网连接到上面
- 创建路由器:
<pre>$ neutron router-create demo-router
Created a new router: | |
---|---|
Field | Value |
admin_state_up | True |
external_gateway_info | |
id | 74be9215-abad-4d63-ad75-9d365ae6ad6a |
name | demo-router |
routes | |
status | ACTIVE |
tenant_id | d1f7caccc65840b68258997a759da07f |
+-----------------------+--------------------------------------+</pre>
- 把路由和demo租户网络子网连接起来:
<pre>$ neutron router-interface-add demo-router demo-subnet
Added interface 0c38b462-9f15-4fc4-abdf-9039048c733d to router demo-router.</pre>
- 把路由和外网连接起来,通过把外网设为路由的网关(Attach the router to the external network by setting it as the gateway):
<pre>$ neutron router-gateway-set demo-router ext-net
Set gateway for router demo-router</pre>
验证操作
外网的子网是192.168.100.0/24,由于租户路由网关理论上在其浮动IP地址范围中是占据了最小的(lowest)IP地址,即192.168.100.101。要验证外部物理网络和虚拟网络设置的正确与否,从任意的外部网络的主机上可以ping通192.168.100.101即可验证。
在创建虚拟机的主机上ping上述IP:
<pre>Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:UsersTiger>ping 192.168.100.101
正在 Ping 192.168.100.101 具有 32 字节的数据:
来自 192.168.100.101 的回复: 字节=32 时间=2ms TTL=64
来自 192.168.100.101 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.101 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.101 的回复: 字节=32 时间<1ms TTL=64
192.168.100.101 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 2ms,平均 = 0ms</code></pre>