Heat入門第一步

Heat 類似於AWS的CloudFormation, 是OpenStack Orchestration進程的一個項目,OpenStack Orchestration旨在創建一個人性化的服務去管理整個雲架構,服務和應用的生命週期。heat實現了一種自動化的通過簡單定義和配置就能實現的雲部署方式。可以在heat模板中定義連串相關任務(例如用某配置開幾臺虛擬機,然後再去在其中一臺中安裝一個mysql服務,設定相關數據庫屬性,然後再配置幾臺虛擬機安裝web服務集羣等等),然後交由heat,由heat按照一定的順序執行heat模板中定義的一連串任務。利用heat還可以連接到neutron來幫助編排負載均衡和其他網絡功能。
學習heat主要學習heat的template,heat的template描述了所用的所有組件資源以及組件資源之間的關係。 這些資源包括:servers,floating ips, volumes, security groups, users and others. Heat管理這些組件的生命週期,當你需要對現在的部署做一些修改的時候,你只需要對template做一些修改即可。Heat還可以與其他軟件管理工具集成比如Puppet以及chef。


安裝Heat
選擇一個VM鏡像,可以選擇 http://cloud.fedoraproject.org/fedora-20.x86_64.qcow2F20,它包含了heat-cfntools包,當運行./stack.sh的時候Heat將會被加載到screen中前綴是h-. 假如需要使用Ceilometer Alarms功能你需要添加Ceilometer功能。需要做的是在devstack的localrc文件中添加如下:

  1. CEILOMETER_BACKEND=mysql
  2. enable_service ceilometer-acompute ceilometer-acentral ceilometer-collector ceilometer-api
  3. enable_service ceilometer-alarm-notifier ceilometer-alarm-evaluator
  4. #sudo apt-get install gitgit-review ctags
複製代碼

首先下載devstack
#git clone https://github.com/openstack-dev/devstack.git
然後準備localrc如下,靈活選擇你感興趣的項目,做減法處理。注意假如你的環境在proxy後面,而proxy又不支持git時,可以將stackrc中的GIT_BASE=${GIT_BASE:-git://git.openstack.org}改爲GIT_BASE=${GIT_BASE:-https://github.com}

  1. #The localrc is used to deploy a Neutron+OVS+heat+ceilometer+tempest development env  
  2. #OFFLINE True if no need to pull necessary packages again  
  3. #OFFLINE=True  
  4. #RECLONE True if all need a fresh repo environment  
  5. #RECLONE=True  
  6. ADMIN_PASSWORD=123  
  7. MYSQL_PASSWORD=123  
  8. RABBIT_PASSWORD=123  
  9. SERVICE_PASSWORD=123  
  10. SERVICE_TOKEN=123  
  11. Q_PLUGIN=openvswitch  
  12. disable_service n-net  
  13. #enable necessary network comps  
  14. ENABLED_SERVICES+=,neutron,q-svc,q-agt,q-dhcp,q-l3,q-meta  
  15. #enable advanced services  
  16. enable_service q-vpn q-lbaas q-fwaas  
  17. #enable tempest for learning tempest  
  18. enable_service tempest  
  19. #enable heat  
  20. enable_service heat h-api h-api-cfn h-api-cw h-eng  
  21. #enable ceilometer for Ceilometer Alarms  
  22. CEILOMETER_BACKEND=mysql  
  23. enable_service ceilometer-acompute ceilometer-acentral ceilometer-collector ceilometer-anotification  
  24. enable_service ceilometer-api  
  25. enable_service ceilometer-alarm-notifier ceilometer-alarm-evaluator  
  26.    
  27. HOST_IP=<Host-IP>  
  28. #VM images  
  29. IMAGE_URLS="http://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-i386-uec.tar.gz"  
  30. #IMAGE_URLS+=",http://uec-images.ubuntu.com/precise/current/precise-server-cloudimg-amd64-disk1.img"  
  31. IMAGE_URLS+=",http://cloud.fedoraproject.org/fedora-20.x86_64.qcow2"  
  32.    
  33. http_proxy=<http-proxy>  
  34. https_proxy=<https-proxy>  
  35. no_proxy=localhost,<Host-IP>  
  36.    
  37. #for IPSec VPNaaS  
  38. IPSEC_PACKAGE=openswan  
  39.    
  40. #LOG configure  
  41. SCREEN_LOGDIR=/opt/stack/screen-logs  
  42. SYSLOG=True  
  43. #DEST=/opt/stack/project  
複製代碼
一切準備就緒,最好先update&upgrade一下,然後運行./stack.sh 可以部署openstack環境了。
模板
https://github.com/openstack/heat-templates 提供一些templates參考例子來展示heat的一些核心功能。heat目前支持兩種模板: 與CloudFormatior兼容的cnf目錄下的模板以及自研的在hot目錄下的HOT模板。 HOT模板基於YAML來展示,下面僅研究HOT模板。

寫一個HOT hello world 模板
hello template file:

  1. heat_template_version: 2013-05-23  
  2.    
  3. description: Simple template to deploy a single compute instance  
  4.    
  5. resources:  
  6.     my_instance:  
  7.         type: OS::Nova::Server  
  8.         properties:  
  9.             key_name: heat_key  
  10.             image: cirros-0.3.0-i386-uec  
  11.             flavor: m1.tiny
複製代碼
“heat_template_version: 2013-05-23” 是必須制定的,標識當前heat的模板版本。Resources是必須的,其中一個resources內必須至少包含一個resource定義,在該例子中的key_name, image以及flavor都是hardcoded,該問題可以通過input parameters解決。
查找上面模板相關參數的CLI過程如下:
  1. #admin tenant  
  2. stack@vm:~/devstack$ . openrc admin admin  
  3. #create heat_key keypair  
  4. stack@vm:~$ nova keypair-add heat_key  
  5. #get available image name  
  6. stack@vm:~$ nova image-list  
  7. #get instance ttype  
  8. stack@vm:~$ nova flavor-list  
複製代碼
部署模板的CLI過程如下:
  1. #create stack hello-stack with helo HOT  
  2. stack@vm:~/hot-files$ heat stack-create -f ./hello hello-stack  
  3. #list stack status  
  4. stack@vm:~/hot-files$ heat stack-list  
  5. #show events status of hello-stack  
  6. stack@vm:~/hot-files$ heat event-list hello-stack  
  7. #show status of hello-stack  
  8. stack@vm:~/hot-files$ heat stack-show hello-stack  
複製代碼
去硬編碼,使模板更加靈活,需要加入parameters屬性,這樣調用模板時,可以輸入相關參數,hello帶input的模板如下:
  1. heat_template_version: 2013-05-23  
  2.    
  3. description: Simple template to deploy a single compute instance  
  4.    
  5. parameters:  
  6.     key_name:  
  7.         type: string  
  8.         label: Key Name  
  9.         description: Name of key-pair to be used for compute instance  
  10.     image_name:  
  11.         type: string     
  12.         label: Image Name  
  13.         description: Image to be used for compute instance  
  14.     instance_type:  
  15.         type: string  
  16.         label: Instance Type  
  17.         description: Type of instance (flavor) to be used  
  18.    
  19. outputs:  
  20.     instance_ip:  
  21.         description: the ip addresss of the deployed instance_type  
  22.         value: {get_attr: [my_instance, first_address]}  
  23.    
  24. resources:  
  25.     my_instance:  
  26.         type: OS::Nova::Server  
  27.         properties:  
  28.             key_name: { get_param: key_name }  
  29.             image: { get_param: image_name }  
  30.             flavor: { get_param: instance_type }  
複製代碼
執行如下:
  1. stack@vm:~/hot-files$ heat stack-create -f ./hello_input -P "key_name=heat_key;instance_type=m1.tiny;image_name=cirros-0.3.0-i386-uec" stack-vm2

  2. stack@vm:~/hot-files$ heat stack-list
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章