Using Heat for autoscaling
OpenStack provides autoscaling features through Heat. This feature reduces the need to manually provision instance capacities in advance. You can use Heat resources to detect when a Ceilometer alarm triggers and provision or de-provision a new VM depending on the trigger. These groups of VMs must be under a load balancer which distributes the load among the VMs on the scaling group.
Whether you are running one instance or thousands, you can use autoscaling to detect, increase, decrease, and replace instances without manual intervention.
In the following examples, the following two policies are defined:
- When the CPU utilization rate is above 50%, a new instance is created automatically until the number of instances reaches six.
- When any CPU utilization rate is below 15%, an instance is terminated until the number of instances reaches one.
Creating stacks
- Download the simple scale file.
- Log on to the Horizon dashboard with the heat_stack_owner role.
- Click Project -> Orchestration -> Stacks.
- Click Launch Stack.
- On the Select Template page, click Choose File and select the simple scale file on your local machine.
- Update the image ID, network ID, key pair name, and security group ID based on your cloud instance.
- Click Launch.
Monitoring stacks
- Log on to the Horizon dashboard with the heat_stack_owner role.
- Click Project -> Instance and find the instance created as part of the stack.
- Connect to the instance with SSH connection tools such as Putty.
- Run the
top
command to check CPU utilization rate. The utilization rate consumed by the dd process should be at least 99%. This triggers the CPU high utilization alarm. - Wait approximately ten minutes and verify that a new instance is created as part of the stack.
- Log in to both instances, find the dd process, and terminate it. This triggers the CPU low utilization alarm.
- Wait approximately ten minutes and verify that one of the instance is terminated and only one remains.
Removing stacks
- Log on to the Horizon dashboard with the heat_stack_owner role.
- Click Project -> Orchestration -> Stacks.
- Select the stack that you want to remove and click Delete.
An example of deploying a WordPress stack
You can find an auto-scaling example template for Heat on the official OpenStack Github repository. The following example uses a similar template based on the official one, which uses a snapshot of a VM with WordPress already installed and configured over an Ubuntu operating system as a base image.
You need the following parameters:
The first resource you will provision is the health monitor configuration that will check the VMs under the load balancer. If the VM is down, the load balancer will not send traffic to it. Create the pool using the health monitor and specifying the protocol, the network and subnet, the algorithm to use for distributing the traffic, and the port that will receive the traffic on the virtual IP. Finally, create the load balancer using this pool.
- Create the health monitor, pool, and load balancer:
- Associate a public IP for the load balancer so that it can be accessed from the Internet. Use the following syntax to create a floating IP for the load balancer:
- Use the following syntax to create the auto-scaling group (VMs):
Use a Heat specific resource to define the scaling group: size and image that will be used to scale. You must make some changes on the database as every time the saved image is initialized, the public IP must be updated on the database. Use the user data to do this. See another file lb_server.yaml.
- Using specific Heat resources, define both up and down scaling policies. Use the following syntax to create scaling policies:
- Use Ceilometer to establish the alarms (both high and low) for the auto-scaling group for a specific metric. Use the following syntax to create Ceilometer alarms:
In the lb_server.yaml file, define the image to initialize each time that the alarm triggers. These are the parameters that will be filled from the original template (the official one):
- The first resource is the Nova server that you will initialize from a specific image. In our example, this is Ubuntu 14.04 with WordPress already installed and configured:
- The last resource for this template is a pool member in Neutron that will add the IP of the new server that was initialized to the pool on the load balancer:
Process diagrams
The following diagram shows the process when you launch the Heat stack:
When the VM reaches the high CPU load limit that you set, the alarm in Ceilometer triggers and a new VM is initialized. This diagram shows the process:
The same process occurs when a VM reaches the low CPU load limit as shown in this diagram: