简单分析如何使用aggregate_instance_extra_spec filter

来源:互联网 发布:陕西和泰单片机 编辑:程序博客网 时间:2024/04/30 16:14
在 Nova 中,aggregate_instance_extra_spec filter的一般的使用方法都是在host的metadata中写好需要用的键值对,并且也在flavor中写好相应的键值对。再使用aggregate_instance_extra_spec filter 进行匹配,当这两个值匹配时,就认为是合适的主机。但其实还有这样一种需求,我们可能会要求在创建虚拟机时让其不创建在某些主机上。其实aggregate_instance_extra_spec filter 已经提供了这种功能。在进行匹配的过程中,会调用extra_spec_ops的match方法:

for aggregate_val in aggregate_vals:
if extra_specs_ops.match(aggregate_val, req):
break
在extra_spec_ops中,预先定义好了一系列可使用的操作符:

op_methods = {'=': lambda x, y: float(x) >= float(y),
'<in>': lambda x, y: y in x,
'<all-in>': lambda x, y: all(val in x for val in y),
'==': lambda x, y: float(x) == float(y),
'!=': lambda x, y: float(x) != float(y),
'>=': lambda x, y: float(x) >= float(y),
'<=': lambda x, y: float(x) <= float(y),
's==': operator.eq,
's!=': operator.ne,
's<': operator.lt,
's<=': operator.le,
's>': operator.gt,
's>=': operator.ge}
以及 '<or>' 操作符

这些操作符的使用方式是这样的:
除 '<or>' 之外, 在flavor 中定义extra spec时这样定义: 'key': '操作符' '字段',
如定义不选择在aggragate1上创建 'key1':  's!=' 'aggragate1'

'<or>' 操作符的使用方法是:
在flavor中定义 extra spec 时这样定义:
'key' :  '<or>'  'value1' '<or>'  'value2' '<or>'  'value3' 只要其中有一个value匹配就能匹配。



1. 根据extra_spec_ops的代码逻辑,不指定任何操作符和指定 's=='('=='for非string)的作用是相同的。 
使用的时候,先使用aggregate-set-metadata(CLI)或 os-aggregates API (REST) 对某个aggregate 的 metadata 进行编辑,比如增加这样一组键值对:

"reserved_for": "GPU" 用来表示这个aggregate是期望只保留给有GPU要求的虚拟机所使用的。

root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-create aggregate-test 817 512 1 1 
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+
| ID  | Name           | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+
| 817 | aggregate-test | 512       | 1    | 0         |      | 1     | 1.0         | True      |
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+

root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-key 817 set "reserved_for"="!= GPU"
root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-show 817
+----------------------------+----------------------------+
| Property                   | Value                      |
+----------------------------+----------------------------+
| OS-FLV-DISABLED:disabled   | False                      |
| OS-FLV-EXT-DATA:ephemeral  | 0                          |
| disk                       | 1                          |
| extra_specs                | {"reserved_for": "!= GPU"} |
| id                         | 817                        |
| name                       | aggregate-test             |
| os-flavor-access:is_public | True                       |
| ram                        | 512                        |
| rxtx_factor                | 1.0                        |
| swap                       |                            |
| vcpus                      | 1                          |
+----------------------------+----------------------------+

root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova aggregate-create GPU az_1
+----+------+-------------------+-------+--------------------------+
| Id | Name | Availability Zone | Hosts | Metadata                 |
+----+------+-------------------+-------+--------------------------+
| 1  | GPU  | az_1              |       | 'availability_zone=az_1' |
+----+------+-------------------+-------+--------------------------+

root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova aggregate-set-metadata GPU "reserved_for"="GPU"
Metadata has been successfully updated for aggregate 1.
+----+------+-------------------+-------+----------------------------------------------+
| Id | Name | Availability Zone | Hosts | Metadata                                     |
+----+------+-------------------+-------+----------------------------------------------+
| 1  | GPU  | az_1              |       | 'availability_zone=az_1', 'reserved_for=GPU' |
+----+------+-------------------+-------+----------------------------------------------+


root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova aggregate-add-host GPU SZX1000042608 
Host SZX1000042608 has been successfully added for aggregate 1 
+----+------+-------------------+-----------------+----------------------------------------------+
| Id | Name | Availability Zone | Hosts           | Metadata                                     |
+----+------+-------------------+-----------------+----------------------------------------------+
| 1  | GPU  | az_1              | 'SZX1000042608' | 'availability_zone=az_1', 'reserved_for=GPU' |
+----+------+-------------------+-----------------+----------------------------------------------+


然后再创建一个flavor,并使用flavor-key(CLI)或os-flavor API (REST) 接口在这个flavor中添加一个metadata:
"reserved_for": "!= GPU" 用来表明使用这个flavor创建的虚拟机没有对于GPU的要求。


root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-create aggregate-test 817 512 1 1 
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+
| ID  | Name           | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+
| 817 | aggregate-test | 512       | 1    | 0         |      | 1     | 1.0         | True      |
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+

root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-key 817 set "reserved_for"="!= GPU"
root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-show 817
+----------------------------+----------------------------+
| Property                   | Value                      |
+----------------------------+----------------------------+
| OS-FLV-DISABLED:disabled   | False                      |
| OS-FLV-EXT-DATA:ephemeral  | 0                          |
| disk                       | 1                          |
| extra_specs                | {"reserved_for": "!= GPU"} |
| id                         | 817                        |
| name                       | aggregate-test             |
| os-flavor-access:is_public | True                       |
| ram                        | 512                        |
| rxtx_factor                | 1.0                        |
| swap                       |                            |
| vcpus                      | 1                          |
+----------------------------+----------------------------+

创建虚拟机:

root@SZX1000042608:/var/log/nova# nova boot --flavor 817 --image 78359cf6-8cf9-4784-8233-71d10dda120f  --nic net-id=11abd3d4-1d87-4876-9742-e78b132b609b test_GPU

可以看到,虚拟机创建失败了:

root@SZX1000042608:/var/log/nova# nova list
+--------------------------------------+----------+--------+------------+-------------+---------------------------------+
| ID                                   | Name     | Status | Task State | Power State | Networks                        |
+--------------------------------------+----------+--------+------------+-------------+---------------------------------+
| 334b9e87-38ce-4347-ae10-b7e76ce096e8 | test_GPU | ERROR  | -          | NOSTATE     |                                 |
+--------------------------------------+----------+--------+------------+-------------+---------------------------------+

而原因就是,和预期一样:

2015-11-19 19:41:53.423 14130 DEBUG nova.scheduler.filters.compute_capabilities_filter [req-e7694577-3bb2-4ebc-a398-bddd6d4b9843 88b93a0edf064bd2918b828517d6a292 18d273a324304a7aaad66d88fa94ec15 - - -] (SZX1000042608, SZX1000042608) ram:7451 disk:10240 io_ops:0 instances:0 fails. There are no capabilities to retrieve. _get_capabilities /opt/stack/nova/nova/scheduler/filters/compute_capabilities_filter.py:63
2015-11-19 19:41:53.423 14130 DEBUG nova.scheduler.filters.compute_capabilities_filter [req-e7694577-3bb2-4ebc-a398-bddd6d4b9843 88b93a0edf064bd2918b828517d6a292 18d273a324304a7aaad66d88fa94ec15 - - -] (SZX1000042608, SZX1000042608) ram:7451 disk:10240 io_ops:0 instances:0 fails instance_type extra_specs requirements host_passes /opt/stack/nova/nova/scheduler/filters/compute_capabilities_filter.py:101
2015-11-19 19:41:53.423 14130 INFO nova.filters [req-e7694577-3bb2-4ebc-a398-bddd6d4b9843 88b93a0edf064bd2918b828517d6a292 18d273a324304a7aaad66d88fa94ec15 - - -] Filter ComputeCapabilitiesFilter returned 0 hosts

而再使用普通flavor创建一台虚拟机:
root@SZX1000042608:/var/log/nova# nova boot --flavor 1 --image 78359cf6-8cf9-4784-8233-71d10dda120f  --nic net-id=11abd3d4-1d87-4876-9742-e78b132b609b test_GPU_1

成功了:

root@SZX1000042608:/var/log/nova# nova list
+--------------------------------------+------------+--------+------------+-------------+---------------------------------+
| ID                                   | Name       | Status | Task State | Power State | Networks                        |
+--------------------------------------+------------+--------+------------+-------------+---------------------------------+
| 334b9e87-38ce-4347-ae10-b7e76ce096e8 | test_GPU   | ERROR  | -          | NOSTATE     |                                 |
| 391a043b-27aa-4935-8456-00d9b2e75c6d | test_GPU_1 | ACTIVE | -          | Running     | public=172.24.4.12, 2001:db8::c |
+--------------------------------------+------------+--------+------------+-------------+---------------------------------+

2. 带s前缀的op所对应的的操作,例如's=='对应的operator.eq,其实在查看operator.py后发现,这些富比较(rich comparision)方法并没有被重写,即operator.eq和 == 作用相同,因此不是string也可以用这类操作符,当然string类的就不能用不带 s 前缀的操作符了。


3. <in> 的用法, 如:在host 的aggregate metada 中指定 "reserved_for":"CPU GPU" 表示为CPU 和GPU预留(在使用时会通过string.split()分解为list),在flavor中配置
"reserved_for": 'CPU' 或 "reserved_for": '<in> GPU'  就可以匹配到这台主机上,由于才用的是 in 因此同时满足多个关键字使用 <in> 是不行的, 代码逻辑中只会取第一个值。

<all-in> 可以用来实现对多个关键字的匹配,如:在host 的aggregate metada 中指定 "reserved_for":"CPU GPU Multi-NIC" 表示为CPU,GPU和多网卡的预留,
在flavor中配置
"reserved_for": '<all-in> CPU' 或 "reserved_for": '<all-in> CPU GPU '  等 都可以匹配到这台主机上.

root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-create aggregate-test 817 512 1 1 
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+
| ID  | Name           | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+
| 817 | aggregate-test | 512       | 1    | 0         |      | 1     | 1.0         | True      |
+-----+----------------+-----------+------+-----------+------+-------+-------------+-----------+

root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-key 817 set "reserved_for"="!= GPU"
root@SZX1000042608:/usr/local/lib/python2.7/dist-packages/novaclient# nova flavor-show 817
+----------------------------+----------------------------+
| Property                   | Value                      |
+----------------------------+----------------------------+
| OS-FLV-DISABLED:disabled   | False                      |
| OS-FLV-EXT-DATA:ephemeral  | 0                          |
| disk                       | 1                          |
| extra_specs                | {"reserved_for": "!= GPU"} |
| id                         | 817                        |
| name                       | aggregate-test             |
| os-flavor-access:is_public | True                       |
| ram                        | 512                        |
| rxtx_factor                | 1.0                        |
| swap                       |                            |
| vcpus                      | 1                          |
+----------------------------+----------------------------+
0 0