Nova list 背后做了什么?

1 查看当前运行的虚拟机

在OpenStack上,我们通常用nova list命令来查看所有当前租户的虚拟机。例如:

+--------------------------------------+---------------+--------+------------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ID                                   | Name          | Status | Task State | Power State | Networks                                                                                                                                                                                                                                                    |
+--------------------------------------+---------------+--------+------------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 46909036-6701-4dbf-af9d-b5fc188f8b30 | RCP-1234-cp-0 | ACTIVE | -          | Running     | RCP-1234-internal-net=192.168.1.7; RCP-1234-intmsg0-net=169.254.0.9                                                                                                                                                                                         |
| 32fd180d-0e7f-43b4-80a9-f911e9802201 | RCP-1234-ei-0 | ACTIVE | -          | Running     | extnet-vlan-2=2a00:8a00:8000:5002:0:17:0:224, 10.107.115.144; extnet-vlan-0=2a00:8a00:8000:5002:0:17:0:a, 10.107.115.49; extnet-vlan-1=10.107.115.86, 2a00:8a00:8000:5002:0:17:0:123; RCP-1234-internal-net=192.168.1.10; RCP-1234-intmsg0-net=169.254.0.14 |
| 60a5e01b-5f1b-4a86-b959-920c97f1e86c | RCP-1234-ei-1 | ACTIVE | -          | Running     | extnet-vlan-2=2a00:8a00:8000:5002:0:17:0:229, 10.107.115.146; extnet-vlan-0=2a00:8a00:8000:5002:0:17:0:14, 10.107.115.27; extnet-vlan-1=10.107.115.79, 2a00:8a00:8000:5002:0:17:0:107; RCP-1234-internal-net=192.168.1.15; RCP-1234-intmsg0-net=169.254.0.5 |
| 74a8678d-0e41-45c9-839a-81d47c13bf22 | RCP-1234-mn-0 | ACTIVE | -          | Running     | RCP-1234-internal-net=192.168.1.13; extnet-vlan-4=2a00:8a00:8000:5002:0:17:0:404, 10.107.116.9; RCP-1234-intmsg0-net=169.254.0.11                                                                                                                           |
| b7e4cb84-738d-4046-b86c-466fd69cee54 | RCP-1234-up-0 | ACTIVE | -          | Running     | extnet-vlan-0=2a00:8a00:8000:5002:0:17:0:4, 10.107.115.9; RCP-1234-internal-net=192.168.1.12; RCP-1234-intmsg0-net=169.254.0.12                                                                                                                             |
+--------------------------------------+---------------+--------+------------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

类似的命令还有:openstack server list

+--------------------------------------+---------------+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------+---------------+
| ID                                   | Name          | Status | Networks                                                                                                                                                                                                                                                    | Image                     | Flavor        |
+--------------------------------------+---------------+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------+---------------+
| 60a5e01b-5f1b-4a86-b959-920c97f1e86c | RCP-1234-ei-1 | ACTIVE | extnet-vlan-2=2a00:8a00:8000:5002:0:17:0:229, 10.107.115.146; extnet-vlan-0=2a00:8a00:8000:5002:0:17:0:14, 10.107.115.27; extnet-vlan-1=10.107.115.79, 2a00:8a00:8000:5002:0:17:0:107; RCP-1234-internal-net=192.168.1.15; RCP-1234-intmsg0-net=169.254.0.5 | rcp2.0-ci2019-03-01-10-47 | rcp_xlarge_v4 |
| 32fd180d-0e7f-43b4-80a9-f911e9802201 | RCP-1234-ei-0 | ACTIVE | extnet-vlan-2=2a00:8a00:8000:5002:0:17:0:224, 10.107.115.144; extnet-vlan-0=2a00:8a00:8000:5002:0:17:0:a, 10.107.115.49; extnet-vlan-1=10.107.115.86, 2a00:8a00:8000:5002:0:17:0:123; RCP-1234-internal-net=192.168.1.10; RCP-1234-intmsg0-net=169.254.0.14 | rcp2.0-ci2019-03-01-10-47 | rcp_xlarge_v4 |
| b7e4cb84-738d-4046-b86c-466fd69cee54 | RCP-1234-up-0 | ACTIVE | extnet-vlan-0=2a00:8a00:8000:5002:0:17:0:4, 10.107.115.9; RCP-1234-internal-net=192.168.1.12; RCP-1234-intmsg0-net=169.254.0.12                                                                                                                             | rcp2.0-ci2019-03-01-10-47 | rcp_large_v4  |
| 46909036-6701-4dbf-af9d-b5fc188f8b30 | RCP-1234-cp-0 | ACTIVE | RCP-1234-internal-net=192.168.1.7; RCP-1234-intmsg0-net=169.254.0.9                                                                                                                                                                                         | rcp2.0-ci2019-03-01-10-47 | rcp_medium_v4 |
| 74a8678d-0e41-45c9-839a-81d47c13bf22 | RCP-1234-mn-0 | ACTIVE | RCP-1234-internal-net=192.168.1.13; extnet-vlan-4=2a00:8a00:8000:5002:0:17:0:404, 10.107.116.9; RCP-1234-intmsg0-net=169.254.0.11                                                                                                                           | rcp2.0-ci2019-03-01-10-47 | rcp_large_v4  |
+--------------------------------------+---------------+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------+---------------+

或者,使用Openstack的dashboard:


image.png

不管是命令行,还是Web UI,都使用了OpenStack的RESTful API来得到虚拟机的信息。下面我们来推导这个过程。

2 “--debug”帮我们看到更多信息

通过nova --debug list拿到运行时的log:

[cranuser3@controller-1 ~(keystone_cranuser3)]$ nova --debug list
DEBUG (extension:189) found extension EntryPoint.parse('aodh-noauth = aodhclient.noauth:AodhNoAuthLoader')
...
3 从”--debug”中梳理出Restful API

我们知道,Restful API就是每个资源都有独立的URL,并且通过HTTP协议来操作的API。在“--debug”中,有下面这些HTTP消息:

curl -g -i -X GET http://192.168.54.5:5000/v3
http://192.168.54.5:5000 "POST /v3/auth/tokens HTTP/1.1
curl -g -i -X GET http://192.168.54.5:8774/v2.1
curl -g -i -X GET http://192.168.54.5:8774/v2.1/servers/detail 

可以看到这里使用了5000和8774端口,通过endpoint命令,

[cranuser2@controller-1 ~(keystone_cranuser2)]$ openstack endpoint list | grep 5000
| eff831537fd4497794a0cbbb0449854c | RegionOne | keystone     | identity       | True    | public    | https://10.107.184.20:5000                  |
| f1deba856a6f4b4fb7cc800956eccf39 | RegionOne | keystone     | identity       | True    | internal  | http://192.168.54.5:5000                    |
[cranuser2@controller-1 ~(keystone_cranuser2)]$ openstack endpoint list | grep 8774
| 0b554bbb3a3043dd9cc6482e0a3526c2 | RegionOne | nova         | compute        | True    | admin     | http://192.168.54.5:8774/v2.1               |
| 35c3df0004cb40d1a0512f4f04083b9d | RegionOne | nova         | compute        | True    | internal  | http://192.168.54.5:8774/v2.1               |
| 35f6aff1cc444321b2318141472dbc27 | RegionOne | nova         | compute        | True    | public    | https://10.107.184.20:8774/v2.1             |

可以看到5000是keystone, 8774是nova的服务端口。

4 查看keystone 版本3的状态
$ curl http://192.168.54.5:5000/v3
{"version": {"status": "stable", "updated": "2018-02-28T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v3+json"}], "id": "v3.10", "links": [{"href": "http://192.168.54.5:5000/v3/", "rel": "self"}]}}
5 得到token
$ curl -i -d '
    {
        "auth": {
            "identity": {
                "methods": ["password"],
                "password": {
                    "user": {
                        "domain": {"name": "Default"},
                        "name": "cranuser3",
                        "password": "systeM!23"
                    }
                }
            }
        }
    }' -H "Content-type: application/json" "http://192.168.54.5:5000/v3/auth/tokens?nocatalog"

HTTP/1.1 201 Created
Server: nginx
Date: Mon, 04 Mar 2019 07:52:41 GMT
Content-Type: application/json
Content-Length: 660
X-Subject-Token: gAAAAABcfNlJ4TofivayZJDwWOdE2R0aM9DRQ4mdfdKft7LSHNGzx_e9cKODjYbDMJDaxkwh8K7Alg9xFgUQ2oyGKuMnrRvizxpNHEZWyrNryGPdFXoLTuU66RId1Ll9Oc9o-d6exlWuC6TWwJka9SftxWgDnrvk4EP8XAOtiisCATmfkwuijek
Vary: X-Auth-Token
x-openstack-request-id: req-12271058-fe16-4dd8-a7ea-445e1a4a4cfb
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Security-Policy: default-src 'self' https: wss:;
X-Frame-Options: DENY
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15768000

{"token": {"is_domain": false, "methods": ["password"], "roles": [{"id": "9fe2ff9ee4384b1894a90878d3e92bab", "name": "_member_"}, {"id": "6e5cf37d5deb494eba30a74391113d03", "name": "admin"}, {"id": "c7f537ec56a74d1da2fffac2b22bc68b", "name": "network_user"}], "expires_at": "2019-03-04T19:52:41.000000Z", "project": {"domain": {"id": "default", "name": "Default"}, "id": "cf0ee152f3ef4136a2811af804d4cf19", "name": "cran3"}, "user": {"password_expires_at": null, "domain": {"id": "default", "name": "Default"}, "id": "0404c966a1a94f90922c803ebb4308bf", "name": "cranuser3"}, "audit_ids": ["kQIfNlgtSQO1N0uvhevl0g"], "issued_at": "2019-03-04T07:52:41.000000Z"}}

注意X-Subject-Token

6 拿虚拟机列表
$ curl -X GET http://192.168.54.5:8774/v2.1/servers -H "Accept: application/json" -H "X-Auth-Token: gAAAAABcfNlJ4TofivayZJDwWOdE2R0aM9DRQ4mdfdKft7LSHNGzx_e9cKODjYbDMJDaxkwh8K7Alg9xFgUQ2oyGKuMnrRvizxpNHEZWyrNryGPdFXoLTuU66RId1Ll9Oc9o-d6exlWuC6TWwJka9SftxWgDnrvk4EP8XAOtiisCATmfkwuijek"

{"servers": [{"id": "60a5e01b-5f1b-4a86-b959-920c97f1e86c", "links": [{"href": "http://192.168.54.5:8774/v2.1/servers/60a5e01b-5f1b-4a86-b959-920c97f1e86c", "rel": "self"}, {"href": "http://192.168.54.5:8774/servers/60a5e01b-5f1b-4a86-b959-920c97f1e86c", "rel": "bookmark"}], "name": "RCP-1234-ei-1"}, {"id": "32fd180d-0e7f-43b4-80a9-f911e9802201", "links": [{"href": "http://192.168.54.5:8774/v2.1/servers/32fd180d-0e7f-43b4-80a9-f911e9802201", "rel": "self"}, {"href": "http://192.168.54.5:8774/servers/32fd180d-0e7f-43b4-80a9-f911e9802201", "rel": "bookmark"}], "name": "RCP-1234-ei-0"}, {"id": "b7e4cb84-738d-4046-b86c-466fd69cee54", "links": [{"href": "http://192.168.54.5:8774/v2.1/servers/b7e4cb84-738d-4046-b86c-466fd69cee54", "rel": "self"}, {"href": "http://192.168.54.5:8774/servers/b7e4cb84-738d-4046-b86c-466fd69cee54", "rel": "bookmark"}], "name": "RCP-1234-up-0"}, {"id": "46909036-6701-4dbf-af9d-b5fc188f8b30", "links": [{"href": "http://192.168.54.5:8774/v2.1/servers/46909036-6701-4dbf-af9d-b5fc188f8b30", "rel": "self"}, {"href": "http://192.168.54.5:8774/servers/46909036-6701-4dbf-af9d-b5fc188f8b30", "rel": "bookmark"}], "name": "RCP-1234-cp-0"}, {"id": "74a8678d-0e41-45c9-839a-81d47c13bf22", "links": [{"href": "http://192.168.54.5:8774/v2.1/servers/74a8678d-0e41-45c9-839a-81d47c13bf22", "rel": "self"}, {"href": "http://192.168.54.5:8774/servers/74a8678d-0e41-45c9-839a-81d47c13bf22", "rel": "bookmark"}], "name": "RCP-1234-mn-0"}]}
image.png
7 再做一个实验

从外部服务器,拿虚拟机列表。

$ openstack endpoint list | grep 5000
| 0a2e01e53a6a4e1ca4d4b2db08ae565a | RegionOne | keystone     | identity       | True    | public    | https://10.107.177.68:5000                  |
| 8620deadea0b48c0b662ea6deb8b1bf6 | RegionOne | keystone     | identity       | True    | internal  | http://192.168.54.5:5000          

这次从外部服务器,向OpenStack的public ip addr发Restful API消息。

[cbam@elam-cbam-18-0-02-single ~]$ curl -i -k -d '
>     {
>         "auth": {
>             "identity": {
>                 "methods": ["password"],
>                 "password": {
>                     "user": {
>                         "domain": {"name": "Default"},
>                         "name": "cranuser6",
>                         "password": "systeM!23"
>                     }
>                 }
>             }
>         }
>     }' -H "Content-type: application/json" "https://10.107.177.68:5000/v3/auth/tokens?nocatalog"
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 12 Mar 2019 06:52:08 GMT
Content-Type: application/json
Content-Length: 660
X-Subject-Token: gAAAAABch1cYnYbACkKm-75Th8rvE4XJz9I13pcYOBDkUpkNVfuvmXrk6pz1HfAgXSKaJ94gtIojcuG0-UdPIrcD7hzcZtVlsB4AizVcXAMWE1qZwcVt2Ne17fuOG39_6li1zOgzCP539wE-TAdmoQxrMscp2Rn_8OSA0W8LeS07uRu2Zv9HC3w
Vary: X-Auth-Token
x-openstack-request-id: req-514b7c1d-3766-42a3-b548-835c8e5aa95a
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Security-Policy: default-src 'self' https: wss:;
X-Frame-Options: DENY
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15768000

{"token": {"is_domain": false, "methods": ["password"], "roles": [{"id": "08151093a3934add8b3052ca8b3c215c", "name": "network_user"}, {"id": "edc530f6bda94da5be2531d3f4a5d0ae", "name": "admin"}, {"id": "9fe2ff9ee4384b1894a90878d3e92bab", "name": "_member_"}], "expires_at": "2019-03-12T18:52:08.000000Z", "project": {"domain": {"id": "default", "name": "Default"}, "id": "5a6c5961dab242b0a2532bf998bc3270", "name": "cran6"}, "user": {"password_expires_at": null, "domain": {"id": "default", "name": "Default"}, "id": "399e7e6ef5fb4f8197916fd652601b0c", "name": "cranuser6"}, "audit_ids": ["ofHQlXqCRrWx_uSDUTx9Lw"], "issued_at": "2019-03-12T06:52:08.000000Z"}}
$ curl -X GET -k https://10.107.177.68:8774/v2.1/servers -H "Accept: application/json" -H "X-Auth-Token: gAAAAABch1cYnYbACkKm-75Th8rvE4XJz9I13pcYOBDkUpkNVfuvmXrk6pz1HfAgXSKaJ94gtIojcuG0-UdPIrcD7hzcZtVlsB4AizVcXAMWE1qZwcVt2Ne17fuOG39_6li1zOgzCP539wE-TAdmoQxrMscp2Rn_8OSA0W8LeS07uRu2Zv9HC3w"
{"servers": [{"id": "c7f5ef2d-63d6-4997-9d1c-5047505341b4", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/c7f5ef2d-63d6-4997-9d1c-5047505341b4", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/c7f5ef2d-63d6-4997-9d1c-5047505341b4", "rel": "bookmark"}], "name": "RCP-1234-xxx-cps-0"}, {"id": "4fe62408-17ab-4761-8778-2dd352bb1c24", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/4fe62408-17ab-4761-8778-2dd352bb1c24", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/4fe62408-17ab-4761-8778-2dd352bb1c24", "rel": "bookmark"}], "name": "RCP-1234-xxx-ei-0"}, {"id": "dd12cae9-9837-4441-9293-f849cdf2b303", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/dd12cae9-9837-4441-9293-f849cdf2b303", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/dd12cae9-9837-4441-9293-f849cdf2b303", "rel": "bookmark"}], "name": "RCP-1234-xxx-ei-1"}, {"id": "0ee1c039-1fc9-4adb-9ea9-942feb05d597", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/0ee1c039-1fc9-4adb-9ea9-942feb05d597", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/0ee1c039-1fc9-4adb-9ea9-942feb05d597", "rel": "bookmark"}], "name": "RCP-1234-xxx-dbs-0"}, {"id": "633a2f4e-6e39-40af-a146-82bbbe1679e1", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/633a2f4e-6e39-40af-a146-82bbbe1679e1", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/633a2f4e-6e39-40af-a146-82bbbe1679e1", "rel": "bookmark"}], "name": "RCP-1234-xxx-cp-1"}, {"id": "e50877d7-245f-4ba2-b535-96a65767b2ba", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/e50877d7-245f-4ba2-b535-96a65767b2ba", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/e50877d7-245f-4ba2-b535-96a65767b2ba", "rel": "bookmark"}], "name": "RCP-1234-xxx-up-0"}, {"id": "b0400a88-0a50-4520-a37d-a7f2b27d2ba0", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/b0400a88-0a50-4520-a37d-a7f2b27d2ba0", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/b0400a88-0a50-4520-a37d-a7f2b27d2ba0", "rel": "bookmark"}], "name": "RCP-1234-xxx-cp-0"}, {"id": "aa2b0e3e-deb5-40fb-98c9-8d01e137e51b", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/aa2b0e3e-deb5-40fb-98c9-8d01e137e51b", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/aa2b0e3e-deb5-40fb-98c9-8d01e137e51b", "rel": "bookmark"}], "name": "RCP-1234-xxx-mn-1"}, {"id": "f6a3c38c-8ddb-4eb6-81d5-3c39e4afc9cc", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/f6a3c38c-8ddb-4eb6-81d5-3c39e4afc9cc", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/f6a3c38c-8ddb-4eb6-81d5-3c39e4afc9cc", "rel": "bookmark"}], "name": "RCP-1234-xxx-mn-0"}, {"id": "60c9eca2-45b1-4340-9f8e-5acd84b06f4e", "links": [{"href": "https://10.107.177.68:8774/v2.1/servers/60c9eca2-45b1-4340-9f8e-5acd84b06f4e", "rel": "self"}, {"href": "https://10.107.177.68:8774/servers/60c9eca2-45b1-4340-9f8e-5acd84b06f4e", "rel": "bookmark"}], "name": "RCP-1234-xxx-dbm-0"}]}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容