快速入門:透過 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. 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. Verify 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. Verify 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: Kubernetes Engine Admin, Storage Admin, Service Account User

    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.

      前往「身分與存取權管理」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
    8. 建立 GKE Autopilot 叢集

      1. 在 Google Cloud 控制台中,前往「Create an Autopilot cluster」(建立 Autopilot 叢集) 頁面。

        前往「Create an Autopilot cluster」(建立 Autopilot 叢集) 頁面

      2. 在「Cluster basics」(叢集基本資訊) 下方,執行下列操作:

        1. 在「Name」(名稱) 欄位中,輸入叢集的名稱。

        2. 其餘設定均保留預設值。

      3. 如要建立叢集,請按一下「建立」

      建立 Cloud Storage 值區

      1. 在 Google Cloud 控制台,前往「Cloud Storage bucket」頁面。

        前往「Buckets」(值區) 頁面

      2. 按一下「 Create」(建立)

      3. 在「建立 bucket」頁面的「開始使用」部分,執行下列操作:

        1. 輸入符合值區命名規定的全域不重複值區名稱。

        2. 其餘設定均保留預設值。

      4. 如要建立 bucket,請按一下「建立」

      將物件上傳至 Cloud Storage 值區

      1. 在 Google Cloud 控制台,前往「Cloud Storage bucket」頁面。

        前往「Buckets」(值區) 頁面

      2. 在值區清單中,按一下您剛建立的值區名稱。

      3. 在值區的「物件」分頁中,執行下列任一操作:

        • 將檔案從桌面或檔案管理員拖曳到 Google Cloud 控制台的主要窗格。

        • 按一下「Upload Files」(上傳檔案) 按鈕。在隨即顯示的對話方塊中,選取要上傳的檔案,然後按一下「開啟」

      設定 Cloud Storage bucket 的存取權

      如要確保 GKE 叢集可存取 Cloud Storage 值區,請按照「設定 Cloud Storage 值區的存取權」一文中的步驟操作。

      部署 Pod,使用 FUSE 存取 Cloud Storage bucket

      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 中附加 Sidecar 容器,以管理與 Cloud Storage 的互動。

        如果 Pod 未執行,您可以執行 kubectl describe pod gcsfuse-test 指令來擷取事件記錄,協助診斷問題。詳情請參閱 GitHub 上的疑難排解

      5. Pod 執行後,您可以探索其檔案系統:

        kubectl exec -it gcsfuse-test -- find /data 
      6. 清除所用資源

        如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除 Pod:

        kubectl delete pod gcsfuse-test 

        後續步驟