Yarn的配置因集群环境而异。不同的集群节点配置可能参差不齐,有的性能好,有的性能差。而Yarn作为资源管理器,并不具有自动识别当前机器可以用的虚拟核以及物理内存的功能。在默认情况下,无论当前节点CPU核心与物理内存,都默认识为8vcores和8G内存。如果想更充分的利用集群资源,需要依据集群中每个节点的具体配置来编写配置文件,让Yarn准确识别到当前节点可用的虚拟核心数量和物理内存容量。下文是具体的配置教程,分为节点配置信息查看和配置文件编写两部分。

1、收集节点配置信息

1.1 虚拟核心数量

  1. # 查看物理CPU个数
  2. [root@master /]# cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
  3. # 查看每个物理CPU中core的个数(即核心数量)
  4. [root@master /]# cat /proc/cpuinfo| grep "cpu cores"| uniq
  5. # 查看逻辑CPU的个数(即操作系统所识别的虚拟核心数量)
  6. [root@master /]# cat /proc/cpuinfo| grep "processor"| wc -l

执行样例如下图所示。演示机的物理CPU为1个,即单路CPU。当前CPU包含有4个物理核心。由于每个物理核心能同时保存多个线程状态,具有超线程的功能,所以对于操作系统来说,实际可用核心数量为8个虚拟核心。当前节点的CPU信息:单路4核心8线程。之后做配置需要使用的主要信息是虚拟核心数vcores。是一个关乎提交应用所能申请Container数量的信息。
image.png

1.2 物理内存容量

  1. # 查看当前节点物理内存。-h表示用人类易读的方式展示。
  2. [root@master /]# free -h

在Centos 7系统中执行free -h命令,结果样例如下:
total表示操作系统总共的物理内存容量。
available表示操作系统剩余未分配给进程所使用的物理内存容量。
之后做配置需要使用的主要信息是totalavailable。total关乎分配给Yarn所能调度的内存总量,available关乎提交的应用所能启动Container的最大数量。
image.png

available = free + buffer/cache - 不可被回收内存。 buffer/cache在系统需要时,有些部分可以被回收利用,有些部分不能被回收利用。

swap作为从磁盘划分出来当做物理内存使用的一块区域,就不考虑使用了。

1.3 快速查看

手动查看每个节点的状态比较麻烦,推荐先配置启动好Spark。vcorestotal memory信息在Spark WebUI(默认端口8080)首页都有显示。
image.png

2、编写配置文件

2.1 yarn-site.xml中追加配置

有了vcorestotal memory信息就可以编写Yarn的配置文件了。打开hadoop安装目录下的etc/hadoop/yarn-site.xml,追加以下配置。

yarn.nodemanager.resource.memory-mb 当前节点分配给Yarn使用的物理内存。小于等于total memroy。推荐total memory的70%~80%,因为其他程序还需要使用内存。单位MB。
yarn.nodemanager.resource.cpu-vcores 当前节点分配给Yarn使用的虚拟核数。小于等于vcores。可以设置等于vcores
yarn.scheduler.minimum-allocation-mb Yarn资源调度时,分配给Container的最小内存容量。单位MB。
yarn.scheduler.maximum-allocation-mb Yarn资源调度时,分配给Container的最大内存容量。单位MB。
yarn.scheduler.minimum-allocation-vcores Yarn资源调度时,分配给Container的最小虚拟核数。
yarn.scheduler.maximum-allocation-**vcores** Yarn资源调度时,分配给Container的最大虚拟核数。

示例:一台8vcores,12G内存的节点可以做如下配置。当前节点分配大约80%可用内存,8核心给Yarn进行调度。当Yarn进行资源调度时,分配给Container的最小内存容量是1G,最大4G,最小核心1vcore,最大4vcore。

  1. <configuration>
  2. ... ...
  3. <!-- nodeManager所在节点的物理内存容量和虚拟核数。占比约80%,依照机器剩余内存配置。 -->
  4. <property>
  5. <name>yarn.nodemanager.resource.memory-mb</name>
  6. <value>10240</value>
  7. </property>
  8. <property>
  9. <name>yarn.nodemanager.resource.cpu-vcores</name>
  10. <value>8</value>
  11. </property>
  12. <!-- 分配给容器的最小最大内存容量及虚拟核数量 -->
  13. <property>
  14. <name>yarn.scheduler.minimum-allocation-mb</name>
  15. <value>1024</value>
  16. </property>
  17. <property>
  18. <name>yarn.scheduler.maximum-allocation-mb</name>
  19. <value>4192</value>
  20. </property>
  21. <property>
  22. <name>yarn.scheduler.minimum-allocation-vcores</name>
  23. <value>1</value>
  24. </property>
  25. <property>
  26. <name>yarn.scheduler.maximum-allocation-vcores</name>
  27. <value>4</value>
  28. </property>
  29. ... ...
  30. </configuration>

2.2 修改capacity-scheduler.xml配置

此外,还需要修改capacity-scheduler.xml配置文件。
打开hadoop安装目录下的etc/hadoop/capacity-scheduler.xml,找到yarn.scheduler.capacity.maximum-am-resource-percentyarn.scheduler.capacity.resource-calculator属性,将<value>内容修改为如下值。

  1. <configuration>
  2. ......
  3. <property>
  4. <name>yarn.scheduler.capacity.maximum-am-resource-percent</name>
  5. <value>0.5</value>
  6. <description>
  7. Maximum percent of resources in the cluster which can be used to run
  8. application masters i.e. controls number of concurrent running
  9. applications.
  10. </description>
  11. </property>
  12. ......
  13. <property>
  14. <name>yarn.scheduler.capacity.resource-calculator</name>
  15. <value>org.apache.hadoop.yarn.util.resource.DominantResourceCalculator</value>
  16. <description>
  17. The ResourceCalculator implementation to be used to compare
  18. Resources in the scheduler.
  19. The default i.e. DefaultResourceCalculator only uses Memory while
  20. DominantResourceCalculator uses dominant-resource to compare
  21. multi-dimensional resources such as Memory, CPU etc.
  22. </description>
  23. </property>
  24. ......
  25. </configuration>
yarn.scheduler.capacity.maximum-am-resource-percent 集群中可用于运行application master的资源比例上限,这通常用于限制并发运行的应用程序数目。调大一些,防止以后会有多个应用上线。
org.apache.hadoop.yarn.util.resource.DominantResourceCalculator 指定调度器使用的资源计算器。默认值时DefaultResourceCalculator,只使用内存进行比较。在使用时会出现每个Container的内存分配正常,但是vcore会被限制为1个,不能充分利用集群资源。建议修改为DominantResourceCalculator,将资源的分配同时基于内存和CPU。

3、重启Yarn

以上配置后,别忘记分发后重启Yarn。

4、检查配置是否生效

登录Yarn Web UI (默认端口8088)。点击左侧列表Nodes选项进行查看。从下图中可以看出
圈1:表示申请Container的资源范围。和2.1小节中示例配置的一样。
圈2:表示当前节点分配给Yarn可使用的物理内存以及虚拟核心数。和2.1小节中示例配置的一样。
image.png