前期博客
Spark on YARN模式的安裝(spark-1.6.1-bin-hadoop2.6.tgz + hadoop-2.6.0.tar.gz)(master、slave1和slave2)(博主推薦)
Spark On YARN模式
這是一種很有前景的部署模式。但限於YARN自身的發展,目前僅支持粗粒度模式(Coarse-grained Mode)。這是由於YARN上的Container資源是不可以動態伸縮的,一旦Container啟動之后,可使用的資源不能再發生變化,不過這個已經在YARN計划中了。
spark on yarn 的支持兩種模式:
1) yarn-cluster:適用於生產環境;
2) yarn-client:適用於交互、調試,希望立即看到app的輸出
yarn-cluster和yarn-client的區別在於yarn appMaster,每個yarn app實例有一個appMaster進程,是為app啟動的第一個container;負責從ResourceManager請求資源,獲取到資源后,告訴NodeManager為其啟動container。yarn-cluster和yarn-client模式內部實現還是有很大的區別。如果你需要用於生產環境,那么請選擇yarn-cluster;而如果你僅僅是Debug程序,可以選擇yarn-client。
YARN概述
YARN是什么
Apache Hadoop YARN(Yet Another Resource Negotiator,另一種資源協調者)是一種新的 Hadoop 資源管理器,它是一個通用資源管理系統,可為上層應用提供統一的資源管理和調度,它的引入為集群在利用率、資源統一管理和數據共享等方面帶來了巨大好處。
YARN在Hadoop生態系統中的位置
YARN產生的背景
隨着互聯網高速發展導致數據量劇增,MapReduce 這種基於磁盤的離線計算框架已經不能滿足應用要求,從而出現了一些新的計算框架以應對各種場景,包括內存計算框架、流式計算框架和迭代式計算框架等,而MRv1 不能支持多種計算框架並存。
YARN基本架構
ResourceManager(RM)
ResourceManager負責集群資源的統一管理和調度,承擔了 JobTracker 的角色,整個集群只有“一個”,總的來說,RM有以下作用:
1.處理客戶端請求
2.啟動或監控ApplicationMaster
3.監控NodeManager
4.資源的分配與調度
NodeManager(NM)
NodeManager管理YARN集群中的每個節點。NodeManager 提供針對集群中每個節點的服務,從監督對一個容器的終生管理到監視資源和跟蹤節點健康。MRv1 通過slot管理 Map 和 Reduce 任務的執行,而 NodeManager 管理抽象容器,這些容器代表着可供一個特定應用程序使用的針對每個節點的資源。NM有以下作用。
1.管理單個節點上的資源
2.處理來自ResourceManager的命令
3.處理來自ApplicationMaster的命令
ApplicationMaster(AM)
每個應用有一個,負責應用程序的管理 。ApplicationMaster 負責協調來自 ResourceManager 的資源,並通過 NodeManager 監視容器的執行和資源使用(CPU、內存等的資源分配)。請注意,盡管目前的資源更加傳統(CPU 核心、內存),但未來會支持新資源類型(比如圖形處理單元或專用處理設備)。AM有以下作用:
1.負責數據的切分
2.為應用程序申請資源並分配給內部的任務
3.任務的監控與容錯
Container
Container 是 YARN 中的資源抽象,它封裝了某個節點上的多維度資源,如內存、CPU、磁盤、網絡等,當AM向RM申請資源時,RM為AM返回的資源便是用Container表示的。YARN會為每個任務分配一個Container,且該任務只能使用該Container中描述的資源。
Container有以下作用:
對任務運行環境進行抽象,封裝CPU、內存等多維度的資源以及環境變量、啟動命令等任務運行相關的信息
Spark on YARN運行架構解析
回顧Spark基本工作流程
以SparkContext為程序運行的總入口,在SparkContext的初始化過程中,Spark會分別創建DAGScheduler作業調度和TaskScheduler任務調度兩級調度模塊。其中作業調度模塊是基於任務階段的高層調度模塊,它為每個Spark作業計算具有依賴關系的多個調度階段(通常根據shuffle來划分),然后為每個階段構建出一組具體的任務(通常會考慮數據的本地性等),然后以TaskSets(任務組)的形式提交給任務調度模塊來具體執行。而任務調度模塊則負責具體啟動任務、監控和匯報任務運行情況。
YARN standalone/YARN cluster
YARN standalone是0.9及之前版本的叫法,1.0開始更名為YARN cluster
yarn-cluster(YarnClusterScheduler),是Driver和AM運行在一起,Client單獨的。
./bin/spark-submit --class path.to.your.Class --master yarn --deploy-mode cluster [options] [app options]
YARN standalone/YARN cluster
Spark Driver首選作為一個ApplicationMaster在Yarn集群中啟動,客戶端提交給ResourceManager的每一個job都會在集群的worker節點上分配一個唯一的ApplicationMaster,由該ApplicationMaster管理全生命周期的應用。因為Driver程序在YARN中運行,所以事先不用啟動Spark Master/Client,應用的運行結果不能再客戶端顯示(可以在history server中查看)。
YARN standalone/YARN cluster
YARN client
yarn-client(YarnClientClusterScheduler)
Client和Driver運行在一起(運行在本地),AM只用來管理資源
./bin/spark-submit --class path.to.your.Class --master yarn --deploy-mode client [options] [app options]
YARN client
在Yarn-client模式下,Driver運行在Client上,通過ApplicationMaster向RM獲取資源。本地Driver負責與所有的executor container進行交互,並將最后的結果匯總。結束掉終端,相當於kill掉這個spark應用。一般來說,如果運行的結果僅僅返回到terminal上時需要配置這個。
如何選擇
如果需要返回數據到client就用YARN client模式。
數據存儲到hdfs的建議用YARN cluster模式。
其他配置和注意事項
如何更改默認配置
spark_home/conf/spark-defaults.conf,每個app提交時都會使用他里面的配置
--conf PROP=VALUE,為單獨的app指定個性化參數
環境變量
spark_home/conf/spark-defaults.conf,每個app提交時都會使用他里面的配置
spark.yarn.appMasterEnv.[EnvironmentVariableName]
相關配置
特別注意
在cluster mode下,yarn.nodemanager.local-dirs對?Spark executors 和Spark driver都管用, spark.local.dir將被忽略
在client mode下, Spark executors 使用yarn.nodemanager.local-dirs, Spark driver使用spark.local.dir
--files and –archives支持用#映射到hdfs
--jars
spark-shell運行在YARN上(這是Spark on YARN模式)
(包含YARN client和YARN cluster)(作為補充)
登陸安裝Spark那台機器
bin/spark-shell --master yarn-client
或者
bin/spark-shell --master yarn
包括可以加上其他的,比如控制內存啊等。這很簡單,不多贅述。
[spark@master spark-1.6.1-bin-hadoop2.6]$ bin/spark-shell --master yarn-client 17/03/29 22:40:04 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 17/03/29 22:40:04 INFO spark.SecurityManager: Changing view acls to: spark 17/03/29 22:40:04 INFO spark.SecurityManager: Changing modify acls to: spark 17/03/29 22:40:04 INFO spark.SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(spark); users with modify permissions: Set(spark) 17/03/29 22:40:05 INFO spark.HttpServer: Starting HTTP Server 17/03/29 22:40:06 INFO server.Server: jetty-8.y.z-SNAPSHOT 17/03/29 22:40:06 INFO server.AbstractConnector: Started SocketConnector@0.0.0.0:35692 17/03/29 22:40:06 INFO util.Utils: Successfully started service 'HTTP class server' on port 35692. Welcome to ____ __ / __/__ ___ _____/ /__ _\ \/ _ \/ _ `/ __/ '_/ /___/ .__/\_,_/_/ /_/\_\ version 1.6.1 /_/ Using Scala version 2.10.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60)
提交spark作業
1、用yarn-client
模式提交spark作業
在/usr/local/spark
目錄下創建文件夾
vi spark_pi.sh
$SPARK_HOME/bin/spark-submit \
--class org.apache.spark.examples.JavaSparkPi \ --master yarn-client \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
$SPARK_HOME/lib/spark-examples-1.6.1-hadoop2.6.0.jar \
chmod 777 spark_pi.sh ./spark_pi.sh
或者
2、用yarn-cluster
模式提交spark作業
在/usr/local/spark
目錄下創建文件夾
vi spark_pi.sh
$SPARK_HOME/bin/spark-submit \
--class org.apache.spark.examples.JavaSparkPi \ --master yarn-cluster \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
$SPARK_HOME/lib/spark-examples-1.6.1-hadoop2.6.0.jar
chmod 777 spark_pi.sh
./spark_pi.sh
或者
[spark@master ~]$ $SPARK_HOME/bin/spark-submit \
> --class org.apache.spark.examples.JavaSparkPi \ > --master yarn-cluster \ > --num-executors 1 \ > --driver-memory 1g \ > --executor-memory 1g \ > --executor-cores 1 \ > $SPARK_HOME/lib/spark-examples-1.6.1-hadoop2.6.0.jar
1、Spark on YARN下運行wordcount
具體,請移步
Spark編程環境搭建(基於Intellij IDEA的Ultimate版本)(包含Java和Scala版的WordCount)(博主強烈推薦)
● wordcount代碼
● mvn 項目打包上傳至Spark集群。
● Spark 集群提交作業
[spark@master hadoop-2.6.0]$ $HADOOP_HOME/bin/hadoop fs -mkdir -p hdfs://master:9000/testspark/inputData/wordcount
[spark@master ~]$ mkdir -p /home/spark/testspark/inputData/wordcount
[spark@master hadoop-2.6.0]$ $HADOOP_HOME/bin/hadoop fs -copyFromLocal /home/spark/testspark/inputData/wordcount/wc.txt hdfs://master:9000/testspark/inputData/wordcount/
這里在/home/spark/testspark下上傳mySpark-1.0-SNAPSHOT.jar省略
[spark@master spark-1.6.1-bin-hadoop2.6]$ $SPARK_HOME/bin/spark-submit \
--master yarn-client \
--name scalawordcount \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
--class zhouls.bigdata.MyScalaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyScalaWordCount
或者
[spark@master spark-1.6.1-bin-hadoop2.6]$ $SPARK_HOME/bin/spark-submit \
--master yarn\
--deploy-mode client \
--name scalawordcount \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
--class zhouls.bigdata.MyScalaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyScalaWordCount
[spark@master spark-1.6.1-bin-hadoop2.6]$ $SPARK_HOME/bin/spark-submit \
--master yarn-cluster\
--name scalawordcount \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
--class zhouls.bigdata.MyScalaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyScalaWordCount
或者
[spark@master spark-1.6.1-bin-hadoop2.6]$ $SPARK_HOME/bin/spark-submit \
--master yarn\
--deploy-mode cluster \
--name scalawordcount \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
--class zhouls.bigdata.MyScalaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyScalaWordCount
[spark@master spark-1.6.1-bin-hadoop2.6]$ $SPARK_HOME/bin/spark-submit \
--master yarn-client \
--name javawordcount \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
--class zhouls.bigdata.MyJavaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyJavaWordCount
或者
[spark@master spark-1.6.1-bin-hadoop2.6]$ $SPARK_HOME/bin/spark-submit \
--master yarn\
--deploy-mode client \
--name javawordcount \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
--class zhouls.bigdata.MyJavaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyJavaWordCount
[spark@master spark-1.6.1-bin-hadoop2.6]$ $SPARK_HOME/bin/spark-submit \
--master yarn-cluster\
--name javawordcount \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
--class zhouls.bigdata.MyJavaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyJavaWordCount
或者
[spark@master spark-1.6.1-bin-hadoop2.6]$ $SPARK_HOME/bin/spark-submit \
--master yarn\
--deploy-mode cluster \
--name javawordcount \ --num-executors 1 \ --driver-memory 1g \ --executor-memory 1g \ --executor-cores 1 \
--class zhouls.bigdata.MyJavaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyJavaWordCount
2、Spark Standalone 下運行wordcount
具體,請移步
Spark編程環境搭建(基於Intellij IDEA的Ultimate版本)(包含Java和Scala版的WordCount)(博主強烈推薦)
● wordcount代碼
● mvn 項目打包上傳至Spark集群。
● Spark 集群提交作業
$SPARK_HOME/bin/spark-submit \
--master spark://master:7077 \
--class zhouls.bigdata.MyScalaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyScalaWordCount
或者
$SPARK_HOME/bin/spark-submit \
--master spark://master:7077 \
--class zhouls.bigdata.MyJavaWordCount \
/home/spark/testspark/mySpark-1.0-SNAPSHOT.jar \
hdfs://master:9000/testspark/inputData/wordcount/wc.txt \
hdfs://master:9000/testspark/outData/MyJavaWordCount