Spark - 模式

爲了規劃和執行分佈式計算,使用job概念,使用Stages和Tasks,跨工作節點執行。
Spark由driver組成,在一組工作節點上協調執行。它還負責跟蹤所有工作節點,以及節點上執行的工作。

  • Driver:包含應用程序和主程序。
  • Executor:工作節點上運行的進程。Inside the executor, the individual tasks or computations are run。一個工作節點上可以有一個或多個executors,每個executor內部可以有多個tasks。當Driver連接到cluster manager,cluster manager分配資源,運行executors。

Cluster Manager負責跨節點的資源調度和分配。

how Spark works
Spark程序的主要入口是SparkContext。SparkContext在Driver組件內,代表與集羣的連接,運行調度、任務分發和編排。
Spark 2.x,引入了新變量SparkSession。現在,SparkContext, SQLContext, and HiveContext 是SparkSession的成員變量。

當你啓動一個Driver程序的時候,使用SparkContext將命令發佈到集羣,然後,executors將執行這些指令。一旦執行完成,Driver程序完成了該job。

可以使用SparkContext增加RDDs、accumulators,和在集羣內廣播變量。每個JVM進程內只能有一個SparkContext。在增加新的之前,必須stop()活動的SparkContext。
Driver解析代碼、規劃執行。When we perform any computations, the computations will actually be done at the local level by each node, using in-memory processing.

Driver coordinates the computations

Driver爲任務增加Directed Acyclic Graph (DAG),DAG分stages and tasks執行。一個DAG代表一個job,job被分成子集,叫做stages,每個stage被當作tasks(one core per task)執行。
下面兩圖分別是一個簡單job,以及DAG被分成stages and tasks。先看job

a simple job

下來是the stages in the job and the tasks:

the stages in the job and the tasks

operations的類型決定了stages的數量和組成。一般來說,transformation以及之前的工作在同一個stage,但是,每個operation(比如reduce or shuffle)總是增加新的stage。
Tasks是stage的一部分,和執行operations的core直接相關。
如果是YARN或者Mesos作爲cluster manager,可以使用動態YARN調度,來增加executors的數量。

因此,driver管理整個執行過程的容錯能力。一旦job完成,輸出可以被寫進文件、database或者控制檯。

Driver程序內的代碼(包括全部變量和對象)必須是完全可序列化的。經常可以看到不可序列化異常,這通常是因爲從塊的外部包含了全局變量。

部署

Spark支持三種部署方式:

  • Spark standalone
  • Spark on YARN
  • Spark on Mesos

In standalone mode, the client can interact with the cluster, either through spark-submit or Spark shell.
In either case, the Driver communicates with the Spark master Node to get the worker nodes, where executors can be started for this application.

Spark standalone

不依賴任何外部調度器(YARN or Mesos)。
Spark standalone uses a built-in scheduler without depending on any external scheduler such as YARN or Mesos. To install Spark in standalone mode, you have to copy the spark binary install package onto all the machines in the cluster.
下面是一個獨立部署示例。
standalone deployment

此時,客戶端通過spark-submit or Spark shell和cluster交互。
Driver和master Node通信,得到worker nodes。
多個客戶端和cluster交互,在Worker Nodes上增加自己的executors。每個客戶端有自己的Driver組件。

Spark Driver also has a Web UI, which helps you to understand everything about the Spark cluster, the executors running, the jobs and tasks, environment variables, and cache. The most important use, of course, is to monitor the jobs.

Spark on YARN

客戶端和YARN資源管理交互,獲得運行Spark execution的容器。可以視爲僅爲你部署的miniSpark-cluster。
多個客戶端在cluster nodes (node managers)上增加自己的executors。
此時,每個客戶端也都有自己的Driver組件。
使用YARN運行的時候,Spark在YARN-client模式,或者在YARN-cluster模式。

YARN client mode

In YARN client mode, the Driver runs on a node outside the cluster (typically where the client is).
Driver首先聯繫資源管理器,請求運行job的資源。然後,Driver在容器0中啓動Spark application master。master在資源管理器分配的容器中增加executors,YARN容器可以在集羣控制的任何節點上。因此,所有的分配都是由資源管理器管理的。
甚至Spark application master也需要和資源管理器通信,以獲取後續容器。

YARN-client mode

YARN cluster mode

Driver運行在集羣內的一個節點上(typically where the application master is)。Client先聯繫資源管理器,請求資源運行job。資源管理器分配容器。然後,client把代碼提交給集羣,然後啓動Driver和Spark application master。Driver和master一起運行,然後在容器上增加executors。YARN容器可以在任何節點。因此,分配都由資源管理器管理。
甚至Spark application master也需要和資源管理器通信,以獲取後續容器。

Yarn-cluster mode

There is no shell mode in YARN cluster mode, since the Driver itself is running inside YARN.

Spark on Mesos

Mesos deployment is similar to Spark standalone mode and the Driver communicates with the Mesos Master, which then allocates the resources needed to run the executors.As seen in standalone mode, the Driver then communicates with the executors to run the job.因此,Driver首先和master聯繫,然後在所有的Mesos slave nodes上確保容器的請求。
當容器分配給job的時候,然後Driver啓動executors,執行代碼。當job完成,Driver退出的時候,提醒Mesos master,Mesos slave nodes節點上該容器的相關資源被回收。
多個clients和集羣交互,在slave節點上增加自己的executors。
每個客戶端也都有自己的Driver組件。

mesos-based deployment

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章