ToB企服应用市场:ToB评测及商务社交产业平台

标题: 06yarn简介 [打印本页]

作者: 十念    时间: 2023-2-9 08:13
标题: 06yarn简介
YARN简介

一、YARN是什么

YARN不是facebook的那个yarn,它从Hadoop 2引入,最初目的是改善MapReduce的实现,但是因为具备足够通用性,同样也可以支持其他的分布式计算模式。YARN全称是Yet Another Resource Negotiator,翻译过来是另一种资源协调者,名字听起来有点奇怪,但是不难看出它的用途是管理和调度Hadoop中的资源,具体来说是计算资源。
YARN和DHFS一样也是主从架构,它有两种服务,分为resourcemanager和nodemanager。resourcemanager负责管理nodemanager和application master进程,nodemanager负责管理容器,以及监控容器使用资源。
YARN有几个重要的概念:
一个YARN应用的启动流程如下,图中每一个虚线框代表一个物理机器:

首先客户端提交作业到resourcemanager资源管理器(图中第1步)。resourcemanager资源管理器寻找集群中可用资源,向nodemanager节点管理器发出创建容器请求(图中2a步骤)。随后容器启动并运行应用程序,也就是application master进程(图中2b步骤),application master会运行周期的向资源管理器报告自己的状态。应用程序可能做什么取决于应用本身,应用可能只是简单执行任务,或者向集群继续申请资源运行多个子任务。例如MapReduce应用,它会把大任务拆分成小任务,然后向resourcemanager资源管理器申请资源(图中第3步),并且运行小任务(图中4a和4b步骤)。YARN本身并不会为应用的进程提供通信手段,应用之间的通信手段是专属于应用本身。
二、一个MapReduce应用运行的简单分析

下面通过在集群运行一个简单MapReduce应用来理解YARN应用的启动流程
1、集群拓扑图

笔者的集群拓扑图如下:

Linux-1运行一个资源管理器和节点管理器,Linux-2只运行一个节点管理器,提交任务的是一台Windows主机。Windows机器的ip是192.168.0.109
2、应用启动

先用jps查看集群各个机器当前运行的进程:
  1. -- Linux-1 192.168.0.200
  2. debian@Linux-1:~$ jps
  3. 848 ResourceManager
  4. 4112 Jps
  5. 752 DataNode
  6. 1088 NodeManager
  7. 663 NameNode
  8. -- Linux-2 192.168.0.201
  9. debian@Linux-2:~$ jps
  10. 15893 Jps
  11. 10219 NodeManager
复制代码
然后向集群提交应用,可能会看到如下输出:
这里提交的应用还是maxSaleMapReduce,唯一不同的是配置文件mapreduce.job.running.map.limit(map同时最大运行数量)设置成3。可以在https://github.com/xunpengliu/hello-hadoop获取代码,如果之前已经下载过需要拉取最新代码后重新打包
  1. 23/02/07 17:26:27 INFO client.RMProxy: Connecting to ResourceManager at /192.168.0.200:8032
  2. 23/02/07 17:26:37 INFO input.FileInputFormat: Total input files to process : 5
  3. 23/02/07 17:26:37 INFO mapreduce.JobSubmitter: number of splits:15
  4. 23/02/07 17:26:38 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1675757920071_0004
  5. 23/02/07 17:26:38 INFO conf.Configuration: resource-types.xml not found
  6. 23/02/07 17:26:38 INFO resource.ResourceUtils: Unable to find 'resource-types.xml'.
  7. 23/02/07 17:26:38 INFO resource.ResourceUtils: Adding resource type - name = memory-mb, units = Mi, type = COUNTABLE
  8. 23/02/07 17:26:38 INFO resource.ResourceUtils: Adding resource type - name = vcores, units = , type = COUNTABLE
  9. 23/02/07 17:26:38 INFO impl.YarnClientImpl: Submitted application application_1675757920071_0004
  10. 23/02/07 17:26:38 INFO mapreduce.Job: The url to track the job: http://192.168.0.200:8088/proxy/application_1675757920071_0004/
  11. 23/02/07 17:26:38 INFO mapreduce.Job: Running job: job_1675757920071_0004
  12. 23/02/07 17:26:43 INFO mapreduce.Job: Job job_1675757920071_0004 running in uber mode : false
  13. 23/02/07 17:26:43 INFO mapreduce.Job:  map 0% reduce 0%
  14. 23/02/07 17:26:59 INFO mapreduce.Job:  map 3% reduce 0%
  15. 23/02/07 17:27:00 INFO mapreduce.Job:  map 11% reduce 0%
复制代码
可以看到当前应用id是application_1675757920071_0004,作业id是job_1675757920071_0004,从 http://192.168.0.200:8088/proxy/application_1675757920071_0004/ 可以查询到应用运行状态。
等一小会,再使用jps查看运行的进程,可以看到多了1个MRAppMaster和3个YarnChild和进程。查看MRAppMaster进程端口绑定情况和NodeManager的进程树:
  1. -- Linux-1 192.168.0.200
  2. debian@Linux-1:~$ jps
  3. 848 ResourceManager
  4. 752 DataNode
  5. 1088 NodeManager
  6. 3649 MRAppMaster
  7. 663 NameNode
  8. 3754 YarnChild
  9. 3789 Jps
  10. debian@Linux-1:~$ pstree -T -p 1088
  11. java(1088)─┬─bash(3628)───bash(3648)───java(3649)
  12.            └─bash(3795)───bash(3750)───java(3754)
  13. debian@Linux-1:~$ lsof -i | grep 3649
  14. java    3649 debian  262u  IPv6  88478      0t0  TCP *:43763 (LISTEN)
  15. java    3649 debian  273u  IPv6  88485      0t0  TCP *:42565 (LISTEN)
  16. java    3649 debian  279u  IPv6  88487      0t0  TCP *:41731 (LISTEN)
  17. java    3649 debian  289u  IPv6  96002      0t0  TCP 192.168.0.200:50682->192.168.0.200:8030 (ESTABLISHED)
  18. java    3649 debian  291u  IPv6  91613      0t0  TCP 192.168.0.200:43763->192.168.0.109:55029 (ESTABLISHED)
  19. java    3649 debian  292u  IPv6  96258      0t0  TCP 192.168.0.200:41731->192.168.0.200:46720 (ESTABLISHED)
  20. java    3649 debian  293u  IPv6  91977      0t0  TCP 192.168.0.200:41731->192.168.0.201:56746 (ESTABLISHED)
  21. java    3649 debian  261u  IPv6  96005      0t0  TCP 192.168.0.200:41731->192.168.0.201:56744 (ESTABLISHED)
  22. -- Linux-2 192.168.0.201
  23. debian@Linux-2:~$ jps
  24. 10219 NodeManager
  25. 15356 Jps
  26. 15326 YarnChild
  27. 15295 YarnChild
  28. debian@Linux-2:~$ pstree -T -p 10219
  29. java(10219)─┬─bash(15291)───bash(15293)───java(15295)
  30.             └─bash(15320)───bash(15323)───java(15326)
复制代码
MRAppMaster也就是MapReduce应用的application master进程,它启动后向资源管理器继续申请资源,然后启动3个YARNChild进程来执行子任务,3个子任务分别运行在Linux-1和Linux-2机器上。
maxSaleMapReduce的map任务和reduce任务在启动和完成会各输出一行日志,日志内容有ip、进程启动时间、主类名称、进程pid。日志文件位置在userlogs/${appid}目录中,如果你的集群像笔者一样没开日志聚合也没修改目录,userlogs目录在Hadoop安装位置的logs目录下。日志是按容器进行分组的,例如Linux-2上的application_1675757920071_0004目录就有下面几个文件夹:
  1. debian@Linux-2:~/program/hadoop-2.10.2/logs/userlogs/application_1675757920071_0004$ ll
  2. 总用量 44
  3. drwx--x--- 2 debian debian 4096  2月  7 17:26 container_1675757920071_0004_01_000002
  4. drwx--x--- 2 debian debian 4096  2月  7 17:26 container_1675757920071_0004_01_000003
  5. drwx--x--- 2 debian debian 4096  2月  7 17:27 container_1675757920071_0004_01_000007
  6. drwx--x--- 2 debian debian 4096  2月  7 17:27 container_1675757920071_0004_01_000008
  7. drwx--x--- 2 debian debian 4096  2月  7 17:27 container_1675757920071_0004_01_000009
  8. drwx--x--- 2 debian debian 4096  2月  7 17:27 container_1675757920071_0004_01_000011
  9. drwx--x--- 2 debian debian 4096  2月  7 17:27 container_1675757920071_0004_01_000012
  10. drwx--x--- 2 debian debian 4096  2月  7 17:28 container_1675757920071_0004_01_000014
  11. drwx--x--- 2 debian debian 4096  2月  7 17:28 container_1675757920071_0004_01_000015
  12. drwx--x--- 2 debian debian 4096  2月  7 17:28 container_1675757920071_0004_01_000017
  13. drwx--x--- 2 debian debian 4096  2月  7 17:28 container_1675757920071_0004_01_000018
  14. debian@Linux-2:~/program/hadoop-2.10.2/logs/userlogs/application_1675757920071_0004$ ll container_1675757920071_0004_01_000002
  15. 总用量 32
  16. -rw-r--r-- 1 debian debian     0  2月  7 17:26 prelaunch.err
  17. -rw-r--r-- 1 debian debian    70  2月  7 17:26 prelaunch.out
  18. -rw-r--r-- 1 debian debian     0  2月  7 17:26 stderr
  19. -rw-r--r-- 1 debian debian     0  2月  7 17:26 stdout
  20. -rw-r--r-- 1 debian debian 27051  2月  7 17:27 syslog
复制代码
syslog文件有我们自定义输出的日志,日志内容如下:
  1. -- Linux-1 192.168.0.200
  2. debian@Linux-1:~/program/hadoop-2.10.2/logs/userlogs/application_1675757920071_0004$ cat */syslog | grep mainClass
  3. 2023-02-07 17:26:44,392 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.200,startTime->2023-02-07 17:26:42,pid->3754,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000002_0, 4]
  4. 2023-02-07 17:26:56,849 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.200,pid->3754,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000002_0, 4]
  5. 2023-02-07 17:27:06,347 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.200,startTime->2023-02-07 17:27:04,pid->3839,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000003_0, 6]
  6. 2023-02-07 17:27:18,586 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.200,pid->3839,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000003_0, 6]
  7. 2023-02-07 17:27:27,675 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.200,startTime->2023-02-07 17:27:25,pid->3904,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000006_0, 10]
  8. 2023-02-07 17:27:39,979 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.200,pid->3904,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000006_0, 10]
  9. 2023-02-07 17:27:48,950 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.200,startTime->2023-02-07 17:27:47,pid->3965,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000009_0, 13]
  10. 2023-02-07 17:28:01,263 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.200,pid->3965,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000009_0, 13]
  11. 2023-02-07 17:28:10,980 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.200,startTime->2023-02-07 17:28:09,pid->4026,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000012_0, 16]
  12. 2023-02-07 17:28:15,964 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.200,pid->4026,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000012_0, 16]
  13. -- Linux-2 192.168.0.201
  14. debian@Linux-2:~/program/hadoop-2.10.2/logs/userlogs/application_1675757920071_0004$ cat */syslog | grep mainClass
  15. 2023-02-07 17:26:43,338 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:26:41,pid->15295,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000000_0, 2]
  16. 2023-02-07 17:26:58,315 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15295,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000000_0, 2]
  17. 2023-02-07 17:26:44,621 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:26:42,pid->15326,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000001_0, 3]
  18. 2023-02-07 17:26:59,053 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15326,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000001_0, 3]
  19. 2023-02-07 17:28:32,397 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleReducer: reducer task setup,ip->192.168.0.201,startTime->2023-02-07 17:27:05,mainClass->org.apache.hadoop.mapred.YarnChild,pid->15421
  20. 2023-02-07 17:28:44,596 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleReducer: reducer task cleanup,ip->192.168.0.201,pid->15421,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_r_000000_0, 7]
  21. 2023-02-07 17:27:09,522 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:27:07,pid->15467,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000004_0, 8]
  22. 2023-02-07 17:27:24,832 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15467,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000004_0, 8]
  23. 2023-02-07 17:27:11,552 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:27:08,pid->15500,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000005_0, 9]
  24. 2023-02-07 17:27:32,893 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15500,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000005_0, 9]
  25. 2023-02-07 17:27:36,450 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:27:34,pid->15567,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000007_0, 11]
  26. 2023-02-07 17:27:52,060 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15567,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000007_0, 11]
  27. 2023-02-07 17:27:41,647 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:27:40,pid->15617,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000008_0, 12]
  28. 2023-02-07 17:27:56,207 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15617,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000008_0, 12]
  29. 2023-02-07 17:28:01,633 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:28:00,pid->15679,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000010_0, 14]
  30. 2023-02-07 17:28:07,591 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15679,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000010_0, 14]
  31. 2023-02-07 17:28:04,827 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:28:03,pid->15725,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000011_0, 15]
  32. 2023-02-07 17:28:11,105 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15725,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000011_0, 15]
  33. 2023-02-07 17:28:16,469 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:28:14,pid->15783,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000013_0, 17]
  34. 2023-02-07 17:28:25,503 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15783,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000013_0, 17]
  35. 2023-02-07 17:28:17,918 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task setup,ip->192.168.0.201,startTime->2023-02-07 17:28:16,pid->15814,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000014_0, 18]
  36. 2023-02-07 17:28:25,095 INFO [main] org.example.helloHadoop.maxSaleMapReduce.MaxSaleMapper: mapper task cleanup,ip->192.168.0.201,pid->15814,mainClass->org.apache.hadoop.mapred.YarnChild,args->[192.168.0.200, 41731, attempt_1675757920071_0004_m_000014_0, 18]
复制代码
3、应用执行分析

通过日志可以得到map任务和reduce任务甘特图:

红条是进程启动时间,绿条是任务真正开始消耗的时间
从图中不难看出同一时间最多只有3个map任务在运行,reduce任务很早就启动了,但是要等map任务全部完成才真正开始。这个是因为MapReduce的“慢启动”机制,由参数mapreduce.job.reduce.slowstart.completedmaps控制,默认值0.05,相当于map任务完成5%就开始启动reduce任务。
MRAppMaster通过网络连接获取子任务的状态,MRAppMaster地址和绑定的端口会通过启动参数传给子进程,也就是日志输出的192.168.0.200, 41731。同时MRAppMaster会周期性向资源管理器报告自己的状态,也就是这条连接192.168.0.200:50682->192.168.0.200:8030
所以基本可以简单说明:
任务会被分配到不同的机器执行,那执行文件是如何共享的呢?答案是通过HDFS共享,在MRAppMaster的日志中有这样两行:
  1. The job-jar file on the remote FS is hdfs://192.168.0.200:8082/tmp/hadoop-yarn/staging/debian/.staging/job_1675757920071_0004/job.jar
  2. The job-conf file on the remote FS is /tmp/hadoop-yarn/staging/debian/.staging/job_1675757920071_0004/job.xml
复制代码
从HDFS下载文件对比可以发现,这两个就是任务的可执行文件和使用的配置,同时当前目录下还有一些其他文件,比如我们的自定义依赖库就在libjars文件夹中。
三、YARN调度资源的几种模式

1、三种调度模式

YARN有三种调度器,分别是FIFO调度器(先进先出),容量调度器和公平调度器。
FIFO调度器是最简单的调度器,不需要任何配置,每个应用通过排队的方式使用集群资源。容量调度器通过队列分割资源,不同队列配置的容量不同,应用运行在不同的队列上。公平调度器通过动态的方式调度资源,当一个应用启动它会占用集群所有资源,当第二个应用启动后,它会慢慢等第一个应用释放出的部分资源,最终达到一个动态的公平共享资源。
2、容量调度器配置

假设有这样一个队列层次结构
  1. root─┬─default
  2.      └─dev
  3.         └─test1
  4.         └─test2
复制代码
修改etc/hadoop/capacity-scheduler.xml文件内容为如下:
更多配置可以参考 https://hadoop.apache.org/docs/r2.10.2/hadoop-yarn/hadoop-yarn-site/CapacityScheduler.html
  1. <configuration>
  2.   <property>
  3.     <name>yarn.scheduler.capacity.maximum-applications</name>
  4.     <value>10000</value>
  5.     <description>
  6.       最大可以运行多少个应用
  7.     </description>
  8.   </property>
  9.   <property>
  10.     <name>yarn.scheduler.capacity.maximum-am-resource-percent</name>
  11.     <value>0.1</value>
  12.     <description>
  13.       application master进程占用的最大资源比例
  14.     </description>
  15.   </property>
  16.   <property>
  17.     <name>yarn.scheduler.capacity.root.queues</name>
  18.     <value>default,dev</value>
  19.     <description>
  20.       root队列下的队列
  21.     </description>
  22.   </property>
  23.   <property>
  24.     <name>yarn.scheduler.capacity.root.dev.queues</name>
  25.     <value>test1,test2</value>
  26.     <description>
  27.       dev队列下的队列
  28.     </description>
  29.   </property>
  30.   <property>
  31.     <name>yarn.scheduler.capacity.root.default.capacity</name>
  32.     <value>70</value>
  33.     <description>
  34.       default队列默认容量
  35.     </description>
  36.   </property>
  37.   
  38.   <property>
  39.     <name>yarn.scheduler.capacity.root.default.maximum-capacity</name>
  40.     <value>90</value>
  41.     <description>
  42.       default队列最大容量
  43.     </description>
  44.   </property>
  45.   <property>
  46.     <name>yarn.scheduler.capacity.root.default.acl_submit_applications</name>
  47.     <value>*</value>
  48.     <description>
  49.       哪些用户可以提交作业到default,*全部
  50.     </description>
  51.   </property>
  52.   <property>
  53.     <name>yarn.scheduler.capacity.root.default.acl_administer_queue</name>
  54.     <value>*</value>
  55.     <description>
  56.       哪些用户可以控制default队列任务,*全部
  57.     </description>
  58.   </property>
  59.   <property>
  60.     <name>yarn.scheduler.capacity.root.default.acl_application_max_priority</name>
  61.     <value>*</value>
  62.     <description>
  63.       哪些用户可以在default队列设置任务优先级,*全部
  64.     </description>
  65.   </property>
  66.   <property>
  67.     <name>yarn.scheduler.capacity.root.dev.capacity</name>
  68.     <value>30</value>
  69.     <description>
  70.       dev队列默认容量
  71.     </description>
  72.   </property>
  73.   <property>
  74.     <name>yarn.scheduler.capacity.root.dev.maximum-capacity</name>
  75.     <value>50</value>
  76.     <description>
  77.       dev队列最大容量
  78.     </description>
  79.   </property>
  80.   <property>
  81.     <name>yarn.scheduler.capacity.root.dev.acl_submit_applications</name>
  82.     <value>*</value>
  83.     <description>
  84.       哪些用户可以提交作业到dev,*全部
  85.     </description>
  86.   </property>
  87.   <property>
  88.     <name>yarn.scheduler.capacity.root.dev.acl_administer_queue</name>
  89.     <value>*</value>
  90.     <description>
  91.       哪些用户可以控制dev队列任务,*全部
  92.     </description>
  93.   </property>
  94.   <property>
  95.     <name>yarn.scheduler.capacity.root.dev.acl_application_max_priority</name>
  96.     <value>*</value>
  97.     <description>
  98.       哪些用户可以在dev队列设置任务优先级,*全部
  99.     </description>
  100.   </property>
  101.   <property>
  102.     <name>yarn.scheduler.capacity.root.dev.test1.capacity</name>
  103.     <value>50</value>
  104.     <description>
  105.       test1队列默认容量
  106.     </description>
  107.   </property>
  108.   <property>
  109.     <name>yarn.scheduler.capacity.root.dev.test2.capacity</name>
  110.     <value>50</value>
  111.     <description>
  112.       test2队列默认容量
  113.     </description>
  114.   </property>
  115. </configuration>
复制代码
default队列和dev队列分别分配到了70%和30%的资源,dev队列又被分成了test1和test2两个队列,test1和test2平分dev队列资源。可以在资源管理器的web服务看到队列配置:

MapReduce应用通过配置mapreduce.job.queuename来指定运行队列,默认值是default。需要注意的是应该配置的是队列名,例如test1,而不是队列的全局限定名root.dev.test1。
修改应用配置文件mapreduce.job.queuenametest1,再次运行maxSaleMapReduce应用,可以发现此时只会启动1个application master和1个YarnChild进程。
启动进程数量和集群资源相关,因为资源受限,可以发现此时应用执行时间明显偏长
队列状态:

任务信息:

3、公平调度器配置

还是刚才的队列层次结构:
  1. root─┬─default
  2.      └─dev
  3.         └─test1
  4.         └─test2
复制代码
首先要修改yarn-site.xml文件中的配置yarn.resourcemanager.scheduler.classorg.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler
然后创建fair-scheduler.xml文件,内容如下:
更多配置可以参考 https://hadoop.apache.org/docs/r2.10.2/hadoop-yarn/hadoop-yarn-site/FairScheduler.html
  1. <?xml version="1.0"?>
  2. <allocations>
  3.    
  4.     <defaultQueueSchedulingPolicy>fair</defaultQueueSchedulingPolicy>
  5.     <queue name="default">
  6.         <weight>70</weight>
  7.         
  8.         <schedulingPolicy>fifo</schedulingPolicy>
  9.         
  10.         <aclSubmitApps>*</aclSubmitApps>
  11.         
  12.         <aclAdministerApps>*</aclAdministerApps>
  13.     </queue>
  14.     <queue name="dev">
  15.         <weight>30</weight>
  16.         
  17.         <aclSubmitApps>*</aclSubmitApps>
  18.         
  19.         <aclAdministerApps>*</aclAdministerApps>
  20.         <queue name="test1">
  21.             <weight>50</weight>
  22.         </queue>
  23.         <queue name="test2">
  24.             <weight>50</weight>
  25.         </queue>
  26.     </queue>
  27.    
  28.     <queuePlacementPolicy>
  29.         <rule name="specified" />
  30.         <rule name="default" queue="dev.test2" />
  31.     </queuePlacementPolicy>
  32. </allocations>
复制代码
defaultQueueSchedulingPolicy配置指定队列内默认调度模式,如果队列自身没有指定调度模式,则采用此配置的值。
queuePlacementPolicy配置包含一个规则,用于匹配应用放置在哪个队列。specified表示优先采用应用指定的队列,如果匹配失败则继续匹配下一条规则。如果所有规则都不匹配则命中default规则,应用会放置在test2队列中。
重启资源管理器,web服务显示信息如下:

不配置mapreduce.job.queuename参数启动应用,可以发现应用被调度到队列test2上:
队列状态:

任务信息:

与容量调度器不同的是,test1虽然只设置了dev的一半的权重,但是实际使用了dev全部资源,而dev也超出了自身的30%的权重。这个是因为当前集群只有一个任务,所以这个任务可以独占集群所有资源。
如果一个集群非常繁忙,当任务被提交后,任务不会立即启动,任务会等待已经运行的任务释放资源才会启动。为了使任务从提交到执行的时间可预测,公平调度器支持“抢占”功能。
所谓抢占就是允许调度器主动终止超过权重的队列中运行的任务,释放的资源会被分配给低于应得权重的队列。但是因为任务被终止,所以被终止的任务会被重新执行,相当于降低了整个集群的运行效率。
通过设置yarn.scheduler.fair.preemption为true启动抢占功能,更详细的参数可以参考 https://hadoop.apache.org/docs/r2.10.2/hadoop-yarn/hadoop-yarn-site/FairScheduler.html 中的Configuration内容。
4、总结

YARN提供了三种调度器,分别是FIFO调度器(先进先出),容量调度器和公平调度器。FIFO调度器规则最为简单,任务可以独占所有资源以便更快的完成,但是如果出现大任务的情况会阻塞后续任务的执行。容量调度器通过队列来分割和保留集群资源,在一个繁忙的集群中可以为某些任务有效的保留可用资源,缺点是无法高效利用集群整体资源。公平调度器通过动态调度的模式调度资源,应用之间支持主动让出和抢占分配资源。
使用哪种模式要根据集群的业务场景来决定,不存在哪种调度模式更好或更差。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4