Spark 3.0.0-SNAPSHOT Access Kerberized HDFS

时间:2022-07-22
本文章向大家介绍Spark 3.0.0-SNAPSHOT Access Kerberized HDFS,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1 Overview

Spark 2.2 on K8S 的 Fork 已经废弃近两年了,那时候的几个主力开发也全部转移到 Spark 2.3/2.4 以及即将发布的 3.0 的 on K8S 模块的开发了。

3.0 相对于 2.2 的 Fork 除了关于 Spark App 的管理外,大部分特性都是 2.2 的改良,甚至镜像文件都只剩下一个(更方便管理)。而比较重要的劣势是 3.0 还不正式支持 Dynamic Resource Allocation,2.2 是已经有一种实现的(基于 DaemotSet 来创建 Shuffle Pod)。

前期调研 2.3 的时候发现,还没有支持 Kerberos 的相关特性,最近重新调研 2.4 的代码的时候,发现在 3.0.0 SNAPSHOT 已经有了支持了,而且方案比 2.2 更好。

2 Design

在 Spark 3.0.0 中,提交 Spark 任务的脚本如下。

/opt/spark/bin/spark-submit 
      --deploy-mode cluster 
      --class org.apache.spark.examples.HdfsTest 
      --master=k8s://https://kubernetes.default.svc 
      --conf spark.executor.instances=1 
      --conf spark.task.cpus=1 
      --conf spark.executor.memory=512M 
      --conf spark.kubernetes.namespace=dbyin 
      --conf spark.driver.extraJavaOptions=-Dlog4j.configuration=file:///opt/spark/logconf/log4j.properties 
      --conf spark.executor.extraJavaOptions=-Dlog4j.configuration=file:///opt/spark/logconf/log4j.properties 
      --conf spark.kubernetes.executor.deleteOnTermination=false 
      --conf spark.app.name=spark-hdfs 
      --conf spark.kerberos.keytab=/etc/DC-sh-cr-kerberos.keytab 
      --conf spark.kubernetes.kerberos.krb5.path=/etc/krb5.conf  
      --conf spark.kerberos.principal=DC-sh-cr-kerberos@HADOOP.COM 
      --conf spark.kubernetes.container.image=hub.oa.com/dbyin/spark:v3.0.4 
      local:///opt/spark/examples/jars/spark-examples_2.12-3.0.0-SNAPSHOT.jar 
      hdfs://sh-kerberos.hdfs.cr.ied.com:8020/sh-cr

Spark 访问 Kerberized HDFS 有几种情况。

  1. keytab: if a kerberos keytab is defined, it is provided to the driver, and the driver will manage the kerberos login and the creation of delegation tokens.
  2. existing tokens: if a secret containing delegation tokens is provided, it will be mounted on the driver pod, and the driver will handle distribution of those tokens to executors.
  3. tgt only: if Hadoop security is enabled, the local TGT will be used to create delegation tokens which will be provided to the driver. The driver will handle distribution of the tokens to executors.

【1】指出了,当在 submit 的客户端如果可以访问到 keytab,并且通过 Spark conf 来指定。那么 submit 的时候会将 krb5.conf 还有 hadoop 相关的配置通过 configmap 来保存,所以后面 driver 和 exectutor 启动,就可以直接通过 configmap 读到 hadoop 相关配置,以及拿到与 datanode 交互的 delegation token,如下图。

【2】指出了,用户也可以通过提前生成 token 的 secret,在 submit 的时候,直接指定需要 mount 的 configmap 和 secret,这种情况下,无需 keytab。 【3】指出了 submit 客户端如果存在本地的 TGT 缓存,可以把 tgt 请求 Namenode 生成 token 再存到 secret 里。