使用 Compute Engine 永久磁碟 CSI 驅動程式


Google Kubernetes Engine (GKE) 提供簡單的方式,讓您在叢集中自動部署及管理 Compute Engine 永久磁碟 Container Storage Interface (CSI) 驅動程式。在 Autopilot 叢集中,Compute Engine 永久磁碟 CSI 驅動程式一律會啟用,且無法停用或編輯。在標準叢集中,您必須啟用 Compute Engine 永久磁碟 CSI 驅動程式。

Compute Engine 永久磁碟 CSI 驅動程式版本與 GKE 版本號碼相關。Compute Engine 永久磁碟 CSI 驅動程式版本通常是 GKE 版本發布時的最新驅動程式。叢集升級至最新 GKE 修補程式時,驅動程式會自動更新。

優點

使用 Compute Engine 永久磁碟 CSI 驅動程式的優點如下:

  • 啟用後,系統會自動部署及管理永久磁碟驅動程式,您不必手動設定。
  • 您可以選擇使用客戶自行管理的加密金鑰 (CMEK)。這些金鑰是用來對加密您資料的資料加密金鑰進行加密。如要進一步瞭解 GKE 的 CMEK,請參閱「使用 CMEK」。
  • 您可以使用 Compute Engine 永久磁碟 CSI 驅動程式建立磁碟區快照。磁碟區快照可讓您在特定時間點建立磁碟區副本。您可以使用這個副本,將磁碟區還原至先前的狀態,或是佈建新的磁碟區。
  • 在執行 GKE 1.22 以上版本的叢集中,您可以使用 Compute Engine 永久磁碟 CSI 驅動程式進行磁碟區複製。磁碟區複製功能可讓您在特定時間點建立磁碟區副本,並佈建來源磁碟區的所有資料。
  • 錯誤修正和功能更新會獨立於 Kubernetes 次要版本推出。這個發布時間表通常會加快發布頻率。

事前準備

開始之前,請確認你已完成下列工作:

  • 啟用 Google Kubernetes Engine API。
  • 啟用 Google Kubernetes Engine API
  • 如要使用 Google Cloud CLI 執行這項工作,請安裝初始化 gcloud CLI。如果您先前已安裝 gcloud CLI,請執行 gcloud components update,取得最新版本。

需求條件

如要使用 Compute Engine 永久磁碟 CSI 驅動程式,叢集必須使用下列版本:

  • Linux 叢集:GKE 1.14 以上版本。
  • Windows 叢集:GKE 1.18 以上版本。

在 1.22 以上版本中,CSI 遷移功能已啟用。使用 gce-pd 供應商的現有磁碟區會改用 CSI 驅動程式通訊。您不需要變更任何 StorageClass。gce-pd 提供者仍不支援 CMEK 或磁碟區快照等功能。您必須在 StorageClass 中使用 pd.csi.storage.gke.io provider,才能啟用這些功能。

如要搭配 Workload Identity Federation for GKE 使用 Compute Engine 永久磁碟 CSI 驅動程式,標準叢集必須使用下列版本:

  • Linux 叢集:GKE 1.16 以上版本。
  • Windows 叢集:GKE 1.20.8-gke.900 以上版本。

在新叢集上啟用 Compute Engine 永久磁碟 CSI 驅動程式

如要建立 Standard 叢集,並使用 Compute Engine 永久磁碟 CSI 驅動程式不會自動啟用的版本,請使用 Google Cloud CLI 或Google Cloud 主控台。

如要在建立叢集時啟用驅動程式,請完成下列步驟:

gcloud

gcloud container clusters create CLUSTER-NAME \
    --addons=GcePersistentDiskCsiDriver \
    --cluster-version=VERSION

更改下列內容:

  • CLUSTER-NAME:叢集名稱。
  • VERSION:GKE 版本號碼。如要使用這項功能,必須選取 1.14 以上版本。

如需完整旗標清單,請參閱 gcloud container clusters create 說明文件。

控制台

  1. 在 Google Cloud 控制台中,前往「建立 Kubernetes 叢集」頁面。

    前往「建立 Kubernetes 叢集」

  2. 視需要設定叢集。

  3. 在導覽窗格的「Cluster」(叢集) 底下,按一下「Features」(功能)

  4. 選取「啟用 Compute Engine 永久磁碟 CSI 驅動程式」核取方塊。

  5. 點選「建立」

啟用 Compute Engine 永久磁碟 CSI 驅動程式後,您可以使用驅動程式和佈建程式名稱 pd.csi.storage.gke.io,在 Kubernetes 磁碟區中使用驅動程式。

在現有叢集上啟用 Compute Engine 永久磁碟 CSI 驅動程式

如要在現有的 Standard 叢集中啟用 Compute Engine 永久磁碟 CSI 驅動程式,請使用 Google Cloud CLI 或 Google Cloud 主控台。

如要在現有叢集上啟用驅動程式,請完成下列步驟:

gcloud

gcloud container clusters update CLUSTER-NAME \
   --update-addons=GcePersistentDiskCsiDriver=ENABLED

CLUSTER-NAME 替換為現有叢集的名稱。

控制台

  1. 前往 Google Cloud 控制台的「Google Kubernetes Engine」頁面。

    前往「Google Kubernetes Engine」

  2. 在叢集清單中,按一下您要修改的叢集名稱。

  3. 在「功能」下方,按一下「Compute Engine 永久磁碟 CSI 驅動程式」欄位旁的 「編輯 Compute Engine CSI 驅動程式」

  4. 選取「啟用 Compute Engine Persistent Disk CSI 驅動程式」核取方塊。

  5. 按一下 [儲存變更]。

停用 Compute Engine 永久磁碟 CSI 驅動程式

如要為標準叢集停用 Compute Engine 永久磁碟 CSI 驅動程式,請使用 Google Cloud CLI 或 Google Cloud 控制台。

如果您停用驅動程式,就無法終止目前使用驅動程式擁有的 PersistentVolume 的任何 Pod。另外,嘗試使用這些 PersistentVolume 的所有新 Pod 也將無法啟動。

如要在現有標準叢集上停用驅動程式,請完成下列步驟:

gcloud

gcloud container clusters update CLUSTER-NAME \
    --update-addons=GcePersistentDiskCsiDriver=DISABLED

CLUSTER-NAME 替換為現有叢集的名稱。

控制台

  1. 前往 Google Cloud 控制台的「Google Kubernetes Engine」頁面。

    前往「Google Kubernetes Engine」

  2. 在叢集清單中,按一下您要修改的叢集名稱。

  3. 在「功能」下方,按一下「Compute Engine 永久磁碟 CSI 驅動程式」欄位旁的 「編輯 Compute Engine CSI 驅動程式」

  4. 取消勾選「啟用 Compute Engine Persistent Disk CSI 驅動程式」核取方塊。

  5. 按一下 [儲存變更]。

在 Linux 叢集使用 Compute Engine 永久磁碟 CSI 驅動程式

下列各節說明在 GKE 中使用 Kubernetes 磁碟區 (由 CSI 驅動程式支援) 的一般程序。這些章節僅適用於使用 Linux 的叢集。

建立 StorageClass

啟用 Compute Engine 永久磁碟 CSI 驅動程式後,GKE 會自動安裝下列 StorageClasses

  • standard-rwo,使用已平衡的永久磁碟
  • premium-rwo,使用 SSD 永久磁碟

在 Autopilot 叢集中,預設的 StorageClass 為 standard-rwo,使用 Compute Engine 永久磁碟 CSI 驅動程式。對於標準叢集,預設的 StorageClass 會使用 Kubernetes 樹狀結構內 gcePersistentDisk 磁碟區外掛程式。

如要查看已安裝的 StorageClass 名稱,請執行下列指令:

kubectl get sc

您也可以在供應器欄位中加入 pd.csi.storage.gke.io,安裝使用 Compute Engine 永久磁碟 CSI 驅動程式的不同 StorageClass。

舉例來說,您可以使用名為 pd-example-class.yaml 的下列檔案建立 StorageClass:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: pd-example
provisioner: pd.csi.storage.gke.io
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
parameters:
  type: pd-balanced

您可以在 type 參數中指定下列永久磁碟類型

  • pd-balanced
  • pd-ssd
  • pd-standard
  • pd-extreme (支援 GKE 1.26 以上版本)

如果使用 pd-standardpd-extreme,請參閱「不支援的機器類型」一文,瞭解其他使用限制。

使用 pd-extreme 選項時,您也必須在資訊清單中新增 provisioned-iops-on-create 欄位。這個欄位必須設為與您建立永久磁碟時指定的佈建 IOPS 值相同。

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: pd-extreme-example
provisioner: pd.csi.storage.gke.io
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
parameters:
  type: pd-extreme
  provisioned-iops-on-create: '10000'

建立 pd-example-class.yaml 檔案後,請執行下列指令:

kubectl create -f pd-example-class.yaml

建立 PersistentVolumeClaim

您可以建立參照 Compute Engine 永久磁碟 CSI 驅動程式 StorageClass 的 PersistentVolumeClaim。

以下名為 pvc-example.yaml 的檔案使用預先安裝的儲存空間類別 standard-rwo

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: podpvc
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: standard-rwo
  resources:
    requests:
      storage: 6Gi

建立 PersistentVolumeClaim 資訊清單後,請執行下列指令:

kubectl create -f pvc-example.yaml

在預先安裝的 StorageClass (standard-rwo) 中,volumeBindingMode 會設為 WaitForFirstConsumer。如果 volumeBindingMode 設為 WaitForFirstConsumer,系統會等到排定參照 PersistentVolumeClaim 的 Pod 時,才佈建 PersistentVolume。如果 StorageClass 中的 volumeBindingMode 設為 Immediate (或省略),系統會在建立 PersistentVolumeClaim 後,佈建以永久磁碟為基礎的 PersistentVolume。

建立使用磁碟區的 Pod

使用 Pod 搭配 PersistentVolumes 時,建議您使用工作負載控制器 (例如 Deployment 或 StatefulSet)。雖然您通常不會使用獨立 Pod,但為求簡單,下列範例會使用獨立 Pod。

下列範例會使用您在前一節建立的磁碟區:

apiVersion: v1
kind: Pod
metadata:
  name: web-server
spec:
  containers:
   - name: web-server
     image: nginx
     volumeMounts:
       - mountPath: /var/lib/www/html
         name: mypvc
  volumes:
   - name: mypvc
     persistentVolumeClaim:
       claimName: podpvc
       readOnly: false

在 Windows 叢集使用 Compute Engine 永久磁碟 CSI 驅動程式

下列各節說明在 GKE 中使用 CSI 驅動程式支援的 Kubernetes 磁碟區時,一般會採取的程序。這些章節專門介紹使用 Windows 的叢集。

請確認下列事項:

  • 叢集版本為 1.19.7-gke.2000、1.20.2-gke.2000 以上版本。
  • 節點版本為 1.18.12-gke.1203、1.19.6-gke.800 以上。

建立 StorageClass

建立 Windows 的 StorageClass 與 Linux 非常相似。請注意,預設安裝的 StorageClass 不適用於 Windows,因為檔案系統類型不同。Windows 適用的 Compute Engine 永久磁碟 CSI 驅動程式需要 NTFS 做為檔案系統類型。

舉例來說,您可以使用下列名為 pd- windows-class.yaml 的檔案建立 StorageClass。請務必將 csi.storage.k8s.io/fstype: NTFS 新增至參數清單:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: pd-sc-windows
provisioner: pd.csi.storage.gke.io
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
parameters:
  type: pd-balanced
  csi.storage.k8s.io/fstype: NTFS

建立 PersistentVolumeClaim

為 Windows 建立 StorageClass 後,您現在可以建立參照該 StorageClass 的 PersistentVolumeClaim:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: podpvc-windows
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: pd-sc-windows
  resources:
    requests:
      storage: 6Gi

建立使用磁碟區的 Pod

以下範例會使用您在上一個工作中建立的磁碟區:

apiVersion: v1
kind: Pod
metadata:
  name: web-server
spec:
  nodeSelector:
    kubernetes.io/os: windows
  containers:
    - name: iis-server
      image: mcr.microsoft.com/windows/servercore/iis
      ports:
      - containerPort: 80
      volumeMounts:
      - mountPath: /var/lib/www/html
        name: mypvc
  volumes:
    - name: mypvc
      persistentVolumeClaim:
        claimName: podpvc-windows
        readOnly: false

使用 Compute Engine 永久磁碟 CSI 驅動程式搭配非預設檔案系統類型

GKE 中 Compute Engine 永久磁碟的預設檔案系統類型為 ext4。只要節點映像檔支援 xfs 儲存空間類型,您也可以使用該類型。如需各節點映像檔支援的驅動程式清單,請參閱「儲存空間驅動程式支援」。

以下範例說明如何使用 Compute Engine 永久磁碟 CSI 驅動程式,將 xfs 設為預設檔案系統類型,而非 ext4

建立 StorageClass

  1. 將下列資訊清單儲存為名為 pd-xfs-class.yaml 的 YAML 檔案:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: xfs-class
    provisioner: pd.csi.storage.gke.io
    parameters:
      type: pd-balanced
      csi.storage.k8s.io/fstype: xfs
    volumeBindingMode: WaitForFirstConsumer
    
  2. 套用資訊清單:

    kubectl apply -f pd-xfs-class.yaml
    

建立 PersistentVolumeClaim

  1. 將下列資訊清單儲存為 pd-xfs-pvc.yaml

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: xfs-pvc
    spec:
      storageClassName: xfs-class
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 10Gi
    
  2. 套用資訊清單:

    kubectl apply -f pd-xfs-pvc.yaml
    

建立使用磁碟區的 Pod

  1. 將下列資訊清單儲存為 pd-xfs-pod.yaml

    apiVersion: v1
    kind: Pod
    metadata:
      name: pd-xfs-pod
    spec:
      containers:
      - name: cloud-sdk
        image: google/cloud-sdk:slim
        args: ["sleep","3600"]
        volumeMounts:
        - mountPath: /xfs
          name: xfs-volume
      volumes:
      - name: xfs-volume
        persistentVolumeClaim:
          claimName: xfs-pvc
    
  2. 套用資訊清單:

    kubectl apply -f pd-xfs-pod.yaml
    

確認磁碟區已正確掛接

  1. 在 Pod 中開啟殼層工作階段:

    kubectl exec -it pd-xfs-pod -- /bin/bash
    
  2. 尋找 xfs 分區:

    df -aTh --type=xfs
    

    畫面會顯示如下的輸出內容:

    Filesystem     Type  Size  Used Avail Use% Mounted on
    /dev/sdb       xfs    30G   63M   30G   1% /xfs
    

查看 Compute Engine 永久磁碟 CSI 驅動程式的記錄

您可以使用 Cloud Logging 查看與 Compute Engine 永久磁碟 CSI 驅動程式相關的事件。 記錄有助於排解問題。

如要進一步瞭解 Cloud Logging,請參閱「查看 GKE 記錄」。

如要查看 Compute Engine 永久磁碟 CSI 驅動程式的記錄,請完成下列步驟:

  1. 前往 Google Cloud 控制台的「Cloud Logging」頁面。

    前往 Cloud Logging

  2. 執行以下查詢:

     resource.type="k8s_container"
     resource.labels.project_id="PROJECT_ID"
     resource.labels.location="LOCATION"
     resource.labels.cluster_name="CLUSTER_NAME"
     resource.labels.namespace_name="kube-system"
     resource.labels.container_name="gce-pd-driver"
    

    更改下列內容:

    • PROJECT_ID:專案名稱。
    • LOCATION:叢集的 Compute Engine 區域或可用區。
    • CLUSTER_NAME:叢集名稱。

已知問題

不支援的機器類型

如果您使用 C3 系列機器,則不支援 pd-standard 永久磁碟類型。

如果您嘗試在機器上執行 Pod,但 Pod 使用不受支援的永久磁碟類型,Pod 會發出類似下列內容的警告訊息:

AttachVolume.Attach failed for volume "pvc-d7397693-5097-4a70-9df0-b10204611053" : rpc error: code = Internal desc = unknown Attach error: failed when waiting for zonal op: operation operation-1681408439910-5f93b68c8803d-6606e4ed-b96be2e7 failed (UNSUPPORTED_OPERATION): [pd-standard] features are not compatible for creating instance.

如果叢集有多個節點集區,且這些集區使用不同的機器系列,您可以運用節點汙點節點親和性,限制工作負載的排程位置。舉例來說,您可以使用這種方法,限制工作負載在不支援的機器系列上執行 pd-standard

如果您使用 pd-extreme 永久磁碟類型,請務必將磁碟連接至具有合適機器外形的 VM 執行個體。詳情請參閱「機器形狀支援」。

後續步驟