轉自:https://m.aliyun.com/yunqi/articles/79700
背景
使用過hadoop的人基本都會考慮集群里面資源的調度和優先級的問題,假設你現在所在的公司有一個大hadoop的集群,有很多不同的業務組同時使用。但是A項目組經常做一些定時的BI報表,B項目組則經常使用一些軟件做一些臨時需求。那么他們肯定會遇到同時提交任務的場景,這個時候到底如何分配資源滿足這兩個任務呢?是先執行A的任務,再執行B的任務,還是同時跑兩個?
目前一些使用EMR的大公司,會使用一個比較大的集群,來共公司內部不同業務組的人共同使用,相對於使用多個小集群,使用大的集群一方面可以達到更高的最大性能,根據不同業務的峰谷情況來調度從而獲得更高的資源利用里,降低總成本。另外一個方面,能夠更好地在不同的業務組之間實現資源的共享和數據的流動。下面結合EMR集群,介紹一下如何進行大集群的資源quota管控。
yarn默認提供了兩種調度規則,capacity scheduler和fair scheduler。現在使用比較多的是capacity scheduler。具體的實現原理和調度源碼可以google一下capacity scheduler。
什么是capacity調度器
Capacity調度器說的通俗點,可以理解成一個個的資源隊列。這個資源隊列是用戶自己去分配的。比如我大體上把整個集群分成了queue1queue2兩個隊列,queue1是給一個業務組使用queue2給另外一個業務組使用。如果第一個業務組下面又有兩個方向,那么還可以繼續分,比如專門做BI的和做實時分析的。那么隊列的分配就可以參考下面的樹形結構:
root
------default[20%] ------q1[60%] |---q1.q11[70%] |---q1.q12[30%] ------q2[20%]
整個集群的queue必須掛在root下面。分成三個queue:default,q1和q2。三個隊列使用集群資源的quota配比為:20%,60%,20%。default這個queue是必須要存在的。
雖然有了這樣的資源分配,但是並不是說提交任務到q2里面,它就只能使用20%的資源,即使剩下的80%都空閑着。它也是能夠實現(通過配置),只要資源實在空閑狀態,那么q2就可以使用100%的資源。但是一旦其它隊列提交了任務,q2就需要在釋放資源后,把資源還給其它隊列,直到達到預設de配比值。粗粒度上資源是按照上面的方式進行,在每個隊列的內部,還是按照FIFO的原則來分配資源的。
capacity調度器特性
capacity調度器具有以下的幾個特性:
- 層次化的隊列設計,這種層次化的隊列設計保證了子隊列可以使用父隊列設置的全部資源。這樣通過層次化的管理,更容易合理分配和限制資源的使用。
容量保證,隊列上都會設置一個資源的占比,這樣可以保證每個隊列都不會占用整個集群的資源。 - 安全,每個隊列又嚴格的訪問控制。用戶只能向自己的隊列里面提交任務,而且不能修改或者訪問其他隊列的任務。
- 彈性分配,空閑的資源可以被分配給任何隊列。當多個隊列出現爭用的時候,則會按照比例進行平衡。
- 多租戶租用,通過隊列的容量限制,多個用戶就可以共享同一個集群,同事保證每個隊列分配到自己的容量,提高利用率。
- 操作性,yarn支持動態修改調整容量、權限等的分配,可以在運行時直接修改。還提供給管理員界面,來顯示當前的隊列狀況。管理員可以在運行時,添加一個隊列;但是不能刪除一個隊列。管理員還可以在運行時暫停某個隊列,這樣可以保證當前的隊列在執行過程中,集群不會接收其他的任務。如果一個隊列被設置成了stopped,那么就不能向他或者子隊列上提交任務了。
capacity調度器的配置
登錄EMR集群master節點,編輯配置文件:/etc/emr/hadoop-conf/capacity-scheduler.xml
這里先貼出來完整的比較復雜一點的配置,后面再詳細說明:
<?xml version="1.0" encoding="utf-8"?> <configuration> <property> <name>yarn.scheduler.capacity.maximum-applications</name> <value>10000</value> <description>Maximum number of applications that can be pending and running.</description> </property> <property> <name>yarn.scheduler.capacity.maximum-am-resource-percent</name> <value>0.25</value> <description>Maximum percent of resources in the cluster which can be used to run application masters i.e. controls number of concurrent running applications.</description> </property> <property> <name>yarn.scheduler.capacity.resource-calculator</name> <value>org.apache.hadoop.yarn.util.resource.DefaultResourceCalculator</value> <description>The ResourceCalculator implementation to be used to compare Resources in the scheduler. The default i.e. DefaultResourceCalculator only uses Memory while DominantResourceCalculator uses dominant-resource to compare multi-dimensional resources such as Memory, CPU etc.</description> </property> <property> <name>yarn.scheduler.capacity.root.queues</name> <value>default,q1,q2</value> <description>The queues at the this level (root is the root queue).</description> </property> <property> <name>yarn.scheduler.capacity.root.q1.queues</name> <value>q11,q12</value> <description>The queues at the this level (root is the root queue).</description> </property> <property> <name>yarn.scheduler.capacity.root.default.capacity</name> <value>20</value> <description>Default queue target capacity.</description> </property> <property> <name>yarn.scheduler.capacity.root.q1.capacity</name> <value>60</value> <description>Default queue target capacity.</description> </property> <property> <name>yarn.scheduler.capacity.root.q2.capacity</name> <value>20</value> <description>Default queue target capacity.</description> </property> <property> <name>yarn.scheduler.capacity.root.q1.q11.capacity</name> <value>70</value> <description>Default queue target capacity.</description> </property> <property> <name>yarn.scheduler.capacity.root.q1.q11.maximum-capacity</name> <value>90</value> <description>Default queue target capacity.</description> </property> <property> <name>yarn.scheduler.capacity.root.q1.q11.minimum-user-limit-percent</name> <value>25</value> <description>Default queue target capacity.</description> </property> <property> <name>yarn.scheduler.capacity.root.q1.q12.capacity</name> <value>30</value> <description>Default queue target capacity.</description> </property> <property> <name>yarn.scheduler.capacity.root.q1.q12.user-limit-factor</name> <value>0.7</value> <description>Default queue target capacity.</description> </property> <property> <name>yarn.scheduler.capacity.root.q2.user-limit-factor</name> <value>0.4</value> <description>Default queue user limit a percentage from 0.0 to 1.0.</description> </property> <property> <name>yarn.scheduler.capacity.root.q2.maximum-capacity</name> <value>100</value> <description>The maximum capacity of the default queue.</description> </property>