使用具備多個讀取器的永久磁碟

本頁說明如何使用 ReadOnlyMany 存取模式,將永久磁碟新增至叢集。這個模式允許多個Pod 在不同節點上掛接磁碟以供讀取。

如要進一步瞭解這個模式,請參閱「持續性磁碟區存取模式」。

需求條件

格式化並填入來源永久磁碟

如要以唯讀模式使用 Persistent Disk,請先在來源 Persistent Disk 中填入資料,然後複製磁碟區或使用磁碟區快照,將資料移至新的 ReadOnlyMany PersistentVolume。

  1. 使用現有永久磁碟或動態佈建建立 PersistentVolume。

  2. 格式化磁碟並填入資料。如要格式化磁碟,請在 Pod 中將磁碟參照為 ReadWriteOnce PersistentVolume。GKE 會自動格式化基礎磁碟,並允許 Pod 將資料寫入磁碟。Pod 啟動後,請確認 Pod 會將所需資料寫入磁碟。

建立 ReadOnlyMany PersistentVolume

您可以使用下列任一方法建立 ReadOnlyMany PersistentVolume:

  • 來源 PersistentVolume 的磁碟區快照,您可以在其中填入資料。
  • 您填入資料的來源 PersistentVolume 的磁碟區副本。
  • 已填入資料的現有永久磁碟。

磁碟區快照

  1. 建立來源 PersistentVolume 的磁碟區快照。

  2. 將下列 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 中要求的容量相同。
  3. 將資訊清單套用至叢集:

    kubectl apply -f snapshot-pvc.yaml
    

這會在叢集中建立名為 PVC_NAME 的新 PersistentVolumeClaim,GKE 會使用這個 PersistentVolumeClaim 動態佈建新的 PersistentVolume,其中包含來源永久磁碟快照中的資料。

磁碟區複製

使用磁碟區複製功能前,請務必先瞭解這種做法的限制。

  1. 將下列資訊清單儲存為 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 要求的大小相同。
  2. 將資訊清單套用至叢集:

    kubectl apply -f cloning-pvc.yaml
    

這會建立名為 PVC_NAME 的 PersistentVolumeClaim,GKE 會使用該 PersistentVolumeClaim,以 ReadOnlyMany 模式建立新的 PersistentVolume,並使用來源 PersistentVolume 中的資料。

如要進一步瞭解磁碟區複製功能,請參閱「建立永久磁碟區的副本」。

現有的永久磁碟

  1. 將下列資訊清單儲存為 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。
  2. 將資訊清單套用至叢集:

    kubectl apply -f preexisting-disk-pv-pvc.yaml
    

這會在 ReadOnlyMany 模式中建立名為 PVC_NAME 的 PersistentVolumeClaim,以及名為 PV_NAME 的 PersistentVolume。

如要進一步瞭解如何使用現有的永久磁碟,請參閱「現有的永久磁碟」。

在 Pod 中使用 PersistentVolumeClaim

您現在可以在多個節點上的多個 Pod 中,同時以唯讀模式參照新的 PersistentVolumeClaim。您無法同時將 ReadWriteOnce 模式的永久磁碟連接至多個節點。詳情請參閱「Deployment 與 StatefulSet」。

在 Pod 規格中,您必須在 volumeMounts 區段和 volumes 區段中指定 readOnly: true,如下列範例所示:

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

後續步驟