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。
