本页面介绍了如何为使用 Thrift 端点协议的 Dataproc Metastore 服务配置 Kerberos。 如果您的 Dataproc Metastore 服务使用 gRPC 端点协议,请参阅 为 gRPC 端点配置 Kerberos。
准备工作
了解 Kerberos的基础知识。
在这些说明中,您将使用 Managed Service for Apache Spark 集群创建以下 Kerberos 资产:
- Keytab 文件。
krb5.conf文件。- Kerberos 主账号。
如需详细了解这些 Kerberos 资产如何与 Dataproc Metastore 服务搭配使用,请参阅关于 Kerberos。
创建并托管您自己的 Kerberos KDC,或了解如何使用 Managed Service for Apache Spark 集群的本地 KDC。
创建 Cloud Storage 存储桶或获取对现有存储分区的访问权限。您必须将
krb5.conf文件存储在此存储桶中。
网络注意事项
在配置 Kerberos 之前,请考虑以下网络设置:
在 VPC 网络和 KDC 之间建立 IP 连接。您必须执行此操作,才能使用 Dataproc Metastore 服务对 KDC 文件进行身份验证。
在您的 KDC 上设置任何必要的 防火墙规则。 这些规则是允许来自 Dataproc Metastore 的流量所必需的。如需了解详情,请参阅服务的防火叫规则。
如果您使用的是 VPC Service Controls,则 Secret Manager Secret 和
krb5.confCloud Storage 对象 必须属于与 Dataproc Metastore 服务位于同一服务边界内的项目。确定要使用的 VPC 对等互连网络。您必须使用相同的 VPC 对等互连网络配置 Managed Service for Apache Spark 集群和 Dataproc Metastore 服务。
所需的角色
如需获得使用 Kerberos 创建 Dataproc Metastore 所需的权限,请让管理员根据最小权限原则向您授予项目的以下 IAM 角色:
- 授予对 Dataproc Metastore 资源的完全控制权 (
roles/metastore.editor) - 授予对所有 Dataproc Metastore 资源的完全访问权限,包括 IAM 政策管理 (
roles/metastore.admin)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
此预定义角色包含
metastore.services.create
权限,
这是使用 Kerberos 创建 Dataproc Metastore 所必需的。
如需了解详情,请参阅 Dataproc Metastore IAM 和访问权限控制。
为 Dataproc Metastore 启用 Kerberos
以下说明介绍了如何为关联到 Managed Service for Apache Spark 集群的 Dataproc Metastore 服务配置 Kerberos。
创建 Managed Service for Apache Spark 集群并启用 Kerberos
gcloud
如需设置使用 Kerberos 的 Managed Service for Apache Spark 集群,
请运行以下 gcloud dataproc clusters create
命令:
gcloud dataproc clusters create CLUSTER_NAME \
--image-version=2.0 \
--enable-kerberos \
--scopes 'https://www.googleapis.com/auth/cloud-platform'
替换以下内容:
CLUSTER_NAME:Managed Service for Apache Spark 集群的名称。
为 Kerberos 配置 Managed Service for Apache Spark 集群
以下说明介绍了如何使用 SSH 连接到与 Dataproc Metastore 服务关联的主 Managed Service for Apache Spark 集群。
之后,您将修改 hive-site.xml 文件并为您的服务配置 Kerberos。
- 在 Google Cloud 控制台中,前往虚拟机实例页面。
在虚拟机实例列表中,点击 Managed Service for Apache Spark 主节点 (
your-cluster-name-m) 所在行中的 SSH 。此时会打开一个浏览器窗口,并显示节点上的主目录。
打开
/etc/hive/conf/hive-site.xml文件。sudo vim /etc/hive/conf/hive-site.xml您会看到类似于以下内容的输出:
<property> <name>hive.metastore.kerberos.principal</name> <value>PRINCIPAL_NAME</value> </property> <property> <name>hive.metastore.kerberos.keytab.file</name> <value>METASTORE_PRINCIPAL_KEYTAB</value> </property>替换:
PRINCIPAL_NAME:主账号名称,格式如下:primary/instance@REALM。例如,hive/test@C.MY-PROJECT.INTERNAL。METASTORE_PRINCIPAL_KEYTAB:您的 Hive Metastore keytab 文件的位置。使用以下值:/etc/security/keytab/metastore.service.keytab。
创建 keytab 文件
以下说明介绍了如何创建 keytab 文件。
keytab 文件包含一对 Kerberos 主账号和一对加密密钥。它用于通过 Kerberos KDC 对服务主账号进行身份验证。
如需创建 keytab 文件,请执行以下操作
在 Managed Service for Apache Spark SSH 会话中,创建 keytab 和主账号。
sudo kadmin.local -q "addprinc -randkey PRINCIPAL_NAME" sudo kadmin.local -q "ktadd -k /etc/security/keytab/metastore.service.keytab PRINCIPAL_NAME"在 Managed Service for Apache Spark SSH 会话中,创建 keytab 文件并将其上传到 Secret Manager。
gcloud secrets create SECRET_NAME --replication-policy automatic sudo gcloud secrets versions add SECRET_NAME --data-file /etc/security/keytab/metastore.service.keytab
替换以下内容:
SECRET_NAME:Secret 的名称。
更新 krb5.conf 文件
接下来,您必须更新 krb5.conf 文件,以将其与 Managed Service for Apache Spark 集群相关联。
确定 Managed Service for Apache Spark 集群的主实例的主内部 IP 地址。
gcloud compute instances list例如,运行此命令会生成类似的输出:
~$ gcloud compute instances list --project kerberos-project NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS cluster-236-m us-central1-c n2-standard-4 192.0.2.2 *.*.*.* RUNNING ...在本例中,集群的内部 IP 地址为
192.0.2.2。打开
krb5.conf文件。sudo vim /etc/krb5.conf在该文件中,将现有的
KDC参数和admin_server参数替换为 Managed Service for Apache Spark 集群的内部 IP 地址。例如,使用上一步中的 IP 地址值看起来类似于此输出。
[realms] US-CENTRAL1-A.C.MY-PROJECT.INTERNAL = { kdc = 192.0.2.2 admin_server = 192.0.2.2 }将
/etc/krb5.conf文件从 Managed Service for Apache Spark 主虚拟机上传到 Cloud Storage 存储桶。gcloud storage cp /etc/krb5.conf gs://PATH_TO_KRB5替换:
PATH_TO_KRB5:包含krb5.conf文件的 Cloud Storage URI。
上传完成后,复制上传的路径。 您需要在创建 Dataproc Metastore 服务时使用它。
授予 IAM 角色和权限
向 Dataproc Metastore 服务帐号授予访问 keytab 文件的权限。此账号由 Google 管理,可通过选择包括 Google 提供的角色授权 在 IAM 权限界面页面上列出。
gcloud projects add-iam-policy-binding PROJECT_ID \ --member serviceAccount:service-PROJECT_NUMBER@gcp-sa-metastore.iam.gserviceaccount.com \ --role roles/secretmanager.secretAccessor
向 Dataproc Metastore 服务帐号授予访问
krb5.conf文件的权限。gcloud projects add-iam-policy-binding PROJECT_ID \ --member serviceAccount:service-PROJECT_NUMBER@gcp-sa-metastore.iam.gserviceaccount.com \ --role roles/storage.objectViewer
使用 Kerberos 创建 Dataproc Metastore 服务
创建使用 Kerberos 文件配置的新 Dataproc Metastore 服务。
请确保在 VPC 网络中创建服务,就像创建 Managed Service for Apache Spark 集群一样。
gcloud metastore services create SERVICE \ --location=LOCATION \ --instance-size=medium \ --network=VPC_NETWORK \ --kerberos-principal=KERBEROS_PRINCIPAL \ --krb5-config=KRB5_CONFIG \ --keytab=CLOUD_SECRET
替换以下内容:
SERVICE:Dataproc Metastore 服务的名称。LOCATION:Dataproc Metastore 服务的位置。VPC_NETWORK:VPC 网络的名称。使用在 Managed Service for Apache Spark 集群中配置的同一网络。KERBEROS_PRINCIPAL:您之前创建的 Kerberos 主账号的名称。KRB5_CONFIG:krb5.conf文件的位置。 使用指向文件的 Cloud Storage 对象 URI。CLOUD_SECRET:Secret Manager Secret 版本的相对资源名称。
创建集群后,Dataproc Metastore 会尝试使用提供的主账号、keytab 和 krb5.conf 文件通过 Kerberos 凭据进行连接。如果连接失败,则 Dataproc Metastore 创建也会失败。
将 Managed Service for Apache Spark 集群关联到 Dataproc Metastore
Dataproc Metastore 服务创建完成后, 找到 Thrift 端点 URI 和 仓库目录。
通过 SSH 连接到主实例 的 Managed Service for Apache Spark 集群。
在 SSH 会话中,打开
/etc/hive/conf/hive-site.xml文件。sudo vim /etc/hive/conf/hive-site.xml修改 Managed Service for Apache Spark 集群上的
/etc/hive/conf/hive-site.xml。<property> <name>hive.metastore.uris</name> <!-- Update this value. --> <value>ENDPOINT_URI</value> </property> <!-- Add this property entry. --> <property> <name>hive.metastore.warehouse.dir</name> <value>WAREHOUSE_DIR</value> </property>重启 HiveServer2:
sudo systemctl restart hive-server2.service
在提交作业之前配置 Managed Service for Apache Spark
如需运行您的 Managed Service for Apache Spark 作业,
您必须将 hive 用户添加到
Hadoop container-executor.cfg 文件中的 allowed.system.users 属性。这样,用户便可以运行查询来访问数据,例如 select * from。
在 SSH 会话中,打开 Hadoop
container-executor.cfg文件。sudo vim /etc/hadoop/conf/container-executor.cfg在每个 Managed Service for Apache Spark 节点上添加以下行。
allowed.system.users=hive
获取 Kerberos 票据
在连接到 Dataproc Metastore 实例之前,请先获取 Kerberos 票据。
sudo klist -kte /etc/security/keytab/metastore.service.keytab sudo kinit -kt /etc/security/keytab/metastore.service.keytab PRINCIPAL_NAME sudo klist # gets the ticket information. sudo hive替换以下内容:
PRINCIPAL_NAME:主账号的名称。