SparkStandalone模式兼Yarn模式部署Sp

Spark Standalone模式兼Yarn模式部署

很多教程都教你如何部署Standalone模式或Yarn模式,然后每部署模式单独一个文件夹(如spark-local、spark-yarn),很浪费空间。此教程针对有一定大数据部署经验者。
注意:
1.workers、spark-env.sh、spark-default.conf是在spark的conf文件夹下;
2.Standalone模式或Yarn模式的任务都会现在在历史服务器 http://hd1:18080 里面。

profile配置

/etc/profile

# yarn模式相关,原本是配置在spark-env.sh
export YARN_CONF_DIR=$HADOOP_HOME/etc/hadoop
复制代码

workers

# 通用Standalone(HA)模式模式配置,计算和存储都需要自己准备,所以需要三台,单独部署yarn模式不用配置这个
hd1
hd2
hd3
复制代码

spark-env.sh

# 通用配置
export JAVA_HOME=/opt/soft/java
# 下面注释的是Standalone单master模式,使用Standalone HA模式就把下面两行注释掉
#SPARK_MASTER_HOST=hd1
#SPARK_MASTER_PORT=7077
# Standalone HA模式
SPARK_MASTER_WEBUI_PORT=8082
export SPARK_DAEMON_JAVA_OPTS="
-Dspark.deploy.recoveryMode=ZOOKEEPER
-Dspark.deploy.zookeeper.url=hd1,hd2,hd3
-Dspark.deploy.zookeeper.dir=/spark"
# 通用历史服务器配置
export SPARK_HISTORY_OPTS="
-Dspark.history.ui.port=18080
-Dspark.history.fs.logDirectory=hdfs://hd1:8020/spark/history
-Dspark.history.retainedApplications=30"

复制代码

针对历史服务器创建对应文件(如果没有)

hdfs dfs -mkdir -p /spark/history

复制代码

spark-default.conf

# 通用历史服务器配置
spark.eventLog.enabled           true
spark.eventLog.dir               hdfs://hd1:8020/spark/history
# add two configuration and click yarn application history to forward to spark ui
# 添加这个配置点击http://hd2:8088/cluster/apps里面对应应用的History能跳到spark 18080服务器
spark.yarn.historyServer.address=hd1:18080
spark.history.ui.port=18080
复制代码

验证

启动相关组件,执行下面对应命令

# standalone(HA)模式
bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://hd1:7077,hd2:7077 \
./examples/jars/spark-examples_2.12-3.1.1.jar \
10
# yarn模式
bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master yarn \
--deploy-mode cluster \
./examples/jars/spark-examples_2.12-3.1.1.jar \
10
复制代码

查看效果:
standalone(HA)模式执行完查看http://hd1:8082 (Spark UI)和 http://hd1:18080/ (历史服务器)是否有对应信息,yarn模式执行完查看http://hd2:8088/cluster/apps (Yarn UI)和 http://hd1:18080/ (历史服务器)是否有对应信息且点击Yarn UI的history能跳到历史服务器。