-
Reynold Xin authored
Based on my conversions with people, I believe the consensus is that the coarse-grained mode is more stable and easier to reason about. It is best to use that as the default rather than the more flaky fine-grained mode. Author: Reynold Xin <rxin@databricks.com> Closes #9795 from rxin/SPARK-11809.
Reynold Xin authoredBased on my conversions with people, I believe the consensus is that the coarse-grained mode is more stable and easier to reason about. It is best to use that as the default rather than the more flaky fine-grained mode. Author: Reynold Xin <rxin@databricks.com> Closes #9795 from rxin/SPARK-11809.
layout: global
title: Running Spark on Mesos
- This will become a table of contents (this text will be scraped). {:toc}
Spark can run on hardware clusters managed by Apache Mesos.
The advantages of deploying Spark with Mesos include:
- dynamic partitioning between Spark and other frameworks
- scalable partitioning between multiple instances of Spark
How it Works
In a standalone cluster deployment, the cluster manager in the below diagram is a Spark master instance. When using Mesos, the Mesos master replaces the Spark master as the cluster manager.
Now when a driver creates a job and starts issuing tasks for scheduling, Mesos determines what machines handle what tasks. Because it takes into account other frameworks when scheduling these many short-lived tasks, multiple frameworks can coexist on the same cluster without resorting to a static partitioning of resources.
To get started, follow the steps below to install Mesos and deploy Spark jobs via Mesos.
Installing Mesos
Spark {{site.SPARK_VERSION}} is designed for use with Mesos {{site.MESOS_VERSION}} and does not require any special patches of Mesos.
If you already have a Mesos cluster running, you can skip this Mesos installation step.
Otherwise, installing Mesos for Spark is no different than installing Mesos for use by other frameworks. You can install Mesos either from source or using prebuilt packages.
From Source
To install Apache Mesos from source, follow these steps:
- Download a Mesos release from a mirror
- Follow the Mesos Getting Started page for compiling and installing Mesos
Note: If you want to run Mesos without installing it into the default paths on your system
(e.g., if you lack administrative privileges to install it), pass the
--prefix
option to configure
to tell it where to install. For example, pass
--prefix=/home/me/mesos
. By default the prefix is /usr/local
.
Third-Party Packages
The Apache Mesos project only publishes source releases, not binary packages. But other third party projects publish binary releases that may be helpful in setting Mesos up.
One of those is Mesosphere. To install Mesos using the binary releases provided by Mesosphere:
- Download Mesos installation package from downloads page
- Follow their instructions for installation and configuration
The Mesosphere installation documents suggest setting up ZooKeeper to handle Mesos master failover, but Mesos can be run without ZooKeeper using a single master as well.
Verification
To verify that the Mesos cluster is ready for Spark, navigate to the Mesos master webui at port
:5050
Confirm that all expected machines are present in the slaves tab.
Connecting Spark to Mesos
To use Mesos from Spark, you need a Spark binary package available in a place accessible by Mesos, and a Spark driver program configured to connect to Mesos.
Alternatively, you can also install Spark in the same location in all the Mesos slaves, and configure
spark.mesos.executor.home
(defaults to SPARK_HOME) to point to that location.
Uploading Spark Package
When Mesos runs a task on a Mesos slave for the first time, that slave must have a Spark binary
package for running the Spark Mesos executor backend.
The Spark package can be hosted at any Hadoop-accessible URI, including HTTP via http://
,
Amazon Simple Storage Service via s3n://
, or HDFS via hdfs://
.
To use a precompiled package:
- Download a Spark binary package from the Spark download page
- Upload to hdfs/http/s3
To host on HDFS, use the Hadoop fs put command: hadoop fs -put spark-{{site.SPARK_VERSION}}.tar.gz /path/to/spark-{{site.SPARK_VERSION}}.tar.gz
Or if you are using a custom-compiled version of Spark, you will need to create a package using
the make-distribution.sh
script included in a Spark source tarball/checkout.
- Download and build Spark using the instructions here
- Create a binary package using
make-distribution.sh --tgz
. - Upload archive to http/s3/hdfs
Using a Mesos Master URL
The Master URLs for Mesos are in the form mesos://host:5050
for a single-master Mesos
cluster, or mesos://zk://host:2181
for a multi-master Mesos cluster using ZooKeeper.
Client Mode
In client mode, a Spark Mesos framework is launched directly on the client machine and waits for the driver output.
The driver needs some configuration in spark-env.sh
to interact properly with Mesos:
- In
spark-env.sh
set some environment variables:
-
export MESOS_NATIVE_JAVA_LIBRARY=<path to libmesos.so>
. This path is typically<prefix>/lib/libmesos.so
where the prefix is/usr/local
by default. See Mesos installation instructions above. On Mac OS X, the library is calledlibmesos.dylib
instead oflibmesos.so
. -
export SPARK_EXECUTOR_URI=<URL of spark-{{site.SPARK_VERSION}}.tar.gz uploaded above>
.
- Also set
spark.executor.uri
to<URL of spark-{{site.SPARK_VERSION}}.tar.gz>
.
Now when starting a Spark application against the cluster, pass a mesos://
URL as the master when creating a SparkContext
. For example:
{% highlight scala %} val conf = new SparkConf() .setMaster("mesos://HOST:5050") .setAppName("My app") .set("spark.executor.uri", "<path to spark-{{site.SPARK_VERSION}}.tar.gz uploaded above>") val sc = new SparkContext(conf) {% endhighlight %}
(You can also use spark-submit
and configure spark.executor.uri
in the conf/spark-defaults.conf file.)
When running a shell, the spark.executor.uri
parameter is inherited from SPARK_EXECUTOR_URI
, so
it does not need to be redundantly passed in as a system property.
{% highlight bash %} ./bin/spark-shell --master mesos://host:5050 {% endhighlight %}
Cluster mode
Spark on Mesos also supports cluster mode, where the driver is launched in the cluster and the client can find the results of the driver from the Mesos Web UI.
To use cluster mode, you must start the MesosClusterDispatcher in your cluster via the sbin/start-mesos-dispatcher.sh
script,
passing in the Mesos master url (e.g: mesos://host:5050).
From the client, you can submit a job to Mesos cluster by running spark-submit
and specifying the master url
to the url of the MesosClusterDispatcher (e.g: mesos://dispatcher:7077). You can view driver statuses on the
Spark cluster Web UI.
Note that jars or python files that are passed to spark-submit should be URIs reachable by Mesos slaves.
Mesos Run Modes
Spark can run over Mesos in two modes: "coarse-grained" (default) and "fine-grained".
The "coarse-grained" mode will launch only one long-running Spark task on each Mesos machine, and dynamically schedule its own "mini-tasks" within it. The benefit is much lower startup overhead, but at the cost of reserving the Mesos resources for the complete duration of the application.
Coarse-grained is the default mode. You can also set spark.mesos.coarse
property to true
to turn it on explictly in SparkConf:
{% highlight scala %} conf.set("spark.mesos.coarse", "true") {% endhighlight %}
In addition, for coarse-grained mode, you can control the maximum number of resources Spark will
acquire. By default, it will acquire all cores in the cluster (that get offered by Mesos), which
only makes sense if you run just one application at a time. You can cap the maximum number of cores
using conf.set("spark.cores.max", "10")
(for example).
In "fine-grained" mode, each Spark task runs as a separate Mesos task. This allows multiple instances of Spark (and other frameworks) to share machines at a very fine granularity, where each application gets more or fewer machines as it ramps up and down, but it comes with an additional overhead in launching each task. This mode may be inappropriate for low-latency requirements like interactive queries or serving web requests.