最近遇到的问题是nova-network创建的网络,一旦使用后无法修改,若要删除重新建,还需要删除掉所有的虚拟机,甚是麻烦,由于创建时候设立了start和end,就想看看能不能通过修改END来扩容,最后发现无论用哪种接口,都无法查到某个网络的end IP,只好一步一步来看看到底是怎么回事。
当然,最后的结果是,发现根本就没有存end ip,不过还是把过程拿出来分享一下吧。这里网络是flatdhcp,单HOST

先找一下api中创建网络的接口,大概是这样子的:

nova_network.networks_create(request,
              label=data['name'],                                                              cidr=data['cidr'],  
allowed_start=data['startIP'],                                 allowed_end=data['endIP'],                                 bridge='br100')

继续查:

nova.novaclient(request).networks.create(**kwargs)

看/usr/lib/python2.7/site-packages/novaclient/v2/client.py

self.networks = networks.NetworkManager(self)

/usr/lib/python2.7/site-packages/novaclient/v2/networks.py

class NetworkManager(base.ManagerWithFind):
"""
Manage :class:`Network` resources.
"""
resource_class = Network
.......
    def create(self, **kwargs):
    """
    Create (allocate) a network. The following parameters are
    optional except for label; cidr or cidr_v6 must be specified, too.

    :param label: str
    :param bridge: str
    :param bridge_interface: str
    :param cidr: str
    :param cidr_v6: str
    :param dns1: str
    :param dns2: str
    :param fixed_cidr: str
    :param gateway: str
    :param gateway_v6: str
    :param multi_host: str
    :param priority: str
    :param project_id: str
    :param vlan: int
    :param vlan_start: int
    :param vpn_start: int
    :param mtu: int
    :param enable_dhcp: int
    :param dhcp_server: str
    :param share_address: int
    :param allowed_start: str
    :param allowed_end: str

    :rtype: object of :class:`Network`
    """
    body = {"network": kwargs}
    return self._create('/os-networks', body, 'network')

请求这就算发出去了

看看服务端代码:

[root@host81191 ~]# cat /usr/bin/nova-network
#!/usr/bin/python
# PBR Generated from u'console_scripts'

import sys

from nova.cmd.network import main

if __name__ == "__main__":
sys.exit(main())

/usr/lib/python2.7/site-packages/nova/network/manager.py

 def create_networks(self, context,
                    label, cidr=None, multi_host=None, num_networks=None,
                    network_size=None, cidr_v6=None,
                    gateway=None, gateway_v6=None, bridge=None,
                    bridge_interface=None, dns1=None, dns2=None,
                    fixed_cidr=None, allowed_start=None,
                    allowed_end=None, **kwargs):


def _do_create_networks(self, context,
                        label, cidr, multi_host, num_networks,
                        network_size, cidr_v6, gateway, gateway_v6, bridge,
                        bridge_interface, dns1=None, dns2=None,
                        fixed_cidr=None, mtu=None, dhcp_server=None,
                        enable_dhcp=None, share_address=None,
                        allowed_start=None, allowed_end=None, **kwargs):

跟ALLOWED_END相关代码

 if allowed_end:
                val = self._index_of(subnet_v4, allowed_end)
                top_reserved = subnet_v4.size - 1 - val

得到一个top_reserved

if cidr and subnet_v4:
            self._create_fixed_ips(context, net.id, fixed_cidr,
                                   extra_reserved, bottom_reserved,
                                   top_reserved)

继续看:

 def _create_fixed_ips(self, context, network_id, fixed_cidr=None,
                      extra_reserved=None, bottom_reserved=0,
                      top_reserved=0):
    """Create all fixed ips for network."""
    network = self._get_network_by_id(context, network_id)
    if extra_reserved is None:
        extra_reserved = []
    if not fixed_cidr:
        fixed_cidr = netaddr.IPNetwork(network['cidr'])
    num_ips = len(fixed_cidr)
    ips = []
    for index in range(num_ips):
        address = str(fixed_cidr[index])
        if (index < bottom_reserved or num_ips - index <= top_reserved or
            address in extra_reserved):
            reserved = True
        else:
            reserved = False

        ips.append({'network_id': network_id,
                    'address': address,
                    'reserved': reserved})
    objects.FixedIPList.bulk_create(context, ips)

大意似乎是按照最大值的IP然后把所有IP都存在了数据库,而没有存什么endIP具体是哪个

去数据库看看数据如何
例如添加网络
10.1.80.0/22
start:10.1.81.214
end:10.1.81.220

[root@host81191 mnt]# nova net-list
+--------------------------------------+-------+--------------+
| ID                                   | Label | CIDR         |
+--------------------------------------+-------+--------------+
| c163d2e7-a8a2-47b7-a84f-665ea84cda6b | net1  | 10.1.80.0/22 |
+--------------------------------------+-------+--------------+
[root@host81191 mnt]# nova network-show net1
+---------------------+--------------------------------------+
| Property            | Value                                |
+---------------------+--------------------------------------+
| bridge              | br100                                |
| bridge_interface    | eno1                                 |
| broadcast           | 10.1.83.255                          |
| cidr                | 10.1.80.0/22                         |
| cidr_v6             | -                                    |
| created_at          | 2016-02-26T08:15:22.000000           |
| deleted             | False                                |
| deleted_at          | -                                    |
| dhcp_server         | 10.1.81.214                          |
| dhcp_start          | 10.1.81.215                          |
| dns1                | 8.8.4.4                              |
| dns2                | -                                    |
| enable_dhcp         | True                                 |
| gateway             | 10.1.81.214                          |
| gateway_v6          | -                                    |
| host                | -                                    |
| id                  | c163d2e7-a8a2-47b7-a84f-665ea84cda6b |
| injected            | False                                |
| label               | net1                                 |
| mtu                 | -                                    |
| multi_host          | False                                |
| netmask             | 255.255.252.0                        |
| netmask_v6          | -                                    |
| priority            | -                                    |
| project_id          | -                                    |
| rxtx_base           | -                                    |
| share_address       | False                                |
| updated_at          | -                                    |
| vlan                | -                                    |
| vpn_private_address | -                                    |
| vpn_public_address  | -                                    |
| vpn_public_port     | -                                    |
+---------------------+--------------------------------------+

查nova数据库里的fixed_ips;
MariaDB [nova]> select count(*) from fixed_ips;

+----------+
| count(*) |
+----------+
|     1024 |
+----------+
1 row in set (0.00 sec)

发现创建了从10.1.80.0到10.1.83.255一共1024个IP地址

select * from fixed_ips where reserved = 0;
+---------------------+------------+------------+-----+-------------+------------+-----------+--------+----------+----------------------+------+---------------+---------+
| created_at          | updated_at | deleted_at | id  | address     | network_id | allocated | leased | reserved | virtual_interface_id | host | instance_uuid | deleted |
+---------------------+------------+------------+-----+-------------+------------+-----------+--------+----------+----------------------+------+---------------+---------+
| 2016-02-26 08:15:22 | NULL       | NULL       | 472 | 10.1.81.215 |          1 |         0 |      0 |        0 |                 NULL | NULL | NULL          |       0 |
| 2016-02-26 08:15:22 | NULL       | NULL       | 473 | 10.1.81.216 |          1 |         0 |      0 |        0 |                 NULL | NULL | NULL          |       0 |
| 2016-02-26 08:15:22 | NULL       | NULL       | 474 | 10.1.81.217 |          1 |         0 |      0 |        0 |                 NULL | NULL | NULL          |       0 |
| 2016-02-26 08:15:22 | NULL       | NULL       | 475 | 10.1.81.218 |          1 |         0 |      0 |        0 |                 NULL | NULL | NULL          |       0 |
| 2016-02-26 08:15:22 | NULL       | NULL       | 476 | 10.1.81.219 |          1 |         0 |      0 |        0 |                 NULL | NULL | NULL          |       0 |
| 2016-02-26 08:15:22 | NULL       | NULL       | 477 | 10.1.81.220 |          1 |         0 |      0 |        0 |                 NULL | NULL | NULL          |       0 |
+---------------------+------------+------------+-----+-------------+------------+-----------+--------+----------+----------------------+------+---------------+---------+

其中start—end之间的IP reserved 字段为0,而其他IP 此字段为1,也就是说其他IP都是保留的IP不可用

创建一个虚拟机试试

 nova list
+--------------------------------------+-------+--------+------------+-------------+------------------+
| ID                                   | Name  | Status | Task State | Power State | Networks         |
+--------------------------------------+-------+--------+------------+-------------+------------------+
| ab417044-d1be-4d1a-af91-7bb38a018128 | test1 | ACTIVE | -          | Running     |                  |
| 5c3fa1bf-4140-4699-9c5b-2fb833ab9851 | test2 | ACTIVE | -          | Running     | net1=10.1.81.215 |
+--------------------------------------+-------+--------+------------+-------------+------------------+

发现215的IP已经有了信息

MariaDB [nova]> select id,address,network_id,allocated,leased,reserved,virtual_interface_id,instance_uuid,deleted from fixed_ips where reserved = 0;
+-----+-------------+------------+-----------+--------+----------+----------------------+--------------------------------------+---------+
| id  | address     | network_id | allocated | leased | reserved | virtual_interface_id | instance_uuid                        | deleted |
+-----+-------------+------------+-----------+--------+----------+----------------------+--------------------------------------+---------+
| 472 | 10.1.81.215 |          1 |         1 |      1 |        0 |                    1 | 5c3fa1bf-4140-4699-9c5b-2fb833ab9851 |       0 |
| 473 | 10.1.81.216 |          1 |         0 |      0 |        0 |                 NULL | NULL                                 |       0 |
| 474 | 10.1.81.217 |          1 |         0 |      0 |        0 |                 NULL | NULL                                 |       0 |
| 475 | 10.1.81.218 |          1 |         0 |      0 |        0 |                 NULL | NULL                                 |       0 |
| 476 | 10.1.81.219 |          1 |         0 |      0 |        0 |                 NULL | NULL                                 |       0 |
| 477 | 10.1.81.220 |          1 |         0 |      0 |        0 |                 NULL | NULL                                 |       0 |
+-----+-------------+------------+-----------+--------+----------+----------------------+--------------------------------------+---------+
6 rows in set (0.01 sec)

我想,如果单纯想让某个IP能用,直接修改reserved 应该是可行的,先探讨到这里,下次再继续。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐