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 01-hello world

the first hello world heat stack

cat hello.yml

heat_template_version: 2013-05-23

description: Simple template to deploy a single compute instance

resources:
  my_instance:
    type: OS::Nova::Server
    properties:
      image: TestVM
      flavor: m1.micro
      #key_name: my_key
      networks:
        - network: admin_internal_net

root@node-6:~/heat# openstack stack create -t hello.yml hello-world

+---------------------+-----------------------------------------------------+
| Field               | Value                                               |
+---------------------+-----------------------------------------------------+
| id                  | 4260a577-e4c1-4081-bc88-831a92f9a527                |
| stack_name          | hello-world                                         |
| description         | Simple template to deploy a single compute instance |
| creation_time       | 2017-01-04T07:54:07                                 |
| updated_time        | None                                                |
| stack_status        | CREATE_IN_PROGRESS                                  |
| stack_status_reason |                                                     |
+---------------------+-----------------------------------------------------+

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

+--------------------------------------+-------------+-----------------+---------------------+--------------+
| ID                                   | Stack Name  | Stack Status    | Creation Time       | Updated Time |
+--------------------------------------+-------------+-----------------+---------------------+--------------+
| 4260a577-e4c1-4081-bc88-831a92f9a527 | hello-world | CREATE_COMPLETE | 2017-01-04T07:54:07 | None         |
+--------------------------------------+-------------+-----------------+---------------------+--------------+

root@node-6:~/heat# nova list

+--------------------------------------+--------------------------------------+--------+------------+-------------+--------------------------------------------------+
| ID                                   | Name                                 | Status | Task State | Power State | Networks                                         |
+--------------------------------------+--------------------------------------+--------+------------+-------------+--------------------------------------------------+
| afed71a5-4956-492f-abb5-b6f5cdc1c53c | hello-world-my_instance-4eegqjxzxvxf | ACTIVE | -          | Running     | admin_internal_net=192.168.111.24                |
+--------------------------------------+--------------------------------------+--------+------------+-------------+--------------------------------------------------+

root@node-6:~/heat# openstack stack delete hello-world

Are you sure you want to delete this stack(s) [y/N]? y

parameter the HOT template

root@node-6:~/heat# cat parameter.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:
  my_instance:
    type: OS::Nova::Server
    properties:
      image: { get_param: image_id }
      flavor: m1.micro
      #key_name: my_key
      networks:
        - network: admin_internal_net

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

+---------------------+-----------------------------------------------------+
| Field               | Value                                               |
+---------------------+-----------------------------------------------------+
| id                  | a7bbcb7b-631e-42b6-bf92-9f2a43d09f0e                |
| stack_name          | parameter-stack                                     |
| description         | Simple template to deploy a single compute instance |
| creation_time       | 2017-01-04T08:47:32                                 |
| updated_time        | None                                                |
| stack_status        | CREATE_IN_PROGRESS                                  |
| stack_status_reason | Stack CREATE started                                |
+---------------------+-----------------------------------------------------+

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

+--------------------------------------+-----------------+-----------------+---------------------+--------------+
| ID                                   | Stack Name      | Stack Status    | Creation Time       | Updated Time |
+--------------------------------------+-----------------+-----------------+---------------------+--------------+
| a7bbcb7b-631e-42b6-bf92-9f2a43d09f0e | parameter-stack | CREATE_COMPLETE | 2017-01-04T08:47:32 | None         |
+--------------------------------------+-----------------+-----------------+---------------------+--------------+

root@node-6:~/heat# openstack stack resource list parameter-stack

+---------------+--------------------------------------+------------------+-----------------+---------------------+
| resource_name | physical_resource_id                 | resource_type    | resource_status | updated_time        |
+---------------+--------------------------------------+------------------+-----------------+---------------------+
| my_instance   | 3ce7aca6-4856-4446-88bd-745e03c781c1 | OS::Nova::Server | CREATE_COMPLETE | 2017-01-04T08:47:32 |
+---------------+--------------------------------------+------------------+-----------------+---------------------+

root@node-6:~/heat# nova list

+--------------------------------------+------------------------------------------+--------+------------+-------------+--------------------------------------------------+
| ID                                   | Name                                     | Status | Task State | Power State | Networks                                         |
+--------------------------------------+------------------------------------------+--------+------------+-------------+--------------------------------------------------+
| 3ce7aca6-4856-4446-88bd-745e03c781c1 | parameter-stack-my_instance-34sanski6byi | ACTIVE | -          | Running     | admin_internal_net=192.168.111.25                |
+--------------------------------------+------------------------------------------+--------+------------+-------------+--------------------------------------------------+

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

+-----------------------+----------------------------------------------------------------------------------------------------------------------------------+
| Field                 | Value                                                                                                                            |
+-----------------------+----------------------------------------------------------------------------------------------------------------------------------+
| id                    | a7bbcb7b-631e-42b6-bf92-9f2a43d09f0e                                                                                             |
| stack_name            | parameter-stack                                                                                                                  |
| description           | Simple template to deploy a single compute instance                                                                              |
| creation_time         | 2017-01-04T08:47:32                                                                                                              |
| updated_time          | None                                                                                                                             |
| stack_status          | CREATE_COMPLETE                                                                                                                  |
| stack_status_reason   | Stack CREATE completed successfully                                                                                              |
| parameters            | OS::project_id: 828076867d8242c98423c6064a7f4e6e                                                                                 |
|                       | OS::stack_id: a7bbcb7b-631e-42b6-bf92-9f2a43d09f0e                                                                               |
|                       | OS::stack_name: parameter-stack                                                                                                  |
|                       | image_id: 027e7611-c85f-4097-8d66-eab63f620987                                                                                   |
|                       |                                                                                                                                  |
| outputs               | []                                                                                                                               |
|                       |                                                                                                                                  |
| links                 | - href: http://192.168.32.8:8004/v1/828076867d8242c98423c6064a7f4e6e/stacks/parameter-stack/a7bbcb7b-631e-42b6-bf92-9f2a43d09f0e |
|                       |   rel: self                                                                                                                      |
|                       |                                                                                                                                  |
| disable_rollback      | True                                                                                                                             |
| parent                | None                                                                                                                             |
| tags                  | null                                                                                                                             |
|                       | ...                                                                                                                              |
|                       |                                                                                                                                  |
| stack_user_project_id | 3494cb4994a545179236af08b53076fd                                                                                                 |
| capabilities          | []                                                                                                                               |
| notification_topics   | []                                                                                                                               |
| timeout_mins          | None                                                                                                                             |
| stack_owner           | None                                                                                                                             |
+-----------------------+----------------------------------------------------------------------------------------------------------------------------------+

parameter and output

root@node-6:~/heat# cat 03_output.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:
  my_instance:
    type: OS::Nova::Server
    properties:
      image: { get_param: image_id }
      flavor: m1.micro
      #key_name: my_key
      networks:
        - network: admin_internal_net

outputs:
  instance_ip:
    description: IP address of the instance
    value: { get_attr: [my_instance, networks, admin_internal_net] }
  vnc_url:
    description: VNC url of the instance
    value: { get_attr: [my_instance, console_urls] }

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

+-----------------------+-------------------------------------------------------------------------------------------------------------------------------+
| Field                 | Value                                                                                                                         |
+-----------------------+-------------------------------------------------------------------------------------------------------------------------------+
| id                    | 7a959e08-446d-47dc-b9ab-7c6c5ce9b4be                                                                                          |
| stack_name            | output-stack                                                                                                                  |
| description           | Simple template to deploy a single compute instance                                                                           |
| creation_time         | 2017-01-05T03:25:58                                                                                                           |
| updated_time          | None                                                                                                                          |
| stack_status          | CREATE_COMPLETE                                                                                                               |
| stack_status_reason   | Stack CREATE completed successfully                                                                                           |
| parameters            | OS::project_id: 828076867d8242c98423c6064a7f4e6e                                                                              |
|                       | OS::stack_id: 7a959e08-446d-47dc-b9ab-7c6c5ce9b4be                                                                            |
|                       | OS::stack_name: output-stack                                                                                                  |
|                       | image_id: 027e7611-c85f-4097-8d66-eab63f620987                                                                                |
|                       |                                                                                                                               |
| outputs               | - description: VNC url of the instance                                                                                        |
|                       |   output_key: vnc_url                                                                                                         |
|                       |   output_value:                                                                                                               |
|                       |     novnc: http://192.168.32.8:6080/vnc_auto.html?token=2a0b1b21-6d33-47d2-961d-8a5af2af5d38                                  |
|                       |     rdp-html5: Unavailable console type rdp-html5.                                                                            |
|                       |     serial: Unavailable console type serial.                                                                                  |
|                       |     spice-html5: Unavailable console type spice-html5.                                                                        |
|                       |     xvpvnc: http://127.0.0.1:6081/console?token=54950d98-12fb-4df6-8d83-f08c5ce9404f                                          |
|                       | - description: IP address of the instance                                                                                     |
|                       |   output_key: instance_ip                                                                                                     |
|                       |   output_value:                                                                                                               |
|                       |   - 192.168.111.26                                                                                                            |
|                       |                                                                                                                               |
| links                 | - href: http://192.168.32.8:8004/v1/828076867d8242c98423c6064a7f4e6e/stacks/output-stack/7a959e08-446d-47dc-b9ab-7c6c5ce9b4be |
|                       |   rel: self                                                                                                                   |
|                       |                                                                                                                               |
| disable_rollback      | True                                                                                                                          |
| parent                | None                                                                                                                          |
| tags                  | null                                                                                                                          |
|                       | ...                                                                                                                           |
|                       |                                                                                                                               |
| stack_user_project_id | 94beab27b5a84e1eaf42651e318d89d2                                                                                              |
| capabilities          | []                                                                                                                            |
| notification_topics   | []                                                                                                                            |
| timeout_mins          | None                                                                                                                          |
| stack_owner           | None                                                                                                                          |
+-----------------------+-------------------------------------------------------------------------------------------------------------------------------+

log

2017-01-05 11:25:56.726 16093 INFO heat.engine.service [req-a69e6664-0874-4af6-b6bb-12219f447f9e - admin - default default] Creating stack output-stack
2017-01-05 11:25:56.759 16093 INFO heat.engine.resource [req-a69e6664-0874-4af6-b6bb-12219f447f9e - admin - default default] Validating Server "my_instance"
2017-01-05 11:25:58.330 16093 INFO heat.engine.stack [-] Stack CREATE IN_PROGRESS (output-stack): Stack CREATE started
2017-01-05 11:25:58.344 16093 INFO heat.engine.resource [-] creating Server "my_instance" Stack "output-stack" [7a959e08-446d-47dc-b9ab-7c6c5ce9b4be]
2017-01-05 11:25:58.618 16094 WARNING heat.engine.attributes [req-07c987e4-a94c-44fe-9994-4a98b0e1099e - admin - default default] Attribute console_urls is not of type Map
2017-01-05 11:25:58.635 16094 WARNING heat.engine.attributes [req-07c987e4-a94c-44fe-9994-4a98b0e1099e - admin - default default] Attribute networks is not of type Map
2017-01-05 11:26:16.872 16093 INFO heat.engine.stack [-] Stack CREATE COMPLETE (output-stack): Stack CREATE completed successfully

@zhanghui9700
Copy link
Author

orchestration by openstack heat 02-user data and cloud init

user-data in HOT interact with cloud-init

root@node-6:~/heat# cat 04_user_data.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:
  my_instance:
    type: OS::Nova::Server
    properties:
      image: { get_param: image_id }
      flavor: m1.micro
      #key_name: my_key
      networks:
        - network: admin_internal_net
      user_data: |
        #!/bin/sh
        echo "hello, the world of heat."
      user_data_format: RAW

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

root@node-6:~/heat# openstack stack create -t 04_user_data.yml --parameter image_id=027e7611-c85f-4097-8d66-eab63f620987 userdata-stack

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

+-----------------------+---------------------------------------------------------------------------------------------------------------------------------+
| Field                 | Value                                                                                                                           |
+-----------------------+---------------------------------------------------------------------------------------------------------------------------------+
| id                    | 95fcf6dd-6803-4fa5-8179-0cc920e1c050                                                                                            |
| stack_name            | userdata-stack                                                                                                                  |
| description           | Simple template to deploy a single compute instance                                                                             |
| creation_time         | 2017-01-05T09:24:19                                                                                                             |
| updated_time          | None                                                                                                                            |
| stack_status          | CREATE_COMPLETE                                                                                                                 |
| stack_status_reason   | Stack CREATE completed successfully                                                                                             |
| parameters            | OS::project_id: 828076867d8242c98423c6064a7f4e6e                                                                                |
|                       | OS::stack_id: 95fcf6dd-6803-4fa5-8179-0cc920e1c050                                                                              |
|                       | OS::stack_name: userdata-stack                                                                                                  |
|                       | image_id: 027e7611-c85f-4097-8d66-eab63f620987                                                                                  |
|                       |                                                                                                                                 |
| outputs               | - description: Name of the instance                                                                                             |
|                       |   output_key: instance_name                                                                                                     |
|                       |   output_value: userdata-stack-my_instance-fwab4snhrdpt                                                                         |
|                       | - description: VNC url of the instance                                                                                          |
|                       |   output_key: vnc_url                                                                                                           |
|                       |   output_value:                                                                                                                 |
|                       |     novnc: http://192.168.32.8:6080/vnc_auto.html?token=c3becbef-c6fd-401d-8c84-ae6b80d3dedc                                    |
|                       |     rdp-html5: Unavailable console type rdp-html5.                                                                              |
|                       |     serial: Unavailable console type serial.                                                                                    |
|                       |     spice-html5: Unavailable console type spice-html5.                                                                          |
|                       |     xvpvnc: http://127.0.0.1:6081/console?token=76b9d33f-fbed-483e-ac7d-acb70a863b94                                            |
|                       | - description: IP address of the instance                                                                                       |
|                       |   output_key: instance_ip                                                                                                       |
|                       |   output_value:                                                                                                                 |
|                       |   - 192.168.111.27                                                                                                              |
|                       |                                                                                                                                 |
| links                 | - href: http://192.168.32.8:8004/v1/828076867d8242c98423c6064a7f4e6e/stacks/userdata-stack/95fcf6dd-6803-4fa5-8179-0cc920e1c050 |
|                       |   rel: self                                                                                                                     |
|                       |                                                                                                                                 |
| disable_rollback      | True                                                                                                                            |
| parent                | None                                                                                                                            |
| tags                  | null                                                                                                                            |
|                       | ...                                                                                                                             |
|                       |                                                                                                                                 |
| stack_user_project_id | 92f2dc6d0e96435694a2f6f37f0498b7                                                                                                |
| capabilities          | []                                                                                                                              |
| notification_topics   | []                                                                                                                              |
| timeout_mins          | None                                                                                                                            |
| stack_owner           | None                                                                                                                            |
+-----------------------+---------------------------------------------------------------------------------------------------------------------------------+

nova console-log userdata-stack-my_instance-fwab4snhrdpt | less

......
info: initramfs: up at 0.75
info: initramfs loading root from /dev/vda1
info: /etc/init.d/rc.sysinit: up at 0.80
info: container: none
Starting logging: OK
WARN: /etc/rc3.d/S10-load-modules failed
Initializing random number generator... [    0.941839] random: dd urandom read with 5 bits of entropy available
done.
Starting acpid: OK
Starting network...
udhcpc (v1.23.2) started
Sending discover...
Sending select for 192.168.111.27...
Lease of 192.168.111.27 obtained, lease time 600
SIOCADDRT: Invalid argument
WARN: failed: route add -net "169.254.169.254/32" gw "192.168.111.1"
SIOCADDRT: File exists
WARN: failed: route add -net "0.0.0.0/0" gw "192.168.111.1"
Starting dropbear sshd: OK
GROWROOT: NOCHANGE: partition 1 is size 98304. it cannot be grown
hello, the world of heat.  <=== here!!!!!!!
/run/cirros/datasource/data/user-data returned 0
/dev/root resized successfully [took 0.02s]
=== system information ===
Platform: OpenStack Foundation OpenStack Nova
Container: none
Arch: x86_64
CPU(s): 1 @ 2399.996 MHz
Cores/Sockets/Threads: 1/1/1
Virt-type: VT-x
RAM Size: 47MB
Disks:
NAME MAJ:MIN     SIZE LABEL         MOUNTPOINT
vda  253:0   51397120               
vda1 253:1   50331648 cirros-rootfs /
vdb  253:16  67108864 config-2  
=== sshd host keys ===
-----BEGIN SSH HOST KEY KEYS-----
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrI/Yb2/sn8S4htEi7KY/adVl2SLvT6QSXHUzncEeZEJpSnZCbVDO/8HlRLtR5WjTR6So9W7ltDHvJXkfvRWoMbTOTnpUn/+OF3mJ4/NsZvl9Ix3zPTiv9vM9kyR+ddo37WTyLy/IfsJpqkEXQT1jNfI204kKdWQX0p5eg2t5PnndWyGMdixRDuBLTyblnZkEYoCFFi8PrrKsMkEZFHpFAYt0Q/vX8hM4H1QKcuIcFSlwv+W3Fhacc/fjxrXxNIbmkdO5syIVJrln66HrV35bIKz5vwUDZG8uY3SbWjXguFARjkCuEE0XsT8FcegmFlFuvtWGCc4Vn5cSFdNCnkHN3 root@userdata-stack-my-instance-fwab4snhrdpt
ssh-dss AAAAB3NzaC1kc3MAAACBAKhPD5FamYqkLX4gfGJ2ZNWwr6blgLXXMsQJ3mIRJE8YZnn2Q2DdfUlllWVXCPSoUt6e4S/0JtZrCTpZpvL/cbpw0bB6LysPArhwF0ASr38hUJgodQ2bMgpZkv9PttfZZIcWzOLkZ8LPGNP/QwgDNFE3t7iXdDNc4jjZoCEJHddRAAAAFQCArSobskLXKYkOgD0BmcmOrKER+wAAAIEAnN01wM2C0dHtCO7SsQk5zVQOIzX5Ny+jODd45buIlIt5Rstug6XQ1TGPQzSeOs9GLyE2KI1qCQnGh7sfRndAaTe4NTlqG9/qGoGyTa6WEc3m0cvkFEiMiaL7oNwCmBkC4dtgUsg4UZyLKk5e2VsGhG6lJonmnhTSENS5Z1GFnJ8AAACAS0yYKbx0Kh96fQtPdUMLbJ1Vm1UqpA0FwvV/lEsJARw0a5tgPxEoUI2JwCEoAerGIl4E8ILyBk/zPZ6mz+clLNeKHeC0GC+6bEVEGFG+/V2E0fBalji35g6Wkq7MhvmPLfdOrDyGri/w6MO4C/ibch0YoWfm5cC2TyrM8GZcXi0= root@userdata-stack-my-instance-fwab4snhrdpt
-----END SSH HOST KEY KEYS-----
=== network info ===
if-info: lo,up,127.0.0.1,8,::1
if-info: eth0,up,192.168.111.27,24,fe80::f816:3eff:fecc:2efc
ip-route:default via 192.168.111.1 dev eth0 
ip-route:192.168.111.0/24 dev eth0  src 192.168.111.27 
=== datasource: configdrive local ===
instance-id: cc0406ce-9481-44a4-9ebd-1454b14e1adc
name: userdata-stack-my_instance-fwab4snhrdpt
availability-zone: nova
local-hostname: userdata-stack-my-instance-fwab4snhrdpt.novalocal
launch-index: 0
=== cirros: current=unreleased uptime=2.02 ===
  ____               ____  ____
 / __/ __ ____ ____ / __ \/ __/
/ /__ / // __// __// /_/ /\ \ 
\___//_//_/  /_/   \____/___/ 
   http://cirros-cloud.net


login as 'cirros' user. default password: 'cubswin:)'. use 'sudo' for root.
userdata-stack-my-instance-fwab4snhrdpt login: 

a small complicated HOT

heat_template_version: 2013-05-23

description: This template deploys a Flasky single instance server with a SQLite database.

parameters:
  image:
    type: string
    label: Image name or ID
    description: Image to be used for the server. Please use an Ubuntu based image.
    default: trusty-server-cloudimg-amd64
  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: my_key
  private_network:
    type: string
    label: Private network name or ID
    description: Private network to attach server to.
    default: private-net
  gmail_username:
    type: string
    label: Gmail account username
    description: Username of the Gmail account to use for notifications.
  gmail_password:
    type: string
    label: Gmail account password
    description: Password of the Gmail account to use for notifications.
    hidden: true

resources:
  flask_secret_key:
    type: OS::Heat::RandomString
    properties:
      length: 32
      sequence: lettersdigits

  flasky_instance:
    type: OS::Nova::Server
    properties:
      image: { get_param: image }
      flavor: { get_param: flavor }
      key_name: { get_param: key }
      networks:
        - network: { get_param: private_network }
      user_data_format: RAW
      user_data:
        str_replace:
          params:
            __gmail_username__: { get_param: gmail_username }
            __gmail_password__: { get_param: gmail_password }
            __flask_secret_key__: { get_attr: [flask_secret_key, value] }
          template: |
            #!/bin/bash -ex

            # install dependencies
            apt-get update
            apt-get -y install build-essential python python-dev python-virtualenv nginx supervisor git

            # create a flasky user to run the server process
            adduser --disabled-password --gecos "" flasky

            # clone flasky from github
            cd /home/flasky
            git clone https://github.com/miguelgrinberg/flasky.git
            cd flasky

            # Write configuration file
            cat >.env <<EOF
            FLASK_CONFIG=heroku
            SECRET_KEY=__flask_secret_key__
            DATABASE_URL=sqlite:////home/flasky/flasky/appdb.sqlite
            MAIL_USERNAME=__gmail_username__
            MAIL_PASSWORD=__gmail_password__
            [email protected]
            SSL_DISABLE=1
            EOF

            # create a virtualenv and install dependencies
            virtualenv venv
            venv/bin/pip install -r requirements/prod.txt
            venv/bin/pip install gunicorn==18.0

            # create database
            venv/bin/python manage.py deploy

            # make the flasky user the owner of the application
            chown -R flasky:flasky ./

            # configure supervisor to run a private gunicorn web server, and
            # to autostart it on boot and when it crashes
            # stdout and stderr logs from the server will go to /var/log/flasky
            mkdir /var/log/flasky
            cat >/etc/supervisor/conf.d/flasky.conf <<EOF
            [program:flasky]
            command=/home/flasky/flasky/venv/bin/gunicorn -b 127.0.0.1:8000 -w 4 --chdir /home/flasky/flasky --log-file - manage:app
            user=flasky
            autostart=true
            autorestart=true
            stderr_logfile=/var/log/flasky/stderr.log
            stdout_logfile=/var/log/flasky/stdout.log
            EOF
            supervisorctl reread
            supervisorctl update

            # configure nginx as the front-end web server with a reverse proxy
            # rule to the gunicorn server
            cat >/etc/nginx/sites-available/flasky <<EOF
            server {
                listen 80;
                server_name _;
                access_log /var/log/nginx/flasky.access.log;
                error_log /var/log/nginx/flasky.error.log;
                location / {
                    proxy_pass http://127.0.0.1:8000;
                    proxy_redirect off;
                    proxy_set_header Host \$host;
                    proxy_set_header X-Real-IP \$remote_addr;
                    proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
                }
                location /static {
                    alias /home/flasky/flasky/static;
                }
                location /favicon.ico {
                    alias /home/flasky/flasky/favicon.ico;
                }
            }
            EOF
            rm -f /etc/nginx/sites-enabled/default
            ln -s /etc/nginx/sites-available/flasky /etc/nginx/sites-enabled/
            service nginx restart

outputs:
  instance_name:
    description: Name of the instance
    value: { get_attr: [flasky_instance, name] }
  instance_ip:
    description: The IP address of the deployed instance
    value: { get_attr: [flasky_instance, first_address] }

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

@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