创建和管理网络

在对OpenStack网络进行操作前,请先设置如下环境变量:

  1. export OS_USERNAME=admin
  2. export OS_PASSWORD=password
  3. export OS_TENANT_NAME=admin
  4. export OS_AUTH_URL=http://localhost:5000/v2.0

创建网络

  1. 列出neutron工具的扩展功能:
  1. $ neutron ext-list -c alias -c name
  2. +-----------------+--------------------------+
  3. | alias | name |
  4. +-----------------+--------------------------+
  5. | agent_scheduler | Agent Schedulers |
  6. | binding | Port Binding |
  7. | quotas | Quota management support |
  8. | agent | agent |
  9. | provider | Provider Network |
  10. | router | Neutron L3 Router |
  11. | lbaas | LoadBalancing service |
  12. | extraroute | Neutron Extra Route |
  13. +-----------------+--------------------------+
  1. 创建网络
  1. $ neutron net-create net1
  2. Created a new network:
  3. +---------------------------+--------------------------------------+
  4. | Field | Value |
  5. +---------------------------+--------------------------------------+
  6. | admin_state_up | True |
  7. | id | 2d627131-c841-4e3a-ace6-f2dd75773b6d |
  8. | name | net1 |
  9. | provider:network_type | vlan |
  10. | provider:physical_network | physnet1 |
  11. | provider:segmentation_id | 1001 |
  12. | router:external | False |
  13. | shared | False |
  14. | status | ACTIVE |
  15. | subnets | |
  16. | tenant_id | 3671f46ec35e4bbca6ef92ab7975e463 |
  17. +---------------------------+--------------------------------------+

注意:
使用net-create命令创建网络时,返回信息中的一些项是只有管理员才能看到的。

  1. 创建网络时,指定网络类型:
  1. $ neutron net-create net2 --provider:network-type local
  2. Created a new network:
  3. +---------------------------+--------------------------------------+
  4. | Field | Value |
  5. +---------------------------+--------------------------------------+
  6. | admin_state_up | True |
  7. | id | 524e26ea-fad4-4bb0-b504-1ad0dc770e7a |
  8. | name | net2 |
  9. | provider:network_type | local |
  10. | provider:physical_network | |
  11. | provider:segmentation_id | |
  12. | router:external | False |
  13. | shared | False |
  14. | status | ACTIVE |
  15. | subnets | |
  16. | tenant_id | 3671f46ec35e4bbca6ef92ab7975e463 |
  17. +---------------------------+--------------------------------------+

正如上面展示的一样,刚刚我们用到的那个--provider:network-type能用来创建一个local的provider network。

创建子网

  1. $ neutron subnet-create net1 192.168.2.0/24 --name subnet1
  2. Created a new subnet:
  3. +------------------+--------------------------------------------------+
  4. | Field | Value |
  5. +------------------+--------------------------------------------------+
  6. | allocation_pools | {"start": "192.168.2.2", "end": "192.168.2.254"} |
  7. | cidr | 192.168.2.0/24 |
  8. | dns_nameservers | |
  9. | enable_dhcp | True |
  10. | gateway_ip | 192.168.2.1 |
  11. | host_routes | |
  12. | id | 15a09f6c-87a5-4d14-b2cf-03d97cd4b456 |
  13. | ip_version | 4 |
  14. | name | subnet1 |
  15. | network_id | 2d627131-c841-4e3a-ace6-f2dd75773b6d |
  16. | tenant_id | 3671f46ec35e4bbca6ef92ab7975e463 |
  17. +------------------+--------------------------------------------------+

subnet-create命令有如下几个位置固定的参数,还有可选参数:

  • 这个子网从属的网络的名字或者ID 在本例中,net1这个参数是位置固定的。
  • 子网的CIDR 在本例中,192.168.2.0/24也是一个位置固定的参数,它标记了要创建的子网的CIDR。
  • 子网的名称 在本例中,--name subnet1指定了要创建的子网名。

创建路由

  1. 创建一个新路由
  1. $ neutron router-create router1
  2. Created a new router:
  3. +-----------------------+--------------------------------------+
  4. | Field | Value |
  5. +-----------------------+--------------------------------------+
  6. | admin_state_up | True |
  7. | external_gateway_info | |
  8. | id | 6e1f11ed-014b-4c16-8664-f4f615a3137a |
  9. | name | router1 |
  10. | status | ACTIVE |
  11. | tenant_id | 7b5970fbe7724bf9b74c245e66b92abf |
  12. +-----------------------+--------------------------------------+

记下返回的路由识别码,这个编码是唯一的,在稍后的步骤中我们将用到它。

  1. 将路由器连接到外部的provider network。
  1. $ neutron router-gateway-set ROUTER NETWORK

将这条命令中的ROUTER字段用刚刚的路由识别码代替,将NETWORK字段用唯一的外部provider network识别码代替。

  1. 将该路由和子网相连。
  1. $ neutron router-interface-add ROUTER SUBNET

将这条命令中的ROUTER字段用刚刚的路由识别码代替,将SUBNET字段用唯一的子网代码代替。

创建端口

  1. 在指定的IP地址上创建一个端口
  1. $ neutron port-create net1 --fixed-ip ip_address=192.168.2.40
  2. Created a new port:
  3. +----------------------+----------------------------------------------------------------------+
  4. | Field | Value |
  5. +----------------------+----------------------------------------------------------------------+
  6. | admin_state_up | True |
  7. | binding:capabilities | {"port_filter": false} |
  8. | binding:vif_type | ovs |
  9. | device_id | |
  10. | device_owner | |
  11. | fixed_ips | {"subnet_id": "15a09f6c-87a5-4d14-b2cf-03d97cd4b456", "ip_address... |
  12. | id | f7a08fe4-e79e-4b67-bbb8-a5002455a493 |
  13. | mac_address | fa:16:3e:97:e0:fc |
  14. | name | |
  15. | network_id | 2d627131-c841-4e3a-ace6-f2dd75773b6d |
  16. | status | DOWN |
  17. | tenant_id | 3671f46ec35e4bbca6ef92ab7975e463 |
  18. +----------------------+----------------------------------------------------------------------+

在前一个指令中,net1参数表示的是网络名,该参数的位置要固定。--fixed-ip ip_address=192.168.2.40则是可选的,指定了该端口的绑定的IP地址是哪一个。

注意: 在创建端口时,您可以指定任意一个在子网中的未分配的IP地址,即便它不在您的云供应商提供的地址池内也可以。

  1. 在不指定IP地址的情况下创建端口
  1. $ neutron port-create net1
  2. Created a new port:
  3. +----------------------+----------------------------------------------------------------------+
  4. | Field | Value |
  5. +----------------------+----------------------------------------------------------------------+
  6. | admin_state_up | True |
  7. | binding:capabilities | {"port_filter": false} |
  8. | binding:vif_type | ovs |
  9. | device_id | |
  10. | device_owner | |
  11. | fixed_ips | {"subnet_id": "15a09f6c-87a5-4d14-b2cf-03d97cd4b456", "ip_address... |
  12. | id | baf13412-2641-4183-9533-de8f5b91444c |
  13. | mac_address | fa:16:3e:f6:ec:c7 |
  14. | name | |
  15. | network_id | 2d627131-c841-4e3a-ace6-f2dd75773b6d |
  16. | status | DOWN |
  17. | tenant_id | 3671f46ec35e4bbca6ef92ab7975e463 |
  18. +----------------------+----------------------------------------------------------------------+

注意: 如果您在使用neutron port-create指令时不指定IP地址,系统会自动分配给您一个IP地址。

  1. 通过固定的IP地址来查询端口
  1. $ neutron port-list --fixed-ips ip_address=192.168.2.2 \
  2. ip_address=192.168.2.40
  3. +----------------+------+-------------------+-------------------------------------------------+
  4. | id | name | mac_address | fixed_ips |
  5. +----------------+------+-------------------+-------------------------------------------------+
  6. | baf13412-26... | | fa:16:3e:f6:ec:c7 | {"subnet_id"... ..."ip_address": "192.168.2.2"} |
  7. | f7a08fe4-e7... | | fa:16:3e:97:e0:fc | {"subnet_id"... ..."ip_address": "192.168.2.40"}|
  8. +----------------+------+-------------------+-------------------------------------------------+

其中,--fixed-ips ip_address=192.168.2.2 ip_address=192.168.2.40是这个命令的unknown option。

如何查找unknown option:unknown option可以通过观察create_xxx或者show_xxx的指令来查找。比如,在用创建端口的命令时,我们能看见fixed_ips这一项,它便可以用作unknown option。