• Yarn资源调度器



    一、Yarn资源调度器

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

    1、架构

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

    在这里插入图片描述

    2、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获取相应分区的数据。程序运行完毕后,MR会向RM申请注销自己。

    3、HDFS、YARN、MR关系

    在这里插入图片描述

    4、作业提交之HDFS&MapReduce

    在这里插入图片描述

    • (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会清理工作状态。作业的信息会被作业历史服务器存储以备之后用户核查。

    二、Yarn调度器和调度算法

    目前,Hadoop作业调度器主要有三种:FIFO、容量(Capacity Scheduler)和公平(Fair Scheduler)。

    • Apache Hadoop3.1.3默认的资源调度器是Capacity Scheduler。

    • CDH框架默认调度器是Fair Scheduler。

    详见yarn-default.xml文件

    <property>
        <description>The class to use as the resource scheduler.description>
        <name>yarn.resourcemanager.scheduler.classname>
    <value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulervalue>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1、先进先出调度器(FIFO)

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

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

    在这里插入图片描述

    2、容量调度器(Capacity Scheduler)

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

    在这里插入图片描述

    在这里插入图片描述

    3、公平调度器(Fair Scheduler)

    3.1 调度器原理

    Fair Schedulere是Facebook开发的多用户调度器。

    在这里插入图片描述

    公平调度器—缺额

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

    在这里插入图片描述

    3.22 资源分配方式

    有3种资源分配方式:FIFO策略、 Fair 策略、 DRF策略。

    (1)、FIFO策略

    公平调度器每个队列资源分配策略如果选择FIFO的话,此时公平调度器相当于上面讲过的容量调度器。

    (2)、Fair 策略

    Fair 策略(默认)是一种基于最大最小公平算法实现的资源多路复用方式,默认情况下,每个队列内部采用该方式分配资源。这意味着,如果一个队列中有两个应用程序同时运行,则每个应用程序可得到1/2的资源;如果三个应用程序同时运行,则每个应用程序可得到1/3的资源。

    在这里插入图片描述

    在这里插入图片描述

    (2)作业资源分配

    • 不加权(关注点是Job的个数):
    需求:有一条队列总资源12个, 有4个job,对资源的需求分别是: 
    job1->1,  job2->2 , job3->6,  job4->5
    
    第一次算:  12 / 4 = 3 
        job1: 分3 --> 多2个 
        job2: 分3 --> 多1个
        job3: 分3 --> 差3个
        job4: 分3 --> 差2个
    
    第二次算: 3 / 2  = 1.5 
        job1: 分1
        job2: 分2
        job3: 分3 --> 差3个 --> 分1.5 --> 最终: 4.5 
        job4: 分3 --> 差2个 --> 分1.5 --> 最终: 4.5
    
    第n次算: 一直算到没有空闲资源
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 加权(关注点是Job的权重):
    需求:有一条队列总资源16,有4个job 
    对资源的需求分别是: 
    job1->4   job2->2  job3->10  job4->4 
    每个job的权重为:   
    job1->5   job2->8  job3->1   job4->2
    
    第一次算: 16 / (5+8+1+2) =  1
        job1:  分5 --> 多1
        job2:  分8 --> 多6
        job3:  分1 --> 少9
        job4:  分2 --> 少2            
    
    第二次算: 7 / (1+2) = 7/3
        job1: 分4
        job2: 分2
        job3: 分1 --> 分7/3(2.33) -->少6.67
        job4: 分2 --> 分14/3(4.66) -->多2.66
    
    第三次算:2.66/1=2.66 
        job1: 分4
        job2: 分2
        job3: 分3.33 --> 分2.66/1 --> 分6
        job4: 分4
    第n次算: 一直算到没有空闲资源
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    (3)、DRF策略

    DRF(Dominant Resource Fairness),我们之前说的资源,都是单一标准,例如只考虑内存(也是Yarn默认的情况)。但是很多时候我们资源有很多种,例如内存,CPU,网络带宽等,这样我们很难衡量两个应用应该分配的资源比例。

    那么在YARN中,我们用DRF来决定如何调度:假设集群一共有100 CPU和10T 内存,而应用A需要(2 CPU, 300GB),应用B需要(6 CPU,100GB)。则两个应用分别需要A(2%CPU, 3%内存)和B(6%CPU, 1%内存)的资源,这就意味着A是内存主导的, B是CPU主导的,针对这种情况,我们可以选择DRF策略对不同应用进行不同资源(CPU和内存)的一个不同比例的限制。

    三、修改Yarn集群

    1、Yarn配置

    资源配置:

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

    • 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster。

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

    修改yarn-site.xml配置参数如下

    
    <property>
    	<description>The class to use as the resource scheduler.description>
    	<name>yarn.resourcemanager.scheduler.classname>
    	<value>org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulervalue>
    property>
    
    
    <property>
    	<description>Number of threads to handle scheduler interface.description>
    	<name>yarn.resourcemanager.scheduler.client.thread-countname>
    	<value>8value>
    property>
    
    
    
    <property>
    	<description>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.
    	description>
    	<name>yarn.nodemanager.resource.count-logical-processors-as-coresname>
    	<value>falsevalue>
    property>
    
    
    <property>
    	<description>Enable auto-detection of node capabilities such as
    	memory and CPU.
    	description>
    	<name>yarn.nodemanager.resource.detect-hardware-capabilitiesname>
    	<value>falsevalue>
    property>
    
    
    
    <property>
    	<description>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.
    	description>
    	<name>yarn.nodemanager.resource.pcores-vcores-multipliername>
    	<value>1.0value>
    property>
    
    
    <property>
    	<description>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.
    	description>
    	<name>yarn.nodemanager.resource.memory-mbname>
    	<value>4096value>
    property>
    
    
    <property>
    	<description>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.description>
    	<name>yarn.nodemanager.resource.cpu-vcoresname>
    	<value>4value>
    property>
    
    
    <property>
    	<description>The minimum allocation for every container request at the RM	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.
    	description>
    	<name>yarn.scheduler.minimum-allocation-mbname>
    	<value>1024value>
    property>
    
    
    <property>
    	<description>The maximum allocation for every container request at the RM	in MBs. Memory requests higher than this will throw an	InvalidResourceRequestException.
    	description>
    	<name>yarn.scheduler.maximum-allocation-mbname>
    	<value>2048value>
    property>
    
    
    <property>
    	<description>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.
    	description>
    	<name>yarn.scheduler.minimum-allocation-vcoresname>
    	<value>1value>
    property>
    
    
    <property>
    	<description>The maximum allocation for every container request at the RM	in terms of virtual CPU cores. Requests higher than this will throw an
    	InvalidResourceRequestException.description>
    	<name>yarn.scheduler.maximum-allocation-vcoresname>
    	<value>2value>
    property>
    
    
    <property>
    	<description>Whether virtual memory limits will be enforced for
    	containers.description>
    	<name>yarn.nodemanager.vmem-check-enabledname>
    	<value>falsevalue>
    property>
    
    
    <property>
    	<description>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.
    	description>
    	<name>yarn.nodemanager.vmem-pmem-rationame>
    	<value>2.1value>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123

    重启Yarn集群

    ./sbin/stop-yarn.sh
    ./sbin/start-yarn.sh
    
    • 1
    • 2

    登录页面查看资源修改:http://hadoop102:8088/cluster

    在这里插入图片描述

    关闭虚拟内存检查

    在这里插入图片描述

    2、多队列提交

    配置参数:

    default队列:占总内存的40%,最大资源容量占总资源60%。

    hive队列:占总内存的60%,最大资源容量占总资源80%。

    修改capacity-scheduler.xml配置

    
    <property>
        <name>yarn.scheduler.capacity.root.queuesname>
        <value>default,hivevalue>
        <description>
          The queues at the this level (root is the root queue).
        description>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.default.capacityname>
        <value>40value>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.default.maximum-capacityname>
        <value>60value>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    添加capacity-scheduler.xml配置

    
    <property>
        <name>yarn.scheduler.capacity.root.hive.capacityname>
        <value>60value>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.hive.user-limit-factorname>
        <value>1value>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.hive.maximum-capacityname>
        <value>80value>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.hive.statename>
        <value>RUNNINGvalue>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.hive.acl_submit_applicationsname>
        <value>*value>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.hive.acl_administer_queuename>
        <value>*value>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.hive.acl_application_max_priorityname>
        <value>*value>
    property>
    
    
    
    
    <property>
        <name>yarn.scheduler.capacity.root.hive.maximum-application-lifetimename>
        <value>-1value>
    property>
    
    
    <property>
        <name>yarn.scheduler.capacity.root.hive.default-application-lifetimename>
        <value>-1value>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    分发修改后配置文件,或者修改ResourceManger所在节点配置

    重启Yarn集群或者刷新配置

    yarn rmadmin -refreshQueues
    
    • 1

    登录页面查看队列更新:http://hadoop102:8088/cluster/scheduler

    在这里插入图片描述

    3、向集群中提交任务

    package com.example.demo.wordcount;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    
    public class WCDriver2 {
        public static void main(String[] args) throws Exception {
            System.out.println(args[0]);
            System.out.println(args[1]);
            //1.创建Job实例
            Configuration conf = new Configuration();//可以设置参数
            conf.set("mapreduce.job.queuename", "hive");
            Job job = Job.getInstance(conf);
    
            //2.给Job赋值
            //2.1关联本程序的jar---如果是本地运行不用设置。如果是在集群上运行(打jar包放在集群上)一定要设置
            job.setJarByClass(WCDriver2.class);
            //2.2设置Mapper和Reducer类
            job.setMapperClass(WCMapper.class);
            job.setReducerClass(WCReducer.class);
            //2.3设置Mapper输出的Key,value的类型
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(LongWritable.class);
            //2.4设置最终输出的key,value的类型(在这是Reducer输出的key,value的类型)
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(LongWritable.class);
            //2.5设置输入和输出路径
            FileInputFormat.setInputPaths(job, new Path(args[0]));
            //注意:输出的目录必须不存在
            FileOutputFormat.setOutputPath(job, new Path(args[1]));
    
            //3.提交Job
            boolean b = job.waitForCompletion(true);
            System.out.println("=======" + b);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    pom文件打包方式

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>com.examplegroupId>
        <artifactId>demoartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>demoname>
        <description>demodescription>
        <properties>
            <java.version>1.8java.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.hadoopgroupId>
                <artifactId>hadoop-clientartifactId>
                <version>3.1.3version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
            dependency>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
                <version>1.7.30version>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.6.1version>
                    <configuration>
                        <source>1.8source>
                        <target>1.8target>
                    configuration>
                plugin>
                <plugin>
                    <artifactId>maven-assembly-pluginartifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependenciesdescriptorRef>
                        descriptorRefs>
                    configuration>
                    <executions>
                        <execution>
                            <id>make-assemblyid>
                            <phase>packagephase>
                            <goals>
                                <goal>singlegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    登录服务器执行脚本

    • demo-0.0.1.jar :运行的jar包,服务器所在全路径
    • com.example.demo.wordcount.WCDriver2 :全类名运行jar包中的哪个类
    • /input :数据的输入路径(HDFS)
    • /output :数据的输出路径(HDFS)
    hadoop jar demo-0.0.1.jar com.example.demo.wordcount.WCDriver2 /input /output
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    电脑文件怎么加密?文件加密软件哪个比较好?
    【PX4-AutoPilot教程-TIPS】PX4控制无人机在Gazebo中飞行时由于视角跟随无人机在画面中心导致视角乱晃的解决方法
    5年Java面试阿里P6经验总结
    7、Docker网络
    【问题记录】一次由filter引发的血案,如何定位上游链路的问题,问题排查与定位思路分享
    IDEA配置自己的Maven以及创建Maven
    使用 prometheus 监控 MySQL
    【毕业设计】基于RFID的门禁系统 - 单片机 物联网 嵌入式 stm32
    微服务项目:尚融宝(50)(核心业务流程:标的管理(2))
    小型气象站浅谈使用说明
  • 原文地址:https://blog.csdn.net/weixin_44624117/article/details/132720669