OpenStack的Heat組件詳解


一:簡介

    一、什么Heat

       1. Heat 是一套業務流程平台,旨在幫助用戶更輕松地配置以 OpenStack 為基礎的雲體系。利用Heat應用程序,開發人員能夠在程序中使用模板以實現資源的自動化部署。Heat能夠啟動應用、創建虛擬機並自動處理整個流程。它還擁有出色的跨平台兼容性,能夠與 Amazon Web Services 業務流程平台 CloudFormation 相對接——這意味着用戶完全可以將 AWS 模板引入 OpenStack 環境當中。

       2. Heat 是 OpenStack 提供的自動編排功能的組件,基於描述性的模板,來編排復合雲應用程序。

 

    二、為什么需要Heat

       1. 更快更有效的管理 OpenStack 的資源:雲平台系統在相對比較穩定的情況下,管理成本逐漸變成首要的解決問題。雲上自動化能力是一個雲平台的剛需,可以有效降低維護難度。Heat 采用了模板方式來設計或者定義編排,為方便用戶使用,Heat 還提供了大量的模板例子,使用戶能夠方便地得到想要的編排。

       2. 更小的研發成本:引入 Heat,對於不了解 OpenStack 的研發者來說,可以更快的接入現有的業務系統。開發者更關心的是授權認證和對虛擬資源的增刪改,而對於底層的狀態並不用太多了解。

 

    三、概念

       1. 堆棧(stack):管理資源的集合。單個模板中定義的實例化資源的集合,是 Heat 管理應用程序的邏輯單元,往往對應一個應用程序。

       2. 模板(template):如何使用代碼定義和描述堆棧。描述了所有組件資源以及組件資源之間的關系,是 Heat 的核心。

       3. 資源(resource):將在編排期間創建或修改的對象。資源可以是網絡、路由器、子網、實例、卷、浮動IP、安全組等。

       4. 參數(parameters):heat模板中的頂級key,定義在創建或更新 stack 時可以傳遞哪些數據來定制模板。

       5. 參數組(parameter_groups):用於指定如何對輸入參數進行分組,以及提供參數的順序。

       6. 輸出(outputs):heat模板中的頂級key,定義實例化后 stack 將返回的數據。

 

二:架構

    一、核心架構

       

       1. heat command-line clientCLI通過與 heat-api 通信,來調用 API 實現相關功能。終端開發者可以直接使用編排 REST API。

       2. heat-api:實現 OpenStack 原生支持的 REST API。該組件通過把 API 請求經由 AMQP 傳送給 Heat engine 來處理 API 請求。

       3. heat-api-cfn:提供與 AWS CloudFormation 兼容的、AWS 風格的查詢 API,處理請求並通過 AMQP 將它們發送到 heat-engine。

       4. heat-engine:執行模板內容,最終完成應用系統的創建和部署,並把執行結果返回給 API 調用者。

       5. heat-cfntools完成虛擬機實例內部的操作配置任務,需要單獨下載。

 

    二、工作流程

       1. 用戶在 Horizon 中或者命令行中提交包含模板和參數輸入的請求

       2. Horizon 或者命令行工具會將接收到的請求轉化為 REST 格式的 API 調用 Heat-api 或者是 Heat-api-cfn。

       3. Heat-api 和 Heat-api-cfn 會驗證模板的正確性,然后通過 AMQP 異步傳遞給 Heat Engine 來處理請求。

       4. Heat Engine 接收到請求后,會把請求解析為各種類型的資源,每種資源都對應 OpenStack 其它的服務客戶端,然后通過發送 REST 的請求給其它服務。

       5. Heat Engine 在這里的作用分為三層: 第一層處理 Heat 層面的請求,就是根據模板和輸入參數來創建 Stack,這里的 Stack 是由各種資源組合而成。 第二層解析 Stack 里各種資源的依賴關系,Stack 和嵌套 Stack 的關系。第三層就是根據解析出來的關系,依次調用各種服務客戶段來創建各種資源。

            

 

三、模板

    一、 概念:Heat 模板全稱為heat orchestration template,簡稱為HOT。

    二、模板詳解

       1. 典型 Heat 模板結構

 1 heat_template_version: 2015-04-30             ### HOT版本
 2 description:                                  ### 說明
 3   # a description of the template
 4 
 5 parameter_groups:                             ### 指定參數順序
 6 - label: <human-readable label of parameter group>
 7   description: <description of the parameter group>
 8   parameters:
 9   - <param name>
10   - <param name>
11 
12 parameters:                                   ### 傳遞的參數
13   <param name>:                               ### 參數名
14     type: <string | number | json | comma_delimited_list | boolean>  ### 參數類型
15     label: <human-readable name of the parameter>  ### 標簽
16     description: <description of the parameter>
17     default: <default value for parameter>
18     hidden: <true | false>                    ### 是否隱藏
19     constraints:                              ### 已有的內置參數:OS::stack_name、OS::stack_id、OS::project_id
20       <parameter constraints>
21     immutable: <true | false>
22 
23 resources:                                  ### 資源對象
24   <resource ID>:                            ### 資源的ID
25     type: <resource type>                   ### 資源的類型
26     properties:                             ### 資源的屬性
27       <property name>: <property value>
28     metadata:                               ### 資源的元數據
29       <resource specific metadata>
30     depends_on: <resource ID or list of ID>
31     update_policy: <update policy>
32     deletion_policy: <deletion policy>
33 
34 outputs:                                    ### 返回值
35   <parameter name>:                         ### 參數名
36     description: <description>              ### 說明
37     value: <parameter value>                ### 輸出值

 

        2. 例子

 1 heat_temp_version:2016-04-30
 2 Description: AWS::CloudWatch::Alarm using Ceilometer.
 3 parameters:
 4   user_name:
 5     type: string
 6     label: User Name
 7     description: User name to be configured for the application
 8   port_number:
 9     type: number
10     label: Port Number
11     description: Port number to be configured for the web server
12 
13 resources:
14   my_instance:
15     type: OS::Nova::Server
16     properties:
17       flavor: m1.small
18       image: F18-x86_64-cfntools
19 
20 outputs:
21   instance_ip:
22     description: IP address of the deployed compute instance
23     value: { get_attr: [my_instance, first_address] } 

   

    三、模板內部函數

        1. get_attr:獲取所創建資源的屬性

 1 語法
 2 get_attr:
 3   - <resource name>   ### 必須是模板 resouce 段中指定的資源。
 4   - <attribute name>  ### 要獲取的屬性,如果屬性對應的值是list 或map, 則可以指定key/index來獲取具體的值。
 5   - <key/index 1> (optional)
 6   - <key/index 2> (optional)
 7   - ...
 8 
 9 示例
10 resources:
11   my_instance:
12     type: OS::Nova::Server
13 # ...
14  
15 outputs:
16   instance_ip:
17     description: IP address of the deployed compute instance
18     value: { get_attr: [my_instance, first_address] }
19   instance_private_ip:
20     description: Private IP address of the deployed compute instance
21     value: { get_attr: [my_instance, networks, private, 0] }

 

        2. get_file:獲取文件的內容

 1 語法
 2 get_file: <content key>
 3 
 4 示例
 5 resources:
 6   my_instance:
 7     type: OS::Nova::Server
 8     properties:
 9       # general properties ...
10       user_data:
11         get_file: my_instance_user_data.sh
12   my_other_instance:
13     type: OS::Nova::Server
14     properties:
15       # general properties ...
16       user_data:
17         get_file: http://example.com/my_other_instance_user_data.sh

 

        3. get_param:引用模板中指定的參數

 1 語法
 2 get_param:
 3  - <parameter name>
 4  - <key/index 1> (optional)
 5  - <key/index 2> (optional)
 6  - ...
 7 
 8 示例
 9 parameters:
10    instance_type:
11     type: string
12     label: Instance Type
13     description: Instance type to be used.
14   server_data:
15     type: json
16  
17 resources:
18   my_instance:
19     type: OS::Nova::Server
20     properties:
21       flavor: { get_param: instance_type}
22       metadata: { get_param: [ server_data, metadata ] }
23       key_name: { get_param: [ server_data, keys, 0 ] }
24
25 輸出
   {"instance_type": "m1.tiny",
   {"server_data": {"metadata": {"foo": "bar"},"keys": ["a_key","other_key"]}}}

 

        4. get_resource:獲取模板中指定的資源

 1 語法
 2 get_resource: <resource ID>
 3 
 4 示例
 5 resources:
 6   instance_port:
 7     type: OS::Neutron::Port
 8     properties: ...
 9  
10   instance:
11     type: OS::Nova::Server
12     properties:
13       ...
14       networks:
15         port: { get_resource: instance_port }

 

        5. list_join:使用指定的分隔符將一個list中的字符串合成一個字符串

1 語法
2 list_join:
3 - <delimiter>
4 - <list to join>
5 
6 示例輸出:one,two,three
7 list_join: [', ', ['one', 'two', 'and three']]

 

        6. digest:在指定的值上使用algorithm 

1 語法
2 digest:
3   - <algorithm>  ### 可用的值是hashlib(md5, sha1, sha224, sha256, sha384, and sha512) 或openssl的相關值
4   - <value>
5 
6 示例
7 # from a user supplied parameter
8 pwd_hash: { digest: ['sha512', { get_param: raw_password }] }

 

        7. repeat:迭代fore_each中的列表,按照template的格式生成一個list

 1 語法
 2 repeat:
 3   template:
 4     <template>
 5   for_each:
 6     <var>: <list>
 7 
 8 示例
 9 parameters:
10   ports:
11     type: comma_delimited_list
12     label: ports
13     default: "80,443,8080"
14   protocols:
15     type: comma_delimited_list
16     label: protocols
17     default: "tcp,udp"
18  
19 resources:
20   security_group:
21     type: OS::Neutron::SecurityGroup
22     properties:
23       name: web_server_security_group
24       rules:
25         repeat:
26           for_each:
27             <%port%>: { get_param: ports }
28             <%protocol%>: { get_param: protocols }
29           template:
30             protocol: <%protocol%>
31             port_range_min: <%port%>
32 
33 結果
34 [{‘protocal’:tpc, ‘prot_range_min’:80},
35 
36 {‘protocal’:tpc, ‘prot_range_min’:443},
37 
38 {‘protocal’:tpc, ‘prot_range_min’:8080},
39 
40 {‘protocal’:udp, ‘prot_range_min’:80},
41 
42 {‘protocal’:udp, ‘prot_range_min’:443},
43 
44 {‘protocal’:udp, ‘prot_range_min’:8080}]

 

        8. resource_facade:檢索資源的數據

        9. str_replace:使用params中的值替換template中的占位符,從而構造一個新的字符串

 1 語法
 2 str_replace:
 3   template: <template string>
 4   params: <parameter mappings>
 5 
 6 示例
 7 resources:
 8   my_instance:
 9     type: OS::Nova::Server
10     # general metadata and properties ...
11  
12 outputs:
13   Login_URL:
14     description: The URL to log into the deployed application
15     value:
16       str_replace:
17         template: http://host/MyApplication
18         params:
19           host: { get_attr: [ my_instance, first_address ] }

 

      10. str_split:將一個字符串按照分隔符分隔成一個list

 1 語法
 2 str_split:
 3   - ','
 4   - string,to,split
 5 
 6 示例
 7 str_split: [',', 'string,to,split']
 8 
 9 結果
10 ['string', 'to', 'split']

 

四:常用操作

    一、棧、資源、模板管理

          

 

    二、軟件、快照管理

         

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM