如何将openstack环境中的虚拟机与public network连通

这期内容当中小编将会给大家带来有关如何将openstack环境中的虚拟机与public 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     |
+---------------------------+--------------------------------------+


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

# ovs-vsctl add-port br-ex eth4
# ovs-vsctl show
8a069c7c-ea05-4375-93e2-b9fc9e4b3ca1
.
.
.
    Bridge br-ex
        Port br-ex
            Interface br-ex
                type: internal
        Port "eth4"
            Interface "eth4"
.
.
.



我们在eth4上创建了一个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,网络拓扑看起来是这样的:

如何将openstack环境中的虚拟机与public network连通  openstack 第1张

进入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     |
+---------------------+--------------------------------------+


根据"my-public" network的能力,用户可以创建很多这样的IP。将浮动IP与虚拟机关联,可以通过命令行或者GUI完成。下图是GUI的例子:

如何将openstack环境中的虚拟机与public network连通  openstack 第2张

在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进行NAT操作。对于router收到的目的地址为180.180.180.3的请求,会被转换成目标地址为20.20.20.2。反之亦然。

绑定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


这两条规则,允许ping和ssh。

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

上述就是小编为大家分享的如何将openstack环境中的虚拟机与public network连通了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注蜗牛博客行业资讯频道。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接