守望的麦子

[OpenStack] 通过命令行创建实例

2017-06-21    横滨    /ericsson/2017/06/21/ic.html ericsson openstack

本文最近更新于 2018 年 07 月 06 日

上一次通过 OpenStack dashboard 创建了一个 instance,通过命令行可以查看到:

[root@localhost ~(keystone_admin)]# nova list
+--------------------------------------+------+--------+------------+-------------+-------------------+
| ID                                   | Name | Status | Task State | Power State | Networks          |
+--------------------------------------+------+--------+------------+-------------+-------------------+
| f0460a3f-20d1-49b1-8ee7-c910fbef4990 | test | ACTIVE | -          | Running     | public=172.24.4.3 |
+--------------------------------------+------+--------+------------+-------------+-------------------+
[root@localhost ~(keystone_admin)]# 
[root@localhost ~(keystone_admin)]# nova show f0460a3f-20d1-49b1-8ee7-c910fbef4990
+--------------------------------------+---------------------------------------------------------------------------------+
| Property                             | Value                                                                           |
+--------------------------------------+---------------------------------------------------------------------------------+
| OS-DCF:diskConfig                    | AUTO                                                                            |
| OS-EXT-AZ:availability_zone          | nova                                                                            |
| OS-EXT-SRV-ATTR:host                 | localhost.localdomain                                                           |
| OS-EXT-SRV-ATTR:hostname             | test                                                                            |
| OS-EXT-SRV-ATTR:hypervisor_hostname  | localhost.localdomain                                                           |
| OS-EXT-SRV-ATTR:instance_name        | instance-00000001                                                               |
| OS-EXT-SRV-ATTR:kernel_id            |                                                                                 |
| OS-EXT-SRV-ATTR:launch_index         | 0                                                                               |
| OS-EXT-SRV-ATTR:ramdisk_id           |                                                                                 |
| OS-EXT-SRV-ATTR:reservation_id       | r-5qlhb8ya                                                                      |
| OS-EXT-SRV-ATTR:root_device_name     | /dev/vda                                                                        |
| OS-EXT-SRV-ATTR:user_data            | -                                                                               |
| OS-EXT-STS:power_state               | 1                                                                               |
| OS-EXT-STS:task_state                | -                                                                               |
| OS-EXT-STS:vm_state                  | active                                                                          |
| OS-SRV-USG:launched_at               | 2017-06-21T02:04:05.000000                                                      |
| OS-SRV-USG:terminated_at             | -                                                                               |
| accessIPv4                           |                                                                                 |
| accessIPv6                           |                                                                                 |
| config_drive                         |                                                                                 |
| created                              | 2017-06-21T02:03:01Z                                                            |
| description                          | test                                                                            |
| flavor                               | m1.tiny (1)                                                                     |
| hostId                               | d18f0387910dd4f9eb8175b0de2fffd6c5e7cd88c349cd6bc7b3bd5f                        |
| host_status                          | UP                                                                              |
| id                                   | f0460a3f-20d1-49b1-8ee7-c910fbef4990                                            |
| image                                | Attempt to boot from volume - no image supplied                                 |
| key_name                             | -                                                                               |
| locked                               | False                                                                           |
| metadata                             | {}                                                                              |
| name                                 | test                                                                            |
| os-extended-volumes:volumes_attached | [{"id": "ad75a7b5-d9c5-44c8-a3ba-e274547b995e", "delete_on_termination": true}] |
| progress                             | 0                                                                               |
| public network                       | 172.24.4.3                                                                      |
| security_groups                      | default                                                                         |
| status                               | ACTIVE                                                                          |
| tags                                 | []                                                                              |
| tenant_id                            | 355c595975894e7d8caa9c3af1ad3cb7                                                |
| updated                              | 2017-06-21T02:04:06Z                                                            |
| user_id                              | 1f8b1698b91c42169437f856426fc0b1                                                |
+--------------------------------------+---------------------------------------------------------------------------------+
[root@localhost ~(keystone_admin)]# 

首先我们使用命令行删除现有的 test 实例:

[root@localhost ~(keystone_admin)]# nova help delete
usage: nova delete [--all-tenants] <server> [<server> ...]

Immediately shut down and delete specified server(s).

Positional arguments:
  <server>       Name or ID of server(s).

Optional arguments:
  --all-tenants  Delete server(s) in another tenant by name (Admin only).
[root@localhost ~(keystone_admin)]# nova delete test
Request to delete server test has been accepted.
[root@localhost ~(keystone_admin)]# nova list
+----+------+--------+------------+-------------+----------+
| ID | Name | Status | Task State | Power State | Networks |
+----+------+--------+------------+-------------+----------+
+----+------+--------+------------+-------------+----------+
[root@localhost ~(keystone_admin)]# 

在通过命令行创建实例之前,我们首先需要收集一些相关信息,必需的信息有 instance source, flavor。

使用 openstack flavor list 列出所有的 flavor 信息:

[root@localhost ~(keystone_admin)]# openstack flavor list
+----+-----------+-------+------+-----------+-------+-----------+
| ID | Name      |   RAM | Disk | Ephemeral | VCPUs | Is Public |
+----+-----------+-------+------+-----------+-------+-----------+
| 1  | m1.tiny   |   512 |    1 |         0 |     1 | True      |
| 2  | m1.small  |  2048 |   20 |         0 |     1 | True      |
| 3  | m1.medium |  4096 |   40 |         0 |     2 | True      |
| 4  | m1.large  |  8192 |   80 |         0 |     4 | True      |
| 5  | m1.xlarge | 16384 |  160 |         0 |     8 | True      |
+----+-----------+-------+------+-----------+-------+-----------+
[root@localhost ~(keystone_admin)]# 

如果 flavor list 中没有我们想要的 flavor,可以通过 openstack flavor create 创建,例如 openstack flavor create --ram 512 --disk 1 --vcpus 1 m1.tiny

使用 openstack image list 列出所有的 image 信息:

[root@localhost ~(keystone_admin)]# openstack image list
+--------------------------------------+--------+--------+
| ID                                   | Name   | Status |
+--------------------------------------+--------+--------+
| eb94652f-4764-4ec9-baca-499e181eb91b | cirros | active |
+--------------------------------------+--------+--------+
[root@localhost ~(keystone_admin)]# 

如果 image list 中没有我们想要的 image,可以通过 openstack image create 创建,具体方法参见 [OpenStack] 创建第一个实例

取得了信息之后,我们就可以使用命令行创建实例了,使用的命令格式如下:

openstack server create --flavor FLAVOR_ID --image IMAGE_ID --key-name KEY_NAME --user-data USER_DATA_FILE --security-group SEC_GROUP_NAME --property KEY=VALUE INSTANCE_NAME

例如根据上面我 list 出来的信息,使用指令如下:

openstack server create --flavor 1 --image eb94652f-4764-4ec9-baca-499e181eb91b test

执行 log 如下:

[root@localhost ~(keystone_admin)]# openstack server create --flavor 1 --image eb94652f-4764-4ec9-baca-499e181eb91b test
+-------------------------------------+-----------------------------------------------+
| Field                               | Value                                         |
+-------------------------------------+-----------------------------------------------+
| OS-DCF:diskConfig                   | MANUAL                                        |
| OS-EXT-AZ:availability_zone         |                                               |
| OS-EXT-SRV-ATTR:host                | None                                          |
| OS-EXT-SRV-ATTR:hypervisor_hostname | None                                          |
| OS-EXT-SRV-ATTR:instance_name       |                                               |
| OS-EXT-STS:power_state              | NOSTATE                                       |
| OS-EXT-STS:task_state               | scheduling                                    |
| OS-EXT-STS:vm_state                 | building                                      |
| OS-SRV-USG:launched_at              | None                                          |
| OS-SRV-USG:terminated_at            | None                                          |
| accessIPv4                          |                                               |
| accessIPv6                          |                                               |
| addresses                           |                                               |
| adminPass                           | R4bFrk6bhwPj                                  |
| config_drive                        |                                               |
| created                             | 2017-06-21T02:42:55Z                          |
| flavor                              | m1.tiny (1)                                   |
| hostId                              |                                               |
| id                                  | 6d54bb1a-78e5-4025-949b-449295b43474          |
| image                               | cirros (eb94652f-4764-4ec9-baca-499e181eb91b) |
| key_name                            | None                                          |
| name                                | test                                          |
| progress                            | 0                                             |
| project_id                          | 355c595975894e7d8caa9c3af1ad3cb7              |
| properties                          |                                               |
| security_groups                     | name='default'                                |
| status                              | BUILD                                         |
| updated                             | 2017-06-21T02:42:55Z                          |
| user_id                             | 1f8b1698b91c42169437f856426fc0b1              |
| volumes_attached                    |                                               |
+-------------------------------------+-----------------------------------------------+
[root@localhost ~(keystone_admin)]# nova list
+--------------------------------------+------+--------+------------+-------------+----------+
| ID                                   | Name | Status | Task State | Power State | Networks |
+--------------------------------------+------+--------+------------+-------------+----------+
| 6d54bb1a-78e5-4025-949b-449295b43474 | test | BUILD  | spawning   | NOSTATE     |          |
+--------------------------------------+------+--------+------------+-------------+----------+
[root@localhost ~(keystone_admin)]# nova list
+--------------------------------------+------+--------+------------+-------------+-------------------+
| ID                                   | Name | Status | Task State | Power State | Networks          |
+--------------------------------------+------+--------+------------+-------------+-------------------+
| 6d54bb1a-78e5-4025-949b-449295b43474 | test | ACTIVE | -          | Running     | public=172.24.4.4 |
+--------------------------------------+------+--------+------------+-------------+-------------------+
[root@localhost ~(keystone_admin)]# 

使用 OpenStack 命令查看实例状态:

[root@localhost ~(keystone_admin)]# openstack server list
+--------------------------------------+------+--------+-------------------+------------+
| ID                                   | Name | Status | Networks          | Image Name |
+--------------------------------------+------+--------+-------------------+------------+
| 6d54bb1a-78e5-4025-949b-449295b43474 | test | ACTIVE | public=172.24.4.4 | cirros     |
+--------------------------------------+------+--------+-------------------+------------+
[root@localhost ~(keystone_admin)]# 

关于 openstack server list 命令的其他可用项,通过下面的命令查看:

openstack help server list

- EOF -

下一篇计划写一下创建实例的流程。

关于作者
麦子,80 后,现从事通信行业。安卓玩家一个人的书房朗读者。
MRJENGLISH
jsntn
jasonwtien
jasonwtien
更多…… /about.html

最近更新: