快速入门:使用 FUSE CSI 驱动程序访问 Cloud Storage 存储桶


在本快速入门指南中,您将了解如何从 Google Kubernetes Engine (GKE) 应用中访问 Cloud Storage 存储桶,就像它们是本地文件系统一样。本快速入门提供了一个示例 Kubernetes 清单,用于创建 Pod 来装载存储桶,并介绍了如何使用 Pod 的文件系统与存储桶的内容进行交互。

本页面适用于开发者、存储专家以及希望预配和管理云资源并部署工作负载的任何其他人员。如需详细了解常见角色,请参阅常见的 GKE Enterprise 用户角色和任务

在阅读本页面内容之前,请确保您熟悉 KubernetesCloud Storage FUSE CSI 驱动程序

本快速入门使用 GKE Autopilot 模式。如果您想使用 Standard 模式,请参阅设置适用于 GKE 的 Cloud Storage FUSE CSI 驱动程序

准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the GKE and Cloud Storage APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the GKE and Cloud Storage APIs.

    Enable the APIs

  8. Make sure that you have the following role or roles on the project: roles/container.admin, roles/storage.admin, roles/storage.objectUser, roles/iam.serviceAccountUser

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      进入 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。

创建 GKE Autopilot 集群

  1. 在 Google Cloud 控制台中,转到 GKE 集群页面。

    转到“集群”

  2. 点击 创建

  3. 创建集群对话框中,点击 Autopilot 模式对应的配置

  4. 集群基本信息下,执行以下操作:

    1. 名称字段中,输入集群的名称。

    2. 保留其余设置的默认值。

  5. 如需创建集群,请点击创建

创建 Cloud Storage 存储桶

  1. 在 Google Cloud 控制台中,进入 Cloud Storage 存储桶页面。

    进入“存储桶”

  2. 点击 创建

  3. 创建存储桶页面上的开始使用部分中,执行以下操作:

    1. 为存储桶输入符合存储桶命名要求的全局唯一名称。

    2. 保留其余设置的默认值。

  4. 如需创建存储桶,请点击创建

将对象上传到 Cloud Storage 存储桶

  1. 在 Google Cloud 控制台中,进入 Cloud Storage 存储桶页面。

    进入“存储桶”

  2. 在存储桶列表中,点击您刚刚创建的存储桶的名称。

  3. 在存储桶的对象标签页中,执行以下任一操作:

    • 将文件从桌面或文件管理器拖动到 Google Cloud 控制台的主窗格中。

    • 点击上传文件按钮。 在显示的对话框中,选择要上传的文件,然后点击打开

配置对 Cloud Storage 存储桶的访问权限

如需确保 GKE 集群可以访问 Cloud Storage 存储桶,请按照配置对 Cloud Storage 存储桶的访问权限中的步骤操作。

部署 Pod 以使用 FUSE 访问 Cloud Storage 存储桶

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    控制台上的框架内会打开一个 Cloud Shell 会话。

  2. 如需设置默认项目,请使用您要在其中部署 Pod 的项目的 ID:

    gcloud config set project PROJECT_ID
    

    PROJECT_ID 替换为您的项目 ID。

  3. 创建并应用包含以下内容的 Pod 清单:

    cat << EOF | kubectl apply -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: gcsfuse-test
      namespace: default
      annotations:
        gke-gcsfuse/volumes: "true"
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - image: busybox
        name: busybox
        command: ["sleep"]
        args: ["infinity"]
        volumeMounts:
        - name: gcsfuse-test
          mountPath: /data
          readOnly: true
      serviceAccountName: default
      volumes:
      - name: gcsfuse-test
        csi:
          driver: gcsfuse.csi.storage.gke.io
          volumeAttributes:
            bucketName: BUCKET_NAME
            mountOptions: "implicit-dirs"
    EOF
    
    • BUCKET_NAME 替换为您的 Cloud Storage 存储桶名称。
    • 如果您使用的是其他命名空间或服务账号,请相应地替换 namespaceserviceAccountName 字段。

    此清单的内容会启动一个名为 gcsfuse-test 的 Pod,以将存储桶装载到其 /data 路径上。

  4. 可使用以下命令验证 Pod 是否正在运行:

    kubectl get pod gcsfuse-test
    

    输出类似于以下内容:

    NAME           READY   STATUS    RESTARTS   AGE
    gcsfuse-test   2/2     Running   0          12s
    

    Cloud Storage FUSE CSI 驱动程序会在 Pod 中附加一个边车容器,以管理与 Cloud Storage 的交互。

    如果 Pod 未运行,您可以通过运行 kubectl describe pod gcsfuse-test 命令来提取事件日志,以帮助诊断问题。如需了解详情,请参阅 GitHub 上的问题排查

  5. Pod 运行后,您可以探索其文件系统:

    kubectl exec -it gcsfuse-test -- find /data
    

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除 Pod:

kubectl delete pod gcsfuse-test

后续步骤