1. 背景需求
在openstack中,nova负责openstack虚拟机的生命周期的管理,neutron则负责虚拟机的网络管理工作,默认情况下,创建一台虚拟机,nova会根据nova-scheduler调度算法,选择一台最合适的compute节点,同时会从neutron的地址池中分配一个ip地址给虚拟机,从而完成虚拟机的创建过程。
  然而,在一些特殊的场景中,如相同业务的虚拟机,希望落在不通的compute节点上,为虚拟机分配原有的ip地址,此时通过nova内置的调度是难以实现的,或者在一些应用场景,基于企业的CMDB联动,让相同业务的虚拟机,散落在openstack nova中的不同节点,从而减小down机的几率,减少故障的发生。本文以在指定的compute节点创建instance,并为其分配一个固定的ip地址为例。
2. 获取创建instance的基本信息
获取镜像image id
[root@controller ~]# glance image-list +--------------------------------------+--------------------------+-------------+------------------+-------------+--------+ | ID | Name | Disk Format | Container Format | Size | Status | +--------------------------------------+--------------------------+-------------+------------------+-------------+--------+ | 37aaedc7-6fe6-4fc8-b110-408d166b8e51 | cirrors | qcow2 | bare | 13200896 | active | #需要创建instance的image id | ff0f7d03-a553-4357-a819-c74e913d649f | win2k8 | qcow2 | bare | 3391881216 | active | +--------------------------------------+--------------------------+-------------+------------------+-------------+--------+
2. 获取套餐flavor id
[root@controller ~]# nova flavor-list +--------------------------------------+------------------+-----------+------+-----------+------+-------+-------------+-----------+ | ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | +--------------------------------------+------------------+-----------+------+-----------+------+-------+-------------+-----------+ | 1 | m1.large | 8192 | 100 | 10 | | 4 | 1.0 | True | | 10 | V.GF1 | 8192 | 10 | 0 | | 4 | 1.0 | True | #需要创建的flavor id
3. 获取安全组id
[root@controller ~]# nova secgroup-list +--------------------------------------+---------+-------------+ | Id | Name | Description | +--------------------------------------+---------+-------------+ | 663468d9-73b1-4b04-8d4c-dac1bf21a94d | default | default | #secgroup的id号码 +--------------------------------------+---------+-------------+
4. 获取keypair
[root@controller ~]# nova keypair-list +------+-------------------------------------------------+ | Name | Fingerprint | +------+-------------------------------------------------+ | KEY | 15:63:f2:de:74:53:a1:03:eb:36:27:e6:d9:00:41:22 | #keypair的名字 +------+-------------------------------------------------+
5. 获取neutron网络id号码
[root@controller ~]# neutron net-list +--------------------------------------+---------------+-------------------------------------------------------+ | id | name | subnets | +--------------------------------------+---------------+-------------------------------------------------------+ | 99c68a93-336a-4605-aa78-343d41ca1206 | vmTest | 79cb82a1-eac1-4311-8e6d-badcabd22e44 192.168.100.0/24 | #需要启动的网络id号 +--------------------------------------+---------------+-------------------------------------------------------+
6. 获取coompute的主机名和zone名称
[root@controller ~]# nova service-list +------------------+-------------------------+-------------------+---------+-------+----------------------------+-----------------+ | Binary | Host | Zone | Status | State | Updated_at | Disabled Reason | +------------------+-------------------------+-------------------+---------+-------+----------------------------+-----------------+ | nova-conductor | ChuangYiYuan_10_16_2_8 | internal | enabled | up | 2016-01-23T04:54:58.000000 | - | | nova-cert | ChuangYiYuan_10_16_2_8 | internal | enabled | up | 2016-01-23T04:54:52.000000 | - | | nova-consoleauth | ChuangYiYuan_10_16_2_8 | internal | enabled | up | 2016-01-23T04:54:57.000000 | - | | nova-scheduler | ChuangYiYuan_10_16_2_8 | internal | enabled | up | 2016-01-23T04:54:57.000000 | - | | nova-compute | ChuangYiYuan_10_16_2_11 | ChuangYiYuanZone1 | enabled | up | 2016-01-23T04:54:56.000000 | - | #需要启动的compute节点 | nova-compute | ChuangYiYuan_10_16_2_14 | ChuangYiYuanZone1 | enabled | up | 2016-01-23T04:54:56.000000 | - |
3. 在指定的compute节点启动虚拟机,并指定固定ip地址
[root@controller ~]# nova boot --flavor 10  \            #flavor名字
--image 37aaedc7-6fe6-4fc8-b110-408d166b8e51 \            #镜像id   
--key-name  KEY  \                         #KEY名字
--availability-zone ChuangYiYuanZone1:ChuangYiYuan_10_16_2_11  \   #在制定的区域和主机名启动instance
--security-groups 663468d9-73b1-4b04-8d4c-dac1bf21a94d  \      #安全组
--nic net-id=99c68a93-336a-4605-aa78-343d41ca1206,v4-fixed-ip=192.168.100.200 happyblog_blog_51cto_com    #指定网络和IP地址
+--------------------------------------+------------------------------------------------+
| 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-000001f9                              |
| 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                            | 4T3vpfaXPTZe                                   |
| config_drive                         |                                                |
| created                              | 2016-01-23T04:44:51Z                           |
| flavor                               | V.GF1 (10)                                     |
| hostId                               |                                                |
| id                                   | 3f694eaf-aa87-456a-99ce-90dd9f4e45ee           |
| image                                | cirrors (37aaedc7-6fe6-4fc8-b110-408d166b8e51) |
| key_name                             | KEY                                            |
| metadata                             | {}                                             |
| name                                 | happyblog_blog_51cto_com                       |
| os-extended-volumes:volumes_attached | []                                             |
| progress                             | 0                                              |
| security_groups                      | 663468d9-73b1-4b04-8d4c-dac1bf21a94d           |
| status                               | BUILD                                          |
| tenant_id                            | 842ab3268a2c47e6a4b0d8774de805ae               |
| updated                              | 2016-01-23T04:44:52Z                           |
| user_id                              | bc5e46fc4204497185ae3ca6f8b7affb               |
+--------------------------------------+------------------------------------------------+校验配置情况
[root@controller ~]# nova list | grep 3f694eaf-aa87-456a-99ce-90dd9f4e45ee
| 3f694eaf-aa87-456a-99ce-90dd9f4e45ee | happyblog_blog_51cto_com | ACTIVE | -          | Running     | vmTest=192.168.100.200   | ChuangYiYuan_10_16_2_11 |
[root@controller ~]# nova show 3f694eaf-aa87-456a-99ce-90dd9f4e45ee       
+--------------------------------------+----------------------------------------------------------+
| Property                             | Value                                                    |
+--------------------------------------+----------------------------------------------------------+
| OS-DCF:diskConfig                    | MANUAL                                                   |
| OS-EXT-AZ:availability_zone          | ChuangYiYuanZone1                                        |
| OS-EXT-SRV-ATTR:host                 | ChuangYiYuan_10_16_2_11                                  |    #指定的compute节点
| OS-EXT-SRV-ATTR:hypervisor_hostname  | ChuangYiYuan_10_16_2_11                                  |
| OS-EXT-SRV-ATTR:instance_name        | instance-000001f9                                        |
| OS-EXT-STS:power_state               | 1                                                        |
| OS-EXT-STS:task_state                | -                                                        |
| OS-EXT-STS:vm_state                  | active                                                   |
| OS-SRV-USG:launched_at               | 2016-01-23T04:45:06.000000                               |
| OS-SRV-USG:terminated_at             | -                                                        |
| accessIPv4                           |                                                          |
| accessIPv6                           |                                                          |
| config_drive                         |                                                          |
| created                              | 2016-01-23T04:44:51Z                                     |
| flavor                               | V.GF1 (10)                                               |
| hostId                               | b3a37b586ae2847a0b18c58ff7693b41762fa0bf6a3cc363c862761a |
| id                                   | 3f694eaf-aa87-456a-99ce-90dd9f4e45ee                     |
| image                                | cirrors (37aaedc7-6fe6-4fc8-b110-408d166b8e51)           |
| key_name                             | KEY                                                      |
| metadata                             | {}                                                       |
| name                                 | happyblog_blog_51cto_com                                 |
| os-extended-volumes:volumes_attached | []                                                       |
| progress                             | 0                                                        |
| security_groups                      | default                                                  |
| status                               | ACTIVE                                                   |
| tenant_id                            | 842ab3268a2c47e6a4b0d8774de805ae                         |
| updated                              | 2016-01-23T04:45:07Z                                     |
| user_id                              | bc5e46fc4204497185ae3ca6f8b7affb                         |
| vmTest network                       | 192.168.100.200                                          |    #指定的ip地址,good,完成
+--------------------------------------+----------------------------------------------------------+4. 总结
以上方法用于openstack的日常运维中,一般而言,不建议使用,建议使用nova内置的调度算法来完成。本人在实际的工作中,经常有指定compute机器和指定ip的需求。
5. 附录
  附录提供了nova boot用法相关的参数
[root@controller ~]# nova help boot
usage: nova boot [--flavor <flavor>] [--image <image>]
[--image-with <key=value>] [--boot-volume <volume_id>]
[--snapshot <snapshot_id>] [--num-instances <number>]
[--meta <key=value>] [--file <dst-path=src-path>]
[--key-name <key-name>] [--user-data <user-data>]
[--availability-zone <availability-zone>]
[--security-groups <security-groups>]
[--block-device-mapping <dev-name=mapping>]
[--block-device key1=value1[,key2=value2...]]
[--swap <swap_size>]
[--ephemeral size=<size>[,format=<format>]]
[--hint <key=value>]
[--nic <net-id=net-uuid,v4-fixed-ip=ip-addr,port-id=port-uuid>]
[--config-drive <value>] [--poll]
<name>
Boot a new server.
Positional arguments:
<name> Name for the new server
Optional arguments:
--flavor <flavor> Name or ID of flavor (see ‘nova flavor-list‘).
--image <image> Name or ID of image (see ‘nova image-list‘).
--image-with <key=value>
Image metadata property (see ‘nova image-show‘).
--boot-volume <volume_id>
Volume ID to boot from.
--snapshot <snapshot_id>
Snapshot ID to boot from (will create a volume).
--num-instances <number>
boot multiple servers at a time (limited by quota).
--meta <key=value> Record arbitrary key/value metadata to /meta.js on the
new server. Can be specified multiple times.
--file <dst-path=src-path>
Store arbitrary files from <src-path> locally to <dst-
path> on the new server. You may store up to 5 files.
--key-name <key-name>
Key name of keypair that should be created earlier
with the command keypair-add
--user-data <user-data>
user data file to pass to be exposed by the metadata
server.
--availability-zone <availability-zone>
The availability zone for server placement.
--security-groups <security-groups>
Comma separated list of security group names.
--block-device-mapping <dev-name=mapping>
Block device mapping in the format <dev-
name>=<id>:<type>:<size(GB)>:<delete-on-terminate>.
--block-device key1=value1[,key2=value2...]
Block device mapping with the keys: id=image_id,
snapshot_id or volume_id, source=source type (image,
snapshot, volume or blank), dest=destination type of
the block device (volume or local), bus=device‘s bus,
device=name of the device (e.g. vda, xda, ...),
size=size of the block device in GB, format=device
will be formatted (e.g. swap, ext3, ntfs, ...),
bootindex=integer used for ordering the boot disks,
type=device type (e.g. disk, cdrom, ...) and
shutdown=shutdown behaviour (either preserve or
remove).
--swap <swap_size> Create and attach a local swap block device of
<swap_size> MB.
--ephemeral size=<size>[,format=<format>]
Create and attach a local ephemeral block device of
<size> GB and format it to <format>.
--hint <key=value> Send arbitrary key/value pairs to the scheduler for
custom use.
--nic <net-id=net-uuid,v4-fixed-ip=ip-addr,port-id=port-uuid>
Create a NIC on the server. Specify option multiple
times to create multiple NICs. net-id: attach NIC to
network with this UUID (required if no port-id), v4
-fixed-ip: IPv4 fixed address for NIC (optional),
port-id: attach NIC to port with this UUID (required
if no net-id)
--config-drive <value>
Enable config drive
--poll Blocks while server builds so progress can be
reported.
本文出自 “Happy实验室” 博客,转载请与作者联系!
openstack运维实战系列(十)之nova指定compute节点和IP地址
原文:http://happylab.blog.51cto.com/1730296/1737842