Sample templates: Heat (auto-scaling and auto-recovery)

The following code is a sample Heat file that is used to create a single Nova instance, using the auto-recovery and auto-scaling capabilities. In this template, the Nova instance is also placed behind a load balancer and a floating IP is assigned to it.

Auto-recovery example

heat_template_version: 2013-05-23
description: A server subject to health maintenance
parameters:
  image:
    type: string
    description: Name of image used to create nova instance
  key_name:
    type: string
    description: name of SSH keypair to connect to the server
  flavor:
    type: string
    description: flavor used by the servers
    default: m1.medium
  private_network:
    type: string
    description: Name or ID of the network to which the server will be attached
  private_subnet_id:
    type: string
  external_network_id:
    type: string
  protocol_port:
    type: number
    description: port number where server listens
    default: 80
  az:
    type: string
    description: availability zone for instances
    default: nova

resources:
  server:
    type: OS::Nova::Server
    properties:
      flavor: {get_param: flavor}
      availability_zone: {get_param: az}
      image: {get_param: image}
      key_name: {get_param: key_name}
      networks:
        - {network: {get_param: private_network}}
      user_data_format: RAW
      user_data: |
        #!/bin/sh
        #yum install -y httpd
        service httpd start
        echo "Open Cloud Foundational Services Demo: " > /var/www/html/index.html
        echo $(ifconfig eth0 | grep 'inet addr' | awk -F: '{ print $2 }' | awk '{ print $1 }') >> /var/www/html/index.html

  restarter:
    type: OS::Heat::HARestarter
    properties:
      InstanceId: {get_resource: server}

  gone_alarm:
    type: OS::Ceilometer::Alarm
    properties:
      description: Detect server being unresponsive
      meter_name: network.services.lb.member
      statistic: avg
      period: 70
      evaluation_periods: 2
      threshold: 1
      alarm_actions: [ {get_attr: [restarter, AlarmUrl]} ]
      repeat_actions: False
      query:
      - field: resource_id
        op: eq
        value: {get_resource: member}  
      comparison_operator: lt
    depends_on: server

#create load balancer
  monitor:
    type: OS::Neutron::HealthMonitor
    properties:
      type: TCP
      delay: 3
      max_retries: 5
      timeout: 3

  pool:
    type: OS::Neutron::Pool
    properties:
      protocol: HTTP
      monitors: [{get_resource: monitor}]
      subnet_id: { get_param: private_subnet_id }
      lb_method: ROUND_ROBIN
      vip:
        protocol_port: 80

  lb:
    type: OS::Neutron::LoadBalancer
    properties:
      protocol_port: 80
      pool_id: {get_resource: pool}

  vip_floating_ip:
    type: OS::Neutron::FloatingIP
    properties:
      floating_network_id: { get_param: external_network_id }

  vip_floating_assoc:
    type: OS::Neutron::FloatingIPAssociation
    properties:
      floatingip_id: { get_resource: vip_floating_ip }
      port_id: { get_attr: [ pool, vip, port_id ] }
      fixed_ip_address: { get_attr: [ pool, vip, address ] }

  member:
    type: OS::Neutron::PoolMember
    properties:
      pool_id: {get_resource: pool}
      address: {get_attr: [server, first_address]}
      protocol_port: {get_param: protocol_port}

outputs:
  server_id:
    value: {get_resource: server}
  server_nets:
    value: {get_attr: [server, addresses]}
  pool_member_id:
    value: {get_resource: member}
  restart_url:
    value: {get_attr: [restarter, AlarmUrl]}
    description: the webhook for the HARestarter
  alrm:
    value: {get_resource: gone_alarm}
  ceilometer_query:
    value:
      str_replace:
        template: ceilometer statistics -m network.services.lb.member -q resource_id=rscid -p 70 -a avg
        params:
          rscid: {get_resource: member}
    description: >
      This is a Ceilometer CLI command that produces statistics
      similar to those used by the alarm
  Web_Console:
    description: The IP address of the loud balancer for the Foundational Services app
    value:
      str_replace:
        template:  http://%loadip%
        params:
          "%loadip%": { get_attr: [ vip_floating_ip, floating_ip_address ] }

Auto-scaling example

For the auto-scaling capability, there are a number of examples available in the OpenStack community. For more information, see:
  • Heat Templates: Basic Example
    Note: This example does not work automatically. Rather, you must manually scale up and down by calling on a Webhook.
  • Heat Templates: Advanced Example
    Note: This example shows the complete template for setting up auto-scaling based on usage. In this example, scaling up and down is automatically triggered based on CPU usage.