josug meetup 28th heat 101
TRANSCRIPT
Japan OpenStack User Group Meetup 28th
Heat によるオーケストレーション入門2016/6/23
Tomoaki Nakajima
@irix_jp
1
Table of Contents
概要
HOTの基礎
特殊なリソース
まとめ
演習
2
概要
3
Heat
OpenStackのコンポーネントの1つで、OpenStack上のリソース(仮想マシンや仮想ネットワーク)を操作する事に特化している。
システムを構成するリソースをデータ構造(YAML)として定義し、それをHeatに読み込ませることで、実際のシステム環境を構築する。
このデータ構造を定義するファイルをHOT(Heat Orchestration
Template)と呼ぶ。
4
parameters:cluster_size:type: numberlabel: Cluster sizedescription: Number of
instances in cluster.default: 2
resources:tiny_cluster:type:
OS::Heat::ResourceGroupproperties:count: { get_param:
cluster_size }resource_def:type: OS::Nova::Server
HOT
WEBサーバー APサーバー DBサーバー
実際のシステム環境
Heatにロードさせる
OpenStack環境
補足:Ansibleとの違い
Ansibleもプレイブックを記述する際はYAML形式を取ります。
プレイブックは「手順」をデータ構造として定義するのに対して、Heatでは「システム構造」をデータ構造で定義します。
そのため、 Heatはシステムを構成するリソース間の依存関係を定義することが可能です。
一方で、手順を記述することは苦手です。
Ansibleは手順を記述することは得意ですが、一方で扱うリソースの依存関係は手順の中で解決する必要があります。
この二つはどちらが優れているという議論ではなく、組み合わせてお互いの苦手な部分を補って活用すると有益です。
5
HOTとは?
Heatで利用するシステム構成をデータ構造(YAML)で記述したファイルを指す。
Heat が定めるデータ構造 YAML形式で記述
作成するリソース、設定するパラメータなどのシステム構造を定義する。
多段ネスト可能
リソース間の依存関係を定義 暗黙の依存関係
明示的な依存関係
定義された依存関係に基づいてリソースの作成順序が決まる 依存関係がないものは平行で作成される
作成された全てのリソースは「状態」を持つ Heatが検知する状態
ユーザーが外部から与える状態
条件文はかけない(if 系、for系は可能)
6
Heatの構造
Heatの構造は他のコンポーネントに比べるとシンプルです。
APIサーバーと、実際のリソース操作を行うエンジンの2つから構成されています。
7
heat-api
heat-engine
keystone
heat-engine
heat-engine
swift
xxx-api操作確認
VM
OpenStack
Heatを使うための条件
イメージ 特にOSに関係なく利用できます。以下の導入ツールの有無によってインスタンスに対して操作できる範囲が異なってきます。
前提ツール cloud-init
インストールされていなくてもHeatは利用できますが、利用可能であればより高度な設定が可能です。
heat-cfntools
AWSのCFN互換コマンド群です。インストールされていなくてもHeatは利用できますが、利用可能であればより高度な設定が可能です。 userdataで連携を行うため cloud-init が導入されていることが前提です。
boot-config(os-xxxx-configツール群)
インストールされていなくてもHeatは利用可能。
主にTripleOで活用されている。高度なソフトウェア設定を行うためのツールです。このツールが導入されたイメージは、スタックの状態変化をフックして、様々な処理を実行させることが可能になります。 cloud-init の導入が前提
ネットワーク Heat管理下で作成するインスタンスは外部ネットワーク(OpenStackのエンドポイント)へ接続できるように構成するのが推奨です。
閉じたネットワークでも利用可能ですが、一部の機能が制限される。
これは状態の通知がエンドポイント経由で行われるためです。
8
HOTの基本
OpenStack上で行える操作がほぼ全て行えます。 例)
仮想サーバーの作成
キーペアの作成
仮想ネットワーク、サブネットの作成
仮想ルーターの作成、ネットワークとの接続
Floating IPの作成、割当て
論理ポートの作成
セキュリティグループの作成
仮想ボリュームの作成、アタッチ
「リソース」をYAMLで記述することで作成できます。
このYAMLファイルを「テンプレート」と呼びます。
9
resources:server1:
type: OS::Nova::Serverproperties:name: "Heat_Deployed_Server"image: { get_param: ImageID }flavor: "m1.small"key_name: "temp-key-001"networks:- network: { get_param: NetID }
web_server_security_group:type: OS::Neutron::SecurityGroupproperties:name: web_server_security_grouprules:- protocol: icmp- protocol: tcpport_range_min: 22port_range_max: 22
- protocol: tcpport_range_min: 443port_range_max: 443
- protocol: tcpport_range_min: 80port_range_max: 80
private_subnet:type: OS::Neutron::Subnetproperties:network_id: { get_resource: private_network }cidr: 10.10.20.0/24dns_nameservers:- 8.8.8.8- 8.8.4.4
セキュリティグループの作成
仮想サーバーの作成
サブネットの作成
テンプレートに記述できるリソース
heat resource-type-list コマンドで一覧を確認できます。 利用できるリソースはHeatのバージョンに依存します。 またHeatのバージョンによって同じリソースでも仕様が異なっている場合があります。
例) パラメータのバリデーション方法など。
これにより、前のバージョンでは動いていたのに、新しいバージョンではエラーになるという場合があります。
以下は利用できるリソースの一部を表示したものです。
10
$ heat resource-type-list+------------------------------------------+| resource_type |+------------------------------------------+| AWS::AutoScaling::AutoScalingGroup || AWS::AutoScaling::LaunchConfiguration || AWS::AutoScaling::ScalingPolicy || AWS::CloudFormation::Stack || AWS::CloudFormation::WaitCondition || AWS::CloudFormation::WaitConditionHandle || AWS::CloudWatch::Alarm || AWS::EC2::EIP || AWS::EC2::EIPAssociation || AWS::EC2::Instance || AWS::EC2::InternetGateway || AWS::EC2::NetworkInterface || AWS::EC2::RouteTable || AWS::EC2::SecurityGroup || AWS::EC2::Subnet || AWS::EC2::SubnetRouteTableAssociation || AWS::EC2::VPC || AWS::EC2::VPCGatewayAttachment || AWS::EC2::Volume || AWS::EC2::VolumeAttachment || AWS::ElasticLoadBalancing::LoadBalancer || AWS::IAM::AccessKey |
| AWS::IAM::User || AWS::RDS::DBInstance || AWS::S3::Bucket || OS::Ceilometer::Alarm || OS::Ceilometer::CombinationAlarm || OS::Cinder::Volume || OS::Cinder::VolumeAttachment || OS::Glance::Image || OS::Heat::AccessPolicy || OS::Heat::AutoScalingGroup || OS::Heat::CWLiteAlarm || OS::Heat::CloudConfig || OS::Heat::HARestarter || OS::Heat::InstanceGroup || OS::Heat::MultipartMime || OS::Heat::RandomString || OS::Heat::ResourceGroup || OS::Heat::ScalingPolicy || OS::Heat::SoftwareComponent || OS::Heat::SoftwareConfig || OS::Heat::SoftwareDeployment || OS::Heat::SoftwareDeployments || OS::Heat::Stack || OS::Heat::StructuredConfig || OS::Heat::StructuredDeployment || OS::Heat::StructuredDeployments |
| OS::Heat::SwiftSignal || OS::Heat::SwiftSignalHandle || OS::Heat::UpdateWaitConditionHandle || OS::Heat::WaitCondition || OS::Heat::WaitConditionHandle || OS::Neutron::Firewall || OS::Neutron::FirewallPolicy || OS::Neutron::FirewallRule || OS::Neutron::FloatingIP || OS::Neutron::FloatingIPAssociation || OS::Neutron::HealthMonitor || OS::Neutron::IKEPolicy || OS::Neutron::IPsecPolicy || OS::Neutron::IPsecSiteConnection || OS::Neutron::LoadBalancer || OS::Neutron::MeteringLabel || OS::Neutron::MeteringRule || OS::Neutron::Net || OS::Neutron::NetworkGateway || OS::Neutron::Pool || OS::Neutron::PoolMember || OS::Neutron::Port || OS::Neutron::ProviderNet || OS::Neutron::Router || OS::Neutron::RouterGateway || OS::Neutron::RouterInterface |
heat_template_version: 2015-04-30
テンプレートのバージョン
Heatはバージョンごとサポートされるリソース種別と、Heat関数の機能が異なっています。
基本的に新しいHeatは過去のテンプレートバージョンをサポートしています。
テンプレートの先頭にどのバージョンを利用するか宣言して利用します。
利用可能なバージョンは以下 年月日の部分がバージョン番号です。
11
2013-05-23Icehouse
2014-10-16Juno
2015-04-30Kilo
2015-10-15Liberty
2016-04-08Mitaka
DSLで利用できる関数
get_attrget_fileget_paramget_resourcelist_joinresource_facadestr_replaceFn::Base64Fn::GetAZsFn::JoinFn::MemberListToMapFn::ReplaceFn::ResourceFacadeFn::SelectFn::SplitRef
get_attrget_fileget_paramget_resourcelist_joinresource_facadestr_replaceFn::Select
get_attrget_fileget_paramget_resourcelist_joinrepeatdigestresource_facadestr_replaceFn::Select
get_attrget_fileget_paramget_resourcelist_joinrepeatdigestresource_facadestr_replacestr_split
digestget_attrget_fileget_paramget_resourcelist_joinmap_mergerepeatresource_facadestr_replacestr_split
http://docs.openstack.org/developer/heat/template_guide/hot_spec.html#heat-template-version
参考:利用できるテンプレートバージョンの確認
Liberty以降では、以下のコマンドでサポートバージョンが確認できます。
12
# heat template-version-list+--------------------------------------+------+| version | type |+--------------------------------------+------+| AWSTemplateFormatVersion.2010-09-09 | cfn || HeatTemplateFormatVersion.2012-12-12 | cfn || heat_template_version.2013-05-23 | hot || heat_template_version.2014-10-16 | hot || heat_template_version.2015-04-30 | hot || heat_template_version.2015-10-15 | hot |+--------------------------------------+------+
シンプルなテンプレート
以下は1台のインスタンスを作成するテンプレートです。 nova boot コマンドと同じようなパラメータを与えています。
テンプレートにはシステムの構造を定義します。 システムの構造とは、以下のような情報を指します。
そのシステムがどのようなネットワークをいくつ持つか、そのシステムはどのような役割のインスタンスをいくつ持つか
システムの起動順序、システムが障害を起こした際にどうするか?そのシステムを制御するための変数は何か?
テンプレートを実行すると「スタック(stack)」が作成されます。
13
heat_template_version: 2015-04-30
resources:server1:type: OS::Nova::Serverproperties:
image: d33f51a3-6a18-4b96-bde6-2a68782336caflavor: "m1.small"key_name: "temp-key-001"networks:- network: 1a684f4a-4f06-4741-b4f9-daa28676f74b
single-instance.yaml
テンプレートのバージョンを宣言します。
操作するリソースについて記述します。ここれは1台のインスタンスを作成しています。ここでは1つのリソースのみ記述していますが、実際にはいくつでも記載可能です。
# heat stack-create -f single-instance.yaml test-stack+--------------------------------------+------------+--------------------+----------------------+| id | stack_name | stack_status | creation_time |+--------------------------------------+------------+--------------------+----------------------+| b4b8d078-7f84-4ab3-b8ca-87dc80d2480e | test-stack | CREATE_IN_PROGRESS | 2015-12-21T14:52:57Z |+--------------------------------------+------------+--------------------+----------------------+
# nova list+--------------------------------------+---------------------------------+--------+------------+-------------+----------------------+| ID | Name | Status | Task State | Power State | Networks |+--------------------------------------+---------------------------------+--------+------------+-------------+----------------------+| abbbbc96-f301-4d63-b13b-b9733d3b6404 | test-stack-server1-cg4vev4dgxga | ACTIVE | - | Running | work-net=10.10.10.16 |+--------------------------------------+---------------------------------+--------+------------+-------------+----------------------+
スタック
スタックとはテンプレートから作成されたシステム全体を表すリソースです。
入力されたパラメーター、作成されたリソースの実体情報(NovaインスタンスのUUID等)、リソースの状態を保持しています。
Heatはこのスタックを操作対象にするテンプレートを記述可能です。
14
heat_template_version: 2015-04-30
resources:server1:type: OS::Nova::Serverproperties:image: d33f51a3-6a18-
4b96-bde6-2a68782336caflavor: "m1.small"key_name: "temp-key-
001"networks:- network: 1a684f4a-
4f06-4741-b4f9-daa28676f74b
テンプレート
スタック1
スタック2
stack-create
stack-create
スタックの状態
作成されたスタックは状態を持ちます。
管理するリソースの状態、操作の状況によってスタックは様々な状態へ変化します。
以下は状態の一例です。
15
init_in_progressinit_completeinit_failedcreate_in_progresscreate_completecreate_faileddelete_in_progressdelete_completedelete_failedupdate_in_progressupdate_completeupdate_failed
snapshot_in_progresssnapshot_completesnapshot_failedcheck_in_progresscheck_completecheck_failed
rollback_in_progressrollback_completerollback_failedsuspend_in_progresssuspend_completesuspend_failedresume_in_progressresume_completeresume_failedadopt_in_progressadopt_completeadopt_failed
スタックの状態の基本
HeatはOpenStackの各APIサーバーと通信してリソースの状態を取得します。
つまり、実体のリソースの本当の状態をHeatは知ることはできません。
例)
インスタンスをHeatのテンプレートで作成した際に、Nova側で「ACTIVE」となれば、Heatはそのリソースが正常に作成されたと判断します。
実際はまだOSの起動中で、起動に失敗するという事もありえます。
Heatはこの問題を解決する方法を用意しています。方法は後述します。
16
HOTの基礎
17
HOTの構造
先にシンプルなテンプレート例を紹介しましたが、実際のテンプレートはもっと多くの情報を含んでいます。
ここではトップレベルの構造を紹介します。現在のテンプレートは5つのセクションをテンプレート内に持ちます。
heat_template_version, resources 以外は省略可能です。
18
heat_template_version: 2015-04-30
description: Demo template for the 09th lecture.
parameters:image:type: stringlabel: Image name or IDdescription: Image to be used for the server. default: CentOS-7-x86_64-GenericCloud-1509-witn-cfntools-1.3.0-2
resources:private_network:type: OS::Neutron::Net
outputs:instance_ip:description: The IP address of the deployed instancevalue: { get_attr: [floating_ip, floating_ip_address] }
テンプレートのバージョン
テンプレートの説明
外部から変更可能なパラメータを定義する
作成するリソースを記述する。最も重要。
スタック内の情報を外部へ出力する
テンプレート例
以下は仮想ルーター、仮想ネットワークを作成・接続し、論理ポートを作成した後で、インスタンスを一台起動して、Floating IPを与える例です。
19
parameters:image:type: stringlabel: Image name or IDdescription: Image to be used for the server. default: CentOS-7-x86_64-GenericCloud-1509-witn-cfntools-1.3.0-2
flavor:type: stringlabel: Flavordescription: Type of instance (flavor) to be used on the compute instance.default: m1.small
key:type: stringlabel: Key namedescription: Name of key-pair to be installed on the compute instance.default: temp-key-001
public_network:type: stringlabel: Public network name or IDdescription: Public network with floating IP addresses.default: public
ext_router:type: stringlabel: Router namedescription: Router name or ID to connect to an external network.default: Ext-Router
secgroup:type: stringlabel: Secgroup namedescription: Security group name.default: web_server_security_group
resources:private_network:type: OS::Neutron::Net
private_subnet:type: OS::Neutron::Subnetproperties:network_id: { get_resource: private_network }cidr: 10.10.20.0/24dns_nameservers:- 8.8.8.8- 8.8.4.4
router-interface:type: OS::Neutron::RouterInterfaceproperties:router_id: { get_param: ext_router }subnet: { get_resource: private_subnet }
neutron-port:type: OS::Neutron::Portproperties:network: { get_resource: private_network }security_groups:- { get_param: secgroup }
test-instance:type: OS::Nova::Serverproperties:image: { get_param: image }flavor: { get_param: flavor }key_name: { get_param: key }networks:- port: { get_resource: neutron-port }
floating_ip:type: OS::Neutron::FloatingIPproperties:floating_network: { get_param: public_network }
floating_ip_assoc:type: OS::Neutron::FloatingIPAssociationproperties:floatingip_id: { get_resource: floating_ip }port_id: { get_resource: flasky_port }
Parameters セクション
定義したパラメーターの値はテンプレート内から get_param
関数で参照可能です。
パラメーターにはデフォルト値を設定することが可能です。
デフォルト値は、スタック作成時に上書き可能です。
20
parameters:image:type: stringlabel: Image name or IDdescription: Image to be used for the server. default: CentOS-7-x86_64-GenericCloud-1509-witn-cfntools-1.3.0-2
flavor:type: stringlabel: Flavordescription: Type of instance (flavor) to be used on the compute
instance.default: m1.small
$ heat stack-create -f heat.yaml -P 'image=Ubuntu14.04lts;flavor=m1.medium' stack-name
test-instance:type: OS::Nova::Serverproperties:image: { get_param: image }flavor: { get_param: flavor }key_name: { get_param: key }networks:
- port: { get_resource: neutron-port }
パラメーターの値を参照(通常はデフォルト値が参照される)
パラメーターを上書きしてスタックを作成する例
Resources セクション
作成するリソースを記述していく。
リソースは作成する単位ごとに1つのタイプを持つ。
作成したリソースの値は get_resource, get_attr関数で値を参照できます。
21
resources:wait_condition:type: OS::Heat::WaitConditionproperties:handle: { get_resource: wait_handle }count: 1timeout: 600
wait_handle:type: OS::Heat::WaitConditionHandle
web_server_security_group:type: OS::Neutron::SecurityGroupproperties:name: web_server_security_grouprules:- protocol: icmp- protocol: tcp
port_range_min: 22port_range_max: 22
- protocol: tcpport_range_min: 443port_range_max: 443
- protocol: tcpport_range_min: 80port_range_max: 80
private_network:type: OS::Neutron::Net
private_subnet:type: OS::Neutron::Subnetproperties:network_id: { get_resource: private_network }cidr: 10.10.20.0/24dns_nameservers:- 8.8.8.8- 8.8.4.4
router-interface:type: OS::Neutron::RouterInterfaceproperties:router_id: { get_param: ext_router }subnet: { get_resource: private_subnet }
neutron-port:type: OS::Neutron::Portproperties:network: { get_resource: private_network }security_groups:
- { get_resource: web_server_security_group }
test-instance:type: OS::Nova::Serverproperties:image: { get_param: image }flavor: { get_param: flavor }key_name: { get_param: key }networks:
- port: { get_resource: neutron-port }user_data_format: RAWuser_data:
str_replace:params:wc_notify: { get_attr: ['wait_handle', 'curl_cli'] }
template: |#!/bin/bash -exsleep 3echo "Hello"wc_notify --data-binary '{"status": "SUCCESS"}'echo '--- end ---'
パラメータによるスタックの制御
パラメータを上手く使うと、1つのテンプレートから複数の環境を作成することが可能です。
22
WEBサーバー APサーバー 汎用サーバー DBサーバー
テンプレート
WEBサーバー APサーバー DBサーバー
システム1
WEBサーバー DBサーバー汎用サーバー
システム2
テンプレート内で、台数をパラメータ化しておく。
WEBサーバー: 3(m1.small)APサーバー: 2(m1.medium)汎用サーバー:0DBサーバー: 2(m1.large)
WEBサーバー: 2(m1.small)APサーバー: 0汎用サーバー:2(m1.large)DBサーバー: 1(m1.medium)
Outputs セクション
作成したスタックの情報を外部に出力する。
他のプログラムとの連携や、別のテンプレートで仕様する。
23
outputs:instance_name:description: Name of the instancevalue: { get_attr: [test-instance, name] }
instance_ip:description: The IP address of the deployed instancevalue: { get_attr: [floating_ip, floating_ip_address] }
[{"output_value": "test3-test-instance-2xmjvlijsyrz","description": "Name of the instance","output_key": "instance_name"
},{"output_value": "172.16.0.104","description": "The IP address of the deployed instance","output_key": "instance_ip"
}]
テンプレート
出力
リソースの依存関係
Resourcesセクションでは get_resource, get_attr で他のリソースを参照した場合に、Heat側で依存関係が設定されます。
リソース作成時に、 depend_onパラメーターを与える事で、ユーザーが依存関係を設定することも可能。
依存関係が設定されている場合、依存元のリソースの作成が完了してから、リソースの作成が行われる。
それ以外のリソースは平行で作成されていく。
以下のテンプレートからスタックを作成した場合・・・
24
heat_template_version: 2015-04-30
description: Demo template for the 09th lecture.
parameters:image:
type: stringlabel: Image name or IDdescription: Image to be used for the server. default: CentOS-7-x86_64-GenericCloud-1509-witn-cfntools-1.3.0-2
flavor:type: stringlabel: Flavordescription: Type of instance (flavor) to be used on the compute instance.default: m1.small
key:type: stringlabel: Key namedescription: Name of key-pair to be installed on the compute instance.default: temp-key-001
public_network:type: stringlabel: Public network name or IDdescription: Public network with floating IP addresses.default: public
ext_router:type: stringlabel: Router namedescription: Router name or ID to connect to an external network.default: a3f094b5-fe83-4a92-a161-dece054ff0b0
resources:wait_condition:
type: OS::Heat::WaitConditionproperties:handle: { get_resource: wait_handle }count: 1timeout: 600
wait_handle:type: OS::Heat::WaitConditionHandle
web_server_security_group:type: OS::Neutron::SecurityGroupproperties:name: web_server_security_grouprules:- protocol: icmp- protocol: tcp
port_range_min: 22port_range_max: 22
- protocol: tcpport_range_min: 443port_range_max: 443
- protocol: tcpport_range_min: 80port_range_max: 80
private_network:type: OS::Neutron::Net
(続く)
リソースの依存関係
右下図の依存関係が自動で設定される。
25
(続き)private_subnet:type: OS::Neutron::Subnetproperties:network_id: { get_resource: private_network }cidr: 10.10.20.0/24dns_nameservers:
- 8.8.8.8- 8.8.4.4
router-interface:type: OS::Neutron::RouterInterfaceproperties:router_id: { get_param: ext_router }subnet: { get_resource: private_subnet }
neutron-port:type: OS::Neutron::Portproperties:network: { get_resource: private_network }security_groups:
- { get_resource: web_server_security_group }
test-instance:type: OS::Nova::Serverproperties:image: { get_param: image }flavor: { get_param: flavor }key_name: { get_param: key }networks:
- port: { get_resource: neutron-port }user_data_format: RAWuser_data:
str_replace:params:wc_notify: { get_attr: ['wait_handle', 'curl_cli'] }
template: |#!/bin/bash -exsleep 3echo "Hello"wc_notify --data-binary '{"status": "SUCCESS"}'echo '--- end ---'
floating_ip:type: OS::Neutron::FloatingIPproperties:floating_network: { get_param: public_network }
floating_ip_assoc:type: OS::Neutron::FloatingIPAssociationproperties:floatingip_id: { get_resource: floating_ip }port_id: { get_resource: neutron-port }
outputs:instance_name:description: Name of the instancevalue: { get_attr: [test-instance, name] }
instance_ip:description: The IP address of the deployed instancevalue: { get_attr: [floating_ip, floating_ip_address] }
リソースの依存関係
作成された依存関係
26
OS::Heat::WaitCondition
OS::Heat::WaitConditionHandle
OS::Nova::Server
OS::Neutron::Port
OS::Neutron::SecurityGroup
OS::Neutron::FloatingIP
OS::Neutron::FloatingIPAssociation
OS::Neutron::Subnet
OS::Neutron::Net
OS::Neutron::RouterInterface
Heat関数(一部)
get_attr
get_file
get_param
get_resource
list_join
repeat
str_replace
27
スタックのアップデート
一度作成したスタックはリソースのプロパティ変更してアップデート可能です。
プロパティをアップデートした時の挙動はリソースごとに決められています。
例)
インスタンスを操作する OS:Nova:Serverにおいて、 flavor プロパティをアップデートした場合、RESIZE処理がデフォルトで実行されます。
この値はリソース作成時に変更可能です。
28
$ heat resource-type-show OS::Nova::Server(抜粋)"flavor_update_policy": {
"description": "Policy on how to apply a flavor update; either by requesting a server resize or by replacing the entire server.","default": "RESIZE","required": false,"update_allowed": true,"type": "string","immutable": false,"constraints": [
{"allowed_values": ["RESIZE","REPLACE"
](省略)
[centos@console ~]$ heat stack-create -f 15_02_simple_server.yaml ¥-P image=cirros ¥-P flavor=m1.tiny ¥-P private_network=heat-handson-net ¥-P key=heat-key ¥-P sec_group=sg-for-heat ¥simple-server
[centos@console ~]$ heat stack-update -f 15_02_simple_server.yaml ¥-P image=cirros ¥-P flavor=m1.smll ¥-P private_network=heat-handson-net ¥-P key=heat-key ¥-P sec_group=sg-for-heat ¥simple-server
+-------------------------------------+--------+------------+-------------+| Name | Status | Task State | Power State |+-------------------------------------+--------+------------+-------------+| simple-server-instance-5xnnypou2f25 | ACTIVE | - | Running |+-------------------------------------+--------+------------+-------------+
+-------------------------------------+--------+---------------+-------------+| Name | Status | Task State | Power State |+-------------------------------------+--------+---------------+-------------+| simple-server-instance-3nflhxpgmq5z | RESIZE | resize_finish | Running |+-------------------------------------+--------+---------------+-------------+
テンプレートのネスト
テンプレートはネスト可能で、1つのテンプレートを別のテンプレート内で1つのリソースとして扱うことができます。
この時、パラメータがリソースのプロパティになり、アウトプット値をリソースの属性値として取り出すことが可能になります。
29
(省略)resources:
server1:type: 15_02_simple_server.yamlproperties:image: { get_param: image }flavor: { get_param: flavor }key: { get_param: key }sec_group: { get_param: sec_group }private_network: { get_param: private_network }
server2:type: 15_02_simple_server.yamlproperties:image: { get_param: image }flavor: { get_param: flavor }key: { get_param: key }sec_group: { get_param: sec_group }private_network: { get_param: private_network }
outputs:instance_name:description: Name of the instancevalue:- { get_attr: [ server1, instance_name ] }- { get_attr: [ server2, instance_name ] }
instance_ip:description: The IP address of the deployed instancevalue:- { get_attr: [ server1, instance_ip ] }- { get_attr: [ server2, instance_ip ] }
(省略)parameters:image:
type: stringdefault: CentOS7-1509
flavor:type: stringdefault: m1.small
private_network:type: stringdefault: work-net
sec_group:type: stringdefault: open_all
key:type: stringdefault: my-key
resources:instance:
type: OS::Nova::Serverproperties:image: { get_param: image }flavor: { get_param: flavor }key_name: { get_param: key }security_groups:
- { get_param: sec_group }networks:
- network: { get_param: private_network }
outputs:instance_name:
description: Name of the instancevalue: { get_attr: [instance, name] }
instance_ip:description: The IP address of the deployed instancevalue: { get_attr: [ instance, first_address ] }
15_02_simple_server.yaml
環境定義ファイル
テンプレートをネストする際に、ファイル名を独自のリソース名にマッピングすることが可能です。
30
resource_registry:My::SimpleServer::file: 15_02_simple_server.yamlMy::SimpleServer::github: https://raw.githubusercontent.com/irixjp/topse-tools/master/hands-on/15_02_simple_server.yaml
15_environment.yaml
$ heat stack-create -f 15_03_nested_temp_from_env.yaml -e 15_environment.yaml nested-stack-from-env
(省略)resources:
instance:type: OS::Nova::Serverproperties:image: { get_param: image }flavor: { get_param: flavor }key_name: { get_param: key }security_groups:
- { get_param: sec_group }networks:
- network: { get_param: private_network }
outputs:instance_name:description: Name of the instancevalue: { get_attr: [instance, name] }
instance_ip:description: The IP address of the deployed instancevalue: { get_attr: [ instance, first_address ] }
15_02_simple_server.yaml
(省略)resources:server1:
type: My::SimpleServer::fileproperties:image: { get_param: image }flavor: { get_param: flavor }key: { get_param: key }sec_group: { get_param: sec_group }private_network: { get_param: private_network }
(省略)
15_03_nested_temp_from_env.yaml
特殊なリソース
31
特殊なリソース
HOTで記述するリソースは基本的にOpenStackが持つリソースが対象になります。
インスタンス、ネットワーク、ボリューム等々・・・
しかし、システム構成を記述する場合には、単純にリソースを配置していくだけでは対応できません。
特殊な動作を行わせるために、Heatはいくつかの独自リソースを備えています。
これらのリソースはHeat::XX に属しています。
以下では、よく使う特殊リソースを紹介します。
32
状態と通知を管理するリソース
コンディションとハンドラ
コンディションで条件(COMPLETEになる条件)を定義
ハンドラでシグナル受信エンドポイントをAPI上に作成
33
OS::Heat::WaitCondition OS::Heat::WaitConditionHandle
シグナル受信エンドポイント
(Heat API上)
COMPLELTE条件・タイムアウト値・受信シグナル数
VM内のスクリプト等
カウントアップ
シグナル送信
curl -i -X POST -H 'X-Auth-Token: 29e480500b9d410489ead0c87c139ce7' -H 'Content-Type: application/json' -H 'Accept: application/json' http://172.16.199.10:8004/v1/f2e4de4318144cc5b7129a2dc5b28bf0/stacks/software-config/53494408-612c-4eb7-a753-15630836c30f/resources/wait_handle/signal --data-binary '{"status": "SUCCESS"}'
グルーピングに関するリソース
リソースのグループ化と、オートスケール
複数の同一リソースを作成する場合に利用します。
単純にN個のリソースを作成する、ResourceGroupと、オートスケールのた
めに各種条件を与えることが可能な AutoScalingGroupがあります。
34
OS::Ceilometer::Alarm OS::Heat::ScalingPolicy OS::Heat::AutoScalingGroup
どのような条件でアラートを発するか
アラート受診時にどのような挙動を取るか?リソースを増やす・減らす
スケールのMax/Min値の設定どのリソースを配下に収めるか
OS::Nova::Server
OS::Heat::ResourceGroup OS::Nova::Server
どのリソースを配下に収めるかそのリソースを何個生成するか
ソフトウェア設定に関するリソース1
起動したインスタンスに対して各種設定を行うためのリソースです。
cloud-init 経由でインスタンスに対する操作を行います。
インスタンス作成時に1回しか動作しません。
リソースをアップデートした場合、インスタンスはRebuildされます。
35
OS::Heat::CloudConfig
VM内のcloud-init
OS::Heat::SoftwareConfig
OS::Heat::MultipartMime
#cloud-config
#!/bin/bash
ソフトウェア設定に関するリソース2
起動したインスタンスに対して各種設定を行うためのリソースです。
boot-configに含まれる各種ツールとMetadataサーバー、シグナル受信機によって動作します。 https://github.com/openstack/heat-templates/tree/master/hot/software-config/boot-config
実行順序の定義が可能で、スタックをアップデートした際に特定の処理のみを再実行することも可能です。
36
OS::Heat::CloudConfig
VM内のboot-config
OS::Heat::SoftwareConfig
OS::Heat::MultipartMime
#cloud-config
#!/bin/bash
OS::Heat::SoftwareDeployment
OS::Heat::SoftwareDeployment
OS::Heat::SoftwareDeployment
Nova Metadata Server
Signal Reciever(Heat API/Swift)
まとめ
37
まとめ
Heat を利用するとOpenStack上のリソースを操作可能
HOT形式でリソースの表現する。
リソースは依存関係を持ち、依存関係の末端から処理が実行されていく。
インスタンスに関する操作は cloud-init か boot-configで実行される。
ただし、リブートのハンドリング等、苦手なこともあるのでインスタンスの操作はAnsible等に比べると苦手。
38
演習
39
演習1
スタック操作の基本
作成済みのテンプレートを使ってスタックの作成、削除、作成、パラメータを上書きしての作成を試してください。
操作例はEtherpad参照
使用テンプレート
$ git clone https://github.com/irixjp/topse-tools.git
$ cd topse-tools/hands-on/
15_01_heat_env_pre_settings.yaml
40
演習2
1台のサーバーを起動
起動時に各種値(フレーバーやイメージ、接続ネットワーク)を変更できるように作成してください。
作成したスタックから、フレーバーの値を変更してスタックをアップデートしてください。 その際の挙動について確認(インスタンスがどうなるか?)
41
演習3
演習2で作成したテンプレートをネストして利用するテンプレートを作成して実行する。
以下の2通り作成する
ファイルを直接指定
環境定義ファイルを利用する
42
演習4
リソースのグループ化を行うテンプレートを作成して実行する。
グループ化するリソースは、演習2で作成したサーバーを1台起動するテンプレートをネストして使用する。
デフォルトで2つのインスタンスを起動するように設定する。
この数値は後で変更可能なようにパラメーター化しておく
インスタンス数を3,2,1,0と変化させるようにスタックをアップデートして、挙動を確認する。
43
演習5
演習2のテンプレートを元に、インスタンス起動時に以下の3つの処理を実行するテンプレートを作成する。
1.インスタンスのパスワードを利用可能にして、タイムゾーンをAsia/Tokyoに設定
2.スタック作成時にパラメータに自分の名前を設定して、その名前が/your_name.txt ファイルに記載されるようにする
3.全てのスクリプトが実行されたらスタックが「Complete」になるようにする
ヒント
Cirrosイメージでは動かないので、cloud-init が導入されたFedoraやCentOSのイメージを利用する。
44
おつかれさまでした
45