本文档描述spark如何在集群运行,便于理解过程中包含的组件。请阅读application submission guide来了解如何向集群提交程序。
Components
spark程序在集群中作为独立的线程集合运行,由主程序(driver程序)中的SparkContext对象来协调。
特别地,SparkContext可以连接几种不同的集群管理者(如standalone集群管理、Mesos或者YARN),用来为程序分配资源。连接建立后,spark获取集群节点上的executors(用来为程序运行计算和数据存储的线程)。然后,将application code发给executors。最后,SparkContext将tasks发送给executors执行。
有几点需要说明:
1、每个程序获取它自己的executor进程,这些进程在整个程序期间存活,并且以多线程方式运行tasks。这样的好处是不同程序之间相互独立,不管是在调度端(每个driver调度自己的tasks)还是在executor application(tasks from different applications run in different JVMs)。然而,这也意味着,不同的spark程序(SparkContext实例)之间不能共享数据,除非将数据写入到外部存储系统。
2、spark对集群管理者是不可知的。只要它能获取到executors进程,并且每个进程之间可以通信,及时集群管理者同时也支持其他程序也是可以的。
3、driver程序必须在整个生命周期监听和接收来自executors的连接,因此,要保证driver程序与worker节点要网络互通。
4、Because the driver schedules tasks on the cluster, it should be run close to the worker nodes, preferably on the same local area network. If you’d like to send requests to the cluster remotely, it’s better to open an RPC to the driver and have it submit operations from nearby than to run a driver far away from the worker nodes.
Cluster Manager Types
目前支持集群管理者:
Standalone:一种简单的spark集群管理者
Apache Mesos:一种也可以运行Hadoop MR和服务程序的通用的集群管理者
Hadoop YARN:Hadoop2的resource manager
Kubernetes:容器化程序的自动化部署与管理的开源系统
Submitting Applications
见
Glossary(专业术语)
关于集群概念的专业术语
Application:用户构建的spark程序,包含driver程序和集群上的executors
Application jar:包含spark application的jar包,包里不应该包含Hadoop或者Spark的依赖,这些会在运行时被添加。
Driver program:运行程序的main方法和创建SparkContext的线程
Cluster manager:用于获取资源的外部服务(standalone,Mesos,YARN)
Deploy mode:决定driver线程运行在哪里,如果是cluster模式,driver在集群里启动,如果是client模式,driver会在集群外运行
Worker node:集群中可以运行程序的任何节点
Executor:worker节点上为程序启动的进程,用来运行tasks和保存内存数据。每个程序有自己的executors
Task:发送到executor上的每个工作单位
Job:响应引起spark action(如save、collect)的包含多个tasks组成的并行计算。在driver的日志中可以看到这个术语
Stage:每个job会被分为更小的tasks集合,称为stage,stage之间互相依赖(类似于MR的map和reduce阶段),在driver的日志中可以看到这个术语