OpenStack Heat AutoScaling details and example code

  • 2020-05-30 21:17:57
  • OfStack

OpenStack Heat AutoScaling

1. The background

Openstack's Heat is a component added after the H release, designed to create a set of business processes that make it easier to manage a cluster. The virtual machines in the cluster can serve customers as a whole. In Heat, functions are defined as resources. In Heat, Nova, Neutron, Ceilometer and other components are used. These can all be regarded as resources and described by template files, which can be in yaml format, json format or yaml format.
The concept of AutoScaling first appeared in AWS. AutoScaling is an Web service, which aims to automatically scale and scale virtual machines by starting or terminating virtual machines according to user-defined policies and schedule running state checks.

Auto Scale in Openstack is a combination of Heat and Ceilometer module 1. Ceilometer is responsible for collecting and processing performance data. Once the threshold defined in the Heat template is reached, an alarm message will be sent to heat-engine. heat-engine will mobilize other OpenStack resources defined in the Heat template to realize auto scale.

2. Heat AutoScaling Resources

The resources involved in implementing AutoScaling are as follows:

1.AWS::AutoScaling::AutoScalingGroup

A flex group is a collection of instances with the same application scenario, defining the maximum and minimum number of instances within the group, cooling time, and so on.
Note: the cooldown time refers to the lock time after 1 expansion activity, during which no other expansion activity can be carried out.

The syntax is as follows:


{
 "Type" : "AWS::AutoScaling::AutoScalingGroup",
 "Properties" : {
  "AvailabilityZones" : [ String, ... ],
  "Cooldown" : String,
  "DesiredCapacity" : String,
  "HealthCheckGracePeriod" : Integer,
  "HealthCheckType" : String,
  "InstanceId" : String,
  "LaunchConfigurationName" : String,
  "LoadBalancerNames" : [ String, ... ],
  "MaxSize" : String,
  "MetricsCollection" : [ MetricsCollection, ... ]
  "MinSize" : String,
  "NotificationConfigurations" : [ NotificationConfigurations, ... ],
  "PlacementGroup" : String,
  "Tags" : [ Auto Scaling Tag, ..., ],
  "TargetGroupARNs" : [ String, ... ],
  "TerminationPolicies" : [ String, ..., ],
  "VPCZoneIdentifier" : [ String, ... ]
 }
}

2.AWS::AutoScaling::LaunchConfiguration

The flex configuration defines the configuration for the instance that is used for elastic scaling. Used by AutoScalingGroup to configure the instances within the group.

The syntax is as follows:


{
  "Type" : "AWS::AutoScaling::LaunchConfiguration",
  "Properties" : {
   "AssociatePublicIpAddress" : Boolean,
   "BlockDeviceMappings" : [ BlockDeviceMapping, ... ],
   "ClassicLinkVPCId" : String,
   "ClassicLinkVPCSecurityGroups" : [ String, ... ],
   "EbsOptimized" : Boolean,
   "IamInstanceProfile" : String,
   "ImageId" : String,
   "InstanceId" : String,
   "InstanceMonitoring" : Boolean,
   "InstanceType" : String,
   "KernelId" : String,
   "KeyName" : String,
   "PlacementTenancy" : String,
   "RamDiskId" : String,
   "SecurityGroups" : [ SecurityGroup, ... ],
   "SpotPrice" : String,
   "UserData" : String
  }
}

3.AWS::AutoScaling::ScalingPolicy

The policy for adding scaling to auto scale group defines the specific operations to expand or contract, and the number of scaling.

The syntax is as follows:


{
  "Type" : "AWS::AutoScaling::ScalingPolicy",
  "Properties" : {
   "AdjustmentType" : String,
   "AutoScalingGroupName" : String,
   "Cooldown" : String,
   "EstimatedInstanceWarmup" : Integer,
   "MetricAggregationType" : String,
   "MinAdjustmentMagnitude" : Integer,
   "PolicyType" : String,
   "ScalingAdjustment" : Integer,
   "StepAdjustments" : [ StepAdjustments, ... ]
  }
}   

In addition, AutoScaling in Heat also needs to cooperate with OS::Ceilometer::Alarm for use. Alarm monitors the operation of the instance. Once the threshold is exceeded, an alarm will be generated.

3.Heat AutoScaling Template

Here's a simple example:


heat_template_version: 2013-05-23
description: Heat template for autoscaling
parameters:# define 1 Some of the variables 
 flavor:
  type: string
  default: m1.small
 image:
  type: string
  default: 1a2b3c4f-1a2b-3c4f-5d6e-4130ff5203de
 availability_zone:
  type: string
  default: nova
 alarm_scaleout_threshold:# The threshold value 
  type: number
  default: 80
 alarm_scalein_threshold:# The threshold value 
  type: number
  default: 20
resources:
 neutron_network:
  type: OS::Neutron::Net
  properties:
   name: {get_param: "OS::stack_name"}

 neutron_subnet:
  type: OS::Neutron::Subnet
  properties:
   name: {get_param: "OS::stack_name"}
   network_id: { get_resource: neutron_network }
   cidr: '192.168.111.0/24'
   gateway_ip: '192.168.111.1'
   allocation_pools:
    - start: '192.168.111.2'
     end: '192.168.111.254'
 neutron_router:
  type: OS::Neutron::Router
  properties:
   name: {get_param: "OS::stack_name"}
 add_router_interface:
  type: OS::Neutron::RouterInterface
  properties:
   router_id: { get_resource: neutron_router }
   subnet_id: { get_resource: neutron_subnet }
 nova_server_security_group:
  type: OS::Neutron::SecurityGroup
  properties:
   description: 'security group for VM'
   name: {get_param: "OS::stack_name"}
   rules: [
    {direction: 'ingress',
     remote_ip_prefix: '0.0.0.0/0',
     port_range_min: 0,
     port_range_max: 30000,
     ethertype: IPv4,
     protocol: 'tcp'},
    {direction: 'egress',
     remote_ip_prefix: '0.0.0.0/0',
     port_range_min: 0,
     port_range_max: 65535,
     ethertype: 'IPv4',
     protocol: 'tcp'},
    {direction: 'egress',
     remote_ip_prefix: '0.0.0.0/0',
     port_range_min: 0,
     port_range_max: 65535,
     ethertype: 'IPv4',
     protocol: 'udp'},
    {direction: 'ingress',
     remote_ip_prefix: '0.0.0.0/0',
     port_range_min: null,
     port_range_max: null,
     ethertype: 'IPv4',
     protocol: 'icmp'},
    {direction: egress,
     remote_ip_prefix: '0.0.0.0/0',
     port_range_min: null,
     port_range_max: null,
     ethertype: 'IPv4',
     protocol: 'icmp'}
   ]
 launch_config:#Scale group The configuration of the instance in 
  type: AWS::AutoScaling::LaunchConfiguration
  properties:
   ImageId: { get_param: image }# Instance used image
   InstanceType: { get_param: flavor }# Instance used flavor
   SecurityGroups: [ get_resource: nova_server_security_group ]
   UserData: |# A script that runs at instance startup 
     #!/bin/bash
     passwd root << EOD
     123456
     123456
     EOD
 server_group:# Telescopic group 
  type: AWS::AutoScaling::AutoScalingGroup
  properties:
   AvailabilityZones: []
   Cooldown: '60'# Cooling time 
   LaunchConfigurationName: { get_resource: launch_config }# Configuration of instances in groups 
   MinSize: '1'# Minimum number of instances 
   MaxSize: '4'# Maximum number of instances 
   VPCZoneIdentifier: [ get_resource: neutron_subnet ]
 scaleout_policy:# The strategy of scaling up 
  type: AWS::AutoScaling::ScalingPolicy
  properties:
   AdjustmentType: ChangeInCapacity
#heat  support 3 Types of adjustment methods: change_in_capacity (new = current + adjustment), #exact_capacity (new = adjustment), percent_change_in_capacity ( in current  The base of # Base on  adjustment  the   Percentage adjustment )
   AutoScalingGroupName: { get_resource: server_group }
   ScalingAdjustment: '1'# Each time the amount of adjustment, namely increase 1 An instance 
 scalein_policy:# A downward contraction strategy 
  type: AWS::AutoScaling::ScalingPolicy
  properties:
   AdjustmentType: ChangeInCapacity
   AutoScalingGroupName: { get_resource: server_group }
   ScalingAdjustment: '-1'# Each time the amount of adjustment, namely reduce 1 An instance 
 neutron_port:
  type: OS::Neutron::Port
  properties:
   network_id: { get_resource: neutron_network }
   fixed_ips:
    - subnet_id: { get_resource: neutron_subnet }
   security_groups: [ { get_resource: nova_server_security_group } ]
 alarm_scaleout: # define 1 a  ceilometer alarm
  type: OS::Ceilometer::Alarm
  properties:
   description: Scale-up if the average CPU > 80% for 10 minute
   meter_name: cpu_util # Monitor the virtual machine  cpu_util
   statistic: avg #statistic  Is calculated as  avg  The method of average value 
   period: 600 # Statistical cycle 
   evaluation_periods: 1 # It takes several cycles to be effective 
   repeat_actions: true
   threshold: { get_param: alarm_scaleout_threshold }# cpu_util  The threshold 
   alarm_actions: # The alarm in alarm  State of the  action . 
    - {get_attr: [scaleout_policy, AlarmUrl]}
   matching_metadata: {'metadata.user_metadata.groupname': {get_resource: 'server_group'}}
   comparison_operator: gt # The comparison method of detection value and threshold value is  gt  That is greater than the 
 alarm_scalein:
  type: OS::Ceilometer::Alarm
  properties:
   description: Scale-down if the average CPU < 20% for 10 minutes
   meter_name: cpu_util
   statistic: avg
   period: 600
   evaluation_periods: 1
   repeat_actions: true
   threshold: { get_param: alarm_scalein_threshold }
   alarm_actions:
    - {get_attr: [scalein_policy, AlarmUrl]}
   matching_metadata: {'metadata.user_metadata.groupname': {get_resource: 'server_group'}}
   comparison_operator: lt# The comparison method of detection value and threshold value is  lt  That is less than 
outputs:
 scale_in_url:
  value: { get_attr: [ scalein_policy, AlarmUrl ] }
 scale_out_url:
  value: { get_attr: [ scaleout_policy, AlarmUrl ] }

The function of stack is to monitor the usage of CPU of the instance. When the usage of CPU is greater than 80%, a new instance will be started, and when the usage of CPU is less than 20%, one instance will be reduced.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: