RHOSP11: Autoscaling - CPU Alarm

Openstack capable of monitoring the instance resource and do the autoscaling based on the rules.
To create the orchestration stack, you need three files (put the filename as what you want):

1. rules.yaml
    - contain scaling policy

2. environment.yaml
    - contain environment

3. os.yaml
    - contain instance configuration

rules.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 > 40%
      metric: cpu_util
      aggregation_method: mean
      granularity: 300
      evaluation_periods: 1
      threshold: 40
      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_util
      aggregation_method: mean
      granularity: 300
      evaluation_periods: 1
      threshold: 5
      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]}

os.yaml 
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: director
  network:
    type: string
    description: project network to attach instance to
    default: private
  external_network:
    type: string
    description: network used for floating IPs
    default: public
  sg_name:
    type: string
    description: security group
    default: mysg

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 }
      user_data_format: RAW
      user_data: |
        #!/bin/sh
        while [ 1 ] ; do echo $((13**99)) 1>/dev/null 2>&1; done

  port:
    type: OS::Neutron::Port
    properties:
      network: {get_param: network}
      security_groups:
        - {get_param: sg_name}

  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

environment.yaml
resource_registry:
    "OS::Nova::Server::Cirros": "os.yaml"




To create the stack:

1. source the rc

# . stackrc


2. Create the stack.


# openstack stack create -t rules.yaml -e environment.yaml  cpualarm


3. Check the stack creation .


[stack@director ~]$ openstack stack list

+--------------------------------------+------------+-----------------+----------------------+--------------+

| ID                                   | Stack Name | Stack Status    | Creation Time        | Updated Time |

+--------------------------------------+------------+-----------------+----------------------+--------------+

| 948ebe91-e933-4dd6-b465-16148c857d05 | cpualarm   | CREATE_COMPLETE | 2017-09-25T14:22:13Z | None         |

+--------------------------------------+------------+-----------------+----------------------+--------------+

[stack@director ~]$





4. Check the alarm.


[stack@director ~]$ openstack alarm list -c name -c state

+--------------------------------------+-------------------+

| name                                 | state             |

+--------------------------------------+-------------------+

| cpualarm-cpu_alarm_low-r44btnzza3yd  | ok                |

| cpualarm-cpu_alarm_high-2sfy6gzpiuwg | alarm             |

| cputhreshold-2                       | insufficient data |

+--------------------------------------+-------------------+

Comments

Popular Posts