8, Yarn system architecture and principles of resource scheduling analysis

@ [TOC]

1, Yarn Introduction

Here Insert Picture Description
   Apache Hadoop YARN apache Software Foundation Hadoop is a sub for resource management and calculation Hadoop2.0 separate component is introduced. YARN due to the birth of the data stored in HDFS need more interactive mode, not just MapReduce model. Hadoop2.0 of YARN architecture provides more processing frameworks, such as spark frame, no longer forced to use MapReduce framework.
   As it can be seen from hadoop2.0 architecture diagram, YARN bear originally undertaken by the MapReduce resource management functions, while these functional part of the package so that they can be used by the new data processing engine. It also simplifies the process of MapReduce, such MapReduce dedicated to processing data to be the best. Use YARN, you can use a common resource management, run a lot of applications on Hadoop. Currently, many organizations have developed YARN-based applications.

2, Yarn architecture

   YARN classical architecture, or from the master (master / slave) configuration, as shown below. See generally, YARN served by a ResourceManager (RM) and a plurality of NodeManager (NM) configuration, ResourceManager master node (master), NodeManager from node (slave)
Here Insert Picture Description
   in YARN architecture, operating as the primary global ResourceManager daemon the arbitration proceedings between the various competing applications cluster quorum resource available. ResourceManager track the number of active nodes and resources available on the cluster, and to coordinate the application submitted by users should get time and time those resources. ResourceManager single process with this information, it can be shared, multi-tenant security and partitioned manner (or rather the scheduling) decision (e.g., based on application priority, queue capacity, ACL, the data location ).
   When the user submits the application to start an instance called ApplicationMaster lightweight process to coordinate the application of all tasks executed. This includes monitoring tasks, restart a failed task, the total value of speculatively slow-running tasks, and computing applications counter. These responsibilities previously assigned to a single JobTracker all work. ApplicationMaster and tasks within their applications running in the resource container NodeManagers control.
   ApplicationMaster can run any type of task in the container. For example, MapReduce ApplicationMaster map or reduce the request to start the task container, and the container Giraph ApplicationMaster run Giraph task request. Create a new distributed application framework shiny You can also run specific tasks implement custom ApplicationMaster, and in this way, the framework can change the big data world. I encourage you to read the Apache Twill, it is designed to simplify writing distributed applications located on top of YARN.
   A distributed application can run any cluster ResourceManager, NodeManager container and the type of application or task does not care. All codes in a particular application frameworks are simply moved to its ApplicationMaster, so YARN distributed can support any frame, as long as it was for it to achieve an appropriate ApplicationMaster. Because of this general method, clusters running Hadoop YARN many different workloads dream come true. Imagine: a single Hadoop cluster in the data center can run MapReduce, Giraph, Storm, Spark, Tez / Impala, MPI and so on.

Core components:
Component Name effect
ResourceManager Equivalent to the Application of guardians and managers, responsible for monitoring and managing all this specific Attempt Application running on each node in the cluster, is also responsible to YarnResourceManager application resources, return of resources;
ApplicationMaster Equivalent to the Application of guardians and managers, responsible for monitoring and managing all this specific Attempt Application running on each node in the cluster, is also responsible to YarnResourceManager application resources, return of resources;
NodeManager Slave is a process on a stand-alone, is responsible for reporting the status of the node (disk, memory, cpu usage information, etc.);
Container Is a unit of resource allocation in the yarn, bear memory, CPU resources, etc., YARN allocate resources in units of Container;

2.1 、ResourceManager

   RM是一个全局的资源管理器,集群只有一个,负责整个系统的资源管理和分配,包括处理客户端请求、启动/监控ApplicationMaster、监控 NodeManager、资源的分配与调度。它主要由两个组件构成:调度器(Scheduler)和应用程序管理器(Applications Manager,ASM)。
   (1)调度器
   调度器根据容量、队列等限制条件(如每个队列分配一定的资源,最多执行一定数量的作业等),将系统中的资源分配给各个正在运行的应用程序。需要注意的是,该调度器是一个“纯调度器”,它从事任何与具体应用程序相关的工作,比如不负责监控或者跟踪应用的执行状态等,也不负责重新启动因应用执行失败或者硬件故障而产生的失败任务,这些均交由应用程序相关的ApplicationMaster完成。
   调度器仅根据各个应用程序的资源需求进行资源分配,而资源分配单位用一个抽象概念“资源容器”(ResourceContainer,简称Container)表示,Container是一个动态资源分配单位,它将内存、CPU、磁盘、网络等资源封装在一起,从而限定每个任务使用的资源量。
   (2)应用程序管理器
应用程序管理器主要负责管理整个系统中所有应用程序,接收job的提交请求,为应用分配第一个 Container 来运
行 ApplicationMaster,包括应用程序提交、与调度器协商资源以启动 ApplicationMaster、监控
ApplicationMaster 运行状态并在失败时重新启动它等。

2.2 、ApplicationMaster

   管理 YARN 内运行的一个应用程序的每个实例。关于 job 或应用的管理都是由 ApplicationMaster 进程负责的,Yarn 允许我们以为自己的应用开发 ApplicationMaster。
   功能:
   (1)数据切分;
   (2)为应用程序申请资源并进一步分配给内部任务(TASK);
   (3)任务监控与容错;
   负责协调来自ResourceManager的资源,并通过NodeManager监视容易的执行和资源使用情况。Yarn 的动态性,就是来源于多个Application 的ApplicationMaster 动态地和 ResourceManager 进行沟通,不断地申请、释放、再申请、再释放资源的过程。

2.3 、NodeManager

   NodeManager 整个集群有多个,负责每个节点上的资源和使用。
   NodeManager 是一个 slave 服务:它负责接收 ResourceManager 的资源分配请求,分配具体的 Container 给应用。同时,它还负责监控并报告 Container 使用信息给 ResourceManager。通过和ResourceManager 配合,NodeManager 负责整个 Hadoop 集群中的资源分配工作。
   功能:本节点上的资源使用情况和各个 Container 的运行状态(cpu和内存等资源)
   (1)接收及处理来自 ResourceManager 的命令请求,分配 Container 给应用的某个任务;
   (2)定时地向RM汇报以确保整个集群平稳运行,RM 通过收集每个 NodeManager 的报告信息来追踪整个集群健康状态的,而 NodeManager 负责监控自身的健康状态;
   (3)处理来自 ApplicationMaster 的请求;
   (4)管理着所在节点每个 Container 的生命周期;
   (5)管理每个节点上的日志;
   (6)执行 Yarn 上面应用的一些额外的服务,比如 MapReduce 的 shuffle 过程;
   当一个节点启动时,它会向 ResourceManager 进行注册并告知 ResourceManager 自己有多少资源可用。在运行期,通过 NodeManager 和 ResourceManager 协同工作,这些信息会不断被更新并保障整个集群发挥出最佳状态。NodeManager 只负责管理自身的 Container,它并不知道运行在它上面应用的信息。负责管理应用信息的组件是ApplicationMaster

2.4 、Container

   Container 是 YARN 中的资源抽象,它封装了某个节点上的多维度资源,如内存、CPU、磁盘、网络等,当 AM 向RM 申请资源时,RM 为 AM 返回的资源便是用 Container 表示的。YARN 会为每个任务分配一个 Container,且任务只能使用该 Container 中描述的资源。
   Container 和集群节点的关系是:一个节点会运行多个 Container,但一个 Container 不会跨节点。任何一个 job或 application 必须运行在一个或多个 Container 中,在 Yarn 框架中,ResourceManager 只负责告诉ApplicationMaster 哪些 Containers 可以用,ApplicationMaster 还需要去找 NodeManager 请求分配具体的Container。
   需要注意的是,Container 是一个动态资源划分单位,是根据应用程序的需求动态生成的。目前为止,YARN 仅支持 CPU 和内存两种资源,且使用了轻量级资源隔离机制 Cgroups 进行资源隔离。

2.5 、Resource Request 及 Container

   Yarn的设计目标就是允许我们的各种应用以共享、安全、多租户的形式使用整个集群。并且,为了保证集群资源调度和数据访问的高效性,Yarn还必须能够感知整个集群拓扑结构。
   为了实现这些目标,ResourceManager的调度器Scheduler为应用程序的资源请求定义了一些灵活的协议,通过它就可以对运行在集群中的各个应用做更好的调度,因此,这就诞生了Resource Request和Container。一个应用先向ApplicationMaster发送一个满足自己需求的资源请求,然后ApplicationMaster把这个资源请求以
   resource-request的形式发送给ResourceManager的Scheduler,Scheduler再在这个原始的resource-request中返回分配到的资源描述Container。每个ResourceRequest可看做一个可序列化Java对象,包含的字段信息如下:

<!--
- resource-name:资源名称,现阶段指的是资源所在的host和rack,后期可能还会支持虚拟机或者更复杂的网络结构
- priority:资源的优先级
- resource-requirement:资源的具体需求,现阶段指内存和cpu需求的数量
- number-of-containers:满足需求的Container的集合
-->
<resource-name, priority, resource-requirement, number-of-containers>

2.6 、JobHistoryServer

   作业历史服务,记录在yarn中调度的作业历史运行情况情况 , 通过mr-jobhistory-daemon.sh start historyserver命令在集群中的数据节点机器上不需要做任何配置,单独使用命令启动直接启动即可, 启动成功后会出现JobHistoryServer进程(使用jps命令查看,下面会有介绍) , 并且可以从19888端口进行查看日志详细信息
Here Insert Picture Description
打开如下图界面,在下图中点击History,页面会进行一次跳转
Here Insert Picture Description
点击History之后 跳转后的页面如下图,是空白的,这时因为我们没有启动jobhistoryserver所导致的。 在三台机器上执行mr-jobhistory-daemon.sh start historyserver命令依次启动jobhistoryserver。在node1节点启动
Here Insert Picture Description
此时我们在三个节点把jobhistoryserver启动后,在此运行wordcount程序(记得启动前把输出目录删除掉)
点击History连接会跳转一个赞新的页面,在页面下方会看到TaskType中列举的map和reduce,Total表示此次运行的mapreduce程序执行所需要的map和reduce的任务数据.

2.7、Timeline Server

   用来写日志服务数据 , 一般来写与第三方结合的日志服务数据(比如spark等),从官网的介绍看,它是对jobhistoryserver功能的有效补充,jobhistoryserver只能对mapreduce类型的作业信息进行记录,除了jobhistoryserver能够进行对作业运行过程中信息进行记录之外还有更细粒度的信息记录,比如任务在哪个队列中运行,运行任务时设置的用户是哪个用户。根据官网的解释jobhistoryserver只能记录mapreduce应用程序的记录,timelineserver功能更强大,但不是替代jobhistory两者是功能间的互补关系。

3、yarn应用运行原理

   YARN 是如何工作的? YARN的基本理念是将JobTracker/TaskTracker 两大职能分割为以下几个实体:
1.一个全局的资源管理ResourceManager

  1. 每个应用程序一个ApplicationMaster
  2. 每个从节点一个NodeManager
  3. 每个应用程序一个运行在NodeManager上的Container
       ResouceManager 和 NodeManager 组成了一个新的、通用的、用分布式管理应用程序的系统。ResourceManager 对系统中的应用程序资源有终极仲裁的权限。ApplicationMaster 是一个特定于框架的实体,它的责任是同ResourceManager 谈判资源 ,同时为NodeManager(s)执行和监控组件任务。RessourceManager有一个调度器,根据不同的约束条件,例如队列容量、用户限制等,将资源进行分配给各类运行着的应用程序。调度器执行调度功能是基于应用程序的资源申请。NodeManager 负责发布应用程序容器,监控资源的使用并向ResourceManager进行汇报。每个ApplicationMaster都有职责从调度器那谈判得到适当的资源容器,追踪它们的状态,并监控他们的进程。从系统的视图看,ApplicationMaster 作为一个普通的容器运行着。Here Insert Picture Description

3.1、yarn应用提交过程

   Application在Yarn中的执行过程,整个执行过程可以总结为三步:
   (1)应用程序提交
   (2)启动应用的ApplicationMaster实例
   (3)ApplicationMaster 实例管理应用程序的执行
具体过程:
   (1)客户端程序向 ResourceManager 提交应用并请求一个 ApplicationMaster 实例;
(2)ResourceManager 找到一个可以运行一个 Container 的 NodeManager,并在这个 Container 中启动ApplicationMaster 实例;
   (3)ApplicationMaster 向 ResourceManager 进行注册,注册之后客户端就可以查询 ResourceManager 获得自己 ApplicationMaster 的详细信息,以后就可以和自己的 ApplicationMaster 直接交互了(这个时候,客户端主动和 ApplicationMaster 交流,应用先向 ApplicationMaster 发送一个满足自己需求的资源请求);
   (4)在平常的操作过程中,ApplicationMaster 根据 resource-request协议 向 ResourceManager 发送 resource-request请求;
   (5)当 Container 被成功分配后,ApplicationMaster 通过向 NodeManager 发送 container-launch-specification信息 来启动Container,container-launch-specification信息包含了能够让Container 和ApplicationMaster 交流所需要的资料;
   (6)应用程序的代码以 task 形式在启动的 Container 中运行,并把运行的进度、状态等信息通过 application-specific协议 发送给ApplicationMaster;
   (7)在应用程序运行期间,提交应用的客户端主动和 ApplicationMaster 交流获得应用的运行状态、进度更新等信息,交流协议也是 application-specific协议;
   (8)一旦应用程序执行完成并且所有相关工作也已经完成,ApplicationMaster 向 ResourceManager 取消注册然后关闭,用到所有的 Container 也归还给系统。

3.2、mapreduce on yarn

   (1)客户端提交hadoop jar
   (2)找到main()方法中的job.waitForCompletition生成job对象,运行job对象的runjob()方法,与ResourceManager通信,
返回ResourceManager分配一个ID号(applicationid),需不需要输出,如果需要输出,判断输出是否存在,如果不存在问题,在看输入,根据hdfs得到输入得到分片信息,根据分片信息得到map个数,将这些信息回传给客户端Job
   (3)把Job所需要的资源,例如Jar包,配置文件,split分片信息,上传到hdfs中去
   (4)Job对象告知与ResourceManager提交应用
   (5)ResourceManager寻找合适的节点开启container容器(本质上是一个JVM虚拟机)
   (6)在container中启动一个applicationMaster,初始化Job,生成一个薄记对象(就是一个记事本),记录map、reduce的状态。把job所有资源上传到hdfs中,包括jar包,配置文件,split信息
   (7)applicationMaster向ResourceManager申请资源,返回资源信息(包括node节点地址,cpu信息,内存占比,IO信息)
   (8)applicationMaster收到信息之后,和NodeManager通信,传递资源信息
   (9)开启YarnChild进程,从hdfs中获得Job详细信息(包括Jar包,配置文件信息)
Here Insert Picture Description
   (一)Job初始化:1、当resourceManager收到了submitApplication()方法的调用通知后,scheduler开始分配container,随之ResouceManager发送applicationMaster进程,告知每个nodeManager管理器。 2、由applicationMaster决定如何运行tasks,如果job数据量比较小,applicationMaster便选择将tasks运行在一个JVM中。那么如何判别这个job是大是小呢?当一个job的mappers数量小于10个,只有一个reducer或者读取的文件大小要小于一个HDFS block时,(可通过修改配置项mapreduce.job.ubertask.maxmaps,mapreduce.job.ubertask.maxreduces以及mapreduce.job.ubertask.maxbytes 进行调整) 3、在运行tasks之前,applicationMaster将会调用setupJob()方法,随之创建output的输出路径(这就能够解释,不管你的mapreduce一开始是否报错,输出路径都会创建)。
   (二)Task 任务分配:1、接下来applicationMaster向ResourceManager请求containers用于执行map与reduce的tasks(step 8),这里map task的优先级要高于reduce task,当所有的map tasks结束后,随之进行sort(这里是shuffle过程后面再说),最后进行reduce task的开始。(这里有一点,当map tasks执行了百分之5%的时候,将会请求reduce,具体下面再总结) 2、运行tasks的是需要消耗内存与CPU资源的,默认情况下,map和reduce的task资源分配为1024MB与一个核,(可修改运行的最小与最大参数配置,mapreduce.map.memory.mb,mapreduce.reduce.memory.mb,mapreduce.map.cpu.vcores,mapreduce.reduce.reduce.cpu.vcores.)
   (三)运行进度与状态更新 1、MapReduce是一个较长运行时间的批处理过程,可以是一小时、
几小时甚至几天,那么Job的运行状态监控就非常重要。每个job以及每个task都有一个包含
job(running,successfully completed,failed)的状态,以及value的计数器,状态信息及描述信息(描述信息一般都是在代码中加的打印信息),那么,这些信息是如何与客户端进行通信的呢? 2、当一个task开始执行,它将会保持运行记录,记录task完成的比例,对于map的任务,将会记录其运行的百分比,对于reduce来说可能复杂点,但系统依旧会估计reduce的完成比例。当一个map或reduce任务执行时,子进程会持续每三秒钟与applicationMaster进行交互。

4、 yarn使用

4.1 、配置文件

<!-- $HADOOP_HOME/etc/hadoop/mapred-site.xml -->
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
<!-- $HADOOP_HOME/etc/hadoop/yarn-site.xml -->
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
</configuration>

4.2、 yarn启动停止

启动 ResourceManager 和 NodeManager (以下分别简称RM、NM)

#主节点运行命令
$HADOOP_HOME/sbin/start-yarn.sh
#主节点运行命令
$HADOOP_HOME/sbin/stop-yarn.sh
若RM没有启动起来,可以单独启动

若RM没有启动起来,可以单独启动

#若RM没有启动,在主节点运行命令
$HADOOP_HOME/sbin/yarn-daemon.sh start resouremanager
#相反,可单独关闭
$HADOOP_HOME/sbin/yarn-daemon.sh stop resouremanager

若NM没有启动起来,可以单独启动

#若NM没有启动,在相应节点运行命令
$HADOOP_HOME/sbin/yarn-daemon.sh start nodemanager
#相反,可单独关闭
$HADOOP_HOME/sbin/yarn-daemon.sh stop nodemanager

4.3、 yarn常用命令

#1.查看正在运行的任务
yarn application -list
#2.杀掉正在运行任务
yarn application -kill 任务id
#3.查看节点列表
yarn node -list
#4.查看节点状况 TODO
yarn node -status node3:40652
#5.查看yarn依赖jar的环境变量
yarn classpath

5、Yarn调度器

在Yarn中有三种调度器可以选择:FIFO Scheduler ,Capacity Scheduler,FairS cheduler

5.1、 FIFO Scheduler

   FIFO Scheduler把应用按提交的顺序排成一个队列,这是一个先进先出队列,在进行资源分配的时候,先给队列中最头上的应用进行分配资源,待最头上的应用需求满足后再给下一个分配,以此类推。FIFO Scheduler是最简单也是最容易理解的调度器,也不需要任何配置,但它并不适用于共享集群。大的应用可能会占用所有集群资源,这就导致其它应用被阻塞。在共享集群中,更适合采用Capacity Scheduler或FairScheduler,这两个调度器都允许大任务和小任务在提交的同时获得一定的系统资源。下面“Yarn调度器对比图”展示了这几个调度器的区别,从图中可以看出,在FIFO 调度器中

5.2、 Capacity Scheduler

而对于Capacity调度器,有一个专门的队列用来运行小任务,但是为小任务专门设置一个队列会预先占用一定的集
群资源,这就导致大任务的执行时间会落后于使用FIFO调度器时的时间。
如何配置容量调度器
队列层级结构如下:
root
├── prod
└── dev
     ├── spark
     └── hdp
HADOOP_HOME/etc/hadoop/中建立一个新的capacity-scheduler.xml;内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <property>
      <name>yarn.scheduler.capacity.root.queues</name>
      <value>prod,dev</value>
   </property>
   <property>
       <name>yarn.scheduler.capacity.root.dev.queues</name>
       <value>hdp,spark</value>
   </property>
   <property>
       <name>yarn.scheduler.capacity.root.prod.capacity</name>
       <value>40</value>
   </property>
   <property>
      <name>yarn.scheduler.capacity.root.dev.capacity</name>
      <value>60</value>
  </property>
  <property>
      <name>yarn.scheduler.capacity.root.dev.maximum-capacity</name>
      <value>75</value>
  </property>
  <property>
   <name>yarn.scheduler.capacity.root.dev.hdp.capacity</name>
     <value>50</value>
     </property>
   <property>
      <name>yarn.scheduler.capacity.root.dev.spark.capacity</name>
      <value>50</value>
    </property>
  </configuration>

Application which will be placed in a queue, depending on the application itself.
For example MR, mapreduce.job.queuename may specify the queue by setting the appropriate property. In WordCount for example, as follows, if the specified queue does not exist, an error occurs. If not specified, the "default" queue, as FIG.
Here Insert Picture Description
Here Insert Picture Description

5.3、 Fair Scheduler

   In Fair scheduler, we do not need to pre-empt some system resources, Fair scheduler will run for the job dynamic adjustment of all system resources. When the first big job submission, only this one job is running, this time it obtained all cluster resources; when the second small job submission, Fair scheduler will allocate resources to this half of small tasks, so that the two task fair share cluster resources. Note that, in the figure Fair scheduler, task submission from the second to the availability of resources there will be some delay, because it needs to wait for the release of the first task occupied Container. Small task execution will release the resources they occupied after the completion of a large task and get all of the system resources. The net effect is Fair Scheduler to obtain a high resource utilization but also to ensure the timely completion of small tasks.

Guess you like

Origin blog.51cto.com/10312890/2463980