引言
OpenStack Mailing List 中有這麼一份郵件,內容如下:
[Openstack] heat autoscaling group instance relationships
I'm trying to figure out how to determine all instances that were created as part of a given autoscaling group. I want to take a given autoscalinggroup and list all of its instances. So far I can't figure out how to dothis. The instances themselves have a tag called "AutoScalingGroupName"( mystack-MyServerGroup-f3r72ifsj2jq for example ) but the value of thatdoesn't seem to map to anything. A resource-show on the autoscaling groupdoesn't seem to show any identifier that maps to the autoscaling groupname.
Any ideas on how I can do this? Thanks!
該郵件大體的意思是,如何通過 Heat API 獲取 Autoscaling Group 裏的虛擬機列表。
一.需求
以 Heat 推薦的 Autoscaling Group 的 HOT 模板爲例子,採用該模板創建 Stack 後,查詢該 Stack 包含的 resource 如下,可知 asg 即爲 AutoScalingGroup resource。
查詢 Nova 發現 Autoscaling Group 旗下有兩臺虛擬機。繼續查詢 asg 詳情,並無任何和虛擬機相關的信息,而事實上,Autoscaling Group 作爲一羣虛擬機的集合,用戶非常希望能獲取 Autoscaling Group 下虛擬機的數量,虛擬機的m名稱等等。如果 Heat 未提供該 API,那麼用戶只能通過 Nova API 查詢相關的虛擬機,這對用戶來說,無疑增加了操作的複雜程度。
二.關於 nested_stack
在講解 nested_stack 之前,先問讀者一個問題:請問剛剛一共創建了多少個 stack?
一個!明明只有一個嘛!它的名字就叫 fuck !......貌似言之有理,再查詢數據庫看看,奇葩出現了,咋跑出了四個 stack,而且四個 stack 的名字均以 fuck 開頭,有着幾分相似和某些規律。再看看 owner_id 和 id 的關係:1. stack-list 查詢到的 stack, 其 owner_id 均未 NULL。
2. nested_stack 的 owner_id 爲父 stack 的 id。
這個四個 stack 的關係如下圖所示
看,熟悉的虛擬機現身了。從上不難發現另外一條規律,nested_stack 的 id 和父 stack 的某個 resource id 完全一致。事實上,Heat 有以下資源支持 nested_stack:
OS::Heat::InstanceGroup
OS::Heat::ResourceGroup
OS::Heat::AutoScalingGroup
AWS::AutoScaling::AutoScalingGroup。
對於初步瞭解 heat 的同學來說,nested_stack 比較陌生晦澀,更多的資料請移步wiki:https://wiki.openstack.org/wiki/Heat/NestedStacks。
三. 解決方案
我們可以按照以上方法查詢 Autoscaling Group 下得虛擬機信息,但是頻繁的 CLI 查詢操作繁瑣、效率低下,用戶體驗極差。最好的方式是查詢 Autoscaling Group 資源時,可返回其旗下的虛擬機列表。如下
核心代碼爲
完整的代碼請見:
https://github.com/DeliangFan/heat/commit/63d35793c47784b4ff0e980a0148eaf96139c853