1. 程式人生 > >Spark on Yarn Client和Cluster模式詳解

Spark on Yarn Client和Cluster模式詳解

Spark在YARN中有yarn-cluster和yarn-client兩種執行模式: I. Yarn Cluster Spark Driver首先作為一個ApplicationMaster在YARN叢集中啟動,客戶端提交給ResourceManager的每一個job都會在叢集的worker節點上分配一個唯一的ApplicationMaster,由該ApplicationMaster管理全生命週期的應用。因為Driver程式在YARN中執行,所以事先不用啟動Spark Master/Client,應用的執行結果不能在客戶端顯示(可以在history server中檢視),所以最好將結果儲存在HDFS而非stdout輸出,客戶端的終端顯示的是作為YARN的job的簡單執行狀況。

by @Sandy Ryza

by 明風@taobao
從terminal的output中看到任務初始化更詳細的四個步驟:
  1. 14/09/28 11:24:52 INFO RMProxy: Connecting to ResourceManager at hdp01/172.19.1.231:8032
  2. 14/09/28 11:24:52 INFO Client: Got Cluster metric info from ApplicationsManager (ASM), number of NodeManagers: 4
  3. 14/09/28 11:24:52 INFO Client: Queue info ... queueName: root.default, queueCurrentCapacity: 0.0, queueMaxCapacity: -1.0,
  4. queueApplicationCount = 0, queueChildQueueCount = 0
  5. 14/09/28 11:24:52 INFO Client: Max mem capabililty of a single resource in this cluster 8192
  6. 14/09/28 11:24:53 INFO Client: Uploading file:/usr/lib/spark/examples/lib/spark-examples_2.10-1.0.0-cdh5.1.0.jar to hdfs://hdp01:8020/user/spark/.sparkStaging/application_1411874193696_0003/spark-examples_2.10-1.0.0-cdh5.1.0.jar
  7. 14/09/28 11:24:54 INFO Client: Uploading file:/usr/lib/spark/assembly/lib/spark-assembly-1.0.0-cdh5.1.0-hadoop2.3.0-cdh5.1.0.jar to hdfs://hdp01:8020/user/spark/.sparkStaging/application_1411874193696_0003/spark-assembly-1.0.0-cdh5.1.0-hadoop2.3.0-cdh5.1.0.jar
  8. 14/09/28 11:24:55 INFO Client: Setting up the launch environment
  9. 14/09/28 11:24:55 INFO Client: Setting up container launch context
  10. 14/09/28 11:24:55 INFO Client: Command for starting the Spark ApplicationMaster: List($JAVA_HOME/bin/java, -server, -Xmx512m, -Djava.io.tmpdir=$PWD/tmp, -Dspark.master="spark://hdp01:7077", -Dspark.app.name="org.apache.spark.examples.SparkPi", -Dspark.eventLog.enabled="true", -Dspark.eventLog.dir="/user/spark/applicationHistory", -Dlog4j.configuration=log4j-spark-container.properties, org.apache.spark.deploy.yarn.ApplicationMaster, --class, org.apache.spark.examples.SparkPi, --jar , file:/usr/lib/spark/examples/lib/spark-examples_2.10-1.0.0-cdh5.1.0.jar, , --executor-memory, 1024, --executor-cores, 1, --num-executors , 2, 1>, <LOG_DIR>/stdout, 2>, <LOG_DIR>/stderr)
  11. 14/09/28 11:24:55 INFO Client: Submitting application to ASM
  12. 14/09/28 11:24:55 INFO YarnClientImpl: Submitted application application_1411874193696_0003
  13. 14/09/28 11:24:56 INFO Client: Application report from ASM:
  14. application identifier: application_1411874193696_0003
  15. appId: 3
  16. clientToAMToken: null
  17. appDiagnostics:
  18. appMasterHost: N/A
  19. appQueue: root.spark
  20. appMasterRpcPort: -1
  21. appStartTime: 1411874695327
  22. yarnAppState: ACCEPTED
  23. distributedFinalState: UNDEFINED
  24. appTrackingUrl: http://hdp01:8088/proxy/application_1411874193696_0003/
  25. appUser: spark
複製程式碼
1. 由client向ResourceManager提交請求,並上傳jar到HDFS上
這期間包括四個步驟:
a). 連線到RM
b). 從RM ASM(ApplicationsManager )中獲得metric、queue和resource等資訊。
c). upload app jar and spark-assembly jar
d). 設定執行環境和container上下文(launch-container.sh等指令碼)

2. 
ResouceManager向NodeManager申請資源,建立Spark ApplicationMaster(每個SparkContext都有一個ApplicationMaster)
3. NodeManager啟動Spark App Master,並向ResourceManager AsM註冊
4. Spark ApplicationMaster從HDFS中找到jar檔案,啟動DAGscheduler和YARN Cluster Scheduler
5. ResourceManager向ResourceManager AsM註冊申請container資源(INFO YarnClientImpl: Submitted application)
6. ResourceManager通知NodeManager分配Container,這時可以收到來自ASM關於container的報告。(每個container的對應一個executor)
7. Spark ApplicationMaster直接和container(executor)進行互動,完成這個分散式任務。
需要注意的是:
a). Spark中的localdir會被yarn.nodemanager.local-dirs替換
b). 允許失敗的節點數(spark.yarn.max.worker.failures)為executor數量的兩倍數量,最小為3.
c). SPARK_YARN_USER_ENV傳遞給spark程序的環境變數
d). 傳遞給app的引數應該通過–args指定。
部署:
環境介紹:

hdp0[1-4]四臺主機
hadoop使用CDH 5.1版本: hadoop-2.3.0+cdh5.1.0+795-1.cdh5.1.0.p0.58.el6.x86_64
直接下載對應2.3.0的pre-build版本http://spark.apache.org/downloads.html
下載完畢後解壓,檢查spark-assembly目錄:
  1. file /home/spark/spark-1.1.0-bin-hadoop2.3/lib/spark-assembly-1.1.0-hadoop2.3.0.jar
  2. /home/spark/spark-1.1.0-bin-hadoop2.3/lib/spark-assembly-1.1.0-hadoop2.3.0.jar: Zip archive data, at least v2.0 to extract
複製程式碼

然後輸出環境變數HADOOP_CONF_DIR/YARN_CONF_DIR和SPARK_JAR(可以設定到spark-env.sh中)
  1. export HADOOP_CONF_DIR=/etc/hadoop/etc
  2. export SPARK_JAR=/home/spark/spark-1.1.0-bin-hadoop2.3/lib/spark-assembly-1.1.0-hadoop2.3.0.jar
複製程式碼

如果使用cloudera manager 5,在Spark Service的操作中可以找到Upload Spark Jar將spark-assembly上傳到HDFS上。




Spark Jar Location (HDFS) 
spark_jar_hdfs_path
/user/spark/share/lib/spark-assembly.jar 預設值 The location of the Spark jar in HDFS
Spark History Location (HDFS) 
spark.eventLog.dir
/user/spark/applicationHistory 預設值 The location of Spark application history logs in HDFS. Changing this value will not move existing logs to the new location.
提交任務,此時在YARN的web UI和history Server上就可以看到執行狀態資訊。
  1. spark-submit --class org.apache.spark.examples.SparkPi --master yarn-cluster /usr/lib/spark/examples/lib/spark-examples_2.10-1.0.0-cdh5.1.0.jar
複製程式碼
II. yarn-client (YarnClientClusterScheduler)檢視對應類的檔案
在yarn-client模式下,Driver執行在Client上,通過ApplicationMaster向RM獲取資源。本地Driver負責與所有的executor container進行互動,並將最後的結果彙總。結束掉終端,相當於kill掉這個spark應用。一般來說,如果執行的結果僅僅返回到terminal上時需要配置這個。


客戶端的Driver將應用提交給Yarn後,Yarn會先後啟動ApplicationMaster和executor,另外ApplicationMaster和executor都 是裝載在container裡執行,container預設的記憶體是1G,ApplicationMaster分配的記憶體是driver- memory,executor分配的記憶體是executor-memory。同時,因為Driver在客戶端,所以程式的執行結果可以在客戶端顯 示,Driver以程序名為SparkSubmit的形式存在。
配置YARN-Client模式同樣需要HADOOP_CONF_DIR/YARN_CONF_DIR和SPARK_JAR變數。
提交任務測試:
  1. spark-submit --class org.apache.spark.examples.SparkPi --deploy-mode client /usr/lib/spark/examples/lib/spark-examples_2.10-1.0.0-cdh5.1.0.jar
  2. terminal output:
  3. 14/09/28 11:18:34 INFO Client: Command for starting the Spark ApplicationMaster: List($JAVA_HOME/bin/java, -server, -Xmx512m, -Djava.io.tmpdir=$PWD/tmp, -Dspark.tachyonStore.folderName="spark-9287f0f2-2e72-4617-a418-e0198626829b", -Dspark.eventLog.enabled="true", -Dspark.yarn.secondary.jars="", -Dspark.driver.host="hdp01", -Dspark.driver.appUIHistoryAddress="", -Dspark.app.name="Spark Pi", -Dspark.jars="file:/usr/lib/spark/examples/lib/spark-examples_2.10-1.0.0-cdh5.1.0.jar", -Dspark.fileserver.uri="http://172.19.17.231:53558", -Dspark.eventLog.dir="/user/spark/applicationHistory", -Dspark.master="yarn-client", -Dspark.driver.port="35938", -Dspark.httpBroadcast.uri="http://172.19.17.231:43804", -Dlog4j.configuration=log4j-spark-container.properties, org.apache.spark.deploy.yarn.ExecutorLauncher, --class, notused, --jar , null, --args 'hdp01:35938' , --executor-memory, 1024, --executor-cores, 1, --num-executors , 2, 1>, <LOG_DIR>/stdout, 2>, <LOG_DIR>/stderr)
  4. 14/09/28 11:18:34 INFO Client: Submitting application to ASM
  5. 14/09/28 11:18:34 INFO YarnClientSchedulerBackend: Application report from ASM:
  6. appMasterRpcPort: -1
  7. appStartTime: 1411874314198
  8. yarnAppState: ACCEPTED
  9. ......
複製程式碼 ##最後將結果輸出到terminal中