1、我们目前使用openstack+docker来搭建自己的私有云
2、openstack有两种网络环境搭建模式,一种是功能较简单的nova-network,一种是neutron方案
3、neutron方案代表着未来的趋势,提供更多高级的功能(例如路由功能和负载均衡服务等),更加方便用户去自定义自己的虚拟化网络
4、在已有的几个集群中,我们在线下开发测试环境中搭建了neutron方案的openstack集群
neutron的设计理念是ip分配应当资源池化,因此在默认的dashboard操作界面上,只能为每个虚拟机指定特定的子网,虚拟机启动时会自动分配该子网可用的ip资源。但是,在很多开发测试场景下,我们还是需要为指定启动的虚拟机配备一个固定的ip,比如需要反复创建、删除虚拟机,这个时候就会希望虚拟机的ip不变,方便测试用例的编写,固定的ip地址也有利于CMDB的管理。
所有的操作都基于openstack项目提供的NeutronClient和NovaClient工具,请确保这两个工具已经安装,可以登录openstack的集群管理节点,在命令行界面执行nova和neutron命令,看是否有帮助提示。执行这两个工具,还需要预先获得权限,例如在管理节点上执行这两个命令的话,首先要执行以下命令,执行后就会在环境变量中保存有admin用户的权限信息:
# source /root/keystonerc_admin
neutron通过修改dhcp服务器的配置文件实现给指定虚拟机配置固定ip,因此要首先要确保虚拟机准备连接的子网的DHCP功能已开启:
neutron subnet-update --enable-dhcp SUBNET_ID
例子:
1.先查询已有的子网,确定哪个是要分配固定ip的子网
# neutron subnet-list
+--------------------------------------+------------+----------------+--------------------------------------------------+ | id | name | cidr | allocation_pools |
+--------------------------------------+------------+----------------+--------------------------------------------------+ | ddc1dc34-af0f-4e49-9c3a-53a1f7de58cb | subnet-331 | 10.13.131.0/24 | {"start": "10.13.131.2", "end": "10.13.131.254"} |
+--------------------------------------+------------+----------------+--------------------------------------------------+
2.为指定子网开启dhcp功能
neutron subnet-update --enable-dhcp ddc1dc34-af0f-4e49-9c3a-53a1f7de58cb
3.查看子网的详细信息,确认子网的dhcp功能已经开启
# neutron subnet-show ddc1dc34-af0f-4e49-9c3a-53a1f7de58cb
+------------------+--------------------------------------------------+
| Field | Value |
+------------------+--------------------------------------------------+
| allocation_pools | {"start": "10.13.131.2", "end": "10.13.131.254"} |
| cidr | 10.13.131.0/24 |
| dns_nameservers | |
| enable_dhcp | True |
| gateway_ip | 10.13.131.1 |
| host_routes | |
| id | ddc1dc34-af0f-4e49-9c3a-53a1f7de58cb |
| ip_version | 4 |
| name | subnet-331 |
| network_id | 25565459-b4d4-4823-8056-61d8207d1a91 |
| tenant_id | d289cbffe30044ff949aff875f46708a |
+------------------+--------------------------------------------------+
1.创建一个和指定子网相关联的端口,并为该端口配置一个固定ip,具体命令格式:
neutron port-create --fixed-ip subnet_id=SUBNET_ID,ip_address=IP_FROM_POOL --name PORT_NAME NETWORK_ID
固定ip由自己决定分配哪一个ip,只要这个ip在子网的可用ip范围内,且该ip还未被使用即可。PORT_NAME自行命名,NETWORK_ID可在前面的neutron subnet-show命令的执行结果中找到。
例子:
# neutron port-create --fixed-ip subnet_id=ddc1dc34-af0f-4e49-9c3a-53a1f7de58cb,ip_address=10.13.131.10 --name test-port1 25565459-b4d4-4823-8056-61d8207d1a91
Created a new port:
+-----------------------+-------------------------------------------------------------------------------------+
| Field | Value |
+-----------------------+-------------------------------------------------------------------------------------+
| admin_state_up | True |
| allowed_address_pairs | |
| binding:host_id | |
| binding:vif_details | {"port_filter": true, "ovs_hybrid_plug": true} |
| binding:vif_type | ovs |
| device_id | |
| device_owner | |
| fixed_ips | {"subnet_id": "ddc1dc34-af0f-4e49-9c3a-53a1f7de58cb", "ip_address": "10.13.131.10"} |
| id | dd913a9f-c452-4262-b66d-44f330df214c |
| mac_address | fa:16:3e:c0:bc:0b |
| name | test-port1 |
| network_id | 25565459-b4d4-4823-8056-61d8207d1a91 |
| security_groups | 792fc95b-a2df-496d-b32c-18b1fa0ed593 |
| status | DOWN |
| tenant_id | b757b12815504b4799936a8620379d91 |
+-----------------------+-------------------------------------------------------------------------------------+
2.启动虚拟机,并在参数中指定要将虚拟机绑定到刚创建的port上,这样虚拟机就会被自动配置port已设置的ip,具体命令格式:
nova boot --flavor FLAVOR_ID --image IMAGE_ID --nic port-id=PORT_ID INSTANCE_NAME
例子:
# nova boot --flavor 4core-8g --image mgj-themis-20150409 --nic port-id=dd913a9f-c452-4262-b66d-44f330df214c test_fixedip_vm1
+--------------------------------------+------------------------------------------------------------+
| Property | Value |
+--------------------------------------+------------------------------------------------------------+
| OS-DCF:diskConfig | MANUAL |
| OS-EXT-AZ:availability_zone | nova |
| OS-EXT-SRV-ATTR:host | - |
| OS-EXT-SRV-ATTR:hypervisor_hostname | - |
| OS-EXT-SRV-ATTR:instance_name | instance-000000a3 |
| OS-EXT-STS:power_state | 0 |
| OS-EXT-STS:task_state | scheduling |
| OS-EXT-STS:vm_state | building |
| OS-SRV-USG:launched_at | - |
| OS-SRV-USG:terminated_at | - |
| accessIPv4 | |
| accessIPv6 | |
| adminPass | BCNBe28E4wMb |
| config_drive | |
| created | 2015-06-22T16:46:29Z |
| flavor | 4core-8g (cb4fe661-4dec-4220-bdb0-993df3ea0968) |
| hostId | |
| id | 8b616e21-98ff-4db1-be4e-167bc412f0ee |
| image | mgj-themis-20150409 (ab8dea83-6c46-45b8-a700-81371004b7a8) |
| key_name | - |
| metadata | {} |
| name | test_fixedip_vm1 |
| os-extended-volumes:volumes_attached | [] |
| progress | 0 |
| qos_info | {} |
| security_groups | default |
| status | BUILD |
| tenant_id | b757b12815504b4799936a8620379d91 |
| updated | 2015-06-22T16:46:29Z |
| user_id | 090c11178ffb49f387f54a360bfe7151 |
+--------------------------------------+------------------------------------------------------------+
至此,neutron环境下指定固定ip的虚拟机启动成功,可以ping一下指定的ip地址验证是否生效!