Skip to content

Instantly share code, notes, and snippets.

@zhanghui9700
Last active September 25, 2017 00:30
Show Gist options
  • Save zhanghui9700/135e22509c960d0bbd97ceb7d6256100 to your computer and use it in GitHub Desktop.
Save zhanghui9700/135e22509c960d0bbd97ceb7d6256100 to your computer and use it in GitHub Desktop.
heat tutorial
@zhanghui9700
Copy link
Author

orchestration by openstack heat 03-a sample completely stack

condition resource in HOT

condition:

root@node-6:~/heat# openstack stack create condition-stack -t 05_condition.yml --parameter image_id=8cf6fd68-adc4-4a08-9b52-9a0014630c72

OS::Heat::WaitCondition & OS::Heat::WaitConditionHandle & wc_notify --data-binary

wc_notify: {get_attr: ['wait_handle', 'curl_cli']}
curl -i -X POST -H X-Auth-Token: gAAAAABYb0g0Nzy0_cWbwyIP8RKNs1VSN-Al5anlCBuHVNv2__K4517Rej2RCpUG51CDQjPjzQ4S5tgFNGUXxRibYch1gP-nZ2zX_1swb16Uyu0pOuRHCsDeURVdUGKIwEiKHVXllBrAvauZdtHCCUQ9zyqE0hAduSISZTWC5h5u6GteoHRqJfY -H Content-Type: application/json -H Accept: application/json http://192.168.32.8:8004/v1/9d5bcc7e5e994f3f83588485719ffbbc/stacks/condition-stack/c9d40cd5-cd20-48c6-8de1-bdc50a678d1e/resources/wait_handle/signal`

root@node-6:~/heat# cat 05_condition.yml

heat_template_version: 2013-05-23

description: Simple template to deploy a single compute instance

parameters:
  image_id:
    type: string
    label: Image ID
    description: Image to be used for compute instance

resources:
  wait_condition:
    type: OS::Heat::WaitCondition
    properties:
      handle: {get_resource: wait_handle}
      count: 1
      timeout: 600
  wait_handle:
    type: OS::Heat::WaitConditionHandle
  my_instance:
    type: OS::Nova::Server
    properties:
      image: { get_param: image_id }
      flavor: m1.small
      #key_name: my_key
      networks:
        - network: admin_internal_net
      user_data: 
        str_replace:
          params:
            wc_notify: {get_attr: ['wait_handle', 'curl_cli']}
          template: |
            #!/bin/sh
            echo "hello, the world of heat."
            # notify heat that we are done here
            wc_notify --data-binary '{"status": "SUCCESS"}'
      user_data_format: RAW

outputs:
  instance_ip:
    description: IP address of the instance
    value: { get_attr: [my_instance, networks, admin_internal_net] }
  instance_name:
    description: Name of the instance
    value: { get_attr: [my_instance, name] }

create a sample completely stack

  1. create network
  2. create router
  3. create subnet
  4. create port
  5. create routerinterface
  6. create server
  7. associate floatingip

root@node-6:~/heat# openstack stack create completely-stack -t 06_completely.yml --parameter "image_id=8cf6fd68-adc4-4a08-9b52-9a0014630c72" --parameter "secgroup_id=df36faf4-228b-46d4-9a9b-84e6490b455e

root@node-6:~/heat# cat 06_completely.yml

heat_template_version: 2013-05-23

description: Simple template to deploy a single compute instance

parameters:
  image_id:
    type: string
    label: Image ID
    description: Image to be used for compute instance
  secgroup_id:
    type: string
    description : Id of the security groupe
  public_net:
    type: string
    description: public network name
    default: admin_floating_net 
  private_net_name:
    type: string
    description: private network name
    default: net04 

resources:
  wait_condition:
    type: OS::Heat::WaitCondition
    properties:
      handle: { get_resource: wait_handle }
      count: 1
      timeout: 600
  wait_handle:
    type: OS::Heat::WaitConditionHandle
  private_net:
    type: OS::Neutron::Net
    properties:
      name: { get_param: private_net_name }
  private_subnet:
    type: OS::Neutron::Subnet
    properties:
      network_id: { get_resource: private_net }
      cidr: 172.16.200.0/24
      gateway_ip: 172.16.200.1 
  router:
    type: OS::Neutron::Router
    properties:
      external_gateway_info:
          network: { get_param: public_net }
  router_interface:
    type: OS::Neutron::RouterInterface
    properties:
      router_id: { get_resource: router }
      subnet_id: { get_resource: private_subnet }
  server_port:
    type: OS::Neutron::Port
    properties:
      network_id: { get_resource: private_net }
      security_groups: [ get_param: secgroup_id ]
      fixed_ips:
        - subnet_id: { get_resource: private_subnet }
  server_floating_ip:
    type: OS::Neutron::FloatingIP
    properties:
      floating_network_id: { get_param: public_net }
      port_id: { get_resource: server_port }
  my_instance:
    type: OS::Nova::Server
    properties:
      image: { get_param: image_id }
      flavor: m1.small
      key_name: stack_key
      networks:
        - port: {get_resource: server_port}
      user_data: 
        str_replace:
          params:
            wc_notify: {get_attr: ['wait_handle', 'curl_cli']}
          template: |
            #!/bin/sh
            echo "hello, the world of heat."
            echo wc_notify

            # notify heat that we are done here
            wc_notify --data-binary '{"status": "SUCCESS"}'
      user_data_format: RAW
outputs:
  instance_ip:
    description: IP address of the instance
    value: { get_attr: [my_instance, first_address] }
  instance_floating_ip:
    description: FloatingIP address of the instance
    value: { get_attr: [server_floating_ip, floating_ip_address] }
  instance_name:
    description: Name of the instance
    value: { get_attr: [my_instance, name] }

root@node-6:~/heat# openstack stack show completely-stack

+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| Field                 | Value                                                                                                                             |
+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| id                    | 661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0                                                                                              |
| stack_name            | completely-stack                                                                                                                  |
| description           | Simple template to deploy a single compute instance                                                                               |
| creation_time         | 2017-01-06T09:19:48                                                                                                               |
| updated_time          | None                                                                                                                              |
| stack_status          | CREATE_COMPLETE                                                                                                                   |
| stack_status_reason   | Stack CREATE completed successfully                                                                                               |
| parameters            | OS::project_id: 828076867d8242c98423c6064a7f4e6e                                                                                  |
|                       | OS::stack_id: 661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0                                                                                |
|                       | OS::stack_name: completely-stack                                                                                                  |
|                       | image_id: 8cf6fd68-adc4-4a08-9b52-9a0014630c72                                                                                    |
|                       | private_net_name: net04                                                                                                           |
|                       | public_net: admin_floating_net                                                                                                    |
|                       | secgroup_id: df36faf4-228b-46d4-9a9b-84e6490b455e                                                                                 |
|                       |                                                                                                                                   |
| outputs               | - description: Name of the instance                                                                                               |
|                       |   output_key: instance_name                                                                                                       |
|                       |   output_value: completely-stack-my_instance-qglvgwgosyl2                                                                         |
|                       | - description: IP address of the instance                                                                                         |
|                       |   output_key: instance_ip                                                                                                         |
|                       |   output_value: 172.16.200.3                                                                                                      |
|                       | - description: FloatingIP address of the instance                                                                                 |
|                       |   output_key: instance_floating_ip                                                                                                |
|                       |   output_value: 192.168.32.47                                                                                                     |
|                       |                                                                                                                                   |
| links                 | - href: http://192.168.32.8:8004/v1/828076867d8242c98423c6064a7f4e6e/stacks/completely-stack/661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0 |
|                       |   rel: self                                                                                                                       |
|                       |                                                                                                                                   |
| disable_rollback      | True                                                                                                                              |
| parent                | None                                                                                                                              |
| tags                  | null                                                                                                                              |
|                       | ...                                                                                                                               |
|                       |                                                                                                                                   |
| stack_user_project_id | 75a7b711f42f4536b4ffb96c13cb223a                                                                                                  |
| capabilities          | []                                                                                                                                |
| notification_topics   | []                                                                                                                                |
| timeout_mins          | None                                                                                                                              |
| stack_owner           | None                                                                                                                              |
+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------+
show log
2017-01-06 17:19:48.677 16093 INFO heat.engine.stack [-] Stack CREATE IN_PROGRESS (completely-stack): Stack CREATE started
2017-01-06 17:19:48.697 16093 INFO heat.engine.resource [-] creating Net "private_net" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:19:48.971 16093 INFO heat.engine.resource [-] creating HeatWaitConditionHandle "wait_handle" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:19:49.333 16093 INFO heat.engine.resource [-] creating Router "router" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:19:50.410 16093 INFO heat.engine.resource [-] creating Subnet "private_subnet" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:19:50.805 16093 INFO heat.engine.resource [-] creating HeatWaitCondition "wait_condition" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:19:51.875 16093 INFO heat.engine.resource [-] creating Port "server_port" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:19:52.439 16093 INFO heat.engine.resource [-] creating RouterInterface "router_interface" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:19:54.637 16093 INFO heat.engine.resource [-] creating Server "my_instance" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:19:55.431 16093 INFO heat.engine.resource [-] creating FloatingIP "server_floating_ip" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0]
2017-01-06 17:20:24.808 16093 INFO heat.engine.resources.openstack.heat.wait_condition [-] HeatWaitCondition "wait_condition" Stack "completely-stack" [661e99f1-a4fa-4cdc-95f7-f7ac75e10eb0] Succeeded
2017-01-06 17:20:24.874 16093 INFO heat.engine.stack [-] Stack CREATE COMPLETE (completely-stack): Stack CREATE completed successfully

reference:
https://developer.rackspace.com/blog/openstack-orchestration-in-depth-part-3-multi-instance-deployments/

@zhanghui9700
Copy link
Author

orchestration by openstack heat 04-resource group

资源组,组内可以包括一个或多个相同的嵌套资源 OS::Heat::ResourceGroup

root@node-6:~/heat# cat 07_resource_group.yml

heat_template_version: 2015-04-30

description: Simple template to deploy a single compute instance

parameters:
  image:
    type: string
    label: Image ID
    description: Image to be used for compute instance
  flavor:
    type: string
    label: Flavor
    description: Type of instance (flavor) to be used on the compute instance.
    default: m1.small
  key:
    type: string
    label: Key name
    description: Name of key-pair to be installed on the compute instance.
    default: stack_key
  secgroup_id:
    type: string
    description : Id of the security groupe
    default: df36faf4-228b-46d4-9a9b-84e6490b455e
  private_net:
    type: string
    description: private network
    default: admin_internal_net
  cluster_size:
    type: number
    label: Cluster size
    description: Number of instances in cluster.
    default: 3

resources:
  tiny_cluster:
    type: OS::Heat::ResourceGroup
    properties:
      count: { get_param: cluster_size }
      resource_def:
        type: OS::Nova::Server
        properties:
          image: { get_param: image }
          flavor: { get_param: flavor }
          key_name: { get_param: key }
          networks:
            - network: { get_param: private_net }

outputs: 
  ips:  
    value: {"get_attr": [tiny_cluster, "attributes", first_address]}  
  refs:  
    value: {"get_attr": [tiny_cluster, refs]} 

root@node-6:~/heat# openstack stack create resgroup-stack -t 07_resource_group.yml --parameter image=027e7611-c85f-4097-8d66-eab63f620987

+---------------------+-----------------------------------------------------+
| Field               | Value                                               |
+---------------------+-----------------------------------------------------+
| id                  | a1de3905-306b-41ff-80f7-d50ebbb141f4                |
| stack_name          | resgroup-stack                                      |
| description         | Simple template to deploy a single compute instance |
| creation_time       | 2017-01-09T03:48:59                                 |
| updated_time        | None                                                |
| stack_status        | CREATE_IN_PROGRESS                                  |
| stack_status_reason | Stack CREATE started                                |
+---------------------+-----------------------------------------------------+

# heat-engine create log
2017-01-09 11:49:00.627 16094 INFO heat.engine.stack [-] Stack CREATE IN_PROGRESS (resgroup-stack-tiny_cluster-exe4vuytqcz5): Stack CREATE started
2017-01-09 11:49:00.642 16094 INFO heat.engine.resource [-] creating Server "1" Stack "resgroup-stack-tiny_cluster-exe4vuytqcz5" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d]
2017-01-09 11:49:01.335 16094 INFO heat.engine.resource [-] creating Server "0" Stack "resgroup-stack-tiny_cluster-exe4vuytqcz5" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d]
2017-01-09 11:49:01.930 16094 INFO heat.engine.resource [-] creating Server "2" Stack "resgroup-stack-tiny_cluster-exe4vuytqcz5" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d]
2017-01-09 11:49:23.266 16094 INFO heat.engine.stack [-] Stack CREATE COMPLETE (resgroup-stack-tiny_cluster-exe4vuytqcz5): Stack CREATE completed successfully
2017-01-09 11:49:23.753 16093 INFO heat.engine.stack [-] Stack CREATE COMPLETE (resgroup-stack): Stack CREATE completed successfully

root@node-6:~/heat# openstack stack output show resgroup-stack --all

+-------+----------------------------------------------+
| Field | Value                                        |
+-------+----------------------------------------------+
| ips   | {                                            |
|       |   "output_value": {                          |
|       |     "1": "192.168.111.38",                   |
|       |     "0": "192.168.111.39",                   |
|       |     "2": "192.168.111.40"                    |
|       |   },                                         |
|       |   "output_key": "ips",                       |
|       |   "description": "No description given"      |
|       | }                                            |
| refs  | {                                            |
|       |   "output_value": [                          |
|       |     "52e466c9-6422-4701-8f33-14972d84d8c9",  |
|       |     "a6ccd47a-588f-4e5b-9501-5196d99cc293",  |
|       |     "95d3b0ee-a6df-4359-94b0-c4565cdf6f9c"   |
|       |   ],                                         |
|       |   "output_key": "refs",                      |
|       |   "description": "No description given"      |
|       | }                                            |
+-------+----------------------------------------------+

root@node-6:~/heat# openstack stack resource list resgroup-stack -n 1

+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+
| resource_name | physical_resource_id                 | resource_type           | resource_status | updated_time        | stack_name                               |
+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+
| tiny_cluster  | b8a42b8d-f1fa-4a48-aade-293468cc3b8d | OS::Heat::ResourceGroup | CREATE_COMPLETE | 2017-01-09T03:48:59 | resgroup-stack                           |
| 1             | a6ccd47a-588f-4e5b-9501-5196d99cc293 | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T03:49:01 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
| 0             | 52e466c9-6422-4701-8f33-14972d84d8c9 | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T03:49:01 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
| 2             | 95d3b0ee-a6df-4359-94b0-c4565cdf6f9c | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T03:49:01 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+

update stack

这时候就面临着另一个问题,如果stack创建完成后需要调整子资源的数量,怎么实现?
调整模板中的count参数,然后用stack-update命令即可。

例如在上面模板的基础上,把count改为5,stack-update之后再查看子资源,就会发现多了一个名称为[3, 4]的子资源。

而如果把count改为2,stack-update之后,就只剩下名称为[0, 1]的子资源了。

注:子资源移除是删除名称数字较大的,保留名称数字较小的。例如上面更新子资源数量为1后,就把名称为1和2的子资源删除了,剩下名称为0的子资源。

update stack resource-group extend

root@node-6:~/heat# openstack stack update resgroup-stack -t 07_resource_group.yml --parameter image=027e7611-c85f-4097-8d66-eab63f620987 --parameter cluster_size=5

# heat-engine update log
2017-01-09 14:38:09.877 16093 INFO heat.engine.service [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Updating stack resgroup-stack
2017-01-09 14:38:09.898 16093 INFO heat.engine.resource [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Validating ResourceGroup "tiny_cluster"
2017-01-09 14:38:09.916 16093 INFO heat.engine.resource [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Validating Server "0"
2017-01-09 14:38:10.332 16093 INFO heat.engine.stack [-] Stack UPDATE IN_PROGRESS (resgroup-stack): Stack UPDATE started
2017-01-09 14:38:10.402 16093 INFO heat.engine.resource [-] updating ResourceGroup "tiny_cluster" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d] Stack "resgroup-stack" [a1de3905-306b-41ff-80f7-d50ebbb141f4]
2017-01-09 14:38:10.482 16094 INFO heat.engine.service [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Updating stack resgroup-stack-tiny_cluster-exe4vuytqcz5
2017-01-09 14:38:10.519 16094 INFO heat.engine.resource [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Validating Server "1"
2017-01-09 14:38:10.781 16094 INFO heat.engine.resource [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Validating Server "0"
2017-01-09 14:38:10.929 16094 INFO heat.engine.resource [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Validating Server "3"
2017-01-09 14:38:11.180 16094 INFO heat.engine.resource [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Validating Server "4"
2017-01-09 14:38:11.371 16094 INFO heat.engine.resource [req-7907a735-103d-4970-8987-88b0df2fbb5a - admin - default default] Validating Server "2"
2017-01-09 14:38:11.566 16094 INFO heat.engine.stack [-] Stack UPDATE IN_PROGRESS (resgroup-stack-tiny_cluster-exe4vuytqcz5): Stack UPDATE started
2017-01-09 14:38:11.686 16094 INFO heat.engine.update [-] Resource 0 for stack resgroup-stack-tiny_cluster-exe4vuytqcz5 updated
2017-01-09 14:38:11.696 16094 INFO heat.engine.update [-] Resource 2 for stack resgroup-stack-tiny_cluster-exe4vuytqcz5 updated
2017-01-09 14:38:11.740 16094 INFO heat.engine.resource [-] creating Server "3" Stack "resgroup-stack-tiny_cluster-exe4vuytqcz5" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d]
2017-01-09 14:38:12.417 16094 INFO heat.engine.resource [-] creating Server "4" Stack "resgroup-stack-tiny_cluster-exe4vuytqcz5" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d]
2017-01-09 14:38:12.977 16094 INFO heat.engine.update [-] Resource 1 for stack resgroup-stack-tiny_cluster-exe4vuytqcz5 updated
2017-01-09 14:38:31.823 16094 INFO heat.engine.stack [-] Stack DELETE IN_PROGRESS (resgroup-stack-tiny_cluster-exe4vuytqcz5*): Stack DELETE started
2017-01-09 14:38:31.881 16094 INFO heat.engine.stack [-] Stack DELETE COMPLETE (resgroup-stack-tiny_cluster-exe4vuytqcz5*): Stack DELETE completed successfully
2017-01-09 14:38:31.930 16094 INFO heat.engine.stack [-] Stack UPDATE COMPLETE (resgroup-stack-tiny_cluster-exe4vuytqcz5): Stack UPDATE completed successfully
2017-01-09 14:38:32.893 16093 INFO heat.engine.update [-] Resource tiny_cluster for stack resgroup-stack updated
2017-01-09 14:38:33.934 16093 INFO heat.engine.stack [-] Stack DELETE IN_PROGRESS (resgroup-stack*): Stack DELETE started
2017-01-09 14:38:34.000 16093 INFO heat.engine.stack [-] Stack DELETE COMPLETE (resgroup-stack*): Stack DELETE completed successfully
2017-01-09 14:38:34.055 16093 INFO heat.engine.stack [-] Stack UPDATE COMPLETE (resgroup-stack): Stack UPDATE completed successfully

root@node-6:~/heat# openstack stack resource list resgroup-stack -n 1

+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+
| resource_name | physical_resource_id                 | resource_type           | resource_status | updated_time        | stack_name                               |
+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+
| tiny_cluster  | b8a42b8d-f1fa-4a48-aade-293468cc3b8d | OS::Heat::ResourceGroup | UPDATE_COMPLETE | 2017-01-09T06:38:10 | resgroup-stack                           |
| 1             | a6ccd47a-588f-4e5b-9501-5196d99cc293 | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T03:49:01 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
| 0             | 52e466c9-6422-4701-8f33-14972d84d8c9 | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T03:49:01 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
| 3             | aacf9486-12dc-460f-ab38-87c1254227b0 | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T06:38:12 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
| 2             | 95d3b0ee-a6df-4359-94b0-c4565cdf6f9c | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T03:49:01 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
| 4             | 4199794e-0ffc-4c6b-8081-06a47f432def | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T06:38:12 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+
update stack resource-group shrink

root@node-6:~/heat# openstack stack update resgroup-stack -t 07_resource_group.yml --parameter image=027e7611-c85f-4097-8d66-eab63f620987 --parameter cluster_size=2

# heat-engine update log
2017-01-09 14:59:34.454 16095 INFO heat.engine.service [req-5848a3f1-f8bb-41dd-8840-9adcdd4b82d7 - admin - default default] Updating stack resgroup-stack
2017-01-09 14:59:34.473 16095 INFO heat.engine.resource [req-5848a3f1-f8bb-41dd-8840-9adcdd4b82d7 - admin - default default] Validating ResourceGroup "tiny_cluster"
2017-01-09 14:59:34.490 16095 INFO heat.engine.resource [req-5848a3f1-f8bb-41dd-8840-9adcdd4b82d7 - admin - default default] Validating Server "0"
2017-01-09 14:59:34.869 16095 INFO heat.engine.stack [-] Stack UPDATE IN_PROGRESS (resgroup-stack): Stack UPDATE started
2017-01-09 14:59:35.007 16095 INFO heat.engine.resource [-] updating ResourceGroup "tiny_cluster" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d] Stack "resgroup-stack" [a1de3905-306b-41ff-80f7-d50ebbb141f4]
2017-01-09 14:59:35.096 16094 INFO heat.engine.service [req-5848a3f1-f8bb-41dd-8840-9adcdd4b82d7 - admin - default default] Updating stack resgroup-stack-tiny_cluster-exe4vuytqcz5
2017-01-09 14:59:35.129 16094 INFO heat.engine.resource [req-5848a3f1-f8bb-41dd-8840-9adcdd4b82d7 - admin - default default] Validating Server "1"
2017-01-09 14:59:35.792 16094 INFO heat.engine.resource [req-5848a3f1-f8bb-41dd-8840-9adcdd4b82d7 - admin - default default] Validating Server "0"
2017-01-09 14:59:36.022 16094 INFO heat.engine.stack [-] Stack UPDATE IN_PROGRESS (resgroup-stack-tiny_cluster-exe4vuytqcz5): Stack UPDATE started
2017-01-09 14:59:36.131 16094 INFO heat.engine.resource [-] deleting Server "4" [4199794e-0ffc-4c6b-8081-06a47f432def] Stack "resgroup-stack-tiny_cluster-exe4vuytqcz5" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d]
2017-01-09 14:59:36.511 16094 INFO heat.engine.update [-] Resource 0 for stack resgroup-stack-tiny_cluster-exe4vuytqcz5 updated
2017-01-09 14:59:36.513 16094 INFO heat.engine.resource [-] deleting Server "2" [95d3b0ee-a6df-4359-94b0-c4565cdf6f9c] Stack "resgroup-stack-tiny_cluster-exe4vuytqcz5" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d]
2017-01-09 14:59:36.784 16094 INFO heat.engine.resource [-] deleting Server "3" [aacf9486-12dc-460f-ab38-87c1254227b0] Stack "resgroup-stack-tiny_cluster-exe4vuytqcz5" [b8a42b8d-f1fa-4a48-aade-293468cc3b8d]
2017-01-09 14:59:37.067 16094 INFO heat.engine.update [-] Resource 1 for stack resgroup-stack-tiny_cluster-exe4vuytqcz5 updated
2017-01-09 14:59:41.763 16094 INFO heat.engine.stack [-] Stack DELETE IN_PROGRESS (resgroup-stack-tiny_cluster-exe4vuytqcz5*): Stack DELETE started
2017-01-09 14:59:41.825 16094 INFO heat.engine.stack [-] Stack DELETE COMPLETE (resgroup-stack-tiny_cluster-exe4vuytqcz5*): Stack DELETE completed successfully
2017-01-09 14:59:41.868 16094 INFO heat.engine.stack [-] Stack UPDATE COMPLETE (resgroup-stack-tiny_cluster-exe4vuytqcz5): Stack UPDATE completed successfully
2017-01-09 14:59:42.085 16095 INFO heat.engine.update [-] Resource tiny_cluster for stack resgroup-stack updated
2017-01-09 14:59:43.113 16095 INFO heat.engine.stack [-] Stack DELETE IN_PROGRESS (resgroup-stack*): Stack DELETE started
2017-01-09 14:59:43.200 16095 INFO heat.engine.stack [-] Stack DELETE COMPLETE (resgroup-stack*): Stack DELETE completed successfully
2017-01-09 14:59:43.251 16095 INFO heat.engine.stack [-] Stack UPDATE COMPLETE (resgroup-stack): Stack UPDATE completed successfully

root@node-6:~/heat# openstack stack resource list resgroup-stack -n 1

+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+
| resource_name | physical_resource_id                 | resource_type           | resource_status | updated_time        | stack_name                               |
+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+
| tiny_cluster  | b8a42b8d-f1fa-4a48-aade-293468cc3b8d | OS::Heat::ResourceGroup | UPDATE_COMPLETE | 2017-01-09T06:59:35 | resgroup-stack                           |
| 1             | a6ccd47a-588f-4e5b-9501-5196d99cc293 | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T03:49:01 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
| 0             | 52e466c9-6422-4701-8f33-14972d84d8c9 | OS::Nova::Server        | CREATE_COMPLETE | 2017-01-09T03:49:01 | resgroup-stack-tiny_cluster-exe4vuytqcz5 |
+---------------+--------------------------------------+-------------------------+-----------------+---------------------+------------------------------------------+

关于异常

Stack update的过程中,如果资源创建异常(eg: Quota exceeded for instances),stack状态:更新失败,可以重新update一个配额范围内的资源数量,stack状态自动变更正常。TODO测试rollback机制。

@zhanghui9700
Copy link
Author

orchestration by openstack heat 05-A-auto scaling

OS::Heat::AutoScalingGroup

root@node-6:~/heat/auto-scale# cat instance.yaml

heat_template_version: 2014-10-16  
description: A simple server. 
resources:  
  server:
    type: OS::Nova::Server
    properties:
      image: TestVM
      flavor: m1.micro
      networks:
        - network: admin_internal_net
      user_data_format: RAW
      user_data: |
        #!/bin/sh
        while [ 1 ] ; do echo $((13**99)) 1>/dev/null 2>&1; done

root@node-6:~/heat/auto-scale# cat asg_of_stacks.yaml

heat_template_version: 2013-05-23

description: >
  This is a very simple template that illustrates the basic features
  of OS::Heat::AutoScalingGroup and demonstrates that these features
  are available even when the scaled resource is a nested stack.  By
  virtue of its simplicity this example should be usable in many
  contexts.  In particular, this example does not require Neutron nor
  a load balancer nor any particular support for software in the VMs.
  In fact, the VMs in this example do not actually do anything.  This
  example does no automatic scaling, but does discuss manual scaling.
  For a more complete example, see autoscaling.yaml.

resources:
  asg:
    type: OS::Heat::AutoScalingGroup
    properties:
      resource:
        type: instance.yaml
      min_size: 1
      desired_capacity: 3
      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'

outputs:
  scale_up_url:
    description: >
      This URL is the webhook to scale up the group.  You can invoke
      the scale-up operation by doing an HTTP POST to this URL; no
      body nor extra headers are needed.
    value: {get_attr: [scale_up_policy, alarm_url]}
  scale_dn_url:
    description: >
      This URL is the webhook to scale down the group.  You can invoke
      the scale-down operation by doing an HTTP POST to this URL; no
      body nor extra headers are needed.
    value: {get_attr: [scale_dn_policy, alarm_url]}

查看scale的web hook url

如果有独立的监控系统,OS::Heat::ScalingPolicy的alarm url属性,是个非常有意义的实现。

root@node-6:~/heat/auto-scale# openstack stack output show asg-stack --all

+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field        | Value                                                                                                                                                                                                                                                                                                                                                                                                       |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| scale_dn_url | {                                                                                                                                                                                                                                                                                                                                                                                                           |
|              |   "output_value": "http://192.168.32.8:8000/v1/signal/arn%3Aopenstack%3Aheat%3A%3A828076867d8242c98423c6064a7f4e6e%3Astacks%2Fasg-stack%2Ffee39f2c-cfb9-466e-a331-d087b385cc36%2Fresources%2Fscale_dn_policy?Timestamp=2017-01-09T09%3A35%3A37Z&SignatureMethod=HmacSHA256&AWSAccessKeyId=61ef2253f40e4962a3e8c4def51d0def&SignatureVersion=2&Signature=NQx9BDE1Gf5GcZDvVjmPACwHhKv5ZwyR8xNxp%2BKErtY%3D",  |
|              |   "output_key": "scale_dn_url",                                                                                                                                                                                                                                                                                                                                                                             |
|              |   "description": "This URL is the webhook to scale down the group.  You can invoke the scale-down operation by doing an HTTP POST to this URL; no body nor extra headers are needed.\n"                                                                                                                                                                                                                     |
|              | }                                                                                                                                                                                                                                                                                                                                                                                                           |
| scale_up_url | {                                                                                                                                                                                                                                                                                                                                                                                                           |
|              |   "output_value": "http://192.168.32.8:8000/v1/signal/arn%3Aopenstack%3Aheat%3A%3A828076867d8242c98423c6064a7f4e6e%3Astacks%2Fasg-stack%2Ffee39f2c-cfb9-466e-a331-d087b385cc36%2Fresources%2Fscale_up_policy?Timestamp=2017-01-09T09%3A35%3A37Z&SignatureMethod=HmacSHA256&AWSAccessKeyId=83737133a505471493d4d88bebbdbb0d&SignatureVersion=2&Signature=eCgV6KfQv%2FKSUsE6yPuULdWBwljHi7dHHn9rzrSS4wg%3D",  |
|              |   "output_key": "scale_up_url",                                                                                                                                                                                                                                                                                                                                                                             |
|              |   "description": "This URL is the webhook to scale up the group.  You can invoke the scale-up operation by doing an HTTP POST to this URL; no body nor extra headers are needed.\n"                                                                                                                                                                                                                         |
|              | }                                                                                                                                                                                                                                                                                                                                                                                                           |
+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

扩容集群规模

root@node-6:~/heat/auto-scale# curl -X POST 'http://192.168.32.8:8000/v1/signal/arn%3Aopenstack%3Aheat%3A%3A828076867d8242c98423c6064a7f4e6e%3Astacks%2Fasg-stack%2Ffee39f2c-cfb9-466e-a331-d087b385cc36%2Fresources%2Fscale_up_policy?Timestamp=2017-01-09T09%3A35%3A37Z&SignatureMethod=HmacSHA256&AWSAccessKeyId=83737133a505471493d4d88bebbdbb0d&SignatureVersion=2&Signature=eCgV6KfQv%2FKSUsE6yPuULdWBwljHi7dHHn9rzrSS4wg%3D'

收缩集群规模

root@node-6:~# curl -X POST 'http://192.168.32.8:8000/v1/signal/arn%3Aopenstack%3Aheat%3A%3A828076867d8242c98423c6064a7f4e6e%3Astacks%2Fasg-stack%2Ffee39f2c-cfb9-466e-a331-d087b385cc36%2Fresources%2Fscale_dn_policy?Timestamp=2017-01-09T09%3A35%3A37Z&SignatureMethod=HmacSHA256&AWSAccessKeyId=61ef2253f40e4962a3e8c4def51d0def&SignatureVersion=2&Signature=NQx9BDE1Gf5GcZDvVjmPACwHhKv5ZwyR8xNxp%2BKErtY%3D'

root@node-6:~/heat/auto-scale# openstack stack resource list asg-stack -n 1

# 多次收缩在会停留在auto-scale-group的min size大小
+-----------------+--------------------------------------+--------------------------------------------+-----------------+---------------------+----------------------------+
| resource_name   | physical_resource_id                 | resource_type                              | resource_status | updated_time        | stack_name                 |
+-----------------+--------------------------------------+--------------------------------------------+-----------------+---------------------+----------------------------+
| scale_up_policy | 7d07239923f046c58c01a739a5bce1d7     | OS::Heat::ScalingPolicy                    | CREATE_COMPLETE | 2017-01-09T09:35:37 | asg-stack                  |
| asg             | da32c720-9f5d-4cbe-addb-81027c55d2f3 | OS::Heat::AutoScalingGroup                 | CREATE_COMPLETE | 2017-01-09T09:35:37 | asg-stack                  |
| 4gpvzswknzwn    | 64e2fc72-b476-4fa0-939a-d8b42f86b3d4 | file:///root/heat/auto-scale/instance.yaml | UPDATE_COMPLETE | 2017-01-09T10:27:49 | asg-stack-asg-gfjjpij7j7ii |
| scale_dn_policy | 6b5a39709fc046d7b39705f8ae1ddfb9     | OS::Heat::ScalingPolicy                    | CREATE_COMPLETE | 2017-01-09T09:35:37 | asg-stack                  |
+-----------------+--------------------------------------+--------------------------------------------+-----------------+---------------------+----------------------------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment