In addition to running on the Mesos or YARN cluster managers, Spark also provides a simple standalone deploy mode. You can launch a standalone cluster either manually, by starting a master and workers by hand, or use our provided launch scripts. It is also possible to run these daemons on a single machine for testing.
Installing Spark Standalone to a Cluster
To install Spark Standalone mode, you simply place a compiled version of Spark on each node on the cluster. You can obtain pre-built versions of Spark with each release or build it yourself.
環境
issue #382
10.144.226.42 kdc.cht.local
10.144.226.28 0a90e21c.cht.local 0a90e21c
10.144.226.29 0a90e21d.cht.local 0a90e21d
10.144.226.30 0a90e21e.cht.local 0a90e21e
10.144.226.31 0a90e21f.cht.local 0a90e21f
|
@@安裝java
[root@0a90e21c ~]# cat /etc/profile.d/jdk6.sh
export JAVA_HOME=/usr/java/jdk1.6.0_33
export PATH=$PATH:$JAVA_HOME/bin
.
@@安裝sbt
[root@0a90e21c ~]# unzip sbt-0.13.5.zip
[root@0a90e21c ~]# chmod -R 755 sbt
[root@0a90e21c ~]# chown -R root:root sbt/
[root@0a90e21c ~]# mv sbt/ /usr/lib/
[root@0a90e21c ~]# cat /usr/lib/sbt/sbt
SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
java $SBT_OPTS -jar `dirname $0`/sbt-launch.jar "$@"
[root@0a90e21c sbt]# chmod u+x /usr/lib/sbt/sbt
@@安裝scala
[root@0a90e21c ~]# tar zxvf scala-2.10.4.tgz
[root@0a90e21c ~]# chmod -R 755 scala-2.10.4/
[root@0a90e21c ~]# chown -R root:root scala-2.10.4/
[root@0a90e21c ~]# mv scala-2.10.4/ /usr/lib/
[root@0a90e21c ~]# cat /etc/profile.d/scala210.sh
export SCALA_HOME=/usr/lib/scala-2.10.4
export SBT_HOME=/usr/lib/sbt
export PATH=$PATH:$SCALA_HOME/bin:$SBT_HOME/bin
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do scp -r /usr/lib/scala-2.10.4 $i:/usr/lib/; done
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do ssh $i chmod -R 755 /usr/lib/scala-2.10.4/; done
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do ssh $i chown -R root:root /usr/lib/scala-2.10.4/; done
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do scp /etc/profile.d/scala210.sh $i:/etc/profile.d/; done
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do ssh $i chmod -R 755 /etc/profile.d/scala210.sh; done
.
@@安裝spark
[root@0a90e21c ~]# tar zxvf spark-1.0.0-bin-cdh4.tgz
[root@0a90e21c ~]# chmod -R 755 spark-1.0.0-bin-cdh4
[root@0a90e21c ~]# chown -R root:root spark-1.0.0-bin-cdh4
[root@0a90e21c ~]# mv spark-1.0.0-bin-cdh4 /usr/lib/
[root@0a90e21c ~]# cat /etc/profile.d/spark100.sh
export SPARK_HOME=/usr/lib/spark-1.0.0-bin-cdh4
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do scp -r /usr/lib/spark-1.0.0-bin-cdh4/ $i:/usr/lib/; done
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do ssh $i chmod -R 755 /usr/lib/spark-1.0.0-bin-cdh4/; done
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do ssh $i chown -R root:root /usr/lib/spark-1.0.0-bin-cdh4/; done
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do scp /etc/profile.d/spark100.sh $i:/etc/profile.d/; done
[root@0a90e21c ~]# HOSTS="0a90e21d 0a90e21e 0a90e21f";for i in `echo $HOSTS`; do ssh $i chmod -R 755 /etc/profile.d/spark100.sh; done
@@設定系統proxy
[root@0a90e21c ~]# cat /etc/profile.d/proxy.sh
export http_proxy=http://127.0.0.1:1888
|
Starting a Cluster Manually
You can start a standalone master server by executing:
./sbin/start-master.sh
Once started, the master will print out a spark://HOST:PORT URL for itself, which you can use to connect workers to it, or pass as the “master” argument to SparkContext. You can also find this URL on the master’s web UI, which is http://localhost:8080 by default.
Similarly, you can start one or more workers and connect them to the master via:
./bin/spark-class org.apache.spark.deploy.worker.Worker spark://IP:PORT
Once you have started a worker, look at the master’s web UI (http://localhost:8080 by default). You should see the new node listed there, along with its number of CPUs and memory (minus one gigabyte left for the OS).
Finally, the following configuration options can be passed to the master and worker:
Argument
|
Meaning
|
-i IP, --ip IP
|
IP address or DNS name to listen on
|
-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
|
Cluster Launch Scripts
To launch a Spark standalone cluster with the launch scripts, you need to create a file called conf/slaves in your Spark directory, which should contain the hostnames of all the machines where you would like to start Spark workers, one per line. The master machine must be able to access each of the slave machines via password-less ssh (using a private key). For testing, you can just put localhost in this file.
Once you’ve set up this file, you can launch or stop your cluster with the following shell scripts, based on Hadoop’s deploy scripts, and available in SPARK_HOME/bin:
@@script execution flow
./sbin/start-master.sh
. "sbin/spark-config.sh"
export SPARK_PREFIX
export SPARK_HOME
export SPARK_CONF_DIR
. "bin/load-spark-env.sh"
. "conf/spark-env.sh"
sbin/spark-daemon.sh start org.apache.spark.deploy.master.Master 1 --ip $SPARK_MASTER_IP --port $SPARK_MASTER_PORT --webui-port $SPARK_MASTER_WEBUI_PORT
. "sbin/spark-config.sh"
. "bin/load-spark-env.sh"
export SPARK_IDENT_STRING="$USER"
export SPARK_LOG_DIR="$SPARK_HOME/logs"
SPARK_PID_DIR=/tmp
export SPARK_LOGFILE=spark-$SPARK_IDENT_STRING-$command-$instance-$HOSTNAME.log
export SPARK_ROOT_LOGGER="INFO,DRFA"
log=$SPARK_LOG_DIR/spark-$SPARK_IDENT_STRING-$command-$instance-$HOSTNAME.out
pid=$SPARK_PID_DIR/spark-$SPARK_IDENT_STRING-$command-$instance.pid
nohup nice -n $SPARK_NICENESS "$SPARK_PREFIX"/bin/spark-class $command "$@" >> "$log" 2>&1 < /dev/null &
./sbin/start-slaves.sh
. "sbin/spark-config.sh"
exec "sbin/slaves.sh" cd "$SPARK_HOME" \; "$sbin/start-slave.sh" 1 spark://$SPARK_MASTER_IP:$SPARK_MASTER_PORT
"sbin/slaves.sh" cd "$SPARK_HOME" \; "$sbin/start-slave.sh" $(( $i + 1 )) spark://$SPARK_MASTER_IP:$SPARK_MASTER_PORT --webui-port $(( $SPARK_WORKER_WEBUI_PORT + $i ))
sbin/spark-daemon.sh start org.apache.spark.deploy.worker.Worker "$@"
|
./sbin/stop-slaves.sh
. "$sbin/spark-config.sh"
. "$SPARK_PREFIX/bin/load-spark-env.sh"
"$sbin"/spark-daemons.sh stop org.apache.spark.deploy.worker.Worker 1
"$sbin"/spark-daemons.sh stop org.apache.spark.deploy.worker.Worker $(( $i + 1 ))
. "$sbin/spark-config.sh"
exec "$sbin/slaves.sh" cd "$SPARK_HOME" \; "$sbin/spark-daemon.sh" "$@"
.sbin/stop-master.sh
. "$sbin/spark-config.sh"
"$sbin"/spark-daemon.sh stop org.apache.spark.deploy.master.Master 1
.
|
[root@0a90e21e spark-1.0.0-bin-cdh4]# ls $SPARK_HOME/logs
spark-root-org.apache.spark.deploy.worker.Worker-1-0a90e21e.out
[root@0a90e21e spark-1.0.0-bin-cdh4]# ps -ef|grep deploy.worker
root 16104 1 0 Jun09 ? 03:02:08 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar -XX:MaxPermSize=128m -Dspark.akka.logLifecycleEvents=true -Xms512m -Xmx512m org.apache.spark.deploy.worker.Worker spark://10.144.226.28:7077
[root@0a90e21e spark-1.0.0-bin-cdh4]# echo "16104" > /tmp/spark-root-org.apache.spark.deploy.worker.Worker-1.pid
[root@0a90e21e spark-1.0.0-bin-cdh4]# sbin/spark-daemon.sh stop org.apache.spark.deploy.worker.Worker 1
stopping org.apache.spark.deploy.worker.Worker
[root@0a90e21e spark-1.0.0-bin-cdh4]# ps -ef|grep deploy.worker root 12379 12200 0 10:33 pts/0 00:00:00 grep deploy.worker
|
[root@0a90e21c spark-1.0.0-bin-cdh4]# ls logs/
spark-root-org.apache.spark.deploy.master.Master-1-0a90e21c.out
[root@0a90e21c spark-1.0.0-bin-cdh4]# ps -ef|grep deploy.master
root 10401 1 0 Jun09 ? 02:58:18 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar -XX:MaxPermSize=128m -Dspark.akka.logLifecycleEvents=true -Xms512m -Xmx512m org.apache.spark.deploy.master.Master --ip 10.144.226.28 --port 7077 --webui-port 8080
[root@0a90e21c spark-1.0.0-bin-cdh4]# echo "10401" > /tmp/spark-root-org.apache.spark.deploy.master.Master-1.pid
[root@0a90e21c spark-1.0.0-bin-cdh4]# sbin/stop-master.sh
[root@0a90e21c spark-1.0.0-bin-cdh4]# ps -ef|grep deploy.master
root 29634 30597 0 10:43 pts/0 00:00:00 grep deploy.master
|
- sbin/spark-config.sh
- bin/load-spark-env.sh
- sbin/spark-daemon.sh
- sbin/start-master.sh - Starts a master instance on the machine the script is executed on.sbin/start-slave.sh
- sbin/start-slaves.sh - Starts a slave instance on each machine specified in the conf/slaves file.
- sbin/start-daemons.sh
- sbin/slaves.sh
- sbin/start-all.sh - Starts both a master and a number of slaves as described above.
- sbin/stop-master.sh - Stops the master that was started via the bin/start-master.sh script.
- sbin/stop-slaves.sh - Stops all slave instances on the machines specified in the conf/slaves file.
- sbin/stop-all.sh - Stops both the master and the slaves as described above.
Note that these scripts must be executed on the machine you want to run the Spark master on, not your local machine.
You can optionally configure the cluster further by setting environment variables in conf/spark-env.sh. Create this file by starting with theconf/spark-env.sh.template, and copy it to all your worker machines for the settings to take effect. The following settings are available:
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-env.sh的SPARK_WORKER_MEMORY指定該主機用來執行spark所有應用程式的最大可用記憶體
@@spark-submit.sh的--executor-memory或SparkConf物件參數spark.executor.memory指定該應用程式的每個executor最大可用記憶體
|
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: 512m).
@@修改為1024則出現錯誤訊息
failed to launch org.apache.spark.deploy.master.Master:
Error occurred during initialization of VM
Too small initial heap
@@修改為4g則OK
|
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).
|
Note: The launch scripts do not currently support Windows. To run a Spark cluster on Windows, start the master and workers by hand.
SPARK_MASTER_OPTS supports the following system properties:
Property Name
|
Default
|
Meaning
|
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 (SparkConf物件). If not set, applications always get all available cores unless they configure spark.cores.max (SparkConf物件) 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 supports the following system properties:
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. Applications directories are cleaned up regardless of whether the application is still running.
|
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.
|
@@修改slaves檔案, 並設定password-less SSH
[root@0a90e21c]# cat $SPARK_HOME/conf/slaves
# A Spark Worker will be started on each of the machines listed below.
0a90e21d.cht.local
0a90e21e.cht.local
0a90e21f.cht.local
@@根據Spark Configuration說明,
Environment variables can be used to set per-machine settings, such as the IP address, through the conf/spark-env.sh script on each node.
@@修改spark-env.sh
@@PID預設路徑為/tmp會清除檔案, 因此另外設定PID目錄
[root@0a90e21c ~]# cp $SPARK_HOME/conf/spark-env.sh.template $SPARK_HOME/conf/spark-env.sh;chmod 755 $SPARK_HOME/conf/spark-env.sh
[root@0a90e21c ~]# vim $SPARK_HOME/conf/spark-env.sh
[root@0a90e21c util]# sh scpfile.sh $SPARK_HOME/conf/spark-env.sh
@@啟動master
[root@0a90e21c ~]# $SPARK_HOME/sbin/start-master.sh
starting org.apache.spark.deploy.master.Master, logging to /usr/lib/spark-1.0.0-bin-cdh4/sbin/../logs/spark-root-org.apache.spark.deploy.master.Master-1-0a90e21c.out
[root@0a90e21c ~]# vim $SPARK_HOME/logs/spark-root-org.apache.spark.deploy.master.Master-1-0a90e21c.out
@@master占用記憶體4G
[root@0a90e21c ~]# ps -ef|grep deploy.master
root 8027 1 0 10:53 pts/0 00:00:28 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar -XX:MaxPermSize=128m -Dspark.akka.logLifecycleEvents=true -Dspark.deploy.defaultCores=24 -Xms4g -Xmx4g org.apache.spark.deploy.master.Master --ip 10.144.226.28 --port 7077 --webui-port 8080
[root@0a90e21c ~]# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10401 root 20 0 4561m 200m 16m S 0.7 0.2 0:22.59 java
@@log & pid檔案名稱
[root@0a90e21c spark-1.0.0-bin-cdh4]# ls logs/
spark-root-org.apache.spark.deploy.master.Master-1-0a90e21c.out
spark-root-org.apache.spark.deploy.master.Master-1.pid
@@啟動slaves
[root@0a90e21c ~]# $SPARK_HOME/sbin/start-slaves.sh
localhost: starting org.apache.spark.deploy.worker.Worker, logging to /usr/lib/spark-1.0.0-bin-cdh4/sbin/../logs/spark-root-org.apache.spark.deploy.worker.Worker-1-localhost.localdomain.out
[root@0a90e21e ~]# vim $SPARK_HOME/logs/spark-root-org.apache.spark.deploy.worker.Worker-1-0a90e21e.out
@@worker占用記憶體4G
[root@0a90e21e ~]# ps -ef|grep deploy.worker
root 12719 1 2 10:54 ? 00:00:06 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar -XX:MaxPermSize=128m -Dspark.akka.logLifecycleEvents=true -Xms4g -Xmx4g org.apache.spark.deploy.worker.Worker spark://10.144.226.28:7077
[root@0a90e21e ~]# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16104 root 20 0 4627m 197m 16m S 0.7 0.2 0:19.26 java
[root@0a90e21e ~]# ls -l $SPARK_HOME/logs/
spark-root-org.apache.spark.deploy.worker.Worker-1-0a90e21e.out
spark-root-org.apache.spark.deploy.worker.Worker-1.pid
@@停止slaves
[root@0a90e21c spark-1.0.0-bin-cdh4]# ./sbin/stop-slaves.sh
0a90e21f.cht.local: stopping org.apache.spark.deploy.worker.Worker
0a90e21e.cht.local: stopping org.apache.spark.deploy.worker.Worker
0a90e21d.cht.local: stopping org.apache.spark.deploy.worker.Worker
@@停止master
[root@0a90e21c spark-1.0.0-bin-cdh4]# ./sbin/stop-master.sh
stopping org.apache.spark.deploy.master.Master
|
Connecting an Application to the Cluster
To run an application on the Spark cluster, simply pass the spark://IP:PORT URL of the master as to the SparkContext constructor.
To run an interactive Spark shell against the cluster, run the following command:
./bin/spark-shell --master spark://IP:PORT
Note that if you are running spark-shell from one of the spark cluster machines, the bin/spark-shell script will automatically set MASTER from the SPARK_MASTER_IP and SPARK_MASTER_PORT variables in conf/spark-env.sh.
You can also pass an option --cores <numCores> to control the number of cores that spark-shell uses on the cluster.
@@spark-shell只有--driver-cores , --total-executor-cores兩個參數
Launching Compiled Spark Applications
Spark supports two deploy modes: applications may run with the driver inside the client process or entirely inside the cluster. The spark-submitscript provides the most straightforward way to submit a compiled Spark application to the cluster in either deploy mode.
If your application is launched through Spark submit, then the application jar is automatically distributed to all worker nodes. For any additional jars that your application depends on, you should specify them through the --jars flag using comma as a delimiter (e.g. --jars jar1,jar2). To control the application’s configuration or execution environment, see Spark Configuration.
Resource Scheduling
The standalone cluster mode currently only supports a simple FIFO scheduler across applications. However, to allow multiple concurrent users, you can control the maximum number of resources each application will use. By default, it will acquire all cores in the cluster, which only makes sense if you just run one application at a time. You can cap the number of cores by setting spark.cores.max in your SparkConf. For example:
val conf = new SparkConf()
.setMaster(...)
.setAppName(...)
.set("spark.cores.max", "10")
val sc = new SparkContext(conf)
.setMaster(...)
.setAppName(...)
.set("spark.cores.max", "10")
val sc = new SparkContext(conf)
In addition, you can configure spark.deploy.defaultCores on the cluster master process to change the default for applications that don’t set spark.cores.max to something less than infinite. Do this by adding the following to conf/spark-env.sh:
export SPARK_MASTER_OPTS="-Dspark.deploy.defaultCores=<value>"
This is useful on shared clusters where users might not have configured a maximum number of cores individually.
@@當cluster有多餘的core數時可以同時執行多個spark應用程式, 參考Spark Programming Guide啟動如2個spark-shell範例
Monitoring and Logging
Spark’s standalone mode offers a web-based user interface to monitor the cluster. The master and each worker has its own web UI that shows cluster and job statistics. By default you can access the web UI for the master at port 8080. The port can be changed either in the configuration file or via command-line options.
In addition, detailed log output for each job is also written to the work directory of each slave node (SPARK_HOME/work by default). You will see two files for each job, stdout and stderr, with all output it wrote to its console.
Spark Master UI
Application UI -> Executors
@@檢查worker_id=0執行application_id=app-20140703144758-0000的log目錄
[root@0a90e21d ~]# ls /usr/lib/spark-1.0.0-bin-cdh4/work/app-20140703144758-0000/0/
stderr stdout
@@讀取HDFS GZ file進行wordcount
[root@0a90e21d ~]# vim /usr/lib/spark-1.0.0-bin-cdh4/work/app-20140703144758-0000/0/stderr
14/07/03 14:59:42 INFO BlockManager: Found block broadcast_0 locally
14/07/03 14:59:42 INFO BlockManager: Found block broadcast_0 locally
14/07/03 14:59:42 INFO BlockManager: Found block broadcast_0 locally
14/07/03 14:59:42 INFO BlockManager: Found block broadcast_0 locally
14/07/03 14:59:42 INFO BlockManager: Found block broadcast_0 locally
14/07/03 14:59:42 INFO BlockManager: Found block broadcast_0 locally
14/07/03 14:59:42 INFO BlockManager: Found block broadcast_0 locally
14/07/03 14:59:42 INFO HadoopRDD: Input split: hdfs://0a90e21c.cht.local:8020/tmp/s3log/gz/S3log_2013012901_api1-8_cat.gz:0+106646725
14/07/03 14:59:42 INFO HadoopRDD: Input split: hdfs://0a90e21c.cht.local:8020/tmp/s3log/gz/S3log_2013012904_api1-8_cat.gz:0+102971722
14/07/03 14:59:42 INFO HadoopRDD: Input split: hdfs://0a90e21c.cht.local:8020/tmp/s3log/gz/S3log_2013012910_api1-8_cat.gz:0+102971724
14/07/03 14:59:42 INFO HadoopRDD: Input split: hdfs://0a90e21c.cht.local:8020/tmp/s3log/gz/S3log_2013012919_api1-8_cat.gz:0+106648480
@@檢查worker_id=2執行application_id=app-20140703144758-0000的log目錄
[root@0a90e21f ~]# ls $SPARK_HOME/work/app-20140703144758-0000/2
stderr stdout
|
@@參考Spark Programming Guide範例
Running Alongside Hadoop
You can run Spark alongside your existing Hadoop cluster by just launching it as a separate service on the same machines. To access Hadoop data from Spark, just use a hdfs:// URL (typically hdfs://<namenode>:9000/path, but you can find the right URL on your Hadoop Namenode’s web UI). Alternatively, you can set up a separate cluster for Spark, and still have it access HDFS over the network; this will be slower than disk-local access, but may not be a concern if you are still running in the same local area network (e.g. you place a few Spark machines on each rack that you have Hadoop on).
Configuring Ports for Network Security
Spark makes heavy use of the network, and some environments have strict requirements for using tight firewall settings. Below are the primary ports that Spark uses for its communication and how to configure those ports.
From
|
To
|
Default Port
|
Purpose
|
Configuration Setting
|
Notes
|
Browser
|
Standalone Cluster Master
|
8080
|
Web UI
|
master.ui.port
|
Jetty-based
|
Browser
|
Driver
|
4040
|
Web UI
|
spark.ui.port
|
Jetty-based
|
Browser
|
History Server
|
18080
|
Web UI
|
spark.history.ui.port
|
Jetty-based
|
Browser
|
Worker
|
8081
|
Web UI
|
worker.ui.port
|
Jetty-based
|
Application
|
Standalone Cluster Master
|
7077
|
Submit job to cluster
|
spark.driver.port
|
Akka-based. Set to "0" to choose a port randomly
|
Worker
|
Standalone Cluster Master
|
7077
|
Join cluster
|
spark.driver.port
|
Akka-based. Set to "0" to choose a port randomly
|
Application
|
Worker
|
(random)
|
Join cluster
|
SPARK_WORKER_PORT(standalone cluster)
|
Akka-based
|
Driver and other Workers
|
Worker
|
(random)
|
|
None
|
Jetty-based. Each of these services starts on a random port that cannot be configured
|
High Availability
By default, standalone scheduling clusters are resilient to Worker failures (insofar as Spark itself is resilient to losing work by moving it to other workers). However, the scheduler uses a Master to make scheduling decisions, and this (by default) creates a single point of failure: if the Master crashes, no new applications can be created. In order to circumvent this, we have two high availability schemes, detailed below.
Standby Masters with ZooKeeper
Overview
Utilizing ZooKeeper to provide leader election and some state storage, you can launch multiple Masters in your cluster connected to the same ZooKeeper instance. One will be elected “leader” and the others will remain in standby mode. If the current leader dies, another Master will be elected, recover the old Master’s state, and then resume scheduling. The entire recovery process (from the time the the first leader goes down) should take between 1 and 2 minutes. Note that this delay only affects scheduling new applications – applications that were already running during Master failover are unaffected.
Configuration
In order to enable this recovery mode, you can set SPARK_DAEMON_JAVA_OPTS in conf/spark-env.sh using this configuration:
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).
|
Possible gotcha: If you have multiple Masters in your cluster but fail to correctly configure the Masters to use ZooKeeper, the Masters will fail to discover each other and think they’re all leaders. This will not lead to a healthy cluster state (as all Masters will schedule independently).
Details
After you have a ZooKeeper cluster set up, enabling high availability is straightforward. Simply start multiple Master processes on different nodes with the same ZooKeeper configuration (ZooKeeper URL and directory). Masters can be added and removed at any time.
In order to schedule new applications or add Workers to the cluster, they need to know the IP address of the current leader. This can be accomplished by simply passing in a list of Masters where you used to pass in a single one. For example, you might start your SparkContext pointing to spark://host1:port1,host2:port2. This would cause your SparkContext to try registering with both Masters – if host1 goes down, this configuration would still be correct as we’d find the new leader, host2.
There’s an important distinction to be made between “registering with a Master” and normal operation. When starting up, an application or Worker needs to be able to find and register with the current lead Master. Once it successfully registers, though, it is “in the system” (i.e., stored in ZooKeeper). If failover occurs, the new leader will contact all previously registered applications and Workers to inform them of the change in leadership, so they need not even have known of the existence of the new Master at startup.
Due to this property, new Masters can be created at any time, and the only thing you need to worry about is that new applications and Workers can find it to register with in case it becomes the leader. Once registered, you’re taken care of.
@@修改spark-env.sh移除SPARK_MASTER_IP值
[root@0a90e21c ~]# vim $SPARK_HOME/conf/spark-env.sh
[root@0a90e21c util]# sh scpfile.sh $SPARK_HOME/conf/spark-env.sh
@@啟動master@0a90e21c
[root@0a90e21c ~]# $SPARK_HOME/sbin/start-master.sh
starting org.apache.spark.deploy.master.Master, logging to starting org.apache.spark.deploy.master.Master, logging to /usr/lib/spark-1.0.0-bin-cdh4/sbin/../logs/spark-root-org.apache.spark.deploy.master.Master-1-0a90e21c.out
[root@0a90e21c ~]# vim $SPARK_HOME/logs/spark-root-org.apache.spark.deploy.master.Master-1-0a90e21c.out
[root@0a90e21c ~]# ls $SPARK_HOME/logs
spark-root-org.apache.spark.deploy.master.Master-1-0a90e21c.out
spark-root-org.apache.spark.deploy.master.Master-1.pid
[root@0a90e21c util]# ps -ef|grep `cat $SPARK_HOME/logs/spark-root-org.apache.spark.deploy.master.Master-1.pid`
root 1604 1 2 17:00 pts/0 00:00:06 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar -XX:MaxPermSize=128m -Dspark.deploy.recoveryMode=ZOOKEEPER -Dspark.deploy.zookeeper.url=0a90e21c.cht.local:2181 -Dspark.akka.logLifecycleEvents=true -Dspark.deploy.defaultCores=24 -Xms4g -Xmx4g org.apache.spark.deploy.master.Master --ip 0a90e21c --port 7077 --webui-port 8080
[root@0a90e21c ~]# top -p 1604
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1604 root 20 0 8481m 236m 16m S 0.0 0.2 0:06.42 java
@@啟動master@0a90e21d
[root@0a90e21d ~]# $SPARK_HOME/sbin/start-master.sh
starting org.apache.spark.deploy.master.Master, logging to /usr/lib/spark-1.0.0-bin-cdh4/sbin/../logs/spark-root-org.apache.spark.deploy.master.Master-1-0a90e21d.out
[root@0a90e21d ~]# vim $SPARK_HOME/logs/spark-root-org.apache.spark.deploy.master.Master-1-0a90e21d.out
[root@0a90e21d ~]# ls $SPARK_HOME/logs
spark-root-org.apache.spark.deploy.master.Master-1-0a90e21d.out
spark-root-org.apache.spark.deploy.master.Master-1.pid
[root@0a90e21d ~]# ps -ef|grep `cat $SPARK_HOME/logs/spark-root-org.apache.spark.deploy.master.Master-1.pid`
root 27505 1 2 17:02 pts/0 00:00:06 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar -XX:MaxPermSize=128m -Dspark.deploy.recoveryMode=ZOOKEEPER -Dspark.deploy.zookeeper.url=0a90e21c.cht.local:2181 -Dspark.akka.logLifecycleEvents=true -Dspark.deploy.defaultCores=24 -Xms4g -Xmx4g org.apache.spark.deploy.master.Master --ip 0a90e21d --port 7077 --webui-port 8080
[root@0a90e21d ~]# top -p 27505
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27505 root 20 0 8546m 155m 16m S 2.0 0.2 0:06.72 java
@@啟動slaves
[root@0a90e21c ~]# $SPARK_HOME/sbin/start-slaves.sh
[root@0a90e21c ~]# sh sshcmd.sh "ps -ef|grep spark"
[0a90e21d.cht.local]
root 27505 1 2 17:02 pts/0 00:00:06 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar -XX:MaxPermSize=128m -Dspark.deploy.recoveryMode=ZOOKEEPER -Dspark.deploy.zookeeper.url=0a90e21c.cht.local:2181 -Dspark.akka.logLifecycleEvents=true -Dspark.deploy.defaultCores=24 -Xms4g -Xmx4g org.apache.spark.deploy.master.Master --ip 0a90e21d --port 7077 --webui-port 8080
root 27728 1 2 17:04 ? 00:00:06 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar -XX:MaxPermSize=128m -Dspark.deploy.recoveryMode=ZOOKEEPER -Dspark.deploy.zookeeper.url=0a90e21c.cht.local:2181 -Dspark.akka.logLifecycleEvents=true -Xms4g -Xmx4g org.apache.spark.deploy.worker.Worker spark://0a90e21c:7077
[0a90e21e.cht.local]
root 20097 1 3 17:04 ? 00:00:07 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar -XX:MaxPermSize=128m -Dspark.deploy.recoveryMode=ZOOKEEPER -Dspark.deploy.zookeeper.url=0a90e21c.cht.local:2181 -Dspark.akka.logLifecycleEvents=true -Xms4g -Xmx4g org.apache.spark.deploy.worker.Worker spark://0a90e21c:7077
[0a90e21f.cht.local]
root 20747 1 2 17:04 ? 00:00:06 /usr/java/jdk1.6.0_33/bin/java -cp ::/usr/lib/spark-1.0.0-bin-cdh4/conf:/usr/lib/spark-1.0.0-bin-cdh4/lib/spark-assembly-1.0.0-hadoop2.0.0-mr1-cdh4.2.0.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-rdbms-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-api-jdo-3.2.1.jar:/usr/lib/spark-1.0.0-bin-cdh4/lib/datanucleus-core-3.2.2.jar -XX:MaxPermSize=128m -Dspark.deploy.recoveryMode=ZOOKEEPER -Dspark.deploy.zookeeper.url=0a90e21c.cht.local:2181 -Dspark.akka.logLifecycleEvents=true -Xms4g -Xmx4g org.apache.spark.deploy.worker.Worker spark://0a90e21c:7077
@@啟動spark-shell@0a90e21c
[root@0a90e21c ~]# $SPARK_HOME/bin/spark-shell --master spark://0a90e21c:7077,0a90e21d:7077 --executor-memory 48G
...
scala> 14/06/30 17:11:12 INFO SparkDeploySchedulerBackend: Connected to Spark cluster with app ID app-20140630171112-0000
14/06/30 17:11:12 INFO AppClient$ClientActor: Executor added: app-20140630171112-0000/0 on worker-20140630170414-0a90e21f.cht.local-56699 (0a90e21f.cht.local:56699) with 8 cores
14/06/30 17:11:12 INFO SparkDeploySchedulerBackend: Granted executor ID app-20140630171112-0000/0 on hostPort 0a90e21f.cht.local:56699 with 8 cores, 48.0 GB RAM
14/06/30 17:11:12 INFO AppClient$ClientActor: Executor added: app-20140630171112-0000/1 on worker-20140630170414-0a90e21d.cht.local-45268 (0a90e21d.cht.local:45268) with 8 cores
14/06/30 17:11:12 INFO SparkDeploySchedulerBackend: Granted executor ID app-20140630171112-0000/1 on hostPort 0a90e21d.cht.local:45268 with 8 cores, 48.0 GB RAM
14/06/30 17:11:12 INFO AppClient$ClientActor: Executor added: app-20140630171112-0000/2 on worker-20140630170415-0a90e21e.cht.local-35470 (0a90e21e.cht.local:35470) with 8 cores
14/06/30 17:11:12 INFO SparkDeploySchedulerBackend: Granted executor ID app-20140630171112-0000/2 on hostPort 0a90e21e.cht.local:35470 with 8 cores, 48.0 GB RAM
14/06/30 17:11:12 INFO AppClient$ClientActor: Executor updated: app-20140630171112-0000/0 is now RUNNING
14/06/30 17:11:12 INFO AppClient$ClientActor: Executor updated: app-20140630171112-0000/1 is now RUNNING
14/06/30 17:11:12 INFO AppClient$ClientActor: Executor updated: app-20140630171112-0000/2 is now RUNNING
scala> 14/06/30 17:11:16 INFO SparkDeploySchedulerBackend: Registered executor: Actor[akka.tcp://sparkExecutor@0a90e21f.cht.local:36534/user/Executor#-1652731134] with ID 0
14/06/30 17:11:17 INFO BlockManagerInfo: Registering block manager 0a90e21f.cht.local:59636 with 27.6 GB RAM
14/06/30 17:11:19 INFO SparkDeploySchedulerBackend: Registered executor: Actor[akka.tcp://sparkExecutor@0a90e21e.cht.local:54994/user/Executor#961795858] with ID 2
14/06/30 17:11:19 INFO SparkDeploySchedulerBackend: Registered executor: Actor[akka.tcp://sparkExecutor@0a90e21d.cht.local:50298/user/Executor#1083175222] with ID 1
14/06/30 17:11:19 INFO BlockManagerInfo: Registering block manager 0a90e21e.cht.local:35382 with 27.6 GB RAM
14/06/30 17:11:20 INFO BlockManagerInfo: Registering block manager 0a90e21d.cht.local:32822 with 27.6 GB RAM
@@啟動spark-shell@0a90e21d
$SPARK_HOME/bin/spark-shell --master spark://0a90e21c:7077,0a90e21d:7077 --executor-memory 48G
...
scala> 14/06/30 17:12:17 INFO SparkDeploySchedulerBackend: Connected to Spark cluster with app ID app-20140630171218-0001
14/06/30 17:12:17 INFO AppClient$ClientActor: Executor added: app-20140630171218-0001/0 on worker-20140630170414-0a90e21f.cht.local-56699 (0a90e21f.cht.local:56699) with 8 cores
14/06/30 17:12:17 INFO SparkDeploySchedulerBackend: Granted executor ID app-20140630171218-0001/0 on hostPort 0a90e21f.cht.local:56699 with 8 cores, 16.0 GB RAM
14/06/30 17:12:17 INFO AppClient$ClientActor: Executor added: app-20140630171218-0001/1 on worker-20140630170414-0a90e21d.cht.local-45268 (0a90e21d.cht.local:45268) with 8 cores
14/06/30 17:12:17 INFO SparkDeploySchedulerBackend: Granted executor ID app-20140630171218-0001/1 on hostPort 0a90e21d.cht.local:45268 with 8 cores, 16.0 GB RAM
14/06/30 17:12:17 INFO AppClient$ClientActor: Executor added: app-20140630171218-0001/2 on worker-20140630170415-0a90e21e.cht.local-35470 (0a90e21e.cht.local:35470) with 8 cores
14/06/30 17:12:17 INFO SparkDeploySchedulerBackend: Granted executor ID app-20140630171218-0001/2 on hostPort 0a90e21e.cht.local:35470 with 8 cores, 16.0 GB RAM
14/06/30 17:12:17 INFO AppClient$ClientActor: Executor updated: app-20140630171218-0001/2 is now RUNNING
14/06/30 17:12:17 INFO AppClient$ClientActor: Executor updated: app-20140630171218-0001/0 is now RUNNING
14/06/30 17:12:17 INFO AppClient$ClientActor: Executor updated: app-20140630171218-0001/1 is now RUNNING
14/06/30 17:12:20 INFO SparkDeploySchedulerBackend: Registered executor: Actor[akka.tcp://sparkExecutor@0a90e21f.cht.local:58067/user/Executor#-45924075] with ID 0
14/06/30 17:12:20 INFO SparkDeploySchedulerBackend: Registered executor: Actor[akka.tcp://sparkExecutor@0a90e21d.cht.local:56946/user/Executor#2012246877] with ID 1
14/06/30 17:12:21 INFO BlockManagerInfo: Registering block manager 0a90e21f.cht.local:41700 with 9.2 GB RAM
14/06/30 17:12:21 INFO BlockManagerInfo: Registering block manager 0a90e21d.cht.local:42637 with 9.2 GB RAM
scala> 14/06/30 17:12:22 INFO SparkDeploySchedulerBackend: Registered executor: Actor[akka.tcp://sparkExecutor@0a90e21e.cht.local:39789/user/Executor#-2016850795] with ID 2
14/06/30 17:12:23 INFO BlockManagerInfo: Registering block manager 0a90e21e.cht.local:35046 with 9.2 GB RAM
@@中止0a90e21c的master
[root@0a90e21c ~]# kill -9 1604
@@檢視0a90e21d的master UI, 原先application資訊消失
[root@0a90e21d ~]# vim $SPARK_HOME/logs/spark-root-org.apache.spark.deploy.master.Master-1-0a90e21d.out
@@在原先spark-shell@0a90e21c執行程式
@@在原先spark-shell@止0a90e21d執行程式
@@worker UI
|
Single-Node Recovery with Local File System
Overview
ZooKeeper is the best way to go for production-level high availability, but if you just want to be able to restart the Master if it goes down, FILESYSTEM mode can take care of it. When applications and Workers register, they have enough state written to the provided directory so that they can be recovered upon a restart of the Master process.
Configuration
In order to enable this recovery mode, you can set SPARK_DAEMON_JAVA_OPTS in spark-env using this configuration:
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.
|
Details
- This solution can be used in tandem with a process monitor/manager like monit, or just to enable manual recovery via restart.
- While filesystem recovery seems straightforwardly better than not doing any recovery at all, this mode may be suboptimal for certain development or experimental purposes. In particular, killing a master via stop-master.sh does not clean up its recovery state, so whenever you start a new Master, it will enter recovery mode. This could increase the startup time by up to 1 minute if it needs to wait for all previously-registered Workers/clients to timeout.
- While it’s not officially supported, you could mount an NFS directory as the recovery directory. If the original Master node dies completely, you could then start a Master on a different node, which would correctly recover all previously registered Workers/applications (equivalent to ZooKeeper recovery). Future applications will have to be able to find the new Master, however, in order to register.