Spark运行模式(local standalond,yarn-client,yarn-cluster,mesos-client,mesos-cluster)

spark部署在单台机器上面时,可以使用本地模式(Local)运行;当部署在分布式集群上面的时候,可以根据自己的情况选择Standalone模式(Spark自带的模式)、YARN-Client模式或者YARN-Cluster模式、Spark on Mesos模式。
这里写图片描述

本地单机模式

所有的Spark进程都运行在一台机器或一个虚拟机上面。Spark任务提交的方式为:


spark-submit master=local[3] ./bin/run-example org.apache.spark.examples.SparkPi

local 使用一个Worker线程本地化运行SPARK(完全不并行)

local[*]使用逻辑CPU个数数量的线程来本地化运行Spark

local[K]使用K个Worker线程本地化运行Spark(理想情况下,K应该根据运行机器的CPU核数设定)

Spark on Standalone模式

Standalone模式是Spark实现的资源调度框架,其主要的节点有Client节点、Master节点和Worker节点。其中Driver既可以运行在Master节点上中,也可以运行在本地Client端。当用spark-shell交互式工具提交Spark的Job时,Driver在Master节点上运行;当使用spark-submit工具提交Job或者在Eclipse、IDEA等开发平台上使用”new SparkConf.setManager(“spark://master:7077”)”方式运行Spark任务时,Driver是运行在本地Client端上的。

spark://HOST:PORT连接到指定的Spark standalone master。默认端口是7077.


$./spark-shell --master spark://hadoop1:7077 --executor-memory 1g

Yarn-Client模式

Driver在客户端本地运行,这种模式可以使得Spark Application和客户端进行交互,因为Driver在客户端,所以可以通过webUI访问Driver的状态,默认是http://ip:4040访问,而YARN通过http:// ip:8088访问。


$./spark-shell --master YARN-client --num-executors 3 --executor-memory 1g

YARN-Cluster模式

当用户向YARN中提交一个应用程序后,YARN将分两个阶段运行该应用程序:第一个阶段是把Spark的Driver作为一个ApplicationMaster在YARN集群中先启动;第二个阶段是由ApplicationMaster创建应用程序,然后为它向ResourceManager申请资源,并启动Executor来运行Task,同时监控它的整个运行过程,直到运行完成。


$./bin/spark-submit --master YARN-cluster --class xxx --executor-memory 512m xxx.jar

ResourceManager负责将集群的资源分配给各个应用使用,而资源分配和调度的基本单位是Container,其中封装了机器资源,如内存、CPU、磁盘和网络等,每个任务会被分配一个Container,该任务只能在该Container中执行,并使用该Container封装的资源。

YARN-Client 与 YARN-Cluster 区别

理解YARN-Client和YARN-Cluster深层次的区别之前先清楚一个概念:Application Master。在YARN中,每个Application实例都有一个ApplicationMaster进程,它是Application启动的第一个容器。它负责和ResourceManager打交道并请求资源,获取资源之后告诉NodeManager为其启动Container。从深层次的含义讲YARN-Cluster和YARN-Client模式的区别其实就是ApplicationMaster进程的区别。

  • YARN-Cluster模式下,Driver运行在AM(Application Master)中,它负责向YARN申请资源,并监督作业的运行状况。当用户提交了作业之后,就可以关掉Client,作业会继续在YARN上运行,因而YARN-Cluster模式不适合运行交互类型的作业;
  • YARN-Client模式下,Application Master仅仅向YARN请求Executor,Client会和请求的Container通信来调度他们工作,也就是说Client不能离开。

Mesos Client模式

和YARN一样,Mesos在Spark中也分为client和cluster两种模式。使用client模式的话,Mesos框架直接在客户端运行,Driver在客户端本地运行。如果客户端关闭,那么驱动程序也就会停止运行。

使用下面的代码提交的spark任务就是client模式的。

在spark中使用messos

val conf = new SparkConf()
  .setMaster("mesos://HOST:5050")
  .setAppName("My app")
  .set("spark.executor.uri", "<path to spark-2.1.0.tar.gz uploaded above>")
val sc = new SparkContext(conf)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

​ 或者:

./bin/spark-shell --master mesos://host:5050
  
  
  • 1

Mesos Cluster模式

​ 在Mesos的cluster模式中,driver程序在集群中运行,客户端的关闭不影响程序的运行。如果要使用cluster模式,在提交作业的时候需要指定–deploy-mode cluster.

./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
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Mesos的master的URL:

如果mesos只有一个master,master的url为:
mesos://host:5050

如果messos有多个master(比如使用了zookeeper),master的url为:
mesos://zk://host1:2181,host2:2181,host3:2181/mesos
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

参考资料:

http://www.cnblogs.com/shishanyuan/archive/2015/08/19/4721326.html

http://spark.apache.org/docs/latest/running-on-mesos.html

spark部署在单台机器上面时,可以使用本地模式(Local)运行;当部署在分布式集群上面的时候,可以根据自己的情况选择Standalone模式(Spark自带的模式)、YARN-Client模式或者YARN-Cluster模式、Spark on Mesos模式。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/zhonglongshen/article/details/88187260
今日推荐