守望的麦子

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

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

本文最近更新于 2018 年 7 月 6 日

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

 1 [root@localhost ~(keystone_admin)]# nova list
 2 +--------------------------------------+------+--------+------------+-------------+-------------------+
 3 | ID                                   | Name | Status | Task State | Power State | Networks          |
 4 +--------------------------------------+------+--------+------------+-------------+-------------------+
 5 | f0460a3f-20d1-49b1-8ee7-c910fbef4990 | test | ACTIVE | -          | Running     | public=172.24.4.3 |
 6 +--------------------------------------+------+--------+------------+-------------+-------------------+
 7 [root@localhost ~(keystone_admin)]# 
 8 [root@localhost ~(keystone_admin)]# nova show f0460a3f-20d1-49b1-8ee7-c910fbef4990
 9 +--------------------------------------+---------------------------------------------------------------------------------+
10 | Property                             | Value                                                                           |
11 +--------------------------------------+---------------------------------------------------------------------------------+
12 | OS-DCF:diskConfig                    | AUTO                                                                            |
13 | OS-EXT-AZ:availability_zone          | nova                                                                            |
14 | OS-EXT-SRV-ATTR:host                 | localhost.localdomain                                                           |
15 | OS-EXT-SRV-ATTR:hostname             | test                                                                            |
16 | OS-EXT-SRV-ATTR:hypervisor_hostname  | localhost.localdomain                                                           |
17 | OS-EXT-SRV-ATTR:instance_name        | instance-00000001                                                               |
18 | OS-EXT-SRV-ATTR:kernel_id            |                                                                                 |
19 | OS-EXT-SRV-ATTR:launch_index         | 0                                                                               |
20 | OS-EXT-SRV-ATTR:ramdisk_id           |                                                                                 |
21 | OS-EXT-SRV-ATTR:reservation_id       | r-5qlhb8ya                                                                      |
22 | OS-EXT-SRV-ATTR:root_device_name     | /dev/vda                                                                        |
23 | OS-EXT-SRV-ATTR:user_data            | -                                                                               |
24 | OS-EXT-STS:power_state               | 1                                                                               |
25 | OS-EXT-STS:task_state                | -                                                                               |
26 | OS-EXT-STS:vm_state                  | active                                                                          |
27 | OS-SRV-USG:launched_at               | 2017-06-21T02:04:05.000000                                                      |
28 | OS-SRV-USG:terminated_at             | -                                                                               |
29 | accessIPv4                           |                                                                                 |
30 | accessIPv6                           |                                                                                 |
31 | config_drive                         |                                                                                 |
32 | created                              | 2017-06-21T02:03:01Z                                                            |
33 | description                          | test                                                                            |
34 | flavor                               | m1.tiny (1)                                                                     |
35 | hostId                               | d18f0387910dd4f9eb8175b0de2fffd6c5e7cd88c349cd6bc7b3bd5f                        |
36 | host_status                          | UP                                                                              |
37 | id                                   | f0460a3f-20d1-49b1-8ee7-c910fbef4990                                            |
38 | image                                | Attempt to boot from volume - no image supplied                                 |
39 | key_name                             | -                                                                               |
40 | locked                               | False                                                                           |
41 | metadata                             | {}                                                                              |
42 | name                                 | test                                                                            |
43 | os-extended-volumes:volumes_attached | [{"id": "ad75a7b5-d9c5-44c8-a3ba-e274547b995e", "delete_on_termination": true}] |
44 | progress                             | 0                                                                               |
45 | public network                       | 172.24.4.3                                                                      |
46 | security_groups                      | default                                                                         |
47 | status                               | ACTIVE                                                                          |
48 | tags                                 | []                                                                              |
49 | tenant_id                            | 355c595975894e7d8caa9c3af1ad3cb7                                                |
50 | updated                              | 2017-06-21T02:04:06Z                                                            |
51 | user_id                              | 1f8b1698b91c42169437f856426fc0b1                                                |
52 +--------------------------------------+---------------------------------------------------------------------------------+
53 [root@localhost ~(keystone_admin)]# 

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

 1 [root@localhost ~(keystone_admin)]# nova help delete
 2 usage: nova delete [--all-tenants] <server> [<server> ...]
 3 
 4 Immediately shut down and delete specified server(s).
 5 
 6 Positional arguments:
 7   <server>       Name or ID of server(s).
 8 
 9 Optional arguments:
10   --all-tenants  Delete server(s) in another tenant by name (Admin only).
11 [root@localhost ~(keystone_admin)]# nova delete test
12 Request to delete server test has been accepted.
13 [root@localhost ~(keystone_admin)]# nova list
14 +----+------+--------+------------+-------------+----------+
15 | ID | Name | Status | Task State | Power State | Networks |
16 +----+------+--------+------------+-------------+----------+
17 +----+------+--------+------------+-------------+----------+
18 [root@localhost ~(keystone_admin)]# 

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

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

 1 [root@localhost ~(keystone_admin)]# openstack flavor list
 2 +----+-----------+-------+------+-----------+-------+-----------+
 3 | ID | Name      |   RAM | Disk | Ephemeral | VCPUs | Is Public |
 4 +----+-----------+-------+------+-----------+-------+-----------+
 5 | 1  | m1.tiny   |   512 |    1 |         0 |     1 | True      |
 6 | 2  | m1.small  |  2048 |   20 |         0 |     1 | True      |
 7 | 3  | m1.medium |  4096 |   40 |         0 |     2 | True      |
 8 | 4  | m1.large  |  8192 |   80 |         0 |     4 | True      |
 9 | 5  | m1.xlarge | 16384 |  160 |         0 |     8 | True      |
10 +----+-----------+-------+------+-----------+-------+-----------+
11 [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 信息:

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

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

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

1 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 出来的信息,使用指令如下:

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

执行 log 如下:

 1 [root@localhost ~(keystone_admin)]# openstack server create --flavor 1 --image eb94652f-4764-4ec9-baca-499e181eb91b test
 2 +-------------------------------------+-----------------------------------------------+
 3 | Field                               | Value                                         |
 4 +-------------------------------------+-----------------------------------------------+
 5 | OS-DCF:diskConfig                   | MANUAL                                        |
 6 | OS-EXT-AZ:availability_zone         |                                               |
 7 | OS-EXT-SRV-ATTR:host                | None                                          |
 8 | OS-EXT-SRV-ATTR:hypervisor_hostname | None                                          |
 9 | OS-EXT-SRV-ATTR:instance_name       |                                               |
10 | OS-EXT-STS:power_state              | NOSTATE                                       |
11 | OS-EXT-STS:task_state               | scheduling                                    |
12 | OS-EXT-STS:vm_state                 | building                                      |
13 | OS-SRV-USG:launched_at              | None                                          |
14 | OS-SRV-USG:terminated_at            | None                                          |
15 | accessIPv4                          |                                               |
16 | accessIPv6                          |                                               |
17 | addresses                           |                                               |
18 | adminPass                           | R4bFrk6bhwPj                                  |
19 | config_drive                        |                                               |
20 | created                             | 2017-06-21T02:42:55Z                          |
21 | flavor                              | m1.tiny (1)                                   |
22 | hostId                              |                                               |
23 | id                                  | 6d54bb1a-78e5-4025-949b-449295b43474          |
24 | image                               | cirros (eb94652f-4764-4ec9-baca-499e181eb91b) |
25 | key_name                            | None                                          |
26 | name                                | test                                          |
27 | progress                            | 0                                             |
28 | project_id                          | 355c595975894e7d8caa9c3af1ad3cb7              |
29 | properties                          |                                               |
30 | security_groups                     | name='default'                                |
31 | status                              | BUILD                                         |
32 | updated                             | 2017-06-21T02:42:55Z                          |
33 | user_id                             | 1f8b1698b91c42169437f856426fc0b1              |
34 | volumes_attached                    |                                               |
35 +-------------------------------------+-----------------------------------------------+
36 [root@localhost ~(keystone_admin)]# nova list
37 +--------------------------------------+------+--------+------------+-------------+----------+
38 | ID                                   | Name | Status | Task State | Power State | Networks |
39 +--------------------------------------+------+--------+------------+-------------+----------+
40 | 6d54bb1a-78e5-4025-949b-449295b43474 | test | BUILD  | spawning   | NOSTATE     |          |
41 +--------------------------------------+------+--------+------------+-------------+----------+
42 [root@localhost ~(keystone_admin)]# nova list
43 +--------------------------------------+------+--------+------------+-------------+-------------------+
44 | ID                                   | Name | Status | Task State | Power State | Networks          |
45 +--------------------------------------+------+--------+------------+-------------+-------------------+
46 | 6d54bb1a-78e5-4025-949b-449295b43474 | test | ACTIVE | -          | Running     | public=172.24.4.4 |
47 +--------------------------------------+------+--------+------------+-------------+-------------------+
48 [root@localhost ~(keystone_admin)]# 

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

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

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

1 openstack help server list

- EOF -

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

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

最近更新: