【原】Spark Standalone模式
Spark?Standalone模式
- 安裝Spark?Standalone集群
- 手動(dòng)啟動(dòng)集群
- 集群創(chuàng)建腳本
- 提交應(yīng)用到集群
- 創(chuàng)建Spark應(yīng)用
- 資源調(diào)度及分配
- 監(jiān)控與日志
- 與Hadoop共存
- 配置網(wǎng)絡(luò)安全端口
- 高可用性
- 基于Zookeeper的Master
- 本地系統(tǒng)的單節(jié)點(diǎn)恢復(fù)
除了運(yùn)行在mesos或yarn集群管理器中,spark也提供了簡(jiǎn)單的standalone部署模式。你可以通過(guò)手動(dòng)啟動(dòng)master和worker節(jié)點(diǎn)來(lái)創(chuàng)建集群,或者用官網(wǎng)提供的啟動(dòng)腳本。這些守護(hù)進(jìn)程也可以只在一臺(tái)機(jī)器上以便測(cè)試使用。
1.安裝Spark?Standalone集群
安裝Spark?Standalone集群,你只需要在每個(gè)節(jié)點(diǎn)上部署編譯好的Spark即可。你可以在官網(wǎng)上得到已經(jīng)預(yù)編譯好的,也可以根據(jù)自己的需要進(jìn)行編譯。
2.手動(dòng)啟動(dòng)集群
你可以啟動(dòng)Standalone模式的master服務(wù),通過(guò)執(zhí)行如下命令:
./sbin/start-master.sh
一旦啟動(dòng),master節(jié)點(diǎn)將打印出Spark://HOST:PORT?URL,你可以用這個(gè)URL來(lái)連接worker節(jié)點(diǎn)或者把它賦值給“master”參數(shù)傳遞給SparkContext。你也可以在master的WEB?UI找到這個(gè)URL,默認(rèn)的是http://localhost:8080,最好是http://master所在的ip地址:8080,這樣和master在同一個(gè)局域網(wǎng)內(nèi)的機(jī)器都可以訪問(wèn)。
同樣地,你可以啟動(dòng)一個(gè)或多個(gè)worker節(jié)點(diǎn)并把它注冊(cè)到master節(jié)點(diǎn)上,執(zhí)行如下命令:
./sbin/start-slave.sh?<master-spark-URL>
一旦你啟動(dòng)了worker節(jié)點(diǎn),通過(guò)master的WEB?UI,你可以看到注冊(cè)到它上面的worker的信息,比如CPU核數(shù)、內(nèi)存等。
最后,下面的配置選項(xiàng)可以傳遞給master和worker節(jié)點(diǎn)。
| 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.集群創(chuàng)建腳本
如果用腳本啟動(dòng)集群的話,你應(yīng)該在你的Spark_HOME下創(chuàng)建一個(gè)conf/slaves,這個(gè)slaves文件必須包含worker的主機(jī)名,每行一個(gè)。如果conf/slaves不存在的話,創(chuàng)建腳本默認(rèn)值啟動(dòng)本機(jī)單個(gè)節(jié)點(diǎn),這對(duì)于測(cè)試很有用。注意,master通過(guò)ssh來(lái)和worker進(jìn)行通信。
一旦你設(shè)置了這個(gè)文件,你可以通過(guò)下面的Shell腳本來(lái)啟動(dòng)或停止集群,類(lèi)似于Hadoop的部署腳本,這些腳本在SPARK_HOME/sbin下找到。
- sbin/start-master.sh?-?啟動(dòng)腳本所在機(jī)器上的master節(jié)點(diǎn)
- sbin/start-slaves.sh?-?啟動(dòng)conf/slaves文件中指定的slave所有節(jié)點(diǎn)
- sbin/start-slave.sh?-?啟動(dòng)腳本所在的機(jī)器上的slave節(jié)點(diǎn)
- sbin/start-all.sh?-?啟動(dòng)腳本所在的slave節(jié)點(diǎn)及與其相關(guān)的slave節(jié)點(diǎn)
- sbin/stop-master.sh?-?停止腳本所在機(jī)器上的master節(jié)點(diǎn)
- sbin/stop-slaves.sh?-?啟動(dòng)conf/slaves文件中指定的slave所有節(jié)點(diǎn)?
- sbin/stop-all.sh?-?停止腳本所在機(jī)器上的master節(jié)點(diǎn)
注意這些腳本必須在你想要運(yùn)行Spark?master節(jié)點(diǎn)上,而不是你本地機(jī)器
你可以在conf/spark-env.sh中選擇性地配置下面的選項(xiàng),這個(gè)文件集群中的每臺(tái)機(jī)器都必須有。
| 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可以配置下面的系統(tǒng)屬性:
| 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可以配置下面的系統(tǒng)屬性:
| 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.提交應(yīng)用到集群
在Spark集群中運(yùn)行一個(gè)Spark應(yīng)用程序,需要把master節(jié)點(diǎn)的Spark://IP:PORT?URL傳遞給SparkContext?的構(gòu)造函數(shù)中。
在交互式Shell中Spark應(yīng)用程序,需運(yùn)行下面的命令:
./bin/spark-shell?--master?spark://IP:PORT
你也可以傳遞選項(xiàng)--total-executor-cores?<numCores>來(lái)控制Spark?Shell使用的機(jī)器的核數(shù)。
5.創(chuàng)建Spark應(yīng)用
spark-submit腳本提供了提供應(yīng)用到集群最直接的方式。對(duì)于Standalone模式而言,Spark目前支持兩種部署模式。在Client模式中,Driver程序在提交命令的機(jī)器上。在Cluster模式中,Driver從集群中的worker節(jié)點(diǎn)中任取一個(gè)運(yùn)行驅(qū)動(dòng)程序。
如果你的應(yīng)用通過(guò)Spark?submit提交,這個(gè)應(yīng)用jar自動(dòng)分發(fā)到集群中的所有worker節(jié)點(diǎn)上。對(duì)于你的應(yīng)用依賴(lài)的額外的jars,你應(yīng)該通過(guò)--jars?參數(shù)來(lái)指定,多個(gè)之間用逗號(hào)分隔(如果:--jars?jar1,jar2)
另外,standalone?cluster模式也自動(dòng)重啟你的應(yīng)用程序。為了使用這個(gè)特性,你可以在spark-submit啟動(dòng)你的應(yīng)用程序時(shí)傳遞--supervise參數(shù)。
./bin/spark-class?org.apache.spark.deploy.Client?kill?<master?url>?<driver?ID>
6.資源調(diào)度及分配
Standalone?cluster模式目前僅支持應(yīng)用調(diào)度的FIFO模式。為了運(yùn)行多個(gè)用戶,你可以控制每個(gè)應(yīng)用使用的最大資源。默認(rèn),它會(huì)使用集群中所有機(jī)器的核數(shù),這只對(duì)于集群中只有一個(gè)應(yīng)用有效。你可以通過(guò)?spark.cores.max?參數(shù)來(lái)控制核數(shù),如下所示:
val?conf?=?new?SparkConf()
?????????????.setMaster(...)
?????????????.setAppName(...)
?????????????.set("spark.cores.max",?"10")val?sc?=?new?SparkContext(conf)
另外,你可以在集群的master中配置?spark.deploy.defaultCores參數(shù)來(lái)改變默認(rèn)值。如下所示:
export?SPARK_MASTER_OPTS="-Dspark.deploy.defaultCores=<value>"
7.監(jiān)控與日志
Spark?Standalone模式提供了一個(gè)web接口來(lái)監(jiān)控集群。master和每個(gè)worker有他們自己的WEB?UI。默認(rèn)你可以通過(guò)8080端口訪問(wèn)master的WEB?UI。這個(gè)端口可以在配置文件中修改或在命令行中選項(xiàng)修改。
另外,每個(gè)job的詳細(xì)日志默認(rèn)寫(xiě)入到每個(gè)slave節(jié)點(diǎn)的工作目錄(默認(rèn)SPARK_HOME/work)。在目錄下,對(duì)于每個(gè)job,你會(huì)看到兩個(gè)文件分別是stdout和stderr。
8.與Hadoop共存
你可以基于你現(xiàn)有的Hadoop集群運(yùn)行Spark,只需要在同樣的機(jī)器上啟動(dòng)單獨(dú)的服務(wù)即可。在Spark中訪問(wèn)Hadoop中的數(shù)據(jù),只需要使用hdfs://?URL?(典型hdfs://<namenode>:9000/path)路徑即可。另外,你可以為Spark創(chuàng)建一個(gè)獨(dú)立的集群,通過(guò)網(wǎng)絡(luò)仍然可以訪問(wèn)HDFS,這可能比本次磁盤(pán)慢。
9.配置網(wǎng)絡(luò)安全端口
Spark大量使用網(wǎng)絡(luò),一些環(huán)境有嚴(yán)格的防火墻要求。想要了解配置的端口,請(qǐng)看安全模塊。
10.高可用性
默認(rèn),standalone集群調(diào)度對(duì)于worker節(jié)點(diǎn)的失效是有彈性的。然而,集群調(diào)度器通過(guò)master做決策,默認(rèn)只有單個(gè)節(jié)點(diǎn)。如果master宕機(jī)了,將不會(huì)再創(chuàng)建新的應(yīng)用。為了避免單點(diǎn)故障,我們提供兩種高可用性模式,詳情如下。
10.1基于Zookeeper的Master
使用Zookeeper來(lái)提供leader選舉和一些轉(zhuǎn)態(tài)存儲(chǔ),你可以在基于Zookeeper的集群中啟動(dòng)多個(gè)master。一旦一個(gè)master被選中為“leader”,其他的將處于standby轉(zhuǎn)態(tài)。如果當(dāng)前的leader宕機(jī)了,Zookeeper將會(huì)重新選舉出另外一個(gè)master,從前一個(gè)master的轉(zhuǎn)態(tài)中繼續(xù)任務(wù)調(diào)度。整個(gè)的恢復(fù)過(guò)程耗時(shí)在1-2分鐘。注意,這種延遲僅僅影響調(diào)用新的應(yīng)用程序而不影響正在運(yùn)行的應(yīng)用。
配置
為了支持這種恢復(fù)模式,你可以在spark-env.sh中設(shè)置SPARK_DAEMON_JAVA_OPTS配置如下選項(xiàng):
| 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). |
詳情
如果你集群中已經(jīng)安裝好了Zookeeper,允許HA是很簡(jiǎn)單的。只需要在不同的節(jié)點(diǎn)上啟動(dòng)讀個(gè)master進(jìn)程即可,master可以隨時(shí)增刪。
為了調(diào)度新的應(yīng)用或集群中添加worker,他們需要知道當(dāng)期啊leader?的ip地址。這僅需要傳遞一個(gè)list即可。例如,你通過(guò)spark://host1:port1,host2:port2來(lái)啟動(dòng)應(yīng)用程序時(shí),如果host1宕機(jī)了,集群仍讓正常,因?yàn)榧阂呀?jīng)重新找到了一個(gè)新的leader,即host2
10.2本地系統(tǒng)的單節(jié)點(diǎn)恢復(fù)
Zookeeper是最好的HA方式,但如果你想要master如果宕了重啟的話,文件系統(tǒng)模式支持。當(dāng)應(yīng)用程序和worker注冊(cè)到master后,他們有足夠的轉(zhuǎn)態(tài)寫(xiě)入到了特定目錄中,這些轉(zhuǎn)態(tài)可以在master進(jìn)程重啟時(shí)恢復(fù)。
配置
為了支持這種恢復(fù)模式,你可以在spark-env.sh中設(shè)置SPARK_DAEMON_JAVA_OPTS配置如下選項(xiàng):
| 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這樣的系統(tǒng)中。
- 盡管這種文件恢復(fù)模式看起來(lái)很好,但效果不太好。特別,通過(guò)sotp-master.sh來(lái)殺死一個(gè)master不能清除它的恢復(fù)狀態(tài),所以無(wú)論你何時(shí)啟動(dòng)一個(gè)新的master,它將進(jìn)行恢復(fù)模式。這可能導(dǎo)致啟動(dòng)時(shí)間的增加。
盡管這種方式官網(wǎng)不推薦,你可以掛載一個(gè)NFS目錄作為一個(gè)恢復(fù)目錄,如果原來(lái)的master宕了,你可以在一個(gè)新的節(jié)點(diǎn)上啟動(dòng)一個(gè)master,它能正確地恢復(fù)之前注冊(cè)的應(yīng)用程序和workers。
?
轉(zhuǎn)載于:https://www.cnblogs.com/yourarebest/p/5103989.html
總結(jié)
以上是生活随笔為你收集整理的【原】Spark Standalone模式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Win10 下使用 ionic 框架开发
- 下一篇: IOS开发中常量的处理