快速入门:使用 FUSE CSI 驱动程序访问 Cloud Storage 存储桶
在本快速入门指南中,您将了解如何从 Google Kubernetes Engine (GKE) 应用中访问 Cloud Storage 存储桶,就像它们是本地文件系统一样。本快速入门提供了一个示例 Kubernetes 清单,用于创建 Pod 来装载存储桶,并介绍了如何使用 Pod 的文件系统与存储桶的内容进行交互。
本页面适用于开发者、存储专家以及希望预配和管理云资源并部署工作负载的任何其他人员。如需详细了解常见角色,请参阅常见的 GKE 用户角色和任务。
在阅读本页面内容之前,请确保您熟悉 Kubernetes 和 Cloud Storage FUSE CSI 驱动程序。
本快速入门使用 GKE Autopilot 模式。如果您想使用 Standard 模式,请参阅设置适用于 GKE 的 Cloud Storage FUSE CSI 驱动程序。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
确保您对项目拥有以下一个或多个角色: Kubernetes Engine 管理员、存储管理员、服务账号用户
检查角色
-
在 Google Cloud 控制台中,前往 IAM 页面。
转到 IAM - 选择项目。
-
在主账号 列中,找到标识您或您所属群组的所有行。如需了解您属于哪些群组,请与您的 管理员联系。
- 对于指定或包含您的所有行,请检查角色 列以查看 角色列表是否包含所需的角色。
授予角色
-
在 Google Cloud 控制台中,前往 IAM 页面。
转到 IAM - 选择项目。
- 点击 授予访问权限。
-
在新的主账号 字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 点击选择角色,然后搜索相应角色。
- 如需授予其他角色,请点击 添加其他角色 ,然后添加其他各个角色。
- 点击 Save (保存)。
-
创建 GKE Autopilot 集群
在 Google Cloud 控制台中,前往创建 Autopilot 集群页面。
在集群基本信息下,执行以下操作:
在名称字段中,输入集群的名称。
保留其余设置的默认值。
如需创建集群,请点击创建。
创建 Cloud Storage 存储桶
- 在 Google Cloud 控制台中,转到 Cloud Storage 存储桶页面。
点击 创建。
在创建存储桶页面上的开始使用部分中,执行以下操作:
为存储桶输入符合存储桶命名要求的全局唯一名称。
保留其余设置的默认值。
如需创建存储桶,请点击创建。
将对象上传到 Cloud Storage 存储桶
- 在 Google Cloud 控制台中,转到 Cloud Storage 存储桶页面。
在存储桶列表中,点击您刚刚创建的存储桶的名称。
在存储桶的对象标签页中,执行以下任一操作:
将文件从桌面或文件管理器拖动到 Google Cloud 控制台的主窗格中。
点击上传文件按钮。 在显示的对话框中,选择要上传的文件,然后点击打开。
配置对 Cloud Storage 存储桶的访问权限
如需确保 GKE 集群可以访问 Cloud Storage 存储桶,请按照配置对 Cloud Storage 存储桶的访问权限中的步骤操作。
设置 Cloud Shell
在 Google Cloud 控制台中,激活 Cloud Shell。
Cloud Shell 会话随即会启动并显示命令行提示符。该会话可能需要几秒钟来完成初始化。
部署 Pod 以使用 FUSE 访问 Cloud Storage 存储桶
设置要在其中部署 Pod 的默认项目:
gcloud config set project PROJECT_ID将
PROJECT_ID替换为您的项目 ID。创建并应用包含以下内容的 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 存储桶名称。 - 如果您使用的是其他命名空间或服务账号,请相应地替换
namespace和serviceAccountName字段。
此清单的内容会启动一个名为
gcsfuse-test的 Pod,以将存储桶装载到其/data路径上。- 将
可使用以下命令验证 Pod 是否正在运行:
kubectl get pod gcsfuse-test输出类似于以下内容:
NAME READY STATUS RESTARTS AGE gcsfuse-test 2/2 Running 0 12sCloud Storage FUSE CSI 驱动程序会在 Pod 中附加一个边车容器,以管理与 Cloud Storage 的交互。
如果 Pod 未运行,您可以运行
kubectl describe pod gcsfuse-test命令来获取事件日志,以便诊断问题。如需了解详情,请参阅 GitHub 上的问题排查。Pod 运行后,您可以探索其文件系统:
kubectl exec -it gcsfuse-test -- find /data
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除 Pod:
kubectl delete pod gcsfuse-test