1.2. 示例:根据 CPU 使用情况自动扩展
在本例中,编配会检查 Telemetry 数据,并自动增加实例数量,以响应高 CPU 使用。创建堆栈模板和环境模板,以定义规则和后续配置。本例使用现有资源,如网络,并使用与您自己的环境中可能不同的名称。
cpu_util
指标已弃用,并从 Red Hat OpenStack Platform 中删除。
流程
创建环境模板,描述实例类别、网络配置和镜像类型。将模板保存到
/home/<user>/stacks/example1/cirros.yaml
文件中。将 <user
> 变量替换为真实用户名。heat_template_version: 2016-10-14 description: Template to spawn an cirros instance. parameters: metadata: type: json image: type: string description: image used to create instance default: cirros flavor: type: string description: instance flavor to be used default: m1.tiny key_name: type: string description: keypair to be used default: mykeypair network: type: string description: project network to attach instance to default: internal1 external_network: type: string description: network used for floating IPs default: external_network resources: server: type: OS::Nova::Server properties: block_device_mapping: - device_name: vda delete_on_termination: true volume_id: { get_resource: volume } flavor: {get_param: flavor} key_name: {get_param: key_name} metadata: {get_param: metadata} networks: - port: { get_resource: port } port: type: OS::Neutron::Port properties: network: {get_param: network} security_groups: - default floating_ip: type: OS::Neutron::FloatingIP properties: floating_network: {get_param: external_network} floating_ip_assoc: type: OS::Neutron::FloatingIPAssociation properties: floatingip_id: { get_resource: floating_ip } port_id: { get_resource: port } volume: type: OS::Cinder::Volume properties: image: {get_param: image} size: 1
在
~/stacks/example1/environment.yaml
中注册 Orchestration 资源:resource_registry: "OS::Nova::Server::Cirros": ~/stacks/example1/cirros.yaml
创建堆栈模板。描述要监视的 CPU 阈值以及要添加的实例数量。也会创建一个实例组来定义可参与此模板的最小和最大实例数量。
注意cpu_util
指标已弃用,并从 Red Hat OpenStack Platform 中删除。要获得等同的功能,请使用累积的cpu
指标和一个包含rate:mean
聚合方法的归档策略。例如,ceilometer-high-rate
和ceilometer-low-rate
。您必须将阈值从%转换为 ns,以使用 CPU utilisation 警报的cpu
指标。公式为:time_ns = 1,000,000,000 x {granularity} x {percentage_in_decimal}。例如,如果阈值为 80%,粒度为 1s,阈值为 1,000,000,000 x 1 x 0.8 = 800,000,000.0在
~/stacks/example1/template.yaml
中保存以下值:heat_template_version: 2016-10-14 description: Example auto scale group, policy and alarm resources: scaleup_group: type: OS::Heat::AutoScalingGroup properties: cooldown: 300 desired_capacity: 1 max_size: 3 min_size: 1 resource: type: OS::Nova::Server::Cirros properties: metadata: {"metering.server_group": {get_param: "OS::stack_id"}} scaleup_policy: type: OS::Heat::ScalingPolicy properties: adjustment_type: change_in_capacity auto_scaling_group_id: { get_resource: scaleup_group } cooldown: 300 scaling_adjustment: 1 scaledown_policy: type: OS::Heat::ScalingPolicy properties: adjustment_type: change_in_capacity auto_scaling_group_id: { get_resource: scaleup_group } cooldown: 300 scaling_adjustment: -1 cpu_alarm_high: type: OS::Aodh::GnocchiAggregationByResourcesAlarm properties: description: Scale up if CPU > 80% metric: cpu aggregation_method: rate:mean granularity: 1 evaluation_periods: 3 threshold: 800000000.0 resource_type: instance comparison_operator: gt alarm_actions: - str_replace: template: trust+url params: url: {get_attr: [scaleup_policy, signal_url]} query: str_replace: template: '{"=": {"server_group": "stack_id"}}' params: stack_id: {get_param: "OS::stack_id"} cpu_alarm_low: type: OS::Aodh::GnocchiAggregationByResourcesAlarm properties: metric: cpu aggregation_method: rate:mean granularity: 600 evaluation_periods: 3 threshold: 200000000.0 resource_type: instance comparison_operator: lt alarm_actions: - str_replace: template: trust+url params: url: {get_attr: [scaledown_policy, signal_url]} query: str_replace: template: '{"=": {"server_group": "stack_id"}}' params: stack_id: {get_param: "OS::stack_id"} outputs: scaleup_policy_signal_url: value: {get_attr: [scaleup_policy, signal_url]} scaledown_policy_signal_url: value: {get_attr: [scaledown_policy, signal_url]}
构建镜像并部署实例:
$ openstack stack create -t template.yaml -e environment.yaml example +---------------------+--------------------------------------------+ | Field | Value | +---------------------+--------------------------------------------+ | id | 248a98bb-f56e-4934-a281-fffde62d78d8 | | stack_name | example | | description | Example auto scale group, policy and alarm | | creation_time | 2017-03-06T15:00:29Z | | updated_time | None | | stack_status | CREATE_IN_PROGRESS | | stack_status_reason | Stack CREATE started | +---------------------+--------------------------------------------+
编排创建堆栈,并启动定义的最少 cirros 实例数量,如
scaleup_group
定义的min_size
参数中定义。验证实例是否已成功创建:$ openstack server list +--------------------------------------+-------------------------------------------------------+--------+------------+-------------+-------------------------------------+ | ID | Name | Status | Task State | Power State | Networks | +--------------------------------------+-------------------------------------------------------+--------+------------+-------------+-------------------------------------+ | e1524f65-5be6-49e4-8501-e5e5d812c612 | ex-3gax-5f3a4og5cwn2-png47w3u2vjd-server-vaajhuv4mj3j | ACTIVE | - | Running | internal1=10.10.10.9, 192.168.122.8 | +--------------------------------------+-------------------------------------------------------+--------+------------+-------------+-------------------------------------+
编排还创建两个 cpu 警报,它们触发 scale-up 或 scale-down 事件,如
cpu_ alarm_high
和cpu_ alarm_low
中定义的。验证触发器是否存在:$ openstack alarm list +--------------------------------------+--------------------------------------------+-------------------------------------+-------------------+----------+---------+ | alarm_id | type | name | state | severity | enabled | +--------------------------------------+--------------------------------------------+-------------------------------------+-------------------+----------+---------+ | 022f707d-46cc-4d39-a0b2-afd2fc7ab86a | gnocchi_aggregation_by_resources_threshold | example-cpu_alarm_high-odj77qpbld7j | insufficient data | low | True | | 46ed2c50-e05a-44d8-b6f6-f1ebd83af913 | gnocchi_aggregation_by_resources_threshold | example-cpu_alarm_low-m37jvnm56x2t | insufficient data | low | True | +--------------------------------------+--------------------------------------------+-------------------------------------+-------------------+----------+---------+