如何使用Kafka与Kerberos一起使用Spark Streaming?

时间:2017-12-26 10:18:50

标签: apache-spark apache-kafka spark-streaming kerberos jaas

我尝试使用Kerberized Hadoop集群中的Spark Streaming应用程序使用来自Kafka的消息时遇到了一些问题。我尝试了两种方法listed here

  • 基于接收者的方法:KafkaUtils.createStream
  • 直接接近(无接收者):KafkaUtils.createDirectStream

基于接收者的方法(KafkaUtils.createStream)抛出两种类型的异常(不同的例外,无论我处于本地模式(--master local[*])还是YARN模式(--master yarn --deploy-mode client):

  • Spark本地应用程序中的一个奇怪的kafka.common.BrokerEndPointNotAvailableException
  • YARN应用程序中Spark上的Zookeeper超时。我曾经设法完成这项工作(成功连接到Zookeeper),但没有收到任何消息

在两种模式(本地或YARN)中,直接方法(KafkaUtils.createDirectStream)返回无法解释的EOFException(详见下文)。

我的最终目标是在YARN 上启动 Spark Streaming作业,因此我将抛开Spark本地工作。

这是我的测试环境:

  • Cloudera CDH 5.7.0
  • Spark 1.6.0
  • Kafka 0.10.1.0

我正在处理单节点群集(hostname = quickstart.cloudera)以进行测试。对于有兴趣重现测试的人,我正在使用基于cloudera/quickstartGit repo)的自定义Docker容器。

以下是我在spark-shell中使用的示例代码。当然,当没有启用Kerberos时,此代码可以工作:由kafka-console-producer生成的消息由Spark应用程序接收。

import org.apache.spark.streaming.kafka.KafkaUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.storage.StorageLevel
import kafka.serializer.StringDecoder

val ssc = new StreamingContext(sc, Seconds(5))

val topics = Map("test-kafka" -> 1)

def readFromKafkaReceiver(): Unit = {
    val kafkaParams = Map(
        "zookeeper.connect" -> "quickstart.cloudera:2181",
        "group.id" -> "gid1",
        "client.id" -> "cid1",
        "zookeeper.session.timeout.ms" -> "5000",
        "zookeeper.connection.timeout.ms" -> "5000"
    )

    val stream = KafkaUtils.createStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParams, topics, StorageLevel.MEMORY_ONLY_2)
    stream.print
}

def readFromKafkaDirectStream(): Unit = {
    val kafkaDirectParams = Map(
        "bootstrap.servers" -> "quickstart.cloudera:9092",
        "group.id" -> "gid1",
        "client.id" -> "cid1"
    )

    val directStream = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](ssc, kafkaDirectParams, topics.map(_._1).toSet)
    directStream.print
}

readFromKafkaReceiver() // or readFromKafkaDirectStream()

ssc.start

Thread.sleep(20000)

ssc.stop(stopSparkContext = false, stopGracefully = true)

启用Kerberos后,此代码不起作用。我遵循了本指南:Configuring Kafka Security,并创建了两个配置文件:

jaas.conf

KafkaClient {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
keyTab="/home/simpleuser/simpleuser.keytab"
principal="simpleuser@CLOUDERA";
};

client.properties

security.protocol=SASL_PLAINTEXT
sasl.kerberos.service.name=kafka

我可以用以下内容生成消息:

export KAFKA_OPTS="-Djava.security.auth.login.config=/home/simpleuser/jaas.conf"
kafka-console-producer \
    --broker-list quickstart.cloudera:9092 \
    --topic test-kafka \
    --producer.config client.properties

但我不能从Spark Streaming应用程序中使用这些消息。要在spark-shell模式下启动yarn-client,我刚创建了一个新的JAAS配置(jaas_with_zk_yarn.conf),带有Zookeeper部分(Client),并且对keytab的引用是只有文件的名称(keytab然后通过--keytab选项传递):

KafkaClient {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
keyTab="simpleuser.keytab"
principal="simpleuser@CLOUDERA";
};

Client {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
keyTab="simpleuser.keytab"
principal="simpleuser@CLOUDERA";
};

这个新文件在--files选项中传递:

spark-shell --master yarn --deploy-mode client \
    --num-executors 2 \
    --files /home/simpleuser/jaas_with_zk_yarn.conf \
    --conf "spark.executor.extraJavaOptions=-Djava.security.auth.login.config=jaas_with_zk_yarn.conf" \
    --conf "spark.driver.extraJavaOptions=-Djava.security.auth.login.config=jaas_with_zk_yarn.conf" \
    --keytab /home/simpleuser/simpleuser.keytab \
    --principal simpleuser

我使用了与之前相同的代码,只是我添加了两个其他Kafka参数,对应于consumer.properties文件的内容:

"security.protocol" -> "SASL_PLAINTEXT",
"sasl.kerberos.service.name" -> "kafka"
启动Spark Streaming Context后,

readFromKafkaReceiver()会抛出以下错误(无法连接到Zookeeper):

ERROR scheduler.ReceiverTracker: Deregistered receiver for stream 0: Error starting receiver 0 - org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 5000
        at org.I0Itec.zkclient.ZkClient.connect(ZkClient.java:1223)
        at org.I0Itec.zkclient.ZkClient.<init>(ZkClient.java:155)
        at org.I0Itec.zkclient.ZkClient.<init>(ZkClient.java:129)
        at kafka.utils.ZkUtils$.createZkClientAndConnection(ZkUtils.scala:89)
        at kafka.utils.ZkUtils$.apply(ZkUtils.scala:71)
        at kafka.consumer.ZookeeperConsumerConnector.connectZk(ZookeeperConsumerConnector.scala:191)
        at kafka.consumer.ZookeeperConsumerConnector.<init>(ZookeeperConsumerConnector.scala:139)
        at kafka.consumer.ZookeeperConsumerConnector.<init>(ZookeeperConsumerConnector.scala:156)
        at kafka.consumer.Consumer$.create(ConsumerConnector.scala:109)
        at org.apache.spark.streaming.kafka.KafkaReceiver.onStart(KafkaInputDStream.scala:100)
        at org.apache.spark.streaming.receiver.ReceiverSupervisor.startReceiver(ReceiverSupervisor.scala:148)
        at org.apache.spark.streaming.receiver.ReceiverSupervisor.start(ReceiverSupervisor.scala:130)
        at org.apache.spark.streaming.scheduler.ReceiverTracker$ReceiverTrackerEndpoint$$anonfun$9.apply(ReceiverTracker.scala:575)
        at org.apache.spark.streaming.scheduler.ReceiverTracker$ReceiverTrackerEndpoint$$anonfun$9.apply(ReceiverTracker.scala:565)
        at org.apache.spark.SparkContext$$anonfun$38.apply(SparkContext.scala:2003)
        at org.apache.spark.SparkContext$$anonfun$38.apply(SparkContext.scala:2003)
        at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
        at org.apache.spark.scheduler.Task.run(Task.scala:89)
        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

有时建立与ZK的连接(没有达到超时),但是没有收到任何消息。

一旦调用此方法,

readFromKafkaDirectStream()就会抛出以下错误

org.apache.spark.SparkException: java.io.EOFException
        at org.apache.spark.streaming.kafka.KafkaCluster$$anonfun$checkErrors$1.apply(KafkaCluster.scala:366)
        at org.apache.spark.streaming.kafka.KafkaCluster$$anonfun$checkErrors$1.apply(KafkaCluster.scala:366)
        at scala.util.Either.fold(Either.scala:97)
        at org.apache.spark.streaming.kafka.KafkaCluster$.checkErrors(KafkaCluster.scala:365)
        at org.apache.spark.streaming.kafka.KafkaUtils$.getFromOffsets(KafkaUtils.scala:222)
        at org.apache.spark.streaming.kafka.KafkaUtils$.createDirectStream(KafkaUtils.scala:484)
        at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.readFromKafkaDirectStream(<console>:47)

没有更多解释,只有EOFException。我认为Spark和Kafka经纪人之间存在沟通问题,但没有更多解释。我也尝试了metadata.broker.list而不是bootstrap.servers,但没有成功。

也许我在JAAS配置文件或Kafka参数中遗漏了什么?也许Spark选项(extraJavaOptions)无效?我尝试了很多可能性,我有点失落。

如果有人能帮助我解决至少其中一个问题(直接接近或基于接收者),我会很高兴。谢谢:))

1 个答案:

答案 0 :(得分:2)

Spark 1.6不支持它,如Cloudera docs中所述:

  

Spark Streaming无法从安全的Kafka中消耗,直到它开始使用Kafka 0.9 Consumer API

https://www.cloudera.com/documentation/enterprise/release-notes/topics/cdh_rn_spark_ki.html#ki_spark_streaming_consumer_api

1.6中的Spark-streaming使用旧的消费者API,不支持安全消费。

您可以使用支持安全Kafka的Spark 2.1: https://blog.cloudera.com/blog/2017/05/reading-data-securely-from-apache-kafka-to-apache-spark/