Spark应用提交

spark-submit兼容所有的集群模式。

1 打包依赖

使用sbt或maven将依赖的代码和包打包。

不必包含Spark或Hadoop,因为集群管理器在运行时自动添加。

2 使用spark-submit运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
./bin/spark-submit \
# 应用入口,如org.apache.spark.examples.SparkPi
--class <main-class> \
# 主节点URL
--master <master-url> \
# 部署模式,默认client
--deploy-mode <deploy-mode> \
# Spark配置,为了包含空格,建议使用双引号,如“key=value”
--conf <key>=<value> \
... # other options
# 应用及其依赖jar路径,需要所有节点可访问
<application-jar> \
# 主函数参数
[application-arguments]

通常,在与工作节点同一网关环境中提交应用。这种设置适宜使用client模式。驱动程序作为客户端在spark-submit进程中运行。默认输入输出在控制台中,适宜REPL(读取-求值-输出循环)。

如需远程提交应用,为了减少驱动和执行器通信延迟,可以使用cluster模式。

注意:当前standalone部署不支持Python应用的cluster模式。

思考:client和cluster的区别在driver是否在集群外,前者在集群外,后者在集群内。

不同的集群管理器有不同的专用选项,可以通过–help查看。如standalone模式可以使用–supervise启用失败重启。

示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Run application locally on 8 cores
./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master local[8] \
/path/to/examples.jar \
100

# Run on a Spark standalone cluster in client deploy mode
./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://207.184.161.138:7077 \
--executor-memory 20G \
--total-executor-cores 100 \
/path/to/examples.jar \
1000

# Run on a Spark standalone cluster in cluster deploy mode with supervise
./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://207.184.161.138:7077 \
--deploy-mode cluster \
--supervise \
--executor-memory 20G \
--total-executor-cores 100 \
/path/to/examples.jar \
1000

# Run on a YARN cluster
export HADOOP_CONF_DIR=XXX
./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master yarn \
--deploy-mode cluster \ # can be client for client mode
--executor-memory 20G \
--num-executors 50 \
/path/to/examples.jar \
1000

# Run a Python application on a Spark standalone cluster
./bin/spark-submit \
--master spark://207.184.161.138:7077 \
examples/src/main/python/pi.py \
1000

# Run on a Mesos cluster in cluster deploy mode with supervise
./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master mesos://207.184.161.138:7077 \
--deploy-mode cluster \
--supervise \
--executor-memory 20G \
--total-executor-cores 100 \
http://path/to/examples.jar \
1000

# Run on a Kubernetes cluster in cluster deploy mode
./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master k8s://xx.yy.zz.ww:443 \
--deploy-mode cluster \
--executor-memory 20G \
--num-executors 50 \
http://path/to/examples.jar \
1000

3 主节点URL

image-20200727215852311

4 从文件加载配置

Spark默认从conf/spark-defaults读取Spark configuration values 。详见loading default configurations

配置采用优先级:

SparkConf显式设置>spark-submit参数>文件参数

获取参数来源可以使用spark-submit –verbose查看

5 高级依赖管理

使用spark-submit时,应用程序包和–jars指定的包(逗号分隔)将自动传输到集群中。列表将被包含进驱动和执行器节点的类路径中。注意:不支持目录扩容

文件协议:

  • file: 绝对路径和驱动节点提供的文件服务
  • hdfs:, http:, https:, ftp:
  • local: 工作节点路径,不会触发网络传输

由于jar包和文件传输到每个SparkContext的工作节点的工作目录中,需要清理。YARN自动清理,standalone通过设置spark.worker.cleanup.appDataTtl自动清理。

–packages提供Maven包定位信息

–repositories提供sbt等额外仓库地址,可以通过 https://user:password@host/....提供密码,需要注意安全。

6 更多信息

详见cluster mode overview

参考资料

[Submitting Appli

cations](https://spark.apache.org/docs/2.3.0/submitting-applications.html)