本頁說明如何設定及準備使用 GKE 的 Cloud Storage FUSE CSI 驅動程式。
如要使用 Cloud Storage FUSE CSI 驅動程式,請完成下列步驟:
建立 Cloud Storage 值區
如果尚未建立 Cloud Storage 值區,請建立值區。 您會在 GKE 叢集中將這些 bucket 掛接為磁碟區。為提升效能,請將「位置類型」設為「區域」,並選取與 GKE 叢集相符的區域。
啟用 Cloud Storage FUSE CSI 驅動程式
請按照下列步驟操作,具體做法取決於您使用的是 GKE Autopilot 或 Standard 叢集。建議您使用 Autopilot 叢集,享受全代管的 Kubernetes 體驗。如要選擇最適合工作負載的模式,請參閱「選擇 GKE 作業模式」。
Autopilot
Autopilot 叢集預設會啟用 Cloud Storage FUSE CSI 驅動程式。您可以直接跳到「設定 Cloud Storage 值區的存取權」。
標準
如果 Standard 叢集已啟用 Cloud Storage FUSE CSI 驅動程式,請跳至「設定 Cloud Storage bucket 的存取權」。
Standard 叢集預設不會啟用 Cloud Storage FUSE CSI 驅動程式。如要建立啟用 Cloud Storage FUSE CSI 驅動程式的標準叢集,可以使用 gcloud container clusters create
指令:
gcloud container clusters create CLUSTER_NAME \
--addons GcsFuseCsiDriver \
--cluster-version=VERSION \
--location=LOCATION \
--workload-pool=PROJECT_ID.svc.id.goog
更改下列內容:
CLUSTER_NAME
:叢集名稱。VERSION
:GKE 版本號碼。 必須選取 1.24 以上版本。LOCATION
:叢集的 Compute Engine 地區或區域。PROJECT_ID
:您的專案 ID。
如要在現有標準叢集上啟用驅動程式,請使用 gcloud container clusters update
指令:
gcloud container clusters update CLUSTER_NAME \
--update-addons GcsFuseCsiDriver=ENABLED \
--location=LOCATION
如要確認叢集是否已啟用 Cloud Storage FUSE CSI 驅動程式,請執行下列指令:
gcloud container clusters describe CLUSTER_NAME \
--location=LOCATION \
--project=PROJECT_ID \
--format="value(addonsConfig.gcsFuseCsiDriverConfig.enabled)"
設定 Cloud Storage 值區的存取權
Cloud Storage FUSE CSI 驅動程式使用 GKE 適用的工作負載身分聯盟,因此您可以針對 GKE Pod 存取 Cloud Storage 中儲存資料的方式,設定精細的權限。
如要讓 GKE 叢集存取 Cloud Storage 值區,請使用 Workload Identity Federation for GKE 進行驗證,並在 Pod 規格中掛接所需的 Cloud Storage 值區:
- 如果尚未啟用 GKE 適用的工作負載身分聯盟,請按照這些步驟啟用。如要使用現有節點集區,請在叢集上啟用 Workload Identity Federation for GKE 後,手動在節點集區上啟用 Workload Identity Federation for GKE。
取得叢集憑證:
gcloud container clusters get-credentials CLUSTER_NAME \ --location=LOCATION
更改下列內容:
CLUSTER_NAME
:叢集名稱,該叢集已啟用 Workload Identity Federation for GKE。LOCATION
:叢集的 Compute Engine 地區或區域。
建立要用於 Kubernetes ServiceAccount 的命名空間。您也可以使用
default
命名空間或任何現有命名空間。kubectl create namespace NAMESPACE
將
NAMESPACE
替換為 Kubernetes ServiceAccount 的 Kubernetes 命名空間名稱。為應用程式建立要使用的 Kubernetes ServiceAccount。您也可以在任何命名空間中使用現有的 Kubernetes ServiceAccount,包括
default
Kubernetes ServiceAccount。kubectl create serviceaccount KSA_NAME \ --namespace NAMESPACE
將
KSA_NAME
替換為 Kubernetes ServiceAccount 的名稱。將其中一個 Cloud Storage 適用的 IAM 角色授予 Kubernetes ServiceAccount。請按照下列步驟操作,視您是要將 Kubernetes ServiceAccount 存取權授予特定 Cloud Storage 值區,還是授予專案中所有值區的存取權而定。
特定 bucket 存取權
gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \ --member "principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/KSA_NAME" \ --role "ROLE_NAME"
更改下列內容:
BUCKET_NAME
:您的 Cloud Storage bucket 名稱。PROJECT_NUMBER
:GKE 叢集的數值專案編號。 如要找出專案編號,請參閱「識別專案」。PROJECT_ID
:GKE 叢集的專案 ID。NAMESPACE
:Kubernetes ServiceAccount 的 Kubernetes 命名空間名稱。KSA_NAME
:新 Kubernetes ServiceAccount 的名稱。ROLE_NAME
:要指派給 Kubernetes ServiceAccount 的 IAM 角色。- 如果是唯讀工作負載,請使用「Storage 物件檢視者」角色 (
roles/storage.objectViewer
)。 - 如為讀寫工作負載,請使用 Storage Object User 角色 (
roles/storage.objectUser
)。
- 如果是唯讀工作負載,請使用「Storage 物件檢視者」角色 (
全域 bucket 存取權
gcloud projects add-iam-policy-binding GCS_PROJECT \ --member "principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/NAMESPACE/sa/KSA_NAME" \ --role "ROLE_NAME"
更改下列內容:
GCS_PROJECT
:Cloud Storage 值區的專案 ID。PROJECT_NUMBER
:GKE 叢集的數值專案編號。 如要找出專案編號,請參閱「識別專案」。PROJECT_ID
:GKE 叢集的專案 ID。NAMESPACE
:Kubernetes ServiceAccount 的 Kubernetes 命名空間名稱。KSA_NAME
:新 Kubernetes ServiceAccount 的名稱。ROLE_NAME
:要指派給 Kubernetes ServiceAccount 的 IAM 角色。- 如果是唯讀工作負載,請使用「Storage 物件檢視者」角色 (
roles/storage.objectViewer
)。 - 如為讀寫工作負載,請使用 Storage Object User 角色 (
roles/storage.objectUser
)。
- 如果是唯讀工作負載,請使用「Storage 物件檢視者」角色 (
後續步驟
- 瞭解如何透過在 Pod 規格中內嵌指定 bucket,掛接 Cloud Storage bucket。
- 瞭解如何使用 PersistentVolume 資源掛接 Cloud Storage 值區。
- 進一步瞭解如何設定應用程式,以使用 GKE 適用的工作負載身分聯盟。