在应用中添加 File Storage for VPC

Red Hat OpenShift on IBM Cloud 提供了预定义的存储类,您可以使用这些存储类为集群中的应用程序配置 File Storage for VPC。 每个存储类都指定了您所配置的文件共享的可用容量、IOPS、文件系统和保留策略。 您还可以根据自己的使用场景创建自定义存储类。

在继续之前,请查看以下说明和注意事项。

  • 使用存储类配置了特定类型的存储后,您将无法更改该存储设备的类型或保留策略。 但是,如果要增加存储容量和性能,可以 更改大小IOPS。 若要更改存储的类型和保留策略,您必须创建一个新的存储实例,并将旧存储实例中的数据复制到新存储实例中。
  • 如果群集和 VPC 分属不同的资源组,则在配置文件共享之前,必须创建自己的存储类,并在 resourceGroup 部分提供 VPC 资源组 ID,以及 kube-<clusterID> securityGroupIDs 部分提供安全组 ID。 要检索安全组 ID,请执行以下操作。 有关详细信息,请参阅 创建自己的存储类
  • 4.11 及更高版本的群集引入了新的安全组规则。 这些规则更改意味着您必须先同步安全组,然后才能使用File Storage for VPC。 有关详细信息,请参阅For more information, see 添加File Storage for VPC到应用程序
  • 版本中添加了新的存储类别2.0的附加组件。 您无法再配置使用旧存储类别的新文件共享。 使用较旧存储类别的现有卷可以继续运行,但无法扩展使用较旧类别创建的卷。 有关详细信息,请参阅 迁移到新的存储类别
  • 不支持使用 StorageClassSecrets 不支持创建 PVC。
  • 确保创建群集的用户拥有 VPC 基础架构服务的阅读器、写入器和操作员权限。
  • 如果计划在文件共享上使用加密,请确保将 设置服务授权 从 VPC Infrastructure 设为 KMS/HPCS。

IBM Cloud 正在将专用密钥管理服务从 Hyper Protect Crypto Services 迁移至 Key Protect Dedicated。

从 4.11 及更高版本开始引入了新的安全组规则。 这些规则变更意味着您必须先同步安全组,才能使用 File Storage for VPC。 如果您的集群最初是在 4.11 或更早版本创建的,请运行以下命令以同步安全组设置。

  1. 获取您的集群 ID。
    ibmcloud oc cluster ls
    
  2. 获取 kube-<clusterID> 安全组的标识。
    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. 获取集群标识。

    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. 创建共享挂载目标,并指定 kube-<clusterID> 安全组 ID,该 ID 是您之前在 --vni-sgs 选项中获取的。

    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,将文件共享附加到应用程序 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,IOPS 最高可达 35000,默认吞吐量为 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 参数用于在基础架构级别设置文件共享的初始用户和组标识符。 有关补充 ID 在 VPC 文件存储中的工作原理的背景信息,请参阅《 VPC 文件共享的补充 ID 》。

  1. 创建您自己的存储类,并指定要用于应用程序的组标识或用户标识。

    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 配置文件,测试版)。

更新容器资源请求和限制

您可以自定义在 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. 列出集群并记下要在其中部署文件存储器的集群标识。

    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. 列出安全组并记下集群的标识 kube-<clusterID> 安全组。 稍后在添加安全组规则时需要安全组标识。

    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. 创建您自己的存储类,并输入先前创建的定制安全组的标识。 从此存储类创建的所有 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 状态。