标签:style blog http color os java 使用 io strong
Spark Standalone集群是Master-Slaves架构的集群模式,和大部分的Master-Slaves结构集群一样,存在着Master单点故障的问题。如何解决这个单点故障的问题,Spark提供了两种方案:
ZooKeeper提供了一个Leader Election机制,利用这个机制可以保证虽然集群存在多个Master,但是只有一个是Active的,其他的都是Standby。当Active的Master出现故障时,另外的一个Standby Master会被选举出来。由于集群的信息,包括Worker, Driver和Application的信息都已经持久化到文件系统,因此在切换的过程中只会影响新Job的提交,对于正在进行的Job没有任何的影响。加入ZooKeeper的集群整体架构如下图所示。
本文的测试是在Spark0.9.0 Standalone ,同样适用于Spark1.0.0 Standalone 以上版本。
1.基于文件系统的单点恢复
主要用于开发或测试环境。当spark提供目录保存spark Application和worker的注册信息,并将他们的恢复状态写入该目录中,这时,一旦Master发生故障,就可以通过重新启动Master进程(sbin/start-master.sh),恢复已运行的spark Application和worker的注册信息。
基于文件系统的单点恢复,主要是在spark-env里对SPARK_DAEMON_JAVA_OPTS设置:
System property | Meaning |
---|---|
spark.deploy.recoveryMode |
Set to FILESYSTEM to enable single-node recovery mode (default: NONE).(设成FILESYSTEM , 缺省值为NONE) |
spark.deploy.recoveryDirectory |
The directory in which Spark will store recovery state, accessible from the Master‘s perspective.(Spark 保存恢复状态的目录) |
可以考虑使用NFS的共享目录来保存Spark恢复状态。
1.1配置
[root@bigdata001 spark]# vi conf/spark-env.sh
添加property
export SPARK_DAEMON_JAVA_OPTS="-Dspark.deploy.recoveryMode=FILESYSTEM -Dspark.deploy.recoveryDirectory=/nfs/spark/recovery"
1.2测试
1.启动Spark Standalone集群:[root@bigdata001 spark]# ./sbin/start-all.sh
2.启动一个spark-shell客户端并做部分操作后,然后用sbin/stop-master.sh杀死Master进程
[root@bigdata003 spark]# MASTER=spark://bigdata001:7077 bin/spark-shell
[root@bigdata001 spark]# ./sbin/stop-master.sh
3.测试结果:可以在bigdata003看到information,连接不上master。
14/08/26 13:54:01 WARN AppClient$ClientActor: Connection to akka.tcp://sparkMaster@bigdata001:7077 failed; waiting for master to reconnect...
14/08/26 13:54:01 WARN SparkDeploySchedulerBackend: Disconnected from Spark cluster! Waiting for reconnection...
14/08/26 13:54:01 WARN AppClient$ClientActor: Connection to akka.tcp://sparkMaster@bigdata001:7077 failed; waiting for master to reconnect...
14/08/26 13:54:01 WARN AppClient$ClientActor: Could not connect to akka.tcp://sparkMaster@bigdata001:7077: akka.remote.EndpointAssociationException: Association failed with [akka.tcp://sparkMaster@bigdata001:7077]
4.重新启动一下master,可以恢复正常:
[root@bigdata001 spark]# ./sbin/start-master.sh
2.Standby Masters with ZooKeeper
用于生产模式。其基本原理是通过zookeeper来选举一个Master,其他的Master处于Standby状态。
将Standalone集群连接到同一个ZooKeeper实例并启动多个Master,利用zookeeper提供的选举和状态保存功能,可以使一个Master被选举,而其他Master处于Standby状态。如果现任Master死去,另一个Master会通过选举产生,并恢复到旧的Master状态,然后恢复调度。整个恢复过程可能要1-2分钟。
注意:
该HA方案使用起来很简单,首先启动一个ZooKeeper集群,然后在不同节点上启动Master,注意这些节点需要具有相同的zookeeper配置(ZooKeeper URL 和目录)。
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). |
Master可以在任何时候添加或移除。如果发生故障切换,新的Master将联系所有以前注册的Application和Worker告知Master的改变。
注意:不能将Master定义在conf/spark-env.sh里了,而是直接在Application中定义。涉及的参数是 export SPARK_MASTER_IP=bigdata001,这项不配置或者为空。否则,无法启动多个master。
2.1 配置
[root@bigdata001 spark]# vi conf/spark-env.sh
添加Property
#ZK HA export SPARK_DAEMON_JAVA_OPTS="-Dspark.deploy.recoveryMode=ZOOKEEPER -Dspark.deploy.zookeeper.url=bigdata001:2181,bigdata002:2181,bi gdata003:2181 -Dspark.deploy.zookeeper.dir=/spark"
2.2 测试
1.前提:zookeeper集群已经启动。
2.关闭集群后,重新启动spark集群:
[root@bigdata001 spark]# ./sbin/stop-all.sh [root@bigdata001 spark]# ./sbin/start-all.sh
3.在另一个节点上,启动新的master:[root@bigdata002 spark]# ./sbin/start-master.sh
4.查看Web UI:http://bigdata001:8081/
5.启动一个spark-shell客户端:[root@bigdata003 spark]# MASTER=spark://bigdata001:7077,bigdata002:7077 bin/spark-shell
MASTER is spark://bigdata001:7077,bigdata002:7077=-====
-----------------------/home/zjw/tachyon/tachyon-0.4.1/target/tachyon-0.4.1-jar-with-dependencies.jar:/home/zjw/tachyon/tachyon-0.4.1/target/tachyon-0.4.1-jar-with-dependencies.jar:/home/zjw/tachyon/tachyon-0.4.1/target/tachyon-0.4.1-jar-with-dependencies.jar:/home/zjw/tachyon/tachyon-0.4.1/target/tachyon-0.4.1-jar-with-dependencies.jar::/src/java/target/mesos-0.19.0.jar:/src/java/target/mesos-0.19.0.jar:/root/spark/conf:/root/spark/assembly/target/scala-2.10/spark-assembly-0.9.0-incubating-hadoop2.2.0.jar
*********RUNNER=/home/zjw/jdk1.7/jdk1.7.0_51//bin/java
*********CLASSPATH=/home/zjw/tachyon/tachyon-0.4.1/target/tachyon-0.4.1-jar-with-dependencies.jar:/home/zjw/tachyon/tachyon-0.4.1/target/tachyon-0.4.1-jar-with-dependencies.jar:/home/zjw/tachyon/tachyon-0.4.1/target/tachyon-0.4.1-jar-with-dependencies.jar:/home/zjw/tachyon/tachyon-0.4.1/target/tachyon-0.4.1-jar-with-dependencies.jar::/src/java/target/mesos-0.19.0.jar:/src/java/target/mesos-0.19.0.jar:/root/spark/conf:/root/spark/assembly/target/scala-2.10/spark-assembly-0.9.0-incubating-hadoop2.2.0.jar
*********JAVA_OPTS=-Dspark.executor.uri=hdfs://192.168.1.101:8020/user/spark/spark-0.9.2.tar.gz -Dspark.akka.frameSize=20 -Djava.library.path= -Xms512m -Xmx512m
6.停掉正在service的Master:[root@bigdata001 spark]# ./sbin/stop-master.sh
spark-shell输出如下信息:用sbin/stop-master.sh杀死bigdata001 的Master进程,这时saprk-shell花费了30秒左右的时候切换到bigdata002 上的Master了。
14/08/26 13:54:01 WARN AppClient$ClientActor: Connection to akka.tcp://sparkMaster@bigdata001:7077 failed; waiting for master to reconnect...
14/08/26 13:54:01 WARN SparkDeploySchedulerBackend: Disconnected from Spark cluster! Waiting for reconnection...
14/08/26 13:54:01 WARN AppClient$ClientActor: Connection to akka.tcp://sparkMaster@bigdata001:7077 failed; waiting for master to reconnect...
14/08/26 13:54:01 WARN AppClient$ClientActor: Could not connect to akka.tcp://sparkMaster@bigdata001:7077: akka.remote.EndpointAssociationException: Association failed with [akka.tcp://sparkMaster@bigdata001:7077]
14/08/26 13:54:01 WARN AppClient$ClientActor: Connection to akka.tcp://sparkMaster@bigdata001:7077 failed; waiting for master to reconnect...
14/08/26 13:54:01 WARN AppClient$ClientActor: Could not connect to akka.tcp://sparkMaster@bigdata001:7077: akka.remote.EndpointAssociationException: Association failed with [akka.tcp://sparkMaster@bigdata001:7077]
14/08/26 13:54:01 WARN AppClient$ClientActor: Connection to akka.tcp://sparkMaster@bigdata001:7077 failed; waiting for master to reconnect...
14/08/26 13:54:01 WARN AppClient$ClientActor: Could not connect to akka.tcp://sparkMaster@bigdata001:7077: akka.remote.EndpointAssociationException: Association failed with [akka.tcp://sparkMaster@bigdata001:7077]
14/08/26 13:54:01 WARN AppClient$ClientActor: Connection to akka.tcp://sparkMaster@bigdata001:7077 failed; waiting for master to reconnect...
14/08/26 13:54:30 INFO AppClient$ClientActor: Master has changed, new master is at spark://bigdata002:7077
7.查看UI监控器,这是Active Master是bigdata002。正在运行的Application资源没发生变化。
http://bigdata002:8082/
设计理念
为了解决Standalone模式下的Master的SPOF,Spark采用了ZooKeeper提供的选举功能。Spark并没有采用ZooKeeper原生的Java API,而是采用了Curator,一个对ZooKeeper进行了封装的框架。采用了Curator后,Spark不用管理与ZooKeeper的连接,这些对于Spark来说都是透明的。Spark仅仅使用了100行代码,就实现了Master的HA。
进阶源码学习:Spark技术内幕:Master基于ZooKeeper的High Availability(HA)源码实现
参考资料:
http://www.cnblogs.com/hseagle/p/3673147.html
https://spark.apache.org/docs/0.9.0/spark-standalone.html#standby-masters-with-zookeeper
Spark:Master High Availability(HA)高可用配置的2种实现
标签:style blog http color os java 使用 io strong
原文地址:http://www.cnblogs.com/byrhuangqiang/p/3937654.html