题记
昨天同事有一个需求,希望我创建一个固定IP的虚拟机,因为他的代码写死IP了,所以不好修改,我的云环境是OpenStack,但是OpenStack的虚拟机都是通过DHCP获得,那么如何
创建一个固定IP的虚拟机呢?
环境
OpenStack icehouse
网络模式:FlatDHCP
步骤
首先需要说明一下,OpenStack不支持,你创建一个其他VM后,然后手动更改配置文件设置IP,这个VM是无法访问的。
1、先查看OpenStack子网的IP范围,由于我使用了扁平DHCP,子网范围135-200,如果设置的IP地址不在该范围内,也无法访问。
root@controller:~# neutron subnet-list +--------------------------------------+------+-----------------+------------------------------------------------------+ | id | name | cidr | allocation_pools | +--------------------------------------+------+-----------------+------------------------------------------------------+ | 68e5be93-03a4-42c5-8867-5ea2eb81246f | | 192.168.12.0/24 | {"start": "192.168.12.135", "end": "192.168.12.200"} | +--------------------------------------+------+-----------------+------------------------------------------------------+
2、创建VM之前,我们需要创建一个端口(该端口需要了解在哪个租户下,哪个网络和子网下,设置什么IP)
查看租户信息,我直接使用admin租户
root@controller:~# keystone tenant-list +----------------------------------+---------+---------+ | id | name | enabled | +----------------------------------+---------+---------+ | 98061d7393bb48d1b5f4b4014e04d5dc | admin | True | | 20d00699486c498eac7a81e21e40c7fe | demo | True | | b09e7002151b4053aebcfea714ddc46b | service | True | +----------------------------------+---------+---------+
查看网络和内网信息
root@controller:~# neutron net-list +--------------------------------------+------------+------------------------------------------------------+ | id | name | subnets | +--------------------------------------+------------+------------------------------------------------------+ | dfb6737c-99ec-4956-b560-71769d1cc595 | sharednet1 | 68e5be93-03a4-42c5-8867-5ea2eb81246f 192.168.12.0/24 | +--------------------------------------+------------+------------------------------------------------------+
通过命令创建固定IP,例如192.168.12.150
neutron port-create --tenant-id 98061d7393bb48d1b5f4b4014e04d5dc(租户ID) --fixed-ip subnet_id=68e5be93-03a4-42c5-8867-5ea2eb81246f(子网ID),ip_address=192.168.12.150(固定IP) dfb6737c-99ec-4956-b560-71769d1cc595(网络ID)
root@controller:~# neutron port-create --tenant-id 98061d7393bb48d1b5f4b4014e04d5dc --fixed-ip subnet_id=68e5be93-03a4-42c5-8867-5ea2eb81246f,ip_address=192.168.12.150 dfb6737c-99ec-4956-b560-71769d1cc595 Created a new port: +-----------------------+---------------------------------------------------------------------------------------+ | Field | Value | +-----------------------+---------------------------------------------------------------------------------------+ | admin_state_up | True | | allowed_address_pairs | | | binding:host_id | | | binding:profile | {} | | binding:vif_details | {} | | binding:vif_type | unbound | | binding:vnic_type | normal | | device_id | | | device_owner | | | fixed_ips | {"subnet_id": "68e5be93-03a4-42c5-8867-5ea2eb81246f", "ip_address": "192.168.12.150"} | | id | 8bbe4913-e82b-4719-aab9-bf856ffb10be | | mac_address | fa:16:3e:44:79:cf | | name | | | network_id | dfb6737c-99ec-4956-b560-71769d1cc595 | | security_groups | e9943728-0046-40e1-a2be-d97caa68aab9 | | status | DOWN | | tenant_id | 98061d7393bb48d1b5f4b4014e04d5dc | +-----------------------+---------------------------------------------------------------------------------------+
接下来我们要创建VM,创建VM,我们需要使用哪个镜像,哪个云主机配置信息,设置哪个端口绑定,所以我们需要查看
镜像列表
root@controller:~# nova image-list +--------------------------------------+------------+--------+--------+ | ID | Name | Status | Server | +--------------------------------------+------------+--------+--------+ | 7725f89f-d537-4aa6-8532-1a1f8951532f | cirros | ACTIVE | | | e3c7c952-db4c-42ae-b429-8b8521b3bde7 | idesktop | ACTIVE | | | a8e84028-06a7-4d9e-9bb8-ea3152752104 | ubuntu1404 | ACTIVE | | +--------------------------------------+------------+--------+--------+
云主机配置列表
root@controller:~# nova flavor-list +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+ | ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+ | 1 | m1.tiny | 512 | 1 | 0 | | 1 | 1.0 | True | | 2 | m1.small | 2048 | 20 | 0 | | 1 | 1.0 | True | | 3 | m1.medium | 4096 | 40 | 0 | | 2 | 1.0 | True | | 4 | m1.large | 8192 | 80 | 0 | | 4 | 1.0 | True | | 5 | m1.xlarge | 16384 | 160 | 0 | | 8 | 1.0 | True | | b40e993f-196a-4854-8383-79a608a1d7dd | MQ | 4096 | 20 | 0 | | 2 | 1.0 | True | +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+
然后通过以下命令创建云主机
nova boot --flavor MQ(云主机配置) --image a8e84028-06a7-4d9e-9bb8-ea3152752104(镜像) --security_group default (安全组)--nic port-id=8bbe4913-e82b-4719-aab9-bf856ffb10be(端口映射) test150(VM名称)
root@controller:~# nova boot --flavor MQ --image a8e84028-06a7-4d9e-9bb8-ea3152752104 --security_group default --nic port-id=8bbe4913-e82b-4719-aab9-bf856ffb10be test150 +--------------------------------------+---------------------------------------------------+ | 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-00000010 | | 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 | Rdk3WD3g5EQx | | config_drive | | | created | 2016-11-02T09:08:59Z | | flavor | MQ (b40e993f-196a-4854-8383-79a608a1d7dd) | | hostId | | | id | 2c9efb18-18aa-4fce-bb41-ef8a900ea1fa | | image | ubuntu1404 (a8e84028-06a7-4d9e-9bb8-ea3152752104) | | key_name | - | | metadata | {} | | name | test150 | | os-extended-volumes:volumes_attached | [] | | progress | 0 | | security_groups | default | | status | BUILD | | tenant_id | 98061d7393bb48d1b5f4b4014e04d5dc | | updated | 2016-11-02T09:09:00Z | | user_id | f46ec9dacf2741e2a2f8789788785950 | +--------------------------------------+---------------------------------------------------+
查看虚拟机信息