Example: ./bin/spark-submit \ --[your class] \ --master yarn \ --deploy-mode cluster \ --num-exectors 17 --conf spark.yarn.executor.memoryOverhead=4096 \ --executor-memory 35G \ //Amount of memory to use per executor process --conf spark.yarn.driver.memoryOverhead=4096 \ --driver-memory 35G \ //Amount of memory to be used for the driver process --executor-cores 5 --driver-cores 5 \ //number of cores to use for the driver process --conf spark.default.parallelism=170 /path/to/examples.jar
上圖所示的作業,直接使用了 Spark 官方的 example 包,所以不需要自己上傳 jar 包。
我一般:
spark-submit --master yarn-client --driver-memory 10g –-num-executors 25 --executor-memory 6g --executor-cores 4 --conf spark.yarn.driver.memoryOverhead=4096 path_walk_learning.py >log202102100938.txt
--class org.apache.spark.examples.SparkPi --master yarn --deploy-mode client --driver-memory 4g --num-executors 2 --executor-memory 2g --executor-cores 2 /opt/apps/spark-1.6.0-bin-hadoop2.6/lib/spark-examples*.jar 10
參數說明如下所示:
參數 | 參考值 | 說明 |
---|---|---|
class | org.apache.spark.examples.SparkPi | 作業的主類。 |
master | yarn | 因為 E-MapReduce 使用 yarn 的模式,所以這里只能是 yarn 模式。 |
yarn-client | 等同於 –-master yarn —deploy-mode client, 此時不需要指定deploy-mode。 | |
yarn-cluster | 等同於 –-master yarn —deploy-mode cluster, 此時不需要指定deploy-mode。 | |
deploy-mode | client | client 模式表示作業的 AM 會放在 Master 節點上運行。要注意的是,如果設置這個參數,那么需要同時指定上面 master 為 yarn。 |
cluster | cluster 模式表示 AM 會隨機的在 worker 節點中的任意一台上啟動運行。要注意的是,如果設置這個參數,那么需要同時指定上面 master 為yarn。 | |
driver-memory | 4g | driver 使用的內存,不可超過單機的總內存。 |
num-executors | 2 | 創建多少個 executor。 |
executor-memory | 2g | 各個 executor 使用的最大內存,不可超過單機的最大可使用內存。 |
executor-cores | 2 | 各個 executor 使用的並發線程數目,即每個 executor 最大可並發執行的 Task 數目。 |
資源計算
- yarn-client 模式的資源計算
節點 資源類型 資源量(結果使用上面的例子計算得到) master core 1 mem driver-memroy = 4G worker core num-executors * executor-cores = 4 mem num-executors * executor-memory = 4G - 作業主程序(Driver 程序)會在 master 節點上執行。按照作業配置將分配 4G(由 —driver-memroy 指定)的內存給它(當然實際上可能沒有用到)。
- 會在 worker 節點上起 2 個(由 —num-executors 指定)executor,每一個 executor 最大能分配 2G(由 —executor-memory 指定)的內存,並最大支持 2 個(由—executor-cores 指定)task 的並發執行。
- yarn-cluster 模式的資源計算
節點 資源類型 資源量(結果使用上面的例子計算得到) master - 一個很小的 client 程序,負責同步 job 信息,占用很小。 worker core num-executors * executor-cores+spark.driver.cores = 5 mem num-executors * executor-memory + driver-memroy = 8g 說明 這里的 spark.driver.cores 默認是 1,也可以設置為更多。
資源使用的優化
- yarn-client 模式
若您有了一個大作業,使用 yarn-client 模式,想要多用一些這個集群的資源,請參見如下配置:
--master yarn-client --driver-memory 5g –-num-executors 20 --executor-memory 4g --executor-cores 4
注意- Spark 在分配內存時,會在用戶設定的內存值上溢出 375M 或 7%(取大值)。
- Yarn 分配 container 內存時,遵循向上取整的原則,這里也就是需要滿足 1G 的整數倍。
按照上述的資源計算公式,
-
master 的資源量為:
- core:1
- mem:6G (5G + 375M 向上取整為 6G)
-
workers 的資源量為:
- core: 20*4 = 80
- mem:20*5G (4G + 375M 向上取整為 5G)= 100G
可以看到總的資源沒有超過集群的總資源,那么遵循這個原則,您還可以有很多種配置,例如:--master yarn-client --driver-memory 5g --num-executors 40 --executor-memory 1g --executor-cores 2
--master yarn-client --driver-memory 5g --num-executors 15 --executor-memory 4g --executor-cores 4
--master yarn-client --driver-memory 5g --num-executors 10 --executor-memory 9g --executor-cores 6
原則上,按照上述的公式計算出來的需要資源不超過集群的最大資源量就可以。但在實際場景中,因為系統、hdfs 以及 E-MapReduce 的服務會需要使用 core 和 mem 資源,如果把 core 和 mem 都占用完了,反而會導致性能的下降,甚至無法運行。
executor-cores 數一般也都會被設置成和集群的可使用核一致,因為如果設置的太多,CPU 會頻繁切換,性能並不會提高。
- yarn-cluster 模式
當使用 yarn-cluster 模式后,Driver 程序會被放到 worker 節點上。會占用到 worker 資源池里的資源,這時若想要多用一些這個集群的資源,請參見如下配置。
--master yarn-cluster --driver-memory 5g --num-executors 15 --executor-memory 4g --executor-cores 4
配置建議
-
如果將內存設置的很大,要注意 GC 所產生的消耗。一般我們會推薦每一個 executor 的內存 <= 64G。
-
如果是進行 HDFS 讀寫的作業,建議是每個 executor 中使用 <= 5個並發來讀寫。
-
如果是進行 OSS 讀寫的作業,我們建議是將 executor 分布在不同的 ECS 上,這樣可以將每一個 ECS 的帶寬都用上。例如,有 10 台 ECS,那么就可以配置 num-executors=10,並設置合理的內存和並發。
-
如果作業中使用了非線程安全的代碼,那么在設置 executor-cores 的時候需要注意多並發是否會造成作業的不正常。如果會,那么推薦就設置 executor-cores=1。