Skip to content
Snippets Groups Projects
  • Thomas Graves's avatar
    0058b5d2
    SPARK-1408 Modify Spark on Yarn to point to the history server when app ... · 0058b5d2
    Thomas Graves authored
    ...finishes
    
    Note this is dependent on https://github.com/apache/spark/pull/204 to have a working history server, but there are no code dependencies.
    
    This also fixes SPARK-1288 yarn stable finishApplicationMaster incomplete. Since I was in there I made the diagnostic message be passed properly.
    
    Author: Thomas Graves <tgraves@apache.org>
    
    Closes #362 from tgravescs/SPARK-1408 and squashes the following commits:
    
    ec89705 [Thomas Graves] Fix typo.
    446122d [Thomas Graves] Make config yarn specific
    f5d5373 [Thomas Graves] SPARK-1408 Modify Spark on Yarn to point to the history server when app finishes
    0058b5d2
    History
    SPARK-1408 Modify Spark on Yarn to point to the history server when app ...
    Thomas Graves authored
    ...finishes
    
    Note this is dependent on https://github.com/apache/spark/pull/204 to have a working history server, but there are no code dependencies.
    
    This also fixes SPARK-1288 yarn stable finishApplicationMaster incomplete. Since I was in there I made the diagnostic message be passed properly.
    
    Author: Thomas Graves <tgraves@apache.org>
    
    Closes #362 from tgravescs/SPARK-1408 and squashes the following commits:
    
    ec89705 [Thomas Graves] Fix typo.
    446122d [Thomas Graves] Make config yarn specific
    f5d5373 [Thomas Graves] SPARK-1408 Modify Spark on Yarn to point to the history server when app finishes
running-on-yarn.md 10.63 KiB
layout: global
title: Launching Spark on YARN

Support for running on YARN (Hadoop NextGen) was added to Spark in version 0.6.0, and improved in 0.7.0 and 0.8.0.

Building a YARN-Enabled Assembly JAR

We need a consolidated Spark JAR (which bundles all the required dependencies) to run Spark jobs on a YARN cluster. This can be built by setting the Hadoop version and SPARK_YARN environment variable, as follows:

SPARK_HADOOP_VERSION=2.0.5-alpha SPARK_YARN=true sbt/sbt assembly

The assembled JAR will be something like this: ./assembly/target/scala-{{site.SCALA_BINARY_VERSION}}/spark-assembly_{{site.SPARK_VERSION}}-hadoop2.0.5.jar.

The build process now also supports new YARN versions (2.2.x). See below.

Preparations

  • Building a YARN-enabled assembly (see above).
  • The assembled jar can be installed into HDFS or used locally.
  • Your application code must be packaged into a separate JAR file.

If you want to test out the YARN deployment mode, you can use the current Spark examples. A spark-examples_{{site.SCALA_BINARY_VERSION}}-{{site.SPARK_VERSION}} file can be generated by running sbt/sbt assembly. NOTE: since the documentation you're reading is for Spark version {{site.SPARK_VERSION}}, we are assuming here that you have downloaded Spark {{site.SPARK_VERSION}} or checked it out of source control. If you are using a different version of Spark, the version numbers in the jar generated by the sbt package command will obviously be different.

Configuration

Most of the configs are the same for Spark on YARN as for other deployment modes. See the Configuration page for more information on those. These are configs that are specific to Spark on YARN.

Environment variables:

  • SPARK_YARN_USER_ENV, to add environment variables to the Spark processes launched on YARN. This can be a comma separated list of environment variables, e.g. SPARK_YARN_USER_ENV="JAVA_HOME=/jdk64,FOO=bar".

System Properties:

  • spark.yarn.applicationMaster.waitTries, property to set the number of times the ApplicationMaster waits for the the spark master and then also the number of tries it waits for the Spark Context to be intialized. Default is 10.
  • spark.yarn.submit.file.replication, the HDFS replication level for the files uploaded into HDFS for the application. These include things like the spark jar, the app jar, and any distributed cache files/archives.
  • spark.yarn.preserve.staging.files, set to true to preserve the staged files(spark jar, app jar, distributed cache files) at the end of the job rather then delete them.
  • spark.yarn.scheduler.heartbeat.interval-ms, the interval in ms in which the Spark application master heartbeats into the YARN ResourceManager. Default is 5 seconds.
  • spark.yarn.max.executor.failures, the maximum number of executor failures before failing the application. Default is the number of executors requested times 2 with minimum of 3.
  • spark.yarn.historyServer.address, the address of the Spark history server (i.e. host.com:18080). The address should not contain a scheme (http://). Defaults to not being set since the history server is an optional service. This address is given to the Yarn ResourceManager when the Spark application finishes to link the application from the ResourceManager UI to the Spark history server UI.

Launching Spark on YARN

Ensure that HADOOP_CONF_DIR or YARN_CONF_DIR points to the directory which contains the (client side) configuration files for the Hadoop cluster. These configs are used to connect to the cluster, write to the dfs, and connect to the YARN ResourceManager.

There are two deploy modes that can be used to launch Spark applications on YARN. In yarn-cluster mode, the Spark driver runs inside an application master process which is managed by YARN on the cluster, and the client can go away after initiating the application. In yarn-client mode, the driver runs in the client process, and the application master is only used for requesting resources from YARN.

Unlike in Spark standalone and Mesos mode, in which the master's address is specified in the "master" parameter, in YARN mode the ResourceManager's address is picked up from the Hadoop configuration. Thus, the master parameter is simply "yarn-client" or "yarn-cluster".

The spark-submit script described in the cluster mode overview provides the most straightforward way to submit a compiled Spark application to YARN in either deploy mode. For info on the lower-level invocations it uses, read ahead. For running spark-shell against YARN, skip down to the yarn-client section.

Launching a Spark application with yarn-cluster mode.

The command to launch the Spark application on the cluster is as follows:

SPARK_JAR=<SPARK_ASSEMBLY_JAR_FILE> ./bin/spark-class org.apache.spark.deploy.yarn.Client \
  --jar <YOUR_APP_JAR_FILE> \
  --class <APP_MAIN_CLASS> \
  --arg <APP_MAIN_ARGUMENT> \
  --num-executors <NUMBER_OF_EXECUTOR_PROCESSES> \
  --driver-memory <MEMORY_FOR_ApplicationMaster> \
  --executor-memory <MEMORY_PER_EXECUTOR> \
  --executor-cores <CORES_PER_EXECUTOR> \
  --name <application_name> \
  --queue <queue_name> \
  --addJars <any_local_files_used_in_SparkContext.addJar> \
  --files <files_for_distributed_cache> \
  --archives <archives_for_distributed_cache>

To pass multiple arguments the "arg" option can be specified multiple times. For example:

# Build the Spark assembly JAR and the Spark examples JAR
$ SPARK_HADOOP_VERSION=2.0.5-alpha SPARK_YARN=true sbt/sbt assembly

# Configure logging
$ cp conf/log4j.properties.template conf/log4j.properties

# Submit Spark's ApplicationMaster to YARN's ResourceManager, and instruct Spark to run the SparkPi example
$ SPARK_JAR=./assembly/target/scala-{{site.SCALA_BINARY_VERSION}}/spark-assembly-{{site.SPARK_VERSION}}-hadoop2.0.5-alpha.jar \
    ./bin/spark-class org.apache.spark.deploy.yarn.Client \
      --jar examples/target/scala-{{site.SCALA_BINARY_VERSION}}/spark-examples-assembly-{{site.SPARK_VERSION}}.jar \
      --class org.apache.spark.examples.SparkPi \
      --arg yarn-cluster \
      --arg 5 \
      --num-executors 3 \
      --driver-memory 4g \
      --executor-memory 2g \
      --executor-cores 1

The above starts a YARN client program which starts the default Application Master. Then SparkPi will be run as a child thread of Application Master. The client will periodically poll the Application Master for status updates and display them in the console. The client will exit once your application has finished running. Refer to the "Viewing Logs" section below for how to see driver and executor logs.

Because the application is run on a remote machine where the Application Master is running, applications that involve local interaction, such as spark-shell, will not work.

Launching a Spark application with yarn-client mode.

With yarn-client mode, the application will be launched locally, just like running an application or spark-shell on Local / Mesos / Standalone client mode. The launch method is also the same, just make sure to specify the master URL as "yarn-client". You also need to export the env value for SPARK_JAR.