天天看點

Heat 支援查詢 Autoscaling Group 虛拟機清單

引言

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。      
Heat 支援查詢 Autoscaling Group 虛拟機清單
查詢 Nova 發現 Autoscaling Group 旗下有兩台虛拟機。
Heat 支援查詢 Autoscaling Group 虛拟機清單
繼續查詢 asg 詳情,并無任何和虛拟機相關的資訊,而事實上,Autoscaling Group 作為一群虛拟機的集合,使用者非常希望能擷取 Autoscaling Group 下虛拟機的數量,虛拟機的m名稱等等。如果 Heat 未提供該 API,那麼使用者隻能通過 Nova API 查詢相關的虛拟機,這對使用者來說,無疑增加了操作的複雜程度。
Heat 支援查詢 Autoscaling Group 虛拟機清單

二.關于 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。
Heat 支援查詢 Autoscaling Group 虛拟機清單
這個四個 stack 的關系如下圖所示
Heat 支援查詢 Autoscaling Group 虛拟機清單
Heat 支援查詢 Autoscaling Group 虛拟機清單
Heat 支援查詢 Autoscaling Group 虛拟機清單
Heat 支援查詢 Autoscaling Group 虛拟機清單
看,熟悉的虛拟機現身了。從上不難發現另外一條規律,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 資源時,可傳回其旗下的虛拟機清單。如下

        
Heat 支援查詢 Autoscaling Group 虛拟機清單
核心代碼為
Heat 支援查詢 Autoscaling Group 虛拟機清單
完整的代碼請見: https://github.com/DeliangFan/heat/commit/63d35793c47784b4ff0e980a0148eaf96139c853

繼續閱讀