在應用程式中加入 File Storage for VPC

Red Hat OpenShift on IBM Cloud 提供預先定義的儲存類別,您可以利用這些儲存類別為叢集中的應用程式配置 File Storage for VPC。 每個儲存類別皆會指定您所配置的檔案共用所具備的可用容量、IOPS、檔案系統及保留政策。 您也可以根據自己的使用情境,自行建立儲存類別。

在繼續之前,請檢閱下列注意事項和注意事項。

  • 使用儲存類別配置特定類型的儲存空間後,您將無法變更該儲存裝置的類型或保留政策。 不過,如果您想要增加儲存空間容量及效能,則可以 變更大小IOPS。 若要變更儲存空間的類型與保留政策,您必須建立一個新的儲存實例,並將舊儲存實例中的資料複製到新儲存實例中。
  • 如果您的叢集和 VPC 位於不同的資源群組中,則在設定檔共用之前,您必須建立自己的儲存類別並在 resourceGroup 部分下提供您的 VPC 資源群組 ID 以及 securityGroupIDs 部分下的 kube-<clusterID> 安全群組 ID。 若要檢索安全性群組 ID,請執行下列操作。 有關更多信息,請參閱 創建您自己的存儲類
  • 叢集版本引入了新的安全群組規則4.11然後。 這些規則變更意味著您必須同步安全群組才能使用File Storage for VPC。 有關更多信息,請參閱 添加File Storage for VPC到應用程式
  • 版本中新增了新的儲存類別2.0的附加元件。 您無法再配置使用舊儲存類別的新檔案共用。 使用舊儲存類別的現有磁碟區將繼續運行,但是您無法擴充使用舊類別建立的磁碟區。 欲了解更多信息,請參閱 遷移到新的儲存類別
  • 不支援使用 StorageClassSecrets 不支援。
  • 確保建立群集的使用者擁有 VPC Infrastructure Services 的讀取、寫入和操作員權限。
  • 如果您打算在檔案共用上使用加密,請確保 設定服務授權 從 VPC Infrastructure 到 KMS/HPCS。

IBM Cloud 正在將專用金鑰管理服務從 Hyper Protect Crypto Services 變更為 Key Protect 專用。

  • 依預設,File Storage for VPC 叢集附加程式會在 kube-<clusterID> 安全群組中佈建檔案共用。 這表示 Pod 可以跨節點和區域存取檔案共用。
  • 如果您使用基於上下文的限制,請務必設定您的網路區域和規則。 如需詳細資訊,請參閱 使用基於上下文的限制保護虛擬私有雲(VPC)基礎結構服務

新版安全群組規則已於 4.11 及後續版本中導入。 這些規則變更意味著您必須先同步您的安全性群組,才能使用 File Storage for VPC。 若您的叢集最初是在 4.11 或更早版本建立,請執行以下指令以同步您的安全群組設定。

  1. 取得您的叢集 ID。
    ibmcloud oc cluster ls
    
  2. 取得 kube-<clusterID> 安全群組的 ID。
    ibmcloud is sg kube-CLUSTER_ID  | grep ID
    
  3. 使用您在上一個步驟中檢索到的 ID 同步 kube-<clusterID> 安全性群組。
    ibmcloud ks security-group sync -c CLUSTER_ID --security-group ID
    

快速入門指南:File Storage for VPC

建立一個持久性卷請求 (PVC),以便為您的叢集動態配置 File Storage for VPC。 動態配置會自動建立相應的持久性卷 (PV),並在您的帳戶中訂購檔案共用。

現在,您可以選擇 第二代儲存等級

  1. 存取您的 Red Hat OpenShift 叢集

  2. 執行下列指令,以檢閱預先安裝的儲存類別。 如需相關資訊,請參閱 儲存類別參照

    oc get sc | grep vpc-file
    
  3. 將下列 YAML 儲存至檔案。 此範例會使用大小為 10Giibmc-vpc-file-min-iops 儲存類別來建立名為 my-pvc 的要求。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-pvc # Enter a name for your PVC.
    spec:
      accessModes:
      - ReadWriteMany # The file share can be mounted on multiple nodes and pods.
      resources:
        requests:
          storage: 20Gi # Enter the size of the storage in gigabytes (Gi).
      storageClassName: ibmc-vpc-file-min-iops # Enter the name of the storage class that you want to use.
    
  4. 建立 PVC。

    oc apply -f my-pvc.yaml
    
  5. 驗證您的 PVC 已建立並已連結至 PV。

    oc describe pvc my-pvc
    

    輸出範例

    Name:        my-pvc
    Namespace:    default
    StorageClass:    ""
    Status:        Bound
    Volume:        pvc-0d787071-3a67-11e7-aafc-eef80dd2dea2
    Labels:        <none>
    Capacity:    20Gi
    Access Modes:    RWX
    Events:
        FirstSeen    LastSeen    Count    From                                SubObjectPath    Type        Reason            Message
        ---------    --------    -----    ----                                -------------    --------    ------            -------
        1m        1m        1    {ibm.io/ibmc-vpc-file 31898035-3011-11e7-a6a4-7a08779efd33 }            Normal        ProvisioningSucceeded    Successfully provisioned volume pvc-0d787071-3a67-11e7-aafc-eef80dd2dea2
    
  6. 將下列部署配置儲存至稱為 deployment.yaml 的檔案,並參照您在前一個步驟中建立的 PVC。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-deployment
      labels:
        app: my-deployment
    spec:
      selector:
        matchLabels:
          app: busybox
      template:
        metadata:
          labels:
            app: busybox
        spec:
          containers:
          - name: busybox
            image: busybox:1.28
            command: [ "sh", "-c", "sleep 1h" ]
            volumeMounts:
            - name: my-vol
              mountPath: /data/demo # Mount path for the application.
          volumes:
          - name: my-vol
            persistentVolumeClaim:
              claimName: my-pvc # Your PVC name.
    
    volumeMounts.mountPath
    在儲存器磁區裝載區段中,輸入在儲存器內裝載磁區的目錄絕對路徑。 寫入掛載路徑的資料,會儲存於您的實體 File Storage for VPC 實例中的 root 目錄下。 若要讓不同應用程式共用同一個卷,您可以為每個應用程式指定 卷的子路徑
    volumeMounts.name
    在容器磁區裝載區段中,輸入要裝載至 Pod 的磁區名稱。
    volume.name
    在磁區區段中,輸入要裝載至 Pod 的磁區名稱。 通常這個名稱與 volumeMounts.name 相同
    claimName
    在磁區持續性磁區要求區段中,輸入連結您要使用之 PV 的 PVC 名稱。
  7. 建立部署。

    oc apply -f deployment.yaml
    
  8. 驗證已順利裝載 PV。

    oc describe deployment my-deployment
    

    裝載點在 Volume Mounts(磁區裝載)欄位中,而磁區在 Volumes(磁區)欄位中。

    Containers:
      Mounts:
        /data/demo from my-vol (rw)
    Volumes:
    my-vol:
      Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
      ClaimName:  my-pvc
      ReadOnly:   false
    
  9. 選用: 在 Pod 執行之後,請嘗試 擴充儲存空間磁區

遷移到新的儲存類別

  • 版本中新增了新的儲存類別2.0的附加元件。
  • 您無法再配置使用舊儲存類別的新檔案共用。
  • 使用舊儲存類別的現有磁碟區將繼續運行,但是您無法擴充使用舊類別建立的磁碟區。
  • 如果您需要磁碟區擴充功能,請完成以下步驟將您的應用程式遷移到較新的儲存類別。
  • 如果您不需要磁碟區擴充功能,則無需遷移,PVC 會繼續正常運作。
  • 以下步驟涵蓋手動遷移。
  1. 尋找要移轉的 PVC 並記下 PVC 名稱和關聯的 PV 名稱。

    oc get pvc
    
  2. 縮小使用 PVC 的應用程式的規模。

    oc scale deployment DEPLOYMENT --replicas 0
    
  3. 編輯您的應用程式正在使用的 PV 對象,將回收策略變更為 Retain 和儲存類別 ibmc-vpc-file-min-iops

    kubectl edit pv PV
    
    spec:
      accessModes:
      - ReadWriteMany
      capacity:
        storage: 20Gi
      claimRef:
        apiVersion: v1
        kind: PersistentVolumeClaim
        name: <pvc-name>
        namespace: default
        ...
      persistentVolumeReclaimPolicy: Retain # Change delete to retain
      storageClassName: ibmc-vpc-file-min-iops # Enter a new storage class
      volumeMode: Filesystem
    
  4. 刪除現有的 PVC 物件。

    kubectl delete pvc PVC
    
  5. 再次編輯PV並刪除 claimRef 部分。

    kubectl edit pv PV
    
    spec:
      accessModes:
      - ReadWriteMany
      capacity:
        storage: 20Gi
      #claimRef:
        #apiVersion: v1
        #kind: PersistentVolumeClaim
        #name: <pvc-name>
        #namespace: default
        #resourceVersion: "381270"
        #uid: 4042f319-1233-4187-8549-8249a840a8dd
    
  6. 建立一個與先前的 PVC 具有相同名稱和大小的 PVC。 應對所有受影響的 PVC 進行一一執行。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: <pvc-name>
    spec:
      accessModes:
      - ReadWriteMany
      resources:
        requests:
          storage: <size>Gi
      storageClassName: ibmc-vpc-file-min-iops
    
  7. 擴展使用 PVC 的應用程式。

    k scale deployment DEPLOYMENT --replicas x
    
  8. 若要繼續使用磁碟區擴展,請參閱 設定卷擴展

設定卷擴展

若要供應支援擴充的磁區,您必須使用 allowVolumeExpansion 設為 true 的儲存類別。

File Storage for VPC 叢集附加元件支援線上與離線兩種模式的擴充。 然而,擴展僅能在 File Storage for VPC 配置檔的 既定大小與IOPs範圍內實現。

新儲存類別已於版本 2.0 中引入。 磁碟區擴充不適用於使用早期版本的附加元件的儲存類別的共用。

開始之前

擴充已裝載的磁區

  1. 由應用程式 Pod 裝載 PVC 之後,您可以透過編輯 PVC 中 spec.resources.requests.storage 欄位的值來擴充磁區。 若要擴充磁區,請編輯 PVC 並增加 spec.resources.requests.storage 欄位中的值。

    kubectl edit pvc my-pvc
    
    spec:
      accessModes:
      - ReadWriteMany
      resources:
        requests:
          storage: 50Gi
    
  2. 儲存並關閉 PVC。 請等待幾分鐘,讓磁區擴充。

  3. 請確認您的磁碟已展開。

    kubectl get pvc
    

    輸出範例

    NAME     STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS        AGE
    my-pvc   Bound    pvc-25b6912e-75bf-41ca-b6b2-567fa4f9d245   50Gi       RWX            ibmc-vpc-file-min-iops   3m31s
    

將現有檔案儲存空間附加至應用程式

建立一個持久性卷請求 (PVC),以靜態方式為您的叢集配置 File Storage for VPC。 靜態佈建可讓叢集管理者將現有儲存裝置提供給叢集使用。

  1. 取得叢集 ID。

    ibmcloud ks cluster ls
    
  2. 取得您的 kube-<clusterID> 安全群組的 ID。

    ibmcloud is sg kube-CLUSTER_ID  | grep ID
    
  3. 建立檔案共用。 如需相關資訊,請參閱 建立檔案共用及裝載目標

    ibmcloud is share-create --name my-file-share --zone us-south-2 --profile dp2 --size 1000 --iops 1000
    
  4. 建立共用掛載目標並指定您先前在 --vni-sgs 選項中檢索到的 kube-<clusterID> 安全性群組 ID。

    ibmcloud is share-mount-target-create my-file-share --subnet my-subnet --name NAME --vni-name my-share-vni-1  --vni-sgs kube-CLUSTER_ID --resource-group-name Default --vpc ID
    
  5. 在建立持續性磁區 (PV) 之前,請先擷取檔案共用的相關詳細資料。

    ibmcloud is shares
    
  6. 取得共用的詳細資料。 請記下裝載目標。

    ibmcloud is share SHARE-ID
    

    範例指令。

    ibmcloud is share r134-bad98878-1f63-45d2-a3fd-60447094c2e6
    

    輸出範例

    ID                           r134-bad98878-1f63-45d2-a3fd-60447094c2e6   
    Name                         pvc-e7e005a9-e96b-41ad-9d6e-74650a9110a0   
    CRN                          crn:v1:staging:public:is:us-south-1:a/77f2bceddaeb577dcaddb4073fe82c1c::share:r134-bad98878-1f63-45d2-a3fd-60447094c2e6   
    Lifecycle state              stable   
    Access control mode          security_group   
    Zone                         us-south-1   
    Profile                      dp2   
    Size(GB)                     10   
    IOPS                         100   
    User Tags                    clusterid:cpjao3l20dl78jadqkd0,namespace:default,provisioner:vpc.file.csi.ibm.io,pv:pvc-e7e005a9-e96b-41ad-9d6e-74650a9110a0,pvc:pv-file,reclaimpolicy:delete,storageclass:custom-eni   
    Encryption                   provider_managed   
    Mount Targets                ID                                          Name      
                                r134-aa2aabb8-f616-47be-886b-99220852b728   pvc-e7e005a9-e96b-41ad-9d6e-74650a9110a0      
    Resource group               ID                                 Name      
                                300b9469ee8676f9a038ecdf408c1a9d   Default      
    Created                      2024-06-11T19:55:11+05:30   
    Replication role             none   
    Replication status           none   
    Replication status reasons   Status code   Status message      
    
  7. 取得 nfsServerPath,也稱為 Mount Path

    ibmcloud is share-mount-target SHARE-ID SHARE-TARGET-ID
    

    範例指令。

    ibmcloud is share-mount-target  r134-bad98878-1f63-45d2-a3fd-60447094c2e6 r134-aa2aabb8-f616-47be-886b-99220852b728
    

    輸出範例

    ID                          r134-aa2aabb8-f616-47be-886b-99220852b728   
    Name                        pvc-e7e005a9-e96b-41ad-9d6e-74650a9110a0   
    VPC                         ID                                          Name      
                                r134-f05922d4-d8ab-4f64-9a3d-82664b303bc1   vpc-public      
    Access control mode         security_group   
    Resource type               share_mount_target   
    Virtual network interface   ID                                          Name      
                                0716-6407fb4b-e962-49c4-8556-dc94f4574b4b   defective-chloride-huffy-gladly      
    Lifecycle state             stable   
    Mount path                  10.240.0.23:/89d8a454_f552_42bf_8374_4d31481edf4d   
    Transit Encryption          none   
    Created                     2024-06-11T19:55:12+05:30
    
  8. 建立稱為 static-file-share.yaml 的 PV 配置檔,以參照您的檔案共用。

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: static-file-share
    spec:
      mountOptions:
      - hard
      - nfsvers=4.1
      - sec=sys
      accessModes:
      - ReadWriteMany
      capacity:
        storage: 10Gi
      csi:
        volumeAttributes:
          nfsServerPath: NFS-SERVER-PATH
        driver: vpc.file.csi.ibm.io
        volumeHandle: FILE-SHARE-ID#SHARE-TARGET-ID
    
  9. 建立 PV。

    oc apply -f static-file-share.yaml
    
  10. 建立 PVC。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-static
    spec:
      accessModes:
      - ReadWriteMany
      resources:
        requests:
          storage: 10Gi
      storageClassName: "" #Leave the storage class blank.
    
  11. 建立 PVC 以連結 PV。

    oc apply -f pvc-static.yaml
    
  12. 建立部署檔案名稱 testpod.yaml,將您的 fileshare 附加到應用程式 pod。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: testpod
      labels:
        app: testpod
    spec:
      selector:
        matchLabels:
          app: busybox
      template:
        metadata:
          labels:
            app: busybox
        spec:
          containers:
          - name: busybox
            image: busybox:1.28
            command: [ "sh", "-c", "sleep 1h" ]
            volumeMounts:
            - name: my-vol
              mountPath: /data/demo # Mount path for the application.
          volumes:
          - name: pvc-name
            persistentVolumeClaim:
              claimName: pvc-static # The name of the PVC that you created earlier
    
    spec.containers.volumeMounts.mountPath
    輸入在容器中裝載磁區的目錄的絕對路徑。 寫入掛載路徑的資料,會儲存於您的實體 File Storage for VPC 實例中的 root 目錄下。 若要讓不同應用程式共用同一個卷,您可以為每個應用程式指定 卷的子路徑
    volumeMounts.name
    輸入要裝載至 Pod 之磁區的名稱。
    volume.name
    輸入要裝載至 Pod 之磁區的名稱。 通常這個名稱與 volumeMounts.name 相同
    volumes.persistentVolumeClaim.claimName
    請輸入您要使用的 PV 所綁定的 PVC 名稱。
  13. 建立部署。

    oc apply -f testpod.yaml
    

建立您自己的儲存空間類別

您可以使用 File Storage for VPC 範例的偏好設定,建立自己的自訂儲存類別。 為了降低成本,您可以先以最低吞吐量配置 PVC,之後您就可以存取 PVC,並在需求更明確時調整 吞吐量

如果您需要下列特性,則必須 建立自己的儲存空間類別

  • 您的應用程式需要以非 root 使用者身分執行。
  • 您的叢集位於與 VPC 及子網路不同的資源群組中。
  • 您需要限制對給定節點上或給定區域中 Pod 的檔案共用存取權。
  • 您需要使用 KMS 提供者 (例如 HPCS 或 Key Protect) 來自帶 (BYOK) 加密。
  • 您需要手動指定 虛擬網路介面(VNI) 的子網路或 IP 位址。
  • 您需要對容量和頻寬有更多的控制。 在這種情況下,您可以使用 rfs 設定檔。 如需詳細資訊,請參閱 區域檔案共用概觀
  • 您希望將請求的 PVC 容量自動向上取整至固定 IOPS dp2 配置檔所支援的最低容量。 如需更多資訊,請參閱《 為固定 IOPS 配置檔啟用自動容量四捨五入 》。

如果您的叢集和 VPC 不在同一個資源群組中,則必須在 resourceGroup 部分指定 VPC 資源群組 ID,並在 securityGroupIDs 部分指定 kube-<clusterID> 安全群組 ID。 您可以透過執行ibmcloud is sg kube-CLUSTER_ID | grep ID找到kube-<clusterID>安全群組的 ID。

  1. 建立儲存類別配置檔。 以下範例使用 dp2 設定檔。 選擇 第一代和第二代 儲存類別。

    第一代的範例:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: ibmc-vpc-file-custom-sc
      labels:
        app.kubernetes.io/name: ibm-vpc-file-csi-driver
    provisioner: vpc.file.csi.ibm.io
    mountOptions:
      - hard
      - nfsvers=4.1
      - sec=sys
    parameters:
        profile: "dp2" # or rfs
        billingType: "hourly" # hourly or monthly
        encrypted: "false"
        encryptionKey: "" # If encrypted is true, then a user must specify the CRK-CRN.
        resourceGroup: "" # Resource group ID. By default, the resource group of the cluster will be used from storage-secrete-store secret.
        isENIEnabled: "true" # VPC File Share VNI feature will be used by all PVCs created with this storage class.
        securityGroupIDs: "" # By default cluster security group i.e kube-<clusterID> will be used. User can provide their own comma separated SGs.
        subnetID: "" # User can provide subnetID in which the VNI will be created. Zone and region are mandatory for this. If not provided CSI driver will use the subnetID available in the cluster's VPC zone.
        region: "" # VPC CSI driver will select a region from cluster node's topology. The user can override this default.
        zone: "" # VPC CSI driver will select a region from cluster node's topology. The user can override this default.
        primaryIPID: "" # Existing ID of reserved IP from the same subnet as the file share zone. Zone and region are mandatory for this. SubnetID is not mandatory for this.
        primaryIPAddress: "" # IPAddress for VNI to be created in the subnet of the zone. Zone, region and subnetID are mandatory for this.
        iops: "" # Example: 100, this option is valid only for the dp2 profile.
        allowCapacityRoundoffForIops: "true" # Optional. Enables automatic capacity roundoff for fixed IOPS dp2 profiles. When set to "true", if the requested PVC size is lower than the minimum supported capacity for the requested IOPS, the driver automatically provisions the volume at the minimum supported capacity. Disabled by default. Requires a Virtual Private Endpoint Gateway (VPEG) to IBM Global Catalog.
        throughput: "" # Example: 2000, this option is valid only for the rfs profile
        tags: "" # User can add a list of tags "a, b, c" that will be used at the time of provisioning file share, by default CSI driver has its own tags.
        uid: "0" # The initial user identifier for the file share, by default its root.
        gid: "0" # The initial group identifier for the file share, by default its root.
        classVersion: "1"
    reclaimPolicy: "Delete"
    allowVolumeExpansion: true
    

    第二代範例:

    如果您使用第一代 ibmc-vpc-file-min-iops 儲存類別配置 10Gi PVC,則最大 IOPS 為 1,000。 預設分配為 100,吞吐量固定為 210 Mbps,不可調整。

    但是,如果您使用第二代 ibmc-vpc-file-regional 儲存類別配置 10Gi PVC,則 IOPS 會固定為 35,000,最大吞吐量可調整至 8192 Mbps,即使預設為 8 Mbps。

    再舉一個例子,如果您透過使用第一代 ibmc-vpc-file-500-iops 儲存類別,初始佈建一個大小小於 16 TB 且固定 IOPS 為 500 的 PVC,則 IOPS 和吞吐量是固定的,並根據大小和 IOPS 計算。

    若要將該 PVC 擴充至超過 16 TB,那麼第一代 ibmc-vpc-file-500-iops 儲存類別就無法運作。 改用第二代 ibmc-vpc-file-regional-max-bandwidth 儲存類別,任何大小的 PVC 都可配置至 32 TB,並擁有高達 35000 IOPS 和 8192 Mbps 的預設吞吐量(可調整)。

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: ibmc-vpc-file-regional-max-bandwidth
      labels:
        app.kubernetes.io/name: ibm-vpc-file-csi-driver
      annotations:
        version: v2.0
        revision: "1"
    provisioner: vpc.file.csi.ibm.io
    mountOptions:
      - hard
      - nfsvers=4.1
      - sec=sys
    parameters:
      profile: "rfs" # general-purpose, sdp, 5iops-tier, 10iops-tier, or custom
      billingType: "hourly"
      encrypted: "false"
      encryptionKey: ""
      resourceGroup: ""
      throughput: "8192" # Example: 2000
      classVersion: "1"
      isENIEnabled: "true"   # VPC File Share ENI/VNI feature will be used by all PVCs created with this storage class.
      securityGroupIDs: ""   # By default cluster security group i.e kube-<clusterID> will be used. User can provide their own command separated SGs.
      subnetID: ""         # User can provide subnetID in which the ENI/VNI will be created.If not provided CSI driver will use the subnetID available in the cluster' VPC zone.
      primaryIPID: ""      # Existing ID of reserved IP from any of subnets within the VPC,subnetID is not mandatory for this.
      primaryIPAddress: "" # IPAddress for ENI/VNI to be created in the respective subnet,subnetID is mandatory for this.
      uid: "0"             # The initial user identifier for the file share, by default its root.
      gid: "0"             # The initial group identifier for the file share, by default its root.
    allowVolumeExpansion: true # Select true or false. Only supported on version 3.0.1 and later
    reclaimPolicy: "Delete"
    
  2. 在叢集裡建立自訂儲存空間類別。

    oc apply -f custom-storageclass.yaml
    
  3. 驗證您的儲存空間類別可用於叢集。

    oc get sc
    

    輸出範例

    ibmc-vpc-file-custom-sc                       vpc.file.csi.ibm.io
    

設定預設儲存類別

  • 變更預設儲存類別僅適用於附加版本2.0或更高版本。
  • 您可以將預設儲存類別設定為預先安裝的File Storage for VPC類別之一或您自己的自訂儲存類別。 如果您使用自訂儲存類,請確保設定程式設定為 vpc.file.csi.ibm.io
  • 如果在叢集中將多個儲存類別設定為預設儲存類別,則可以使用任何預設儲存類別。 作為最佳實踐,為了確保使用正確的儲存類別,請在設定新的預設類別之前刪除叢集中任何現有的預設儲存類別。 如果也安裝了 VPC 區塊儲存附加元件,則可能已將某個 VPC 區塊儲存類別設定為預設值。 若要在設定新預設值之前先移除它,請參閱《 變更 VPC 區塊儲存的預設儲存類別 》。
  1. 編輯 addon-vpc-file-csi-driver-configmap configmap 並在 SET_DEFAULT_STORAGE_CLASS 參數中指定儲存類別名稱。

    oc edit cm addon-vpc-file-csi-driver-configmap -n kube-system
    

    輸出範例

    SET_DEFAULT_STORAGE_CLASS: "ibmc-vpc-file-eit"
    
  2. 透過描述 file-csi-driver-status configmap 來驗證預設設定是否正確。

    oc describe cm file-csi-driver-status -n kube-system
    

    範例輸出。

    events:
    ----
    - event: EnableVPCFileCSIDriver
      description: 'VPC File CSI Driver enable successful, DriverVersion: v2.0.6'
      timestamp: "2024-09-20 12:01:02"
    - event: Change default storage class request
      description: Successfully set 'ibmc-vpc-file-eit' as default storage class
      timestamp: "2024-09-20 12:01:36"
    

部署以非 root 使用者身分執行的應用程式

儲存類別中的 uidgid 參數,用於在基礎架構層級設定檔案共用空間的初始使用者與群組識別碼。 有關補充識別碼在 VPC 檔案儲存中如何運作的背景資訊,請參閱 《VPC 檔案共用中的補充識別碼》

  1. 建立您自己的儲存空間類別,並指定您要用於應用程式的群組 ID 或使用者 ID。

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: ibmc-vpc-file-custom-sc
      labels:
        app.kubernetes.io/name: ibm-vpc-file-csi-driver
    provisioner: vpc.file.csi.ibm.io
    mountOptions:
      - hard
      - nfsvers=4.1
      - sec=sys
    parameters:
        profile: "dp2"
        iops: "100"
        billingType: "hourly" # hourly or monthly
        encrypted: "false"
        uid: "3000" # The initial user identifier for the file share.
        gid: "1000" # The initial group identifier for the file share.
        classVersion: "1"
    reclaimPolicy: "Delete"
    allowVolumeExpansion: true
    
  2. 將下列 YAML 儲存至稱為 my-pvc.yaml 的檔案。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-pvc
    spec:
      accessModes:
      - ReadWriteMany
      resources:
        requests:
          storage: 10Gi
      storageClassName: ibmc-vpc-file-custom-sc
    
  3. 建立 PVC。

    oc apply -f my-pvc.yaml
    
  4. 建立裝載 PVC 的 Pod。

    apiVersion: v1
    kind: Pod
    metadata:
      name: security-context-demo
    spec:
      securityContext:
        runAsUser: 3000
        runAsGroup: 1000
      volumes:
      - name: sec-ctx-vol
        persistentVolumeClaim:
            claimName: my-pvc
      containers:
      - name: sec-ctx-demo
        image: busybox:1.28
        command: [ "sh", "-c", "sleep 1h" ]
        volumeMounts:
        - name: sec-ctx-vol
          mountPath: /data/demo
        securityContext:
          allowPrivilegeEscalation: false
    
  5. 確認 Pod 是否正在執行。

    oc get pods
    

為File Storage for VPC設定 KMS 加密

使用金鑰管理服務 (KMS) 供應商,例如 IBM® Key Protect 或 Hyper Protect Crypto Services,來建立私有根金鑰。此金鑰將用於您的 File Storage for VPC 執行個體中,在資料寫入儲存空間時進行加密。 建立私有根金鑰後,請使用該根金鑰建立專屬的儲存類別或 Kubernetes 祕密,然後使用此儲存類別或祕密來配置您的 File Storage for VPC 實例。

  1. 建立您要使用的 KMS 提供者的實例。

  2. 在您的 KMS 實例中建立一個根金鑰。

  3. 完成 客戶管理加密的先決條件,並授權 File Storage for VPC 存取 IBM® Key Protect。 一定要給File Storage for VPC至少 Reader 存取您的 KMS 實例。

  4. 建立自訂儲存類別並指定您的 KMS 詳細資訊。

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: custom-sc-encrypted
      labels:
        app.kubernetes.io/name: ibm-vpc-file-csi-driver
    provisioner: vpc.file.csi.ibm.io
    mountOptions:
      - hard
      - nfsvers=4.1
      - sec=sys
    parameters:
        profile: "dp2"
        billingType: "hourly" # hourly or monthly
        encrypted: "true"
        encryptionKey: "" # Specify the root key CRN.
        resourceGroup: "" # Resource group ID. By default, the resource group of the cluster will be used from storage-secrete-store secret.
        isENIEnabled: "true" # VPC File Share VNI feature will be used by all PVCs created with this storage class.
        securityGroupIDs: "" # By default cluster security group i.e kube-<clusterID> will be used. User can provide their own comma separated SGs.
        subnetID: "" # User can provide subnetID in which the VNI will be created. Zone and region are mandatory for this. If not provided CSI driver will use the subnetID available in the cluster's VPC zone.
        region: "" # VPC CSI driver will select a region from cluster node's topology. The user can override this default.
        zone: "" # VPC CSI driver will select a region from cluster node's topology. The user can override this default.
        primaryIPID: "" # Existing ID of reserved IP from the same subnet as the file share zone. Zone and region are mandatory for this. SubnetID is not mandatory for this.
        primaryIPAddress: "" # IPAddress for VNI to be created in the subnet of the zone. Zone, region and subnetID are mandatory for this.
        tags: "" # User can add a list of tags "a, b, c" that will be used at the time of provisioning file share, by default CSI driver has its own tags.
        uid: "0" # The initial user identifier for the file share, by default its root.
        gid: "0" # The initial group identifier for the file share, by default its root.
        classVersion: "1"
    reclaimPolicy: "Delete"
    allowVolumeExpansion: true
    
  5. 建立儲存空間類別。

    oc apply -f encrypted-class.yaml
    
  6. 將下列 YAML 儲存至稱為 my-pvc.yaml 的檔案。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: my-pvc
    spec:
      accessModes:
      - ReadWriteMany
      resources:
        requests:
          storage: 10Gi
      storageClassName: custom-sc-encrypted
    
  7. 建立 PVC。

    oc apply -f my-pvc.yaml
    
  8. 將下列部署配置儲存至稱為 deployment.yaml 的檔案,並參照您在前一個步驟中建立的 PVC。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-deployment
      labels:
        app: my-deployment
    spec:
      selector:
        matchLabels:
          app: busybox
      template:
        metadata:
          labels:
            app: busybox
        spec:
          containers:
          - name: busybox
            image: busybox:1.28
            command: [ "sh", "-c", "sleep 1h" ]
            volumeMounts:
            - name: my-vol
              mountPath: /data/demo # Mount path for the application.
          volumes:
          - name: my-vol
            persistentVolumeClaim:
              claimName: my-pvc # Your PVC name.
    
    volumeMounts.mountPath
    在儲存器磁區裝載區段中,輸入在儲存器內裝載磁區的目錄絕對路徑。 寫入掛載路徑的資料,會儲存於您的實體 File Storage for VPC 實例中的 root 目錄下。 若要讓不同應用程式共用同一個卷,您可以為每個應用程式指定 卷的子路徑
    volumeMounts.name
    在容器磁區裝載區段中,輸入要裝載至 Pod 的磁區名稱。
    volume.name
    在磁區區段中,輸入要裝載至 Pod 的磁區名稱。 通常,此名稱與 volumeMounts.name 相同。
  9. 建立部署。

    oc apply -f deployment.yaml
    

設定傳輸中加密 (EIT)

若要為 File Storage for VPC 設定傳輸中加密,請參閱 《 IBM Cloud File Storage for VPC 的傳輸中加密 》。 該主題涵蓋了區域性檔案共用(dp2 設定檔)以及區域級檔案共用(rfs 設定檔,Beta 版)。

更新容器資源請求值與限制值

您可以自訂在「File Storage for VPC」附加元件中運行的容器的 CPU 和記憶體資源請求值與限制。 調整這些數值有助於您針對大型叢集或資源受限的環境來優化效能。

這些參數的命名格式分別為 <ContainerName>CPULimit<ContainerName>CPURequest<ContainerName>MemoryLimit 以及 <ContainerName>MemoryRequest。 將它們套用至控制器和節點伺服器的容器中。

  1. 編輯 addon-vpc-file-csi-driver-configmap 配置映射,並為您要調整的容器新增資源參數。 以下範例為控制器和節點伺服器容器設定 CPU 和記憶體值。

    oc edit cm addon-vpc-file-csi-driver-configmap -n kube-system
    

    範例值:

    controllerCPULimit: "200m"
    controllerCPURequest: "100m"
    controllerMemoryLimit: "256Mi"
    controllerMemoryRequest: "128Mi"
    nodeServerCPULimit: "200m"
    nodeServerCPURequest: "100m"
    nodeServerMemoryLimit: "256Mi"
    nodeServerMemoryRequest: "128Mi"
    
  2. 請透過檢視 file-csi-driver-status 配置映射,確認資源變更已成功套用。 請在「events」專區中尋找「Successfully set container resources」活動。

    oc describe cm file-csi-driver-status -n kube-system
    

    輸出範例:

    events:
    ----
    - description: Successfully set container resources
      event: Change container resources
      timestamp: "2026-06-26 08:32:05"
    

依工作者節點儲存區、區域或工作者節點限制檔案共用存取權

File Storage for VPC 叢集附加程式的預設行為是任何節點上的 Pod 都可以存取檔案共用。 您也可以對 Pod 如何存取檔案共用套用更精細的控制。 例如,您可以限制檔案共用只能存取特定節點、特定區域、特定工作者節點儲存區上的 Pod。 檢閱下列實務範例,以瞭解如何配置對檔案共用的 Pod 存取權。

建立 PVC 時,會為每個 PVC 建立一個檔案共用目標,並在區域中的該子網路預留一個 VNI IP。 這表示 VPC 檔案儲存體的 PVC 數目上限取決於該子網路上可用的 IP 位址。

如果您使用下列 VNI 特性來限制對檔案共用的 Pod 存取權,則您的應用程式可能不是高可用性。

開始之前

若要依節點、區域或資源群組限制檔案共用存取權,您必須先建立自訂 VPC 安全群組。

  1. 列出叢集,並記下您要在其中部署檔案儲存空間的叢集 ID。

    ibmcloud ks cluster ls
    
  2. 取得您的工作人員池詳細資訊。

    ibmcloud ks worker-pool ls --cluster CLUSTER
    
  3. 取得您的子網路詳細資訊。

    ibmcloud ks worker-pool get WORKER_POOL --cluster CLUSTER | grep -A 3 Subnets
    
  4. 取得您的子網路 CIDR。 對每個子網路重複此步驟。 稍後您會使用此 CIDR 範圍。

    ibmcloud is subnet SUBNET_ID | grep "IPv4 CIDR"
    
  5. 列出安全群組,並記下叢集的 ID kube-<clusterID> 安全群組。 稍後新增安全群組規則時,您需要安全群組 ID。

    ibmcloud is sg
    

    輸出範例

    ID                                          Name                                             Rules   Targets   VPC       Resource group
    r006-4aaec88f-4986-4b7c-a737-401f7fef1555   kube-clusterID                       15      0         my-vpc   default
    
  6. 在與叢集相同的 VPC 中建立自訂安全群組。 您可以使用此安全群組,透過新增安全群組規則來控制對檔案共用的存取權。

    ibmcloud is security-group-create my-custom-security-group VPC-ID
    
  7. 建立您自己的儲存空間類別,並輸入您先前建立之自訂安全群組的 ID。 從這個儲存類別建立的所有 PVC 都在您的自訂安全群組中。

  8. 建立使用您自己的儲存空間類別的 PVC。

      apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: pvc-custom-vni
      spec:
        accessModes:
        - ReadWriteMany
        resources:
          requests:
            storage: 10Gi
        storageClassName: "" # For example: my-custom-storage-class
    
  9. 在 PV 連結至 PVC 之後,請取得 PV 詳細資料,並記下 nfsServerPath 值,以尋找 VNI IP 位址。

    kubectl get pv pvc-XXXX -o yaml | grep nfsServerPath
    

    輸出範例

    nfsServerPath: XXX.XX.XX.XXX:/XX # VNI IP address
    

限制對一個工作者節點上 Pod 的檔案共用存取權

  1. 確保您已 完成先決條件

  2. 將下列規則新增至您先前建立的自訂安全群組。

    ibmcloud is sg-rulec CUSTOM-SG inbound tcp --port-min 111 --port-max 2049 --remote 10.240.0.20 # Worker node IP
    
  3. 將下列規則新增至 kube-clusterID 安全群組。

    ibmcloud is sg-rulec kube-<cluster-id> outbound tcp --port-min 111 --port-max 2049 --remote 10.240.0.10 # VNI IP
    
  4. 建立使用 PVC 的部署。 只有部署在符合您建立之規則的工作者節點上的 Pod 才能裝載或使用 PVC。 部署在其他節點上的 Pod 會停留在儲存器 creating 狀態。

限制檔案共用存取單一區域中工作者節點上的 Pod

  1. 確保您已 完成先決條件

  2. 將下列規則新增至您先前建立的自訂安全群組。

    ibmcloud is sg-rulec CUSTOM-SG inbound tcp --port-min 111 --port-max 2049 --remote 10.240.0.0/24 # zone subnet cidr range
    
  3. 將下列規則新增至 kube-clusterID 安全群組。 指定虛擬網路介面 (VNI) 的 IP 位址。

    ibmcloud is sg-rulec kube-<cluster-ID> outbound tcp --port-min 111 --port-max 2049 --remote 10.240.0.10 # VNI IP
    
  4. 建立使用 PVC 的部署。 只有在前一個規則所列出的區域中部署的 Pod 才能裝載 PVC。 部署在其他區域中的 Pod 無法存取 PVC,且停留在容器 creating 狀態。

限制對單一工作者節點儲存區中工作者節點上 Pod 的檔案共用存取權

  1. 確保您已 完成先決條件

  2. 為每個工作池子網路範圍建立入站規則。

    ibmcloud is sg-rulec CUSTOM-SG inbound tcp --port-min 111 --port-max 2049 --remote 10.240.0.0/24 # zone 1 subnet cidr range
    ibmcloud is sg-rulec CUSTOM-SG inbound tcp --port-min 111 --port-max 2049 --remote 10.240.1.0/24 # zone 2 subnet cidr range
    
  3. 將下列規則新增至 kube-clusterID 安全群組。 指定虛擬網路介面 (VNI) 的 IP 位址作為遠端或來源。

    ibmcloud is sg-rulec kube-<cluster-ID> outbound tcp --port-min 111 --port-max 2049 --remote 10.240.0.10 # VNI IP
    
  4. 部署使用您先前建立之 PVC 的應用程式。 只有先前規則中指出的工作者節點儲存區上的 Pod 才能裝載 PVC。 或者,如果您在 daemonset 中部署應用程式,則 Pod 只會順利部署在您為其建立安全群組規則的工作者節點上。 不在所指定工作者節點儲存區中的工作者節點儲存區上的 Pod 失敗,並發生 MountVolume.SetUp failed for volume "pvc-184b8c92-33ea-4874-b2ac-17665e53c060" : rpc error: code = DeadlineExceeded desc = context deadline exceeded 錯誤。

限制對多個工作者節點儲存區中工作者節點上 Pod 的檔案共用存取權

  1. 確保您已 完成先決條件

  2. 將下列規則新增至自訂安全群組。 指定工作者節點儲存區及子網路 CIDR 範圍作為遠端或來源。

    ibmcloud is sg-rulec CUSTOM-SG inbound tcp --port-min 111 --port-max 2049 --remote 10.240.1.0/24 # worker pool 1, zone 1 subnet CIDR range
    ibmcloud is sg-rulec CUSTOM-SG inbound tcp --port-min 111 --port-max 2049 --remote 10.240.1.0/24 # worker pool 1, zone 2 subnet CIDR range
    ibmcloud is sg-rulec CUSTOM-SG inbound tcp --port-min 111 --port-max 2049 --remote 10.241.0.0/24 # worker pool 2, zone 1 subnet CIDR range
    ibmcloud is sg-rulec CUSTOM-SG inbound tcp --port-min 111 --port-max 2049 --remote 10.241.1.0/24 # worker pool 2, zone 2subnet CIDR range
    
  3. 將下列規則新增至 kube-<clusterID> 安全群組。 指定虛擬網路介面 (VNI) 的 IP 位址作為遠端或來源。

    ibmcloud is sg-rulec kube-<clusterID> outbound tcp --port-min 111 --port-max 2049 --remote 10.240.1.7 # VNI-IP
    
  4. 建立使用 PVC 的部署。 只有部署在規則所指出區域中的 Pod 才能裝載 PVC。 部署在受限工作者節點上的 Pod 停留在容器 creating 狀態。