标签:
除了运行在mesos或yarn集群管理器中,spark也提供了简单的standalone部署模式。你可以通过手动启动master和worker节点来创建集群,或者用官网提供的启动脚本。这些守护进程也可以只在一台机器上以便测试使用。
安装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下找到。
注意这些脚本必须在你想要运行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. |
详情
尽管这种方式官网不推荐,你可以挂载一个NFS目录作为一个恢复目录,如果原来的master宕了,你可以在一个新的节点上启动一个master,它能正确地恢复之前注册的应用程序和workers。
标签:
原文地址:http://www.cnblogs.com/yourarebest/p/5103989.html