Spark集群模式搭建之Yarn模式
配置第一台服务器bigdata01
我的spark压缩包在/opt/modules 将spark解压到/opt/installs
cd /opt/modules/
tar -zxf spark-3.1.2-bin-hadoop3.2.tgz -C /opt/installs
将解压后的文件重命名,并对设置一个软连接。
cd /opt/installs
mv spark-3.1.2-bin-hadoop3.2 spark-yarn
rm -rf /opt/installs/spark
ln -s /opt/installs/spark-yarn /opt/installs/spark
修改spark-env.sh配置文件
cd /opt/installs/spark/conf
mv spark-env.sh.template spark-env.sh
vim /opt/installs/spark/conf/spark-env.sh
## 22行左右设置JAVA安装目录、HADOOP和YARN配置文件
目录
export JAVA_HOME=/opt/installs/jdk
export HADOOP_CONF_DIR=/opt/installs/hadoop/etc/hadoop
export YARN_CONF_DIR=/opt/installs/hadoop/etc/hadoop
## 历史日志服务器
export SPARK_DAEMON_MEMORY=1g
export SPARK_HISTORY_OPTS="-Dspark.history.fs.logDirectory=hdfs://bigdata01:9820/spark/eventLogs/ -Dspark.history.fs.cleaner.enabled=true"
修改spark-defaults.conf 文件:
cd /opt/installs/spark/conf
mv spark-defaults.conf.template spark-defaults.conf
vim spark-defaults.conf
## 添加内容:
spark.eventLog.enabled true
spark.eventLog.dir hdfs://bigdata01:9820/spark/eventLogs
spark.eventLog.compress true
spark.yarn.historyServer.address bigdata01:18080
spark.yarn.jars hdfs://bigdata01:9820/spark/jars/*
修改log4j.properties
mv log4j.properties.template log4j.properties
修改级别为WARN,打印日志少一点。
上传spark jar包:
要启动hdfs
#因为YARN中运行Spark,需要用到Spark的一些类和方法
#如果不上传到HDFS,每次运行YARN都要上传一次,比较慢
#所以自己手动上传一次,以后每次YARN直接读取即可
hdfs dfs -mkdir -p /spark/jars/
hdfs dfs -put /opt/installs/spark/jars/* /spark/jars/
修改yarn-site.xml
cd /opt/installs/hadoop/etc/hadoop
检查以下内置少什么,就配什么。
<property>
<name>yarn.log-aggregation-enable</name>
<value>true</value>
</property>
<!-- 历史日志在HDFS保存的时间,单位是秒 -->
<!-- 默认的是-1,表示永久保存 -->
<property>
<name>yarn.log-aggregation.retain-seconds</name>
<value>604800</value>
</property>
<property>
<name>yarn.log.server.url</name>
<value>http://bigdata01:19888/jobhistory/logs</value>
</property>
<!-- 关闭yarn内存检查 -->
<property>
<name>yarn.nodemanager.pmem-check-enabled</name>
<value>false</value>
</property>
<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
</property>
分发yarn-site.xml
xsync.sh 是分发文件脚本 详细可以看实用-集群分发文件脚本-CSDN博客
xsync.sh yarn-site.xml
分发spark-yarn
xsync.sh /opt/installs/spark-yarn
超链接也分发一下:
xsync.sh /opt/installs/spark
启动
# 启动yarn
start-yarn.sh
# 启动MR的JobHistoryServer:19888
mapred --daemon start historyserver
# 启动Spark的HistoryServer:18080
/opt/installs/spark/sbin/start-history-server.sh
测试
测试官方给的PI值的计算:
/opt/installs/spark/bin/spark-submit --master yarn /opt/installs/spark/examples/src/main/python/pi.py 10
假如你遇到这个输出,说明资源有限,可以等一等