本頁說明如何使用 ReadOnlyMany 存取模式,將永久磁碟新增至叢集。這個模式可讓不同節點上的多個 Pod 掛接磁碟以供讀取。
如要進一步瞭解這個模式,請參閱永久磁碟區存取模式。
需求條件
- 叢集必須執行 GKE 1.22 以上版本。
- 叢集必須使用 Compute Engine 永久磁碟 CSI 驅動程式。
格式化及填入來源永久磁碟
如要以唯讀模式使用永久磁碟,您必須先在來源永久磁碟中填入資料,然後複製磁碟區或使用磁碟區快照,將資料移至新的 ReadOnlyMany PersistentVolume。
格式化磁碟並填入資料。如要格式化磁碟,請在 Pod 中將磁碟參照為 ReadWriteOnce PersistentVolume。GKE 會自動格式化基礎磁碟,並允許 Pod 將資料寫入磁碟。Pod 啟動時,請確認 Pod 會將所需資料寫入磁碟。
建立 ReadOnlyMany
PersistentVolume
您可以使用下列任一方法建立 ReadOnlyMany
PersistentVolume:
- 您填入資料的來源 PersistentVolume 磁碟區快照。
- 來源 PersistentVolume 的磁碟區副本,您會填入資料。
已填入資料的現有永久磁碟。
磁碟區快照
將下列 PersistentVolumeClaim 資訊清單儲存為
snapshot-pvc.yaml
:kind: PersistentVolumeClaim apiVersion: v1 metadata: namespace: PVC_NAMESPACE name: PVC_NAME spec: dataSource: apiGroup: snapshot.storage.k8s.io kind: VolumeSnapshot name: SNAPSHOT_NAME accessModes: - ReadOnlyMany storageClassName: premium-rwo resources: requests: storage: STORAGE_SIZE
更改下列內容:
PVC_NAMESPACE
:新 PersistentVolumeClaim 的命名空間。PVC_NAME
:新 PersistentVolumeClaim 的名稱。SNAPSHOT_NAME
:VolumeSnapshot
物件的名稱。STORAGE_SIZE
:要要求的儲存空間大小。 這必須與來源 PersistentVolumeClaim 中要求的數量相同。
將資訊清單套用至叢集:
kubectl apply -f snapshot-pvc.yaml
這會在叢集中建立名為 PVC_NAME
的新 PersistentVolumeClaim,GKE 會使用這個 PersistentVolumeClaim 動態佈建新的 PersistentVolume,其中包含來源永久磁碟快照中的資料。
磁碟區複製
使用磁碟區複製功能前,請務必先瞭解這種做法的限制。
將下列資訊清單儲存為
cloning-pvc.yaml
:kind: PersistentVolumeClaim apiVersion: v1 metadata: namespace: PVC_NAMESPACE name: PVC_NAME spec: dataSource: name: SOURCE_PVC kind: PersistentVolumeClaim accessModes: - ReadOnlyMany storageClassName: STORAGE_CLASS_NAME resources: requests: storage: STORAGE_SIZE
更改下列內容:
PVC_NAMESPACE
:新 PersistentVolumeClaim 的命名空間。PVC_NAME
:新 PersistentVolumeClaim 的名稱。SOURCE_PVC
:來源 PersistentVolumeClaim 的名稱,其中已填入資料。STORAGE_CLASS_NAME
:新 PersistentVolumeClaim 的儲存空間類別。這必須與來源 PersistentVolumeClaim 的儲存空間類別相同。STORAGE_SIZE
:新 PersistentVolumeClaim 的儲存空間大小。這必須與來源 PersistentVolumeClaim 要求的大小相同。
將資訊清單套用至叢集:
kubectl apply -f cloning-pvc.yaml
這會建立名為 PVC_NAME
的 PersistentVolumeClaim,GKE 會使用這個 PersistentVolumeClaim,以 ReadOnlyMany 模式建立新的 PersistentVolume,並將來源 PersistentVolume 中的資料複製到新磁碟區。
如要進一步瞭解磁碟區複製功能,請參閱「建立永久磁碟區的副本」一文。
現有的永久磁碟
將下列資訊清單儲存為
preexisting-disk-pv-pvc.yaml
:apiVersion: v1 kind: PersistentVolume metadata: name: PV_NAME spec: storageClassName: "STORAGE_CLASS_NAME" capacity: storage: DISK_SIZE accessModes: - ReadOnlyMany claimRef: namespace: PVC_NAMESPACE name: PVC_NAME csi: driver: pd.csi.storage.gke.io volumeHandle: DISK_ID fsType: FS_TYPE readOnly: true --- apiVersion: v1 kind: PersistentVolumeClaim metadata: namespace: PVC_NAMESPACE name: PVC_NAME spec: storageClassName: "STORAGE_CLASS_NAME" volumeName: PV_NAME accessModes: - ReadOnlyMany resources: requests: storage: DISK_SIZE
更改下列內容:
PV_NAME
:新 PersistentVolume 的名稱。STORAGE_CLASS_NAME
:新 StorageClass 的名稱。DISK_SIZE
:現有永久磁碟的大小。例如:500G
。PVC_NAMESPACE
:新 PersistentVolumeClaim 的命名空間。PVC_NAME
:新 PersistentVolumeClaim 的名稱。DISK_ID
:現有永久磁碟的 ID。格式為區域永久磁碟的projects/{project_id}/zones/{zone_name}/disks/{disk_name}
,或地區永久磁碟的projects/{project_id}/regions/{region_name}/disks/{disk_name}
。FS_TYPE
:檔案系統類型。您可以保留預設值 (ext4
),或使用xfs
。 如果叢集使用 Windows Server 節點集區,您必須將此值變更為NTFS
。
將資訊清單套用至叢集:
kubectl apply -f preexisting-disk-pv-pvc.yaml
這會在 ReadOnlyMany 模式中建立名為 PVC_NAME
的 PersistentVolumeClaim,以及名為 PV_NAME
的 PersistentVolume。
如要進一步瞭解如何使用現有永久磁碟,請參閱現有永久磁碟。
在 Pod 中使用 PersistentVolumeClaim
現在您可以在多個節點上的多個 Pod 中,同時以唯讀模式參照新的 PersistentVolumeClaim。您無法同時將 ReadWriteOnce 模式的永久磁碟連接到多個節點。詳情請參閱「Deployments 與 StatefulSets 的比較」。
在 Pod 規格中,您必須在 readOnly: true
區段和 volumes
區段中指定 readOnly: true
,如下列範例所示:volumeMounts
apiVersion: apps/v1
kind: Deployment
metadata:
name: multi-read
labels:
app: web-server
spec:
replicas: 3
selector:
matchLabels:
app: web-server
template:
metadata:
labels:
app: web-server
spec:
containers:
- name: web-server
image: nginx
volumeMounts:
- mountPath: /test-mnt
name: my-volume
readOnly: true
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: PVC_NAME
readOnly: true
後續步驟
- 進一步瞭解如何將既有的永久磁碟做為 PersistentVolumes 使用。
- 進一步瞭解 Compute Engine 永久磁碟。