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指令,取得最新版本。較舊的 gcloud CLI 版本可能不支援執行本文件中的指令。
啟用 Compute Engine 永久磁碟 CSI 驅動程式
如要在現有的 Standard 叢集中啟用 Compute Engine Persistent Disk CSI 驅動程式,請使用 Google Cloud CLI 或 Cloud de Confiance 控制台。
如要在現有叢集啟用驅動程式,請完成下列步驟:
gcloud
gcloud container clusters update CLUSTER-NAME \
--update-addons=GcePersistentDiskCsiDriver=ENABLED
將 CLUSTER-NAME 替換為現有叢集的名稱。
控制台
前往 Cloud de Confiance 控制台的「Google Kubernetes Engine」頁面。
在叢集清單中,按一下您要修改的叢集名稱。
在「功能」下方,按一下「Compute Engine 永久磁碟 CSI 驅動程式」欄位旁的 edit「編輯 Compute Engine CSI 驅動程式」。
選取「啟用 Compute Engine Persistent Disk CSI 驅動程式」核取方塊。
按一下 [儲存變更]。
啟用 Compute Engine 永久磁碟 CSI 驅動程式後,您可以使用驅動程式和佈建工具名稱 pd.csi.storage.gke.io,在 Kubernetes 磁碟區中使用該驅動程式。
停用 Compute Engine 永久磁碟 CSI 驅動程式
您可以使用 Google Cloud CLI 或 Cloud de Confiance 控制台,為標準叢集停用 Compute Engine 永久磁碟 CSI 驅動程式。
如果停用驅動程式,目前使用驅動程式所擁有 PersistentVolumes 的任何 Pod 都不會終止。嘗試使用這些 PersistentVolume 的任何新 Pod 也無法啟動。
如要停用現有標準叢集上的驅動程式,請完成下列步驟:
gcloud
gcloud container clusters update CLUSTER-NAME \
--update-addons=GcePersistentDiskCsiDriver=DISABLED
將 CLUSTER-NAME 替換為現有叢集的名稱。
控制台
前往 Cloud de Confiance 控制台的「Google Kubernetes Engine」頁面。
在叢集清單中,按一下您要修改的叢集名稱。
在「功能」下方,按一下「Compute Engine 永久磁碟 CSI 驅動程式」欄位旁的 edit「編輯 Compute Engine CSI 驅動程式」。
取消勾選「啟用 Compute Engine Persistent Disk CSI 驅動程式」核取方塊。
按一下 [儲存變更]。
為 Linux 叢集使用 Compute Engine 永久磁碟 CSI 驅動程式
以下各節說明在 GKE 中使用 CSI 驅動程式支援的 Kubernetes 磁碟區時,通常會執行的程序。這些章節專門介紹使用 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
# Recommended setting. Delays the binding and provisioning of a PersistentVolume until a Pod that uses the
# PersistentVolumeClaim is created and scheduled on a node.
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
parameters:
type: pd-balanced
您可以在 type 參數中指定下列永久磁碟類型:
pd-balancedpd-ssdpd-standardpd-extreme(支援 GKE 1.26 以上版本)
如果使用 pd-standard 或 pd-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
您可以建立 PersistentVolumeClaim,參照 Compute Engine 永久磁碟 CSI 驅動程式的 StorageClass。
名為 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:
# The path in the container where the volume will be mounted.
- mountPath: /var/lib/www/html
# The name of the volume that is being defined in the "volumes" section.
name: mypvc
volumes:
- name: mypvc
persistentVolumeClaim:
# References the PersistentVolumeClaim created earlier.
claimName: podpvc
readOnly: false
GKE Autopilot 儲存空間相容性排程
在 Autopilot 叢集中,GKE 會自動管理節點佈建和資源分配。如要讓 Autopilot 佈建與所要求儲存空間類型相容的節點,您必須在自訂 StorageClass 定義中指定 use-allowed-disk-topology: "true" 參數。
設定這個參數會強制執行下列排程行為:
- 叢集自動配置器只會佈建支援所要求磁碟類型的新節點。
- Pod 會排程到相容的節點上。這項排程可避免 Pod 卡在
ContainerCreating或Pending狀態。
詳情請參閱「支援的節點排程」。
較新的機器系列 (例如 C3 或 E4) 需要 Google Cloud Hyperdisk 儲存空間 (例如 hyperdisk-balanced)。這些機器系列不支援舊版 Persistent Disk 磁碟區類型,例如標準永久磁碟 (pd-standard) 或 SSD 永久磁碟 (pd-ssd) 磁碟區。儲存空間相容性會影響排程,如下所示:
- 無聲回退:如果有狀態工作負載使用較早的永久磁碟磁碟區類型 (例如標準永久磁碟 (
pd-standard) 或 SSD 永久磁碟 (pd-ssd) 磁碟區),但未明確要求特定機器系列,GKE 會自動在 E2 節點上排定 Pod,因為這些節點支援這些永久磁碟磁碟區類型。工作負載會從預設的較新硬體無聲無息地回復。 - 排程失敗 (待處理):您可能明確要求較新的機器系列 (例如使用
nodeSelector欄位或自訂 ComputeClass)。如果您也附加較早的 Persistent Disk 磁碟區類型,例如標準永久磁碟 (pd-standard) 或 SSD 永久磁碟 (pd-ssd) 磁碟區,GKE 就找不到同時滿足這兩項限制的節點。Pod 會維持在Pending狀態。 - 多種儲存空間類型衝突 (待處理):如果 Pod 參照多個使用不同儲存空間類型的 PVC (例如結合較早的永久磁碟區類型,如標準永久磁碟 (
pd-standard) 和 Google Cloud Hyperdisk 儲存空間),GKE 可能無法找到支援所有限制的節點類型。在這種情況下,Pod 會維持在Pending狀態。 - 極速磁碟的 CPU 要求不足 (待處理):極速磁碟區類型 (例如
hyperdisk-extreme和pd-extreme) 需要大型 VM 形狀,且 vCPU 數量要高。如果 Pod 要求的 vCPU 數量過少,GKE 可能會佈建不支援磁碟區類型的節點型態,導致 Pod 保持在Pending狀態。為確保排程正確,請指定足夠大的 CPU 要求,讓 Autopilot 佈建支援的 VM 形狀。如需支援的機器類型和最低 vCPU 需求清單,請參閱「機器支援」。
如需疑難排解步驟,以及如何檢查記錄檔來驗證這項行為,請參閱「Workload scheduled on older machine series or stuck in Pending state due to storage incompatibility」(工作負載排定在舊版機器系列上,或因儲存空間不相容而停滯在 狀態)。
為 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:
# Node selector to ensure the Pod runs on a Windows node.
nodeSelector:
kubernetes.io/os: windows
containers:
- name: iis-server
# The container image to use.
image: mcr.microsoft.com/windows/servercore/iis
ports:
- containerPort: 80
volumeMounts:
# The path in the container where the volume will be mounted.
- mountPath: /var/lib/www/html
name: mypvc
volumes:
- name: mypvc
persistentVolumeClaim:
# References the PersistentVolumeClaim created earlier.
claimName: podpvc-windows
readOnly: false
使用 VolumeAttributeClass 動態修改 Hyperdisk IOPS 和輸送量
您可以搭配 Compute Engine 永久磁碟 CSI 驅動程式使用 VolumeAttributesClass,動態修改永久磁碟屬性,包括 IOPS 和輸送量。確認 GKE 叢集版本為 1.34 以上。
本節將示範如何使用 VolumeAttributesClass 動態調整音量效能。您建立了兩個 VolumeAttributesClass 資源:silver 和 gold,用來定義不同的 IOPS 和處理量級別。接著,您會建立 StorageClass、參照 silver 層級的 PersistentVolumeClaim,以及使用磁碟區的 Pod。最後,您會更新 PersistentVolumeClaim,以參照 gold 層級,動態更新磁碟區的效能設定。
建立 VolumeAttributesClass 來定義效能層級
本節會定義名為 silver 和 gold 的範例層級,並說明 VolumeAttributesClass 資源。
將下列資訊清單儲存為
vac-classes.yaml:apiVersion: storage.k8s.io/v1 kind: VolumeAttributesClass metadata: name: silver driverName: pd.csi.storage.gke.io parameters: iops: "3000" throughput: "188Mi" --- apiVersion: storage.k8s.io/v1 kind: VolumeAttributesClass metadata: name: gold driverName: pd.csi.storage.gke.io parameters: iops: "6000" throughput: "345Mi"套用資訊清單:
kubectl apply -f vac-classes.yaml
為 Hyperdisk 建立 StorageClass
本節定義用於佈建 Hyperdisk 磁碟區的 StorageClass 資源。
將下列資訊清單儲存為
hyperdisk-sc.yaml:apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: hyperdisk-example provisioner: pd.csi.storage.gke.io volumeBindingMode: WaitForFirstConsumer allowVolumeExpansion: true parameters: type: hyperdisk-balanced套用資訊清單:
kubectl apply -f hyperdisk-sc.yaml
建立具有初始效能層級的 PVC
這個部分會建立 PVC,並使用名為 silver 的初始級別。
將下列資訊清單儲存為
vac-silver-pvc.yaml:apiVersion: v1 kind: PersistentVolumeClaim metadata: name: test-pv-claim spec: accessModes: - ReadWriteOnce storageClassName: hyperdisk-example volumeAttributesClassName: silver resources: requests: storage: 200Gi套用資訊清單:
kubectl apply -f vac-silver-pvc.yaml如要佈建永久磁碟區,請建立會耗用 PVC 的 Pod。 在上一節建立的
StorageClass會設定volumeBindingMode: WaitForFirstConsumer,將磁碟區佈建作業延遲到 Pod 使用 PVC 為止。將下列資訊清單儲存為test-pod.yaml:apiVersion: v1 kind: Pod metadata: name: test-pvc-pod spec: containers: - name: nginx-container image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: pvc-storage mountPath: /usr/share/nginx/html volumes: - name: pvc-storage persistentVolumeClaim: claimName: test-pv-claim套用資訊清單:
kubectl apply -f test-pod.yaml如要驗證磁碟效能設定,請參閱「在 Cloud de Confiance 控制台中驗證磁碟效能設定」。「已佈建的 IOPS」應為
3000,「已佈建的處理量」應為188。
更新 PVC 以使用其他效能層級
這個部分會更新 PVC,改用 gold 級別,而非 silver 級別。
將下列資訊清單儲存為
vac-gold-pvc.yaml:apiVersion: v1 kind: PersistentVolumeClaim metadata: name: test-pv-claim spec: accessModes: - ReadWriteOnce storageClassName: hyperdisk-example volumeAttributesClassName: gold resources: requests: storage: 200Gi套用資訊清單:
kubectl apply -f vac-gold-pvc.yaml如要確認磁碟效能設定是否已更新,請參閱「在 Cloud de Confiance 控制台中驗證磁碟效能設定」。「已佈建的 IOPS」應為
6000,「佈建輸送量」應為345。
在 Cloud de Confiance 控制台中驗證磁碟效能設定
如要確認 IOPS 和輸送量設定是否已套用至永久磁碟,請在 Cloud de Confiance 控制台中查看磁碟詳細資料。
取得磁碟名稱:
PV_NAME=$(kubectl get pvc test-pv-claim -o=jsonpath='{.spec.volumeName}') DISK_NAME=$(kubectl get pv $PV_NAME -o=jsonpath='{.spec.csi.volumeHandle}' | sed 's|.*/||') echo "Persistent disk name: $DISK_NAME"前往 Cloud de Confiance 控制台的「Disks」(磁碟) 頁面。
按一下與上一步輸出內容中的磁碟名稱相符的永久磁碟名稱。
在「磁碟詳細資料」頁面的「效能」部分,查看「佈建的 IOPS」和「佈建的輸送量」。
詳情請參閱「查看 Hyperdisk 的佈建效能設定」。
動態修改的注意事項
- 重新套用先前的設定:如果 PVC 因錯誤 (例如資源無法使用) 而無法繫結至磁碟區屬性類別,您可以安全地重新套用先前的 PVC。
- 配額支援:Kubernetes 控制層可以在參照特定
VolumeAttributesClass的 PVC 上強制執行配額,方法是在ResourceQuota中使用scopeSelector。
搭配非預設檔案系統類型使用 Compute Engine 永久磁碟 CSI 驅動程式
GKE 中 Compute Engine 永久磁碟的預設檔案系統類型為 ext4。只要節點映像檔支援,您也可以使用 xfs 儲存空間類型。如需節點映像檔支援的驅動程式清單,請參閱「儲存空間驅動程式支援」。
以下範例說明如何使用 xfs 做為預設檔案系統類型,而非搭配 Compute Engine 永久磁碟 CSI 驅動程式的 ext4。
建立 StorageClass
將下列資訊清單儲存為名為
pd-xfs-class.yaml的 YAML 檔案:apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: xfs-class provisioner: pd.csi.storage.gke.io parameters: # The type of Compute Engine persistent disk to provision. type: pd-balanced # Specify "xfs" as the filesystem type. csi.storage.k8s.io/fstype: xfs volumeBindingMode: WaitForFirstConsumer套用資訊清單:
kubectl apply -f pd-xfs-class.yaml
建立 PersistentVolumeClaim
將下列資訊清單儲存為
pd-xfs-pvc.yaml:apiVersion: v1 kind: PersistentVolumeClaim metadata: name: xfs-pvc spec: # References the StorageClass created earlier. storageClassName: xfs-class accessModes: - ReadWriteOnce resources: requests: # The amount of storage requested. storage: 10Gi套用資訊清單:
kubectl apply -f pd-xfs-pvc.yaml
建立使用該磁碟區的 Pod
將下列資訊清單儲存為
pd-xfs-pod.yaml:apiVersion: v1 kind: Pod metadata: name: pd-xfs-pod spec: containers: - name: cloud-sdk image: google/cloud-sdk:slim # Keep the container running for 1 hour. args: ["sleep","3600"] volumeMounts: # The path in the container where the volume will be mounted. - mountPath: /xfs name: xfs-volume # Define the volumes available to the containers in the Pod. volumes: - name: xfs-volume persistentVolumeClaim: # References the PersistentVolumeClaim created earlier. claimName: xfs-pvc套用資訊清單:
kubectl apply -f pd-xfs-pod.yaml
確認磁碟區已正確掛接
在 Pod 中開啟 Shell 工作階段:
kubectl exec -it pd-xfs-pod -- /bin/bash尋找
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 驅動程式的記錄,請完成下列步驟:
前往 Cloud de Confiance 控制台的「Cloud Logging」頁面。
如要篩選記錄項目,只顯示與命名空間中執行的 CSI 驅動程式相關的項目,請執行下列 Cloud Logging 查詢:
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 永久磁碟類型。
Autopilot 儲存空間相容性排程
對於在 Autopilot 模式下執行的工作負載 (無論是在 Autopilot 叢集,還是使用 Autopilot ComputeClass 的 Standard 叢集),附加永久儲存空間的 Pod 僅會排定在相容的機器系列上:
- 舊版永久磁碟:使用舊版
pd-standard磁碟的 Pod 只能在支援這些磁碟的機器系列上執行,例如E2節點。 - Hyperdisk:使用新一代儲存空間 (例如
hyperdisk-balanced) 的 Pod 會在較新的機器系列 (例如C3節點) 上執行。 - 待處理狀態:如果您明確設定要在較新的機器系列上執行 Pod (例如使用節點選取器要求
C3節點,或設定自訂 ComputeClass),但附加舊版pd-standard磁碟,Pod 會無限期處於Pending狀態。這是因為 GKE 找不到同時符合機型和磁碟相容性限制的節點。明確選取機器系列時,您有責任確保 PersistentVolume 和磁碟相容。
如果您嘗試在機器上執行 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 執行個體。詳情請參閱「機器型態支援」。