Spark Standalone模式

  • 安装Spark Standalone集群
  • 手动启动集群
  • 集群创建脚本
  • 提交应用到集群
  • 创建Spark应用
  • 资源调度及分配
  • 监控与日志
  • 与Hadoop共存
  • 配置网络安全端口
  • 高可用性
    • 基于Zookeeper的Master
    • 本地系统的单节点恢复

除了运行在mesos或yarn集群管理器中,spark也提供了简单的standalone部署模式。你可以通过手动启动master和worker节点来创建集群,或者用官网提供的启动脚本。这些守护进程也可以只在一台机器上以便测试使用。

1.安装Spark Standalone集群

安装Spark Standalone集群,你只需要在每个节点上部署编译好的Spark即可。你可以在官网上得到已经预编译好的,也可以根据自己的需要进行编译。

2.手动启动集群

你可以启动Standalone模式的master服务,通过执行如下命令:

./sbin/start-master.sh

一旦启动,master节点将打印出Spark://HOST:PORT URL,你可以用这个URL来连接worker节点或者把它赋值给“master”参数传递给SparkContext。你也可以在master的WEB UI找到这个URL,默认的是http://localhost:8080,最好是http://master所在的ip地址:8080,这样和master在同一个局域网内的机器都可以访问。

同样地,你可以启动一个或多个worker节点并把它注册到master节点上,执行如下命令:

./sbin/start-slave.sh <master-spark-URL>

一旦你启动了worker节点,通过master的WEB UI,你可以看到注册到它上面的worker的信息,比如CPU核数、内存等。

最后,下面的配置选项可以传递给master和worker节点。

Argument

Meaning

-h HOST, --host HOST

Hostname to listen on

-i HOST, --ip HOST

Hostname to listen on (deprecated, use -h or --host)

-p PORT, --port PORT

Port for service to listen on (default: 7077 for master, random for worker)

--webui-port PORT

Port for web UI (default: 8080 for master, 8081 for worker)

-c CORES, --cores CORES

Total CPU cores to allow Spark applications to use on the machine (default: all available); only on worker

-m MEM, --memory MEM

Total amount of memory to allow Spark applications to use on the machine, in a format like 1000M or 2G (default: your machine's total RAM minus 1 GB); only on worker

-d DIR, --work-dir DIR

Directory to use for scratch space and job output logs (default: SPARK_HOME/work); only on worker

--properties-file FILE

Path to a custom Spark properties file to load (default: conf/spark-defaults.conf)

3.集群创建脚本

如果用脚本启动集群的话,你应该在你的Spark_HOME下创建一个conf/slaves,这个slaves文件必须包含worker的主机名,每行一个。如果conf/slaves不存在的话,创建脚本默认值启动本机单个节点,这对于测试很有用。注意,master通过ssh来和worker进行通信。

一旦你设置了这个文件,你可以通过下面的Shell脚本来启动或停止集群,类似于Hadoop的部署脚本,这些脚本在SPARK_HOME/sbin下找到。

  • sbin/start-master.sh - 启动脚本所在机器上的master节点
  • sbin/start-slaves.sh - 启动conf/slaves文件中指定的slave所有节点
  • sbin/start-slave.sh - 启动脚本所在的机器上的slave节点
  • sbin/start-all.sh - 启动脚本所在的slave节点及与其相关的slave节点
  • sbin/stop-master.sh - 停止脚本所在机器上的master节点
  • sbin/stop-slaves.sh - 启动conf/slaves文件中指定的slave所有节点
  • sbin/stop-all.sh - 停止脚本所在机器上的master节点

注意这些脚本必须在你想要运行Spark master节点上,而不是你本地机器

你可以在conf/spark-env.sh中选择性地配置下面的选项,这个文件集群中的每台机器都必须有。

Environment Variable

Meaning

SPARK_MASTER_IP

Bind the master to a specific IP address, for example a public one.

SPARK_MASTER_PORT

Start the master on a different port (default: 7077).

SPARK_MASTER_WEBUI_PORT

Port for the master web UI (default: 8080).

SPARK_MASTER_OPTS

Configuration properties that apply only to the master in the form "-Dx=y" (default: none). See below for a list of possible options.

SPARK_LOCAL_DIRS

Directory to use for "scratch" space in Spark, including map output files and RDDs that get stored on disk. This should be on a fast, local disk in your system. It can also be a comma-separated list of multiple directories on different disks.

SPARK_WORKER_CORES

Total number of cores to allow Spark applications to use on the machine (default: all available cores).

SPARK_WORKER_MEMORY

Total amount of memory to allow Spark applications to use on the machine, e.g. 1000m, 2g (default: total memory minus 1 GB); note that each application's individual memory is configured using its spark.executor.memory property.

SPARK_WORKER_PORT

Start the Spark worker on a specific port (default: random).

SPARK_WORKER_WEBUI_PORT

Port for the worker web UI (default: 8081).

SPARK_WORKER_INSTANCES

Number of worker instances to run on each machine (default: 1). You can make this more than 1 if you have have very large machines and would like multiple Spark worker processes. If you do set this, make sure to also set SPARK_WORKER_CORES explicitly to limit the cores per worker, or else each worker will try to use all the cores.

SPARK_WORKER_DIR

Directory to run applications in, which will include both logs and scratch space (default: SPARK_HOME/work).

SPARK_WORKER_OPTS

Configuration properties that apply only to the worker in the form "-Dx=y" (default: none). See below for a list of possible options.

SPARK_DAEMON_MEMORY

Memory to allocate to the Spark master and worker daemons themselves (default: 1g).

SPARK_DAEMON_JAVA_OPTS

JVM options for the Spark master and worker daemons themselves in the form "-Dx=y" (default: none).

SPARK_PUBLIC_DNS

The public DNS name of the Spark master and workers (default: none).

SPARK_MASTER_OPTS可以配置下面的系统属性:

Property Name

Default

Meaning

spark.deploy.retainedApplications

200

The maximum number of completed applications to display. Older applications will be dropped from the UI to maintain this limit.

spark.deploy.retainedDrivers

200

The maximum number of completed drivers to display. Older drivers will be dropped from the UI to maintain this limit.

spark.deploy.spreadOut

true

Whether the standalone cluster manager should spread applications out across nodes or try to consolidate them onto as few nodes as possible. Spreading out is usually better for data locality in HDFS, but consolidating is more efficient for compute-intensive workloads.

spark.deploy.defaultCores

(infinite)

Default number of cores to give to applications in Spark's standalone mode if they don't set spark.cores.max. If not set, applications always get all available cores unless they configure spark.cores.max themselves. Set this lower on a shared cluster to prevent users from grabbing the whole cluster by default.

spark.worker.timeout

60

Number of seconds after which the standalone deploy master considers a worker lost if it receives no heartbeats.

SPARK_WORKER_OPTS可以配置下面的系统属性:

Property Name

Default

Meaning

spark.worker.cleanup.enabled

false

Enable periodic cleanup of worker / application directories. Note that this only affects standalone mode, as YARN works differently. Only the directories of stopped applications are cleaned up.

spark.worker.cleanup.interval

1800 (30 minutes)

Controls the interval, in seconds, at which the worker cleans up old application work dirs on the local machine.

spark.worker.cleanup.appDataTtl

7 * 24 * 3600 (7 days)

The number of seconds to retain application work directories on each worker. This is a Time To Live and should depend on the amount of available disk space you have. Application logs and jars are downloaded to each application work dir. Over time, the work dirs can quickly fill up disk space, especially if you run jobs very frequently.

4.提交应用到集群

在Spark集群中运行一个Spark应用程序,需要把master节点的Spark://IP:PORT URL传递给SparkContext 的构造函数中。

在交互式Shell中Spark应用程序,需运行下面的命令:

./bin/spark-shell --master spark://IP:PORT

你也可以传递选项--total-executor-cores <numCores>来控制Spark Shell使用的机器的核数。

5.创建Spark应用

spark-submit脚本提供了提供应用到集群最直接的方式。对于Standalone模式而言,Spark目前支持两种部署模式。在Client模式中,Driver程序在提交命令的机器上。在Cluster模式中,Driver从集群中的worker节点中任取一个运行驱动程序。

如果你的应用通过Spark submit提交,这个应用jar自动分发到集群中的所有worker节点上。对于你的应用依赖的额外的jars,你应该通过--jars 参数来指定,多个之间用逗号分隔(如果:--jars jar1,jar2)

另外,standalone cluster模式也自动重启你的应用程序。为了使用这个特性,你可以在spark-submit启动你的应用程序时传递--supervise参数。

./bin/spark-class org.apache.spark.deploy.Client kill <master url> <driver ID>

6.资源调度及分配

Standalone cluster模式目前仅支持应用调度的FIFO模式。为了运行多个用户,你可以控制每个应用使用的最大资源。默认,它会使用集群中所有机器的核数,这只对于集群中只有一个应用有效。你可以通过 spark.cores.max 参数来控制核数,如下所示:

val conf = new SparkConf()

.setMaster(...)

.setAppName(...)

.set("spark.cores.max", "10")val sc = new SparkContext(conf)

另外,你可以在集群的master中配置 spark.deploy.defaultCores参数来改变默认值。如下所示:

export SPARK_MASTER_OPTS="-Dspark.deploy.defaultCores=<value>"

7.监控与日志

Spark Standalone模式提供了一个web接口来监控集群。master和每个worker有他们自己的WEB UI。默认你可以通过8080端口访问master的WEB UI。这个端口可以在配置文件中修改或在命令行中选项修改。

另外,每个job的详细日志默认写入到每个slave节点的工作目录(默认SPARK_HOME/work)。在目录下,对于每个job,你会看到两个文件分别是stdout和stderr。

8.与Hadoop共存

你可以基于你现有的Hadoop集群运行Spark,只需要在同样的机器上启动单独的服务即可。在Spark中访问Hadoop中的数据,只需要使用hdfs:// URL (典型hdfs://<namenode>:9000/path)路径即可。另外,你可以为Spark创建一个独立的集群,通过网络仍然可以访问HDFS,这可能比本次磁盘慢。

9.配置网络安全端口

Spark大量使用网络,一些环境有严格的防火墙要求。想要了解配置的端口,请看安全模块。

10.高可用性

默认,standalone集群调度对于worker节点的失效是有弹性的。然而,集群调度器通过master做决策,默认只有单个节点。如果master宕机了,将不会再创建新的应用。为了避免单点故障,我们提供两种高可用性模式,详情如下。

10.1基于Zookeeper的Master

使用Zookeeper来提供leader选举和一些转态存储,你可以在基于Zookeeper的集群中启动多个master。一旦一个master被选中为“leader”,其他的将处于standby转态。如果当前的leader宕机了,Zookeeper将会重新选举出另外一个master,从前一个master的转态中继续任务调度。整个的恢复过程耗时在1-2分钟。注意,这种延迟仅仅影响调用新的应用程序而不影响正在运行的应用。

配置

为了支持这种恢复模式,你可以在spark-env.sh中设置SPARK_DAEMON_JAVA_OPTS配置如下选项:

System property

Meaning

spark.deploy.recoveryMode

Set to ZOOKEEPER to enable standby Master recovery mode (default: NONE).

spark.deploy.zookeeper.url

The ZooKeeper cluster url (e.g., 192.168.1.100:2181,192.168.1.101:2181).

spark.deploy.zookeeper.dir

The directory in ZooKeeper to store recovery state (default: /spark).

详情

如果你集群中已经安装好了Zookeeper,允许HA是很简单的。只需要在不同的节点上启动读个master进程即可,master可以随时增删。

为了调度新的应用或集群中添加worker,他们需要知道当期啊leader 的ip地址。这仅需要传递一个list即可。例如,你通过spark://host1:port1,host2:port2来启动应用程序时,如果host1宕机了,集群仍让正常,因为集群已经重新找到了一个新的leader,即host2

10.2本地系统的单节点恢复

Zookeeper是最好的HA方式,但如果你想要master如果宕了重启的话,文件系统模式支持。当应用程序和worker注册到master后,他们有足够的转态写入到了特定目录中,这些转态可以在master进程重启时恢复。

配置

为了支持这种恢复模式,你可以在spark-env.sh中设置SPARK_DAEMON_JAVA_OPTS配置如下选项:

System property

Meaning

spark.deploy.recoveryMode

Set to FILESYSTEM to enable single-node recovery mode (default: NONE).

spark.deploy.recoveryDirectory

The directory in which Spark will store recovery state, accessible from the Master's perspective.

详情

  • 这种解决方案被用在monit这样的系统中。
  • 尽管这种文件恢复模式看起来很好,但效果不太好。特别,通过sotp-master.sh来杀死一个master不能清除它的恢复状态,所以无论你何时启动一个新的master,它将进行恢复模式。这可能导致启动时间的增加。

尽管这种方式官网不推荐,你可以挂载一个NFS目录作为一个恢复目录,如果原来的master宕了,你可以在一个新的节点上启动一个master,它能正确地恢复之前注册的应用程序和workers。

【原】Spark Standalone模式的更多相关文章

  1. Spark Standalone模式应用程序开发

    作者:过往记忆 | 新浪微博:左手牵右手TEL | 能够转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明博客地址:http://www.iteblog.com/文章标题:<Spar ...

  2. 关于spark standalone模式下的executor问题

    1.spark standalone模式下,worker与executor是一一对应的. 2.如果想要多个worker,那么需要修改spark-env的SPARK_WORKER_INSTANCES为2 ...

  3. Spark Standalone模式HA环境搭建

    Spark Standalone模式常见的HA部署方式有两种:基于文件系统的HA和基于ZK的HA 本篇只介绍基于ZK的HA环境搭建: $SPARK_HOME/conf/spark-env.sh 添加S ...

  4. Spark standalone模式的安装(spark-1.6.1-bin-hadoop2.6.tgz)(master、slave1和slave2)

     前期博客  Spark运行模式概述 Spark standalone简介与运行wordcount(master.slave1和slave2) 开篇要明白 (1)spark-env.sh 是环境变量配 ...

  5. 大数据学习day18----第三阶段spark01--------0.前言(分布式运算框架的核心思想,MR与Spark的比较,spark可以怎么运行,spark提交到spark集群的方式)1. spark(standalone模式)的安装 2. Spark各个角色的功能 3.SparkShell的使用,spark编程入门(wordcount案例)

    0.前言 0.1  分布式运算框架的核心思想(此处以MR运行在yarn上为例)  提交job时,resourcemanager(图中写成了master)会根据数据的量以及工作的复杂度,解析工作量,从而 ...

  6. spark standalone模式单节点启动多个executor

    以前为了在一台机器上启动多个executor都是通过instance多个worker来实现的,因为standalone模式默认在一台worker上启动一个executor,造成了很大的不便利,并且会造 ...

  7. [会装]Spark standalone 模式的安装

    1. 简介 以standalone模式安装spark集群bin运行demo. 2.环境和介质准备 2.1 下载spark介质,根据现有hadoop的版本选择下载,我目前的环境中的hadoop版本是2. ...

  8. Spark Standalone模式伪分布式环境搭建

    前提:安装好jdk1.7,hadoop 安装步骤: 1.安装scala 下载地址:http://www.scala-lang.org/download/ 配置环境变量: export SCALA_HO ...

  9. 在myeclipse中使用Java语言进行spark Standalone模式应用程序开发

    一.环境配置 Myeclipse中虽然已经集成了maven插件,但是由于这个插件版本较低,建立maven project会出现错误. 解决办法:自己到官网http://maven.apache.org ...

随机推荐

  1. App开发中甲乙方冲突会闹出啥后果?H5 APP 开发可以改变现状吗

    随着各种应用的全面App化,因App而起的合作纠纷也日益增多,其中不乏最终对簿公堂的情形.WeX5(html5开发工具)为您汇总了三个典型的真实案例,方便您体会甲乙方冲突情景. 在围观别人争吵之余,您 ...

  2. MongoDB入门三步曲1--安装、基本操作

    mongodb 基本操作 目录 mongodb安装 mongod启动 mongo shell启动 mongod 停止 mongodb基本操作:CRUD 数据插入 数据查询 数据更新 数据删除 集合删除 ...

  3. iis7如何取消目录的可执行权限

    我们需要把IIs中某一个目录的可执行权限去掉.这在IIs6中是非常方便的,可是因为iis7的机制小编也找了不少资料才找到. 第一步:先选择需要取消权限的目录,然后在右边可以看到 “处理程序映射” 双击 ...

  4. cx_Oracle ORA-24315: 非法的属性类型的解决办法

    网上查找原因说: 出现该错误的原因是因为版本不兼容. 检查了一下python版本和oracle 的版本,我的oracle client是10g的,python的版本是2.7, 但我安装的cx_orac ...

  5. org

    controller层单元测试报错 org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of ...

  6. 编写jQuery插件--实现返回顶部插件

    国庆过去一周多了,作为IT界的具有严重’工作狂‘性质的宅人,居然还没走出玩耍的心情,拖了程序猿的脚后跟了.最近工作不顺,心情不佳,想吐槽下公司,想了还是厚道点,以彼之道还施彼身,觉得自己也和他们同流合 ...

  7. 信号量 sem_undo设置

    一 为什么要使用信号量 为了防止出现因多个程序同时访问一个共享资源而引发的一系列问题,我们需要一种方法,它可以通过生成并使用令牌来授权,在任一时刻只能有一个执行线程访问 代码的临界区域.临界区域是指执 ...

  8. java 中 sleep(1000) 和 wait(1000) 的区别?

    1.首先 sleep 方法是Thread类中的静态方法,他的作用是使当前线程暂时睡眠指定的时间,可以不用放在synchronized方法或者代码块中,但是 wait 方法是Object类的方法,它是使 ...

  9. cocos2dx输出信息重定向到控制台

    重定向输出到控制台,方便调试,代码: // uncomment below line, open debug console #define USE_WIN32_CONSOLE int APIENTR ...

  10. 再eclipse的javaweb项目中添加JQuery文件时jquery-2.1.4.min.js报错

    解决方法: eclipse导入jquery包后报错,下面有个不错的解决方法,需要的朋友可以参考下 eclipse导入jquery包后报错,处理步骤如下: 1.打开项目.project文件,去掉如下内容 ...