本次实战用到了三台CentOS7的机器,身份信息如下所示:
IP地址 | hostname(主机名) | 身份 |
---|---|---|
192.168.119.163 | node0 | NameNode、ResourceManager、HistoryServer、Master |
192.168.119.164 | node1 | DataNode、NodeManager、Worker |
192.168.119.165 | node2 | DataNode、NodeManager、Worker 、SecondaryNameNode |
要注意的地方:
部署spark2.2集群on Yarn模式的前提,是先搭建好hadoop集群环境,请参考《Linux部署hadoop2.7.7集群》一文,将hadoop集群环境部署并启动成功;
如果您已经完成了hadoop集群和spark集群(standalone模式)的部署,接下来只需要两步设置即可:
export HADOOP_CONF_DIR=/home/hadoop/hadoop-2.7.7/etc/hadoop
<property>
<name>yarn.nodemanager.pmem-check-enabledname>
<value>falsevalue>
property>
<property>
<name>yarn.nodemanager.vmem-check-enabledname>
<value>falsevalue>
property>
本次实战一共有三台电脑,请确保在每台电脑上都做了上述配置;
hadoop和spark都部署在当前账号的家目录下,因此启动命令和顺序如下:
~/hadoop-2.7.7/sbin/start-dfs.sh \
&& ~/hadoop-2.7.7/sbin/start-yarn.sh \
&& ~/hadoop-2.7.7/sbin/mr-jobhistory-daemon.sh start historyserver \
&& ~/spark-2.3.2-bin-hadoop2.7/sbin/start-all.sh
~/hadoop-2.7.7/bin/hdfs dfs -mkdir /input
~/hadoop-2.7.7/bin/hdfs dfs -put ~/GoneWiththeWind.txt /input
~/spark-2.3.2-bin-hadoop2.7/bin/spark-shell --master yarn --deploy-mode client
以下信息表示启动成功:
2019-02-09 10:13:09 WARN NativeCodeLoader:62 - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
2019-02-09 10:13:15 WARN Client:66 - Neither spark.yarn.jars nor spark.yarn.archive is set, falling back to uploading libraries under SPARK_HOME.
Spark context Web UI available at http://node0:4040
Spark context available as 'sc' (master = yarn, app id = application_1549678248927_0001).
Spark session available as 'spark'.
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/___/ .__/\_,_/_/ /_/\_\ version 2.3.2
/_/
Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_191)
Type in expressions to have them evaluated.
Type :help for more information.
scala>
sc.textFile("hdfs://node0:8020/input/GoneWiththeWind.txt").flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey(_ + _).sortBy(_._2,false).take(10).foreach(println)
控制台输出如下,可见任务执行成功:
scala> sc.textFile("hdfs://node0:8020/input/GoneWiththeWind.txt").flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey(_ + _).sortBy(_._2,false).take(10).foreach(println)
(the,18264)
(and,14150)
(to,10020)
(of,8615)
(a,7571)
(her,7086)
(she,6217)
(was,5912)
(in,5751)
(had,4502)
如果您的开发语言是java,请将应用编译构建为jar包,然后执行以下命令,就会以client模式提交任务到yarn:
~/spark-2.3.2-bin-hadoop2.7/bin/spark-submit \
--master yarn \
--deploy-mode client \
--class com.bolingcavalry.sparkwordcount.WordCount \
--executor-memory 512m \
--total-executor-cores 2 \
~/jars/sparkwordcount-1.0-SNAPSHOT.jar \
192.168.119.163 \
8020 \
GoneWiththeWind.txt
上述命令的最后三个参数是WorkCount类运行时需要用到的参数,该应用的详情请参考《第一个spark应用开发详解(java版)》;
如果需要停止hadoop和spark服务,命令和顺序如下:
~/spark-2.3.2-bin-hadoop2.7/sbin/stop-all.sh \
&& ~/hadoop-2.7.7/sbin/mr-jobhistory-daemon.sh stop historyserver \
&& ~/hadoop-2.7.7/sbin/stop-yarn.sh \
&& ~/hadoop-2.7.7/sbin/stop-dfs.sh
至此,Spark on Yarn模式的集群部署和验证已经完成,希望能够带给您一些参考;