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

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

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

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

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

准备工作

  1. In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Cloud de Confiance project.

  3. Enable the GKE and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  4. Make sure that you have the following role or roles on the project: Kubernetes Engine Admin, Storage Admin, Service Account User

    Check for the roles

    1. In the Cloud de Confiance 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 Cloud de Confiance console, go to the IAM page.

      前往 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是员工身份池中的用户的标识符。如需了解详情,请参阅在 IAM 政策中表示员工池用户,或与您的管理员联系。

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

      1. 在 Cloud de Confiance 控制台中,前往创建 Autopilot 集群页面。

        前往“创建 Autopilot 集群”

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

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

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

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

      创建 Cloud Storage 存储桶

      1. 在 Cloud de Confiance 控制台中,转到 Cloud Storage 存储桶页面。

        进入“存储桶”

      2. 点击 创建

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

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

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

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

      将对象上传到 Cloud Storage 存储桶

      1. 在 Cloud de Confiance 控制台中,转到 Cloud Storage 存储桶页面。

        进入“存储桶”

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

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

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

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

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

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

      设置 Cloud Shell

      In the Cloud de Confiance console, activate Cloud Shell.

      Activate Cloud Shell

      Cloud Shell 会话随即会启动并显示命令行提示符。该会话可能需要几秒钟来完成初始化。

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

      1. 设置要在其中部署 Pod 的默认项目:

        gcloud config set project PROJECT_ID
        

        PROJECT_ID 替换为您的项目 ID。

      2. 创建并应用包含以下内容的 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 路径上。

      3. 可使用以下命令验证 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 上的问题排查

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

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

      清理

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

      kubectl delete pod gcsfuse-test
      

      后续步骤