一.什么是yarn
Yarn是一個資源調度平台,負責為運算程序提供服務器運算資源,相當於一個分布式的操作系統平台,而MapReduce等運算程序則相當於運行於操作系統之上的應用程序。
二.yarn的基本架構和角色
yarn主要由ResourceManager、NodeManager、ApplicationMaster和Container等組件構成。
三.yarn的工作機制
(1)MR程序提交到客戶端所在的節點。
(2)YarnRunner向ResourceManager申請一個Application。
(3)RM將該應用程序的資源路徑返回給YarnRunner。
(4)該程序將運行所需資源提交到HDFS上。
(5)程序資源提交完畢后,申請運行mrAppMaster。
(6)RM將用戶的請求初始化成一個Task。
(7)其中一個NodeManager領取到Task任務。
(8)該NodeManager創建容器Container,並產生MRAppmaster。
(9)Container從HDFS上拷貝資源到本地。
(10)MRAppmaster向RM 申請運行MapTask資源。
(11)RM將運行MapTask任務分配給另外兩個NodeManager,另兩個NodeManager分別領取任務並創建容器。
(12)MR向兩個接收到任務的NodeManager發送程序啟動腳本,這兩個NodeManager分別啟動MapTask,MapTask對數據分區排序。
(13)MrAppMaster等待所有MapTask運行完畢后,向RM申請容器,運行ReduceTask。
(14)ReduceTask向MapTask獲取相應分區的數據。
(15)程序運行完畢后,MR會向RM申請注銷自己。
四.任務提交流程
作業提交全過程詳解
(1):作業提交
第1步:Client調用job.waitForCompletion方法,向整個集群提交MapReduce作業。
第2步:Client向RM申請一個作業id。
第3步:RM給Client返回該job資源的提交路徑和作業id。
第4步:Client提交jar包、切片信息和配置文件到指定的資源提交路徑。
第5步:Client提交完資源后,向RM申請運行MrAppMaster。
(2)作業初始化
第6步:當RM收到Client的請求后,將該job添加到容量調度器中。
第7步:某一個空閑的NM領取到該Job。
第8步:該NM創建Container,並產生MRAppmaster。
第9步:下載Client提交的資源到本地。
(3):任務分配
第10步:MrAppMaster向RM申請運行多個MapTask任務資源。
第11步:RM將運行MapTask任務分配給另外兩個NodeManager,另兩個NodeManager分別領取任務並創建容器。
(4):任務運行
第12步:MR向兩個接收到任務的NodeManager發送程序啟動腳本,這兩個NodeManager分別啟動MapTask,MapTask對數據分區排序。
第13步:MrAppMaster等待所有MapTask運行完畢后,向RM申請容器,運行ReduceTask。
第14步:ReduceTask向MapTask獲取相應分區的數據。
第15步:程序運行完畢后,MR會向RM申請注銷自己。
(5):進度和狀態更新
YARN中的任務將其進度和狀態(包括counter)返回給應用管理器, 客戶端每秒(通過mapreduce.client.progressmonitor.pollinterval設置)向應用管理器請求進度更新, 展示給用戶。
(6):作業完成
除了向應用管理器請求作業進度外, 客戶端每5秒都會通過調用waitForCompletion()來檢查作業是否完成。時間間隔可以通過mapreduce.client.completion.pollinterval來設置。作業完成之后, 應用管理器和Container會清理工作狀態。作業的信息會被作業歷史服務器存儲以備之后用戶核查。
五.資源調度器
Hadoop作業調度器主要有三種:FIFO、Capacity Scheduler和Fair Scheduler。Hadoop3.1.3默認的資源調度器是Capacity Scheduler。
具體設置詳見:yarn-default.xml文件
<property>
<description>The class to use as the resource scheduler.</description>
<name>yarn.resourcemanager.scheduler.class</name>
<value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler</value>
</property>
FIFO
先進先出,同一時間隊列中只有一個任務在執行
容量調度器
多隊列;每個隊列內部先進先出, 同一時間隊列中只有一個任務在執行, 隊列的並行度為隊列的個數。
公平調度器
多隊列;每個隊列內部按照缺額大小分配資源啟動任務,同一時間隊列中有多個任務執行。隊列的並行度大於等於隊列的個數
六.容量調度器多隊列提交案例實操
需求
Yarn默認調度器為Capacity Scheduler(容量調度器),且默認只有一個隊列——default。該調度器單個隊列內的調度策略為FIFO,故單個隊列的任務並行度為1,即一個隊列,同一時刻,只能有一個任務運行。
在實際使用中會出現單個任務阻塞整個隊列的情況。同時,隨着業務的增長,公司需要分業務限制集群使用率。這就需要我們按照業務種類配置多條任務隊列。
1.案例:配置default、hive多隊列
默認Yarn的配置下,容量調度器只有一條Default隊列。在$HADOOP_HOME/etc/hadoop/capacity-schdualer.xml中可以配置多條隊列,並降低default隊列資源占比:
①增加隊列,添加隊列的屬性配置
<!--
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->
<configuration>
<!-- 容量調度器最多可以容納多少個job-->
<property>
<name>yarn.scheduler.capacity.maximum-applications</name>
<value>10000</value>
<description>
Maximum number of applications that can be pending and running.
</description>
</property>
<!-- 當前隊列中啟動的MRAppMaster進程,所占用的資源可以達到隊列總資源的多少
通過這個參數可以限制隊列中提交的Job數量
-->
<property>
<name>yarn.scheduler.capacity.maximum-am-resource-percent</name>
<value>0.1</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>
<!-- 為Job分配資源時,使用什么策略進行計算
-->
<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>
<!-- root隊列中有哪些子隊列-->
<property>
<name>yarn.scheduler.capacity.root.queues</name>
<value>default,hive</value>
<description>
The queues at the this level (root is the root queue).
</description>
</property>
<!-- root隊列中default隊列占用的容量百分比
所有子隊列的容量相加必須等於100
-->
<property>
<name>yarn.scheduler.capacity.root.default.capacity</name>
<value>30</value>
<description>Default queue target capacity.</description>
</property>
<property>
<name>yarn.scheduler.capacity.root.hive.capacity</name>
<value>70</value>
<description>Default queue target capacity.</description>
</property>
<!-- 隊列中用戶能使用此隊列資源的極限百分比
-->
<property>
<name>yarn.scheduler.capacity.root.default.user-limit-factor</name>
<value>1</value>
<description>
Default queue user limit a percentage from 0.0 to 1.0.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.root.hive.user-limit-factor</name>
<value>1</value>
<description>
Default queue user limit a percentage from 0.0 to 1.0.
</description>
</property>
<!-- root隊列中default隊列占用的容量百分比的最大值
-->
<property>
<name>yarn.scheduler.capacity.root.default.maximum-capacity</name>
<value>100</value>
<description>
The maximum capacity of the default queue.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.root.hive.maximum-capacity</name>
<value>100</value>
<description>
The maximum capacity of the default queue.
</description>
</property>
<!-- root隊列中每個隊列的狀態
-->
<property>
<name>yarn.scheduler.capacity.root.default.state</name>
<value>RUNNING</value>
<description>
The state of the default queue. State can be one of RUNNING or STOPPED.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.root.hive.state</name>
<value>RUNNING</value>
<description>
The state of the default queue. State can be one of RUNNING or STOPPED.
</description>
</property>
<!-- 限制向default隊列提交的用戶-->
<property>
<name>yarn.scheduler.capacity.root.default.acl_submit_applications</name>
<value>*</value>
<description>
The ACL of who can submit jobs to the default queue.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.root.hive.acl_submit_applications</name>
<value>*</value>
<description>
The ACL of who can submit jobs to the default queue.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.root.default.acl_administer_queue</name>
<value>*</value>
<description>
The ACL of who can administer jobs on the default queue.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.root.hive.acl_administer_queue</name>
<value>*</value>
<description>
The ACL of who can administer jobs on the default queue.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.node-locality-delay</name>
<value>40</value>
<description>
Number of missed scheduling opportunities after which the CapacityScheduler
attempts to schedule rack-local containers.
Typically this should be set to number of nodes in the cluster, By default is setting
approximately number of nodes in one rack which is 40.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.queue-mappings</name>
<value></value>
<description>
A list of mappings that will be used to assign jobs to queues
The syntax for this list is [u|g]:[name]:[queue_name][,next mapping]*
Typically this list will be used to map users to queues,
for example, u:%user:%user maps all users to queues with the same name
as the user.
</description>
</property>
<property>
<name>yarn.scheduler.capacity.queue-mappings-override.enable</name>
<value>false</value>
<description>
If a queue mapping is present, will it override the value specified
by the user? This can be used by administrators to place jobs in queues
that are different than the one specified by the user.
The default is false.
</description>
</property>
</configuration>
②分發配置文件到集群,重啟Yarn
③測試,向default ,hive隊列分別提交任務
提交到default隊列:
hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar pi -Dmapreduce.job.queuename=hive 1 1
提交到hive隊列
hadoop jar /opt/module/hadoop-3.1.3/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar pi -Dmapreduce.job.queuename=hive 1 1
查看yarn任務ui界面:http://hadoop103:8088/cluster/scheduler
提交就可以查看了,不然任務跑完就看不到了....
2.配置Hive的默認提交隊列
方式一
當前hive的shell窗口有效,關閉就不行了
SET mapreduce.job.queuename=hive;
SET mapreduce.job.priority=HIGH;
方式二
修改配置文件,永久生效
<!--配置hive默認的提交隊列-->
<property>
<name>mapreduce.job.queuename</name>
<value>hive</value>
</property>