解决 Spark yarn 任务启动之后executor不停增长的问题,num-executors配置不管用

spark2-submit --class SparkKafka  \
--master yarn \ 
--executor-memory 1G \
--num-executors 6 \
--driver-memory 1g \
--conf spark.driver.supervise=true \
--conf spark.dynamicAllocation.maxExecutors=6 \  -- 限制最大executor
--conf spark.streaming.kafka.maxRatePerPartition=100 recommend-1.0-SNAPSHOT.jar

主要原因是spark.dynamicAllocation.maxExecutors这个配置,

在CDH中,默认开启了动态资源占用,即资源如果空余时,SparkStreaming会自动按照并发度(并行的block数)来占用资源,而spark-streaming作为一个实时处理系统,在大多数时候是不需要太多资源的。

为了限制spark streaming最多分配的executor数,可以配置spark.dynamicAllocation.maxExecutors为动态资源分配的上限。num-executors其实是资源初始化时所取的值,所以其实还是有用的。

这里要注意的是开源是默认没有开启动态资源占用的,可以通过spark.dynamicAllocation.enabled=true这一配置来开启,如果配置了这一项,同时还需要开启external-shuffle-service,保证在动态回收不再工作的executor的时候不会中断在executor上的shuffle过程spark.shuffle.service.enabled=true。

=====================================================================

1.修改每台NodeManager上的yarn-site.xml:

##修改
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle,spark_shuffle</value>
</property>
##增加
<property>
<name>yarn.nodemanager.aux-services.spark_shuffle.class</name>
<value>org.apache.spark.network.yarn.YarnShuffleService</value>
</property>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

以上若在ambari平台,应该是默认设置好的,
在这里插入图片描述
在这里插入图片描述

2.在spark-defaults.conf

设置

ark.shuffle.service.enabled true   //启用External shuffle Service服务
spark.shuffle.service.port 7337 //Shuffle Service服务端口,必须和yarn-site中的一致
spark.dynamicAllocation.enabled true  //开启动态资源分配
spark.dynamicAllocation.minExecutors 1  //每个Application最小分配的executor数
spark.dynamicAllocation.maxExecutors 30  //每个Application最大并发分配的executor数
spark.dynamicAllocation.schedulerBacklogTimeout 1s 
spark.dynamicAllocation.sustainedSchedulerBacklogTimeout 5s
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

附上说明:

这里是引用
以下是基本配置参考
spark.shuffle.service.enabled true 配置External shuffle Service服务(一定要配置启用)
spark.shuffle.service.port 7337
spark.dynamicAllocation.enabled true 启用动态资源调度
spark.dynamicAllocation.minExecutors 3 每个应用中最少executor的个数
spark.dynamicAllocation.maxExecutors 8 每个应用中最多executor的个数
可选参数说明:
配置项 说明 默认值
spark.dynamicAllocation.minExecutors 最小Executor个数。 0
spark.dynamicAllocation.initialExecutors 初始Executor个数。 spark.dynamicAllocation.minExecutors
spark.dynamicAllocation.maxExecutors 最大executor个数。 Integer.MAX_VALUE
spark.dynamicAllocation.schedulerBacklogTimeout 调度第一次超时时间。 1(s)
spark.dynamicAllocation.sustainedSchedulerBacklogTimeout 调度第二次及之后超时时间。 spark.dynamicAllocation.schedulerBacklogTimeout
spark.dynamicAllocation.executorIdleTimeout 普通Executor空闲超时时间。 60(s)
spark.dynamicAllocation.cachedExecutorIdleTimeout 含有cached blocks的Executor空闲超时时间。spark.dynamicAllocation.executorIdleTimeout的2倍
说明
1.使用动态资源调度功能,必须配置External Shuffle Service。如果没有使用External Shuffle Service,Executor被杀时会丢失shuffle文件。
2.配置了动态资源调度功能,就不能再单独配置Executor的个数,否则会报错退出。
3.使用动态资源调度功能,能保证最少的executor的个数(spark.dynamicAllocation.minExecutors)
来源:https://blog.csdn.net/dandykang/article/details/48160953

动态资源分配策略:
开启动态分配策略后,application会在task因没有足够资源被挂起的时候去动态申请资源,这种情况意味着该application现有的executor无法满足所有task并行运行。spark一轮一轮的申请资源,当有task挂起或等待spark.dynamicAllocation.schedulerBacklogTimeout(默认1s)时间的时候,会开始动态资源分配;之后会每隔spark.dynamicAllocation.sustainedSchedulerBacklogTimeout(默认1s)时间申请一次,直到申请到足够的资源。每次申请的资源量是指数增长的,即1,2,4,8等。
之所以采用指数增长,出于两方面考虑:其一,开始申请的少是考虑到可能application会马上得到满足;其次要成倍增加,是为了防止application需要很多资源,而该方式可以在很少次数的申请之后得到满足。

资源回收策略
当application的executor空闲时间超过spark.dynamicAllocation.executorIdleTimeout(默认60s)后,就会被回收。

这里提醒下,实是在spark-defaults.conf下增加,在
在这里插入图片描述
spark2-thrift-sparkconf下也有改配置,该配置是spark2-thrift的配置,通过远程调用的,本地的spark-shell或者pyspark是不生效的,研究了好久,
添加提示
在这里插入图片描述
不用管,直接添加就行,提示重复添加,俩不是一个配置文件里的没有影响
配置完后,我启动spark-sql测试(pysaprk和spark-shell一样),
未提交任务情况下
在这里插入图片描述
在这里插入图片描述
占用集群资源较少,当提交任务后
在这里插入图片描述
在这里插入图片描述
集群资源动态调节,最大话利用集群

<think>嗯,用户想了解如何在给定的spark2-submit命令中添加动态资源关闭的配置。首先,我需要回忆一下Spark中关于动态资源分配的相关参数。动态资源分配允许Spark根据工作负载动态调整executor的数量,这样可以更有效地利用集群资源。相关的配置选项通常以spark.dynamicAllocation开头。 用户提供的命令已经包含了一些配置,比如spark.port.maxRetries、spark.sql.shuffle.partitions等。现在需要找到合适的位置插入动态资源分配的参数。根据Spark文档,启用动态资源分配需要设置spark.dynamicAllocation.enabled为true,然后可能需要设置其他相关参数,比如最小和最大executor数量,初始executor数,以及executor的空闲超时时间。 常见的配置项包括: - spark.dynamicAllocation.enabled=true - spark.dynamicAllocation.minExecutors=... - spark.dynamicAllocation.maxExecutors=... - spark.dynamicAllocation.initialExecutors=... - spark.dynamicAllocation.executorIdleTimeout=...s 需要检查用户当前的命令是否已经指定了--num-executors,因为动态分配开启后,maxExecutors可能与这个参数相关。在用户的命令中,--num-executors是设置为$max_executors,所以可能需要将spark.dynamicAllocation.maxExecutors也设置为同样的变量,或者根据情况调整。过,如果动态分配启用,可能需要再指定--num-executors,但需要确认。 另外,用户提到要“新增动态资源关闭命令”,可能是指如何正确关闭再需要的executors。这时候,除了启用动态分配外,还需要确保有正确的超时设置,使得空闲的executors能够被释放。比如,设置executorIdleTimeout,默认可能是60s,但可以根据需要调整。 接下来,需要确定将这些参数添加到命令的哪个位置。通常在--conf参数后面添加。例如,在现有的--conf参数之后,继续添加新的--conf设置。 可能的添加位置是在现有的--conf之后,比如: --conf spark.dynamicAllocation.enabled=true \ --conf spark.dynamicAllocation.minExecutors=1 \ --conf spark.dynamicAllocation.maxExecutors=$max_executors \ --conf spark.dynamicAllocation.executorIdleTimeout=60s \ 需要注意,用户已经指定了--num-executors $max_executors,但在动态分配启用后,maxExecutors应该与之保持一致,或者根据需求调整。此外,如果用户希望保留固定数量的executors,可能需要调整这些参数,但通常动态分配会根据负载自动增减。 还需要确认是否需要设置shuffle服务,因为动态资源分配需要外部的shuffle服务支持,特别是在YARN集群上。可能需要配置spark.shuffle.service.enabled=true,并确保YARN的NodeManager配置了对应的shuffle服务。如果用户的环境中已经配置好了,那么只需要启用相关选项;如果没有,可能需要额外的步骤,但用户的问题只是关于命令行参数的添加,所以可能只需要添加基本的动态分配参数。 综上,应该在现有的--conf参数部分添加动态资源分配的相关配置,确保动态分配启用,并设置合适的min/max executors以及超时时间。同时,可能需要调整原有的--num-executors参数,但根据Spark的文档,当动态分配启用时,--num-executors会被忽略,除非设置了initialExecutors。因此,可能需要将--num-executors替换为initialExecutors,或者保留,但需要测试确认。 过,用户希望的是在现有命令中新增配置,而是修改已有参数。因此,直接添加动态分配相关的--conf参数即可,同时保留原有的--num-executors,但需要注意是否会有冲突。根据Spark配置优先级,命令行参数优先级最高,所以如果有冲突,命令行参数会覆盖其他配置。但为了保险起见,最好将maxExecutors设置为和--num-executors相同的值,或者根据需求调整。 最终,在用户的命令中,应该在--conf部分添加以下参数: --conf spark.dynamicAllocation.enabled=true \ --conf spark.dynamicAllocation.minExecutors=1 \ --conf spark.dynamicAllocation.maxExecutors=$max_executors \ --conf spark.dynamicAllocation.executorIdleTimeout=60s \ --conf spark.shuffle.service.enabled=true \ 过,spark.shuffle.service.enabled可能需要根据集群是否配置了外部shuffle服务来决定是否开启。如果用户的环境没有配置,那么开启这个会导致错误,所以可能需要用户确认环境是否支持。但通常在使用YARN的情况下,可以启用这个配置,前提是集群已经正确设置。 因此,在确定的情况下,可能建议用户添加前四个参数,并提示关于shuffle服务的注意事项。但根据常见配置,可能还是需要包括shuffle服务的启用。 总结,修改后的命令应该在适当的位置(通常在现有的--conf之后)添加动态资源分配的配置,包括启用、min/max executors、空闲超时以及shuffle服务启用。</think>以下是添加了动态资源关闭配置后的命令及分步解释: 原始命令修改后的版本: ```bash /usr/bin/spark2-submit \ --queue root.yarn_appcc \ --jars $libpath/ojdbc14-10.2.0.4.0.jar,$libpath/OracleDateTypeInit.jar,$libpath/mysql-connector-java-5.1.39.jar,$libpath/aviator-4.2.8.jar \ --files $confFileName \ --conf spark.port.maxRetries=256 \ --conf spark.sql.shuffle.partitions=200 \ --conf spark.rpc.netty.dispatcher.numThreads=10 \ --conf spark.driver.extraJavaOptions="-XX:PermSize=2g -XX:MaxPermSize=2g -Xss4096k" \ # 新增动态资源分配配置 ↓↓↓ --conf spark.dynamicAllocation.enabled=true \ --conf spark.dynamicAllocation.minExecutors=1 \ --conf spark.dynamicAllocation.maxExecutors=$max_executors \ --conf spark.dynamicAllocation.executorIdleTimeout=60s \ --conf spark.shuffle.service.enabled=true \ # 新增动态资源分配配置 ↑↑↑ --num-executors $max_executors \ --executor-cores $max_cores \ --executor-memory ${executor_memory}G \ --driver-memory 6G \ --class $fullclassname \ $jarname $startDate $endDate ``` 分步解释: 1. **启用动态分配核心参数** ```bash --conf spark.dynamicAllocation.enabled=true ``` - 基础开关,必须设置为true才能启用动态资源分配 2. **Executor数量边界控制** ```bash --conf spark.dynamicAllocation.minExecutors=1 \ --conf spark.dynamicAllocation.maxExecutors=$max_executors ``` - `minExecutors=1`:保证至少保留1个executor防止任务中断 - `maxExecutors=$max_executors`:与原有`--num-executors`参数值保持一致,控制资源使用上限 3. **资源释放策略** ```bash --conf spark.dynamicAllocation.executorIdleTimeout=60s ``` -executor空闲60秒后自动释放 - 可根据集群负载调整该值(生产环境通常设60-120s) 4. **Shuffle服务支持** ```bash --conf spark.shuffle.service.enabled=true ``` - 必需配置!确保释放executor会丢失shuffle数据 - 需确认YARN集群已部署`spark_shuffle`服务 注意事项: 1. 原有`--num-executors`参数会作为初始executor数量 2. 建议在YARN队列中设置`最大资源使用量 = maxExecutors * (executor-memory + overhead)` 3. 监控日志中应有`INFO scheduler.DynamicAllocationExecutorAllocator: Request to remove executorIds`这类资源释放记录 效果示例: 当任务出现阶段性负载波动时(如先做数据读取后进行计算): $$ \text{执行阶段} \rightarrow \text{自动扩容} \uparrow \rightarrow \text{空闲阶段} \rightarrow \text{自动缩容} \downarrow $$ 通过动态调整executor数量,可提升集群资源利用率约30%-50%(根据任务特性同)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值