Dataproc 个人集群身份验证

创建 Dataproc 集群时,您可以启用 Dataproc 个人集群身份验证,以允许集群上的交互式工作负载以用户身份安全地运行。这意味着,与其他 Google Cloud 资源(如 Cloud Storage)的交互将以您自己的身份而非集群服务账号通过身份验证。

注意事项

  • 如果您在创建集群时启用了个人集群身份验证,则只能您的身份才能使用该集群。其他用户将无法在集群上运行作业,也无法访问集群上的组件网关端点。

  • 启用了个人集群身份验证的集群会禁止 SSH 访问和 Compute Engine 功能,例如集群中所有虚拟机上的启动脚本。

  • 启用了个人集群身份验证的集群会自动在集群上启用和配置 Kerberos,以确保集群内通信的安全。但是,集群上的所有 Kerberos 身份都将以同一用户身份与 Google Cloud 资源进行交互。

  • Dataproc 个人集群身份验证目前不支持 Dataproc 工作流

  • Dataproc 个人集群身份验证仅适用于个人(真人)用户运行的交互式作业。长时间运行的作业和操作应配置和使用适当的服务账号身份。

  • 传播的凭据使用凭据访问边界缩小范围。默认访问边界仅限于对包含集群的同一项目拥有的 Cloud Storage 存储分区中的 Cloud Storage 对象执行读写操作。 您可以在 enable_an_interactive_session 时定义非默认访问边界。

目标

  • 创建启用了 Dataproc 个人集群身份验证的 Dataproc 集群。

  • 开始向该集群传播凭据。

  • 在该集群上使用 Jupyter 笔记本来运行 Spark 作业,以使用您的凭据进行身份验证。

准备工作

创建项目

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Dataproc API。

    启用 API

  5. Install the Google Cloud CLI.
  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  8. 确保您的 Google Cloud 项目已启用结算功能

  9. 启用 Dataproc API。

    启用 API

  10. Install the Google Cloud CLI.
  11. To initialize the gcloud CLI, run the following command:

    gcloud init

配置环境

通过 Cloud Shell 或本地终端配置环境:

Cloud Shell

  1. 启动 Cloud Shell 会话。

本地终端

  1. 运行 gcloud auth login 以获取有效的用户凭据。

创建集群并启用交互式会话

  1. 在 gcloud 中查找活跃账号的电子邮件地址。

    gcloud auth list --filter=status=ACTIVE --format="value(account)"
    

  2. 创建集群。

    gcloud dataproc clusters create cluster-name \
        --properties=dataproc:dataproc.personal-auth.user=your-email-address \
        --enable-component-gateway \
        --optional-components=ZEPPELIN \
        --region=region
    

  3. 为集群启用凭据传播会话,以便在与 Google Cloud 资源交互时开始使用您的个人凭据。

    gcloud dataproc clusters enable-personal-auth-session \
        --region=region \
        cluster-name
    

    示例输出:

    Injecting initial credentials into the cluster cluster-name...done.
    Periodically refreshing credentials for cluster cluster-name. This will continue running until the command is interrupted...
    

    1. 缩小范围的访问权限边界示例:以下示例启用了比默认范围缩小的凭据访问边界更具限制性的个人身份验证会话。它会限制对 Dataproc 集群的暂存存储桶的访问(如需了解详情,请参阅使用凭据访问边界缩小范围)。
gcloud dataproc clusters enable-personal-auth-session \
    --project=PROJECT_ID \
    --region=REGION \
    --access-boundary=<(echo -n "{ \
  \"access_boundary\": { \
    \"accessBoundaryRules\": [{ \
      \"availableResource\": \"//storage.googleapis.com/projects/_/buckets/$(gcloud dataproc clusters describe --project=PROJECT_ID --region=REGION CLUSTER_NAME --format="value(config.configBucket)")\", \
      \"availablePermissions\": [ \
        \"inRole:roles/storage.objectViewer\", \
        \"inRole:roles/storage.objectCreator\", \
        \"inRole:roles/storage.objectAdmin\", \
        \"inRole:roles/storage.legacyBucketReader\" \
      ] \
    }] \
  } \
}") \
   CLUSTER_NAME
  1. 保持运行该命令并切换到新的 Cloud Shell 标签页或终端会话。客户端会在运行命令的同时刷新凭据。

  2. 输入 Ctrl-C 以结束会话。

在集群上访问 Jupyter

gcloud

  1. 获取集群详情。
    gcloud dataproc clusters describe cluster-name --region=region
    

    集群详情中列出了 Jupyter 网页界面网址。

    ...
    JupyterLab: https://UUID-dot-us-central1.dataproc.googleusercontent.com/jupyter/lab/
    ...
    
  2. 将网址复制到本地浏览器中以启动 Jupyter 界面。
  3. 检查个人集群身份验证是否成功。
    1. 启动 Jupyter 终端。
    2. 运行 gcloud auth list
    3. 验证您的用户名是否是唯一的活跃账号。
  4. 在 Jupyter 终端中,启用 Jupyter 以通过 Kerberos 进行身份验证并提交 Spark 作业。
    kinit -kt /etc/security/keytab/dataproc.service.keytab dataproc/$(hostname -f)
    
    1. 运行 klist 以验证 Jupyter 是否已获取有效的 TGT。
  5. 在 Juypter 终端中,使用 gcloud CLI 创建 rose.txt 文件(位于项目的 Cloud Storage 存储桶中)。
    echo "A rose by any other name would smell as sweet" > /tmp/rose.txt
    

    gcloud storage cp /tmp/rose.txt gs://bucket-name/rose.txt
    
    1. 将文件标记为不公开,以便只有您的用户账号可以读取其中的内容或写入其中。Jupyter 将使用您的个人凭据进行互动 Cloud Storage
      gcloud storage objects update gs://bucket-name/rose.txt --predefined-acl=private
      
    2. 验证您的专用访问通道。
      gcloud storage objects describe gs://$BUCKET/rose.txt
      

      acl:
  6. email: $USER entity: user-$USER role: OWNER

控制台

  1. 点击组件网关 Jupyter 链接以启动 Jupyter 界面。
  2. 检查个人集群身份验证是否成功。
    1. 启动 Jupyter 终端
    2. 运行 gcloud auth list
    3. 验证您的用户名是否是唯一的活跃账号。
  3. 在 Jupyter 终端中,启用 Jupyter 以通过 Kerberos 进行身份验证并提交 Spark 作业。
    kinit -kt /etc/security/keytab/dataproc.service.keytab dataproc/$(hostname -f)
    
    1. 运行 klist 以验证 Jupyter 是否已获取有效的 TGT。
  4. 在 Jupyter 终端中,使用 gcloud CLI 创建 rose.txt 文件(位于项目的 Cloud Storage 存储桶中)。
    echo "A rose by any other name would smell as sweet" > /tmp/rose.txt
    

    gcloud storage cp /tmp/rose.txt gs://bucket-name/rose.txt
    
    1. 将文件标记为不公开,以便只有您的用户账号可以读取其中的内容或写入其中。Jupyter 将使用您的个人凭据进行互动 Cloud Storage
      gcloud storage objects update gs://bucket-name/rose.txt --predefined-acl=private
      
    2. 验证您的私人访问权限。
      gcloud storage objects describe gs://bucket-name/rose.txt
      
      acl:
  5. email: $USER entity: user-$USER role: OWNER

通过 Jupyter 运行 PySpark 作业

  1. 转到相应文件夹,然后创建 PySpark 笔记本。
  2. 针对上面创建的 rose.txt 文件运行基本字数统计作业。

    text_file = sc.textFile("gs://bucket-name/rose.txt")
    counts = text_file.flatMap(lambda line: line.split(" ")) \
             .map(lambda word: (word, 1)) \
             .reduceByKey(lambda a, b: a + b)
    print(counts.collect())
    

    Spark 能够读取 Cloud Storage 中的 rose.txt 文件,因为它是使用您的用户凭据运行的。

    您还可以检查 Cloud Storage 存储分区 审核日志:用于验证作业是否正在访问 Cloud Storage (请参阅 将 Cloud Audit Logs 与 Cloud Storage 结合使用了解详情)。

清理

  1. 删除 Dataproc 集群。
    gcloud dataproc clusters delete cluster-name --region=region