Yarn资源调度器

  • Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,而MapReduce等运算程序则相当于操作系统之上的应用程序。

    Yarn基础架构

  • YARN主要由ResourceManager、NodeManager、ApplicationMaster和Container等组件构成。

  • image.png

    Yarn工作机制

  • image.png

  • MR程序提交到客户端所在的节点。
  • YarnRunner向ResourceManager申请一个Application。
  • RM将该应用程序的资源路径返回给YarnRunner。
  • 该程序将运行所需资源提交到HDFS上。
  • 程序资源提交完毕后,申请运行mrAppMaster。
  • RM将用户的请求初始化成一个Task。
  • 其中一个NodeManager领取到Task任务。
  • 该NodeManager创建容器Container,并产生MRAppmaster。
  • Container从HDFS上拷贝资源到本地。
  • MRAppmaster向RM 申请运行MapTask资源。
  • RM将运行MapTask任务分配给另外两个NodeManager,另两个NodeManager分别领取任务并创建容器。
  • MR向两个接收到任务的NodeManager发送程序启动脚本,这两个NodeManager分别启动MapTask,MapTask对数据分区排序。
  • MrAppMaster等待所有MapTask运行完毕后,向RM申请容器,运行ReduceTask。
  • ReduceTask向MapTask获取相应分区的数据。
  • 程序运行完毕后,MR会向RM申请注销自己。

    作业提交全过程

  • image.png

    作业提交过程之YARN

  • image.png

    作业提交过程之HDFS&MapReduce

  • image.png

    作业提交全过程详解

    作业提交

  • 第1步:Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业。

  • 第2步:Client向RM申请一个作业id。
  • 第3步:RM给Client返回该job资源的提交路径和作业id。
  • 第4步:Client提交jar包、切片信息和配置文件到指定的资源提交路径。
  • 第5步:Client提交完资源后,向RM申请运行MrAppMaster。

    作业初始化

  • 第6步:当RM收到Client的请求后,将该job添加到容量调度器中。

  • 第7步:某一个空闲的NM领取到该Job。
  • 第8步:该NM创建Container,并产生MRAppmaster。
  • 第9步:下载Client提交的资源到本地。

    任务分配

  • 第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申请注销自己。

    进度和状态更新

  • YARN中的任务将其进度和状态(包括counter)返回给应用管理器, 客户端每秒(通过mapreduce.client.progressmonitor.pollinterval设置)向应用管理器请求进度更新, 展示给用户。

    作业完成

  • 除了向应用管理器请求作业进度外, 客户端每5秒都会通过调用waitForCompletion()来检查作业是否完成。时间间隔可以通过mapreduce.client.completion.pollinterval来设置。作业完成之后, 应用管理器和Container会清理工作状态。作业的信息会被作业历史服务器存储以备之后用户核查。

    Yarn调度器和调度算法

  • 目前,Hadoop作业调度器主要有三种:FIFO、容量(Capacity Scheduler)和公平(Fair Scheduler)。Apache Hadoop3.1.3默认的资源调度器是Capacity Scheduler

  • CDH框架默认调度器是Fair Scheduler
  • 具体设置详见:yarn-default.xml文件
    1. <property>
    2. <description>The class to use as the resource scheduler.</description>
    3. <name>yarn.resourcemanager.scheduler.class</name>
    4. <value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler</value>
    5. </property>

    先进先出调度器(FIFO)

    FIFO调度器(First In First Out):单队列,根据提交作业的先后顺序,先来先服务。

image.png

  • 优点:简单易懂;
  • 缺点:不支持多队列,生产环境很少使用;

    容量调度器(Capaticy Scheduler)

    Capacity Scheduler是Yahoo开发的多用户调度器。

  • image.png

  • 多队列
    • 每个队列可配置一定的资源量,每个队列采用FIFO调度策略
  • 容量保证
    • 管理员可为每个队列设置资源最低保证和资源使用上限
  • 灵活性
    • 如果一个队列中的资源有剩余,可以暂时共享给那些需要资源的队列,而一旦该队列有新的应用程序提交,则其它队列借调的资源会归还给该队列
  • 多租户

    • 支持多用户共享集群和多应用程序同时运行
    • 为了防止同一个用户的作业独占队列中的资源,该调度器会对同一用户提交的作业所占资源量进行限定

      容量调度器资源分配算法

  • image.png

    公平调度器(Fair Scheduler)

    是Facebook开发的多用户调度器

  • image.png

  • 与容量调度器相同点
    • 多队列
    • 容量保证
    • 灵活性
    • 多租户
  • 与容量调度器不同点

    • 核心调度策略不同
      • 容量:有限选择资源利用率低的队列
      • 公平:优先选择对资源的缺额比例大的
    • 每个队列可以单独设置资源分配方式
      • 容量:FIFO、DRF
      • 公平:FIFO、FAIR、DRF

        公平调度器——缺额

  • image.png

  • 公平调度器设计目标是:在时间尺度上,所有作业获得公平的资源。某一时刻一个作业应获资源和实际获取资源的差距叫“缺额”。
  • 调度器会优先为缺额大的作业分配资源

    公平调度器队列资源分配方式

  • image.png

    公平调度器资源分配算法

  • image.png

  • image.png
  • image.png

    Yarn常用命令

    查看任务

  • yarn application -list 列出所有Application

  • yarn application -list -appStates FINISHED 根据Application状态过滤
    • ALL、NEW、NEW_SAVING、SUBMITTED、ACCEPTED、RUNNING、FINISHED、FAILED、KILLED
  • yarn application -kill <任务id> 杀掉Application

    查看日志

  • yarn logs -aaplicationId <任务id> 查询Application日志

  • yarn logs -applicationId <任务id> -containerId <容器id>查询Container日志

    查看尝试运行的任务

  • yarn applicationattempt -list <任务id> 列出所有Application尝试的列表

  • yarn applicationattempt -status <尝试任务id> 打印ApplicationAttempt状态

    查看容器

  • yarn container -list <任务id> 列出所有容器

  • yarn container -status <容器id> 打印Container状态

    查看节点状态

  • yarn node -list -all 列出所有节点

    更新配置

  • yarn rmadmin -refreshQueues 加载队列配置

    查看队列

  • yarn queue -status default 打印队列信息

    Yarn 生产环境核心参数

  • image.png

    Yarn案例实操

    Yarn生产环境核心参数配置案例

    需求

  • 从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。

    需求分析

  • 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster = 10个容器

  • 平均每个节点运行 10个 / 3台≈ 3个任务(4 3 3)

    修改 yarn-site.xml参数

    ```xml The class to use as the resource scheduler. yarn.resourcemanager.scheduler.class org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler
Number of threads to handle scheduler interface. yarn.resourcemanager.scheduler.client.thread-count 8 Enable auto-detection of node capabilities such as memory and CPU. yarn.nodemanager.resource.detect-hardware-capabilities false Flag to determine if logical processors(such as hyperthreads) should be counted as cores. Only applicable on Linux when yarn.nodemanager.resource.cpu-vcores is set to -1 and yarn.nodemanager.resource.detect-hardware-capabilities is true. yarn.nodemanager.resource.count-logical-processors-as-cores false Multiplier to determine how to convert phyiscal cores to vcores. This value is used if yarn.nodemanager.resource.cpu-vcores is set to -1(which implies auto-calculate vcores) and yarn.nodemanager.resource.detect-hardware-capabilities is set to true. The number of vcores will be calculated as number of CPUs * multiplier. yarn.nodemanager.resource.pcores-vcores-multiplier 1.0 Amount of physical memory, in MB, that can be allocated for containers. If set to -1 and yarn.nodemanager.resource.detect-hardware-capabilities is true, it is automatically calculated(in case of Windows and Linux). In other cases, the default is 8192MB. yarn.nodemanager.resource.memory-mb 4096 Number of vcores that can be allocated for containers. This is used by the RM scheduler when allocating resources for containers. This is not used to limit the number of CPUs used by YARN containers. If it is set to -1 and yarn.nodemanager.resource.detect-hardware-capabilities is true, it is automatically determined from the hardware in case of Windows and Linux. In other cases, number of vcores is 8 by default. yarn.nodemanager.resource.cpu-vcores 4 The minimum allocation for every container request at theRM in MBs. Memory requests lower than this will be set to the value of this property. Additionally, a node manager that is configured to have less memory than this value will be shut down by the resource manager. yarn.scheduler.minimum-allocation-mb 1024 The maximum allocation for every container request at the RM in MBs. Memory requests higher than this will throw an InvalidResourceRequestException. yarn.scheduler.maximum-allocation-mb 2048 The minimum allocation for every container request at the RM in terms of virtual CPU cores. Requests lower than this will be set to the value of this property. Additionally, a node manager that is configured to have fewer virtual cores than this value will be shut down by the resource manager. yarn.scheduler.minimum-allocation-vcores 1 The maximum allocation for every container request at the RM in terms of virtual CPU cores. Requests higher than this will throw an InvalidResourceRequestException. yarn.scheduler.maximum-allocation-vcores 2 Whether virtual memory limits will be enforced for containers. yarn.nodemanager.vmem-check-enabled false Ratio between virtual memory to physical memory when setting memory limits for containers. Container allocations are expressed in terms of physical memory, and virtual memory usage is allowed to exceed this allocation by this ratio. yarn.nodemanager.vmem-pmem-ratio 2.1 <a name="FqlFx"></a> ## 容量调度器多队列提交案例 - 生产环境怎么创建队列 - 调度器默认就1个default队列,不能满足生产要求。 - 按照框架:hive /spark/ flink 每个框架的任务放入指定的队列(企业用的不是特别多) - 按照业务模块:登录注册、购物车、下单、业务部门1、业务部门2 - 创建多队列的好处 - 因为担心员工不小心,写递归死循环代码,把所有资源全部耗尽。 - 实现任务的**降级**使用,特殊时期保证重要的任务队列资源充足。 <a name="n3KuY"></a> ### 需求 - default队列占总内存的40%,最大资源容量占总资源60%,hive队列占总内存的60%,最大资源容量占总资源80%。 - 配置队列优先级 <a name="Hy7hz"></a> ### 配置多队列的容量调度器 <a name="gtfvh"></a> #### 配置 `capacity-scheduler.xml` - 修改如下配置xml yarn.scheduler.capacity.root.queues default,hive The queues at the this level (root is the root queue). yarn.scheduler.capacity.root.default.capacity 40 yarn.scheduler.capacity.root.default.maximum-capacity 60 - 为新加队列添加必要属性xml yarn.scheduler.capacity.root.hive.capacity 60 yarn.scheduler.capacity.root.hive.user-limit-factor 1 yarn.scheduler.capacity.root.hive.maximum-capacity 80 yarn.scheduler.capacity.root.hive.state RUNNING yarn.scheduler.capacity.root.hive.acl_submit_applications * yarn.scheduler.capacity.root.hive.acl_administer_queue * yarn.scheduler.capacity.root.hive.acl_application_max_priority * yarn.scheduler.capacity.root.hive.maximum-application-lifetime -1

yarn.scheduler.capacity.root.hive.default-application-lifetime -1 ```