Openstack HEAT tutorials: 3. Autoscaling with Ceilometer
The real power of Heat is creating autoscaling resources which automatically scale up/down resources based on Ceilometer metrics.
In the example below, Heat will create an autoscaling group with a minimum of 1 instances and a maximum of 5 instances (at launch time it will create only one instance) and will adjust the number of instances based on the average CPU utilization. If the average CPU utilization, calculated over all existing instances, will be higher than 50% (average measured on a 10 minutes period), it will add another instance, identical to the existing ones. If the average CPU utilization, calculated over all existing instances is lower than 15% (average measured on a 10 minutes period) it will remove one instance, in order to free up resources.
All the parameters are configurable: the number of instances to be created at launch time, the minimum/maximum number of instances, the CPU utilization thresholds, the number of instances to be added/deleted when an alarm is triggered etc.
For more introductory details about the structure of a template, please see tutorial 1 and tutorial 2.
In the template below there are 5 resources defined. The autoscaling group which configures the desired number of instances to be created at launch time and the minimum/maximum number of instances in the stack and also the typical OS::Nova::Server properties (instance properties).
The next two resources define the scale up and the scale down policy resources. They configure the autoscaling policies and their main parameters are: scaling_adjusment (+/-N – where N is the number of instances to be created or deleted when the Ceilometer alarm is triggered) and the cooldown period (time to wait after an adjustment is made before another adjusment can be done).
The next two resources define the Ceilometer alarms that are triggered when the average CPU utlization is higher/lower than the configured thresholds for a duration >= period * evaluation_periods.
Useful commands for debugging:
[code lang=”bash”] #shows the state of the alarmsceilometer alarm-list
+————————————–+———————————–+——-+———+————+———————————+
| Alarm ID | Name | State | Enabled | Continuous | Alarm condition |
+————————————–+———————————–+——-+———+————+———————————+
| 8918e5a8-d0c8-44ee-8dfa-b87d7b8a6d65 | test123-CPUAlarmLow-4wczpjiq7qy2 | alarm | True | False | cpu_util < 15.0 during 1 x 600s |
| fb3afa33-1851-408f-b4e1-e0a53724d69f | test123-CPUAlarmHigh-niceu3mhxwef | ok | True | False | cpu_util > 50.0 during 1 x 600s |
+————————————–+———————————–+——-+———+————+———————————+
[/code] [code lang=”bash”] #check ceilometer metrics (in this case CPU utilization of the stack)
ceilometer statistics -m cpu_util -q metadata.user_metadata.stack=7bb40802-c36f-4112-add3-95e1ec26e3ce -p 60
+——–+———————+———————+—————–+———-+———————+———————+
| Period | Period Start | Period End | Avg | Duration | Duration Start | Duration End |
+——–+———————+———————+—————–+———-+———————+———————+
| 60 | 2015-04-23T14:36:03 | 2015-04-23T14:37:03 | 0.0616666666667 | 0.0 | 2015-04-23T14:36:40 | 2015-04-23T14:36:40 |
| 60 | 2015-04-23T14:46:03 | 2015-04-23T14:47:03 | 0.0566666666667 | 0.0 | 2015-04-23T14:46:40 | 2015-04-23T14:46:40 |
| 60 | 2015-04-23T14:56:03 | 2015-04-23T14:57:03 | 68.5066666667 | 0.0 | 2015-04-23T14:56:40 | 2015-04-23T14:56:40 |
+——–+———————+———————+—————–+———-+———————+———————+
[/code]
And here is the template used for test:
[code lang=”bash”] heat_template_version: 2013-05-23
parameters:
flavor:
type: string
description: Type of instance (flavor) to be used
default: m1.small
network_id:
type: string
description: ID of admin Neutron network into which servers get deployed
default: 84c9c9b3-ed5b-45e0-8e97-b014a38e8545
key_name:
type: string
description: Name of an existing key pair to use for the instance
default: mbalas
image:
type: string
default: 0b3162c4-60ab-4c10-b9f3-0484365ead57
resources:
asg:
type: OS::Heat::AutoScalingGroup
properties:
resource:
type: OS::Nova::Server
properties:
key_name: { get_param: key_name }
image: { get_param: image }
flavor: { get_param: flavor }
networks: [{network: {get_param: network_id} }]
security_groups: [default, MBA_securityGroup]
metadata: {"metering.stack": {get_param: "OS::stack_id"}}
min_size: 1
desired_capacity: 1
max_size: 5
scale_up_policy:
type: OS::Heat::ScalingPolicy
properties:
adjustment_type: change_in_capacity
auto_scaling_group_id: {get_resource: asg}
cooldown: 60
scaling_adjustment: 1
scale_dn_policy:
type: OS::Heat::ScalingPolicy
properties:
adjustment_type: change_in_capacity
auto_scaling_group_id: {get_resource: asg}
cooldown: 60
scaling_adjustment: ‘-1’
CPUAlarmHigh:
type: OS::Ceilometer::Alarm
properties:
description: Scale-up if the average CPU > 50% for 10 minutes
meter_name: cpu_util
statistic: avg
period: ‘600’
evaluation_periods: ‘1’
threshold: ’50’
alarm_actions: [{get_attr: [scale_up_policy, alarm_url]}]
matching_metadata: {‘metadata.user_metadata.stack’: {get_param: "OS::stack_id"}}
comparison_operator: gt
CPUAlarmLow:
type: OS::Ceilometer::Alarm
properties:
description: Scale-down if the average CPU < 15% for 10 minutes
meter_name: cpu_util
statistic: avg
period: ‘600’
evaluation_periods: ‘1’
threshold: ’15’
alarm_actions: [{get_attr: [scale_dn_policy, alarm_url]}]
matching_metadata: {‘metadata.user_metadata.stack’: {get_param: "OS::stack_id"}}
comparison_operator: lt
[/code]