Create and use VolumePlacementStrategies
This topic explains how to create and apply a VolumePlacementStrategy custom resource (CR) in your cluster. You can use this strategy to influence the placement of volumes created by Portworx for specific workloads.
For more information on Volume Placement Strategies, see Volume Placement Strategies.
Create a VolumePlacementStrategy custom resource
-
Create a YAML file containing the following common fields. All VolumePlacementStrategy CRDs use these fields:
apiVersionasportworx.io/v1beta2kindasVolumePlacementStrategymetadata.namewith the name of your strategy
Add any of the following affinity or antiaffinity sections to the
spec:The following example adds a volumeAffinity rule to colocate Postgres volumes for performance:
apiVersion: portworx.io/v1beta2kind: VolumePlacementStrategymetadata:name: postgres-volume-affinityspec:volumeAffinity:- matchExpressions:- key: appoperator: Invalues:- postgres -
Save and apply your spec with the
kubectl applycommand:kubectl apply -f yourVolumePlacementStrategy.yaml
Use the strategy in storage configurations
After you create your VolumePlacementStrategy, you can use it with your StorageClass or reference it directly in a PersistentVolumeClaim (PVC).
Use with a StorageClass
You can associate your VolumePlacementStrategy with a StorageClass and then reference that StorageClass in your PVC.
-
Create a StorageClass named
storageclass_volumestrategy.yaml.
This must include a reference to theVolumePlacementStrategyyou created in the Create a VolumePlacementStrategy custom resource step above by using theplacement_strategyparameter and specifying the name of your VolumePlacementStrategy:apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:name: postgres-storage-classprovisioner: pxd.portworx.comparameters:placement_strategy: "postgres-volume-affinity" -
Save and apply your StorageClass:
kubectl apply -f storageclass_volumestrategy.yaml -
Create a PVC that uses the
storageClass:kind: PersistentVolumeClaimapiVersion: v1metadata:name: postgres-pvcspec:storageClassName: postgres-storage-classaccessModes:- ReadWriteOnceresources:requests:storage: 2Gi -
Save and apply your PVC:
kubectl apply -f yourPVC.yaml
Reference directly in a PVC
You can also reference your VolumePlacementStrategy directly in the PVC using an annotation.
-
Create a PVC which references the
VolumePlacementStrategyyou created in the Create a VolumePlacementStrategy custom resource step above by specifyingplacement_strategyas an annotation with the name of yourVolumePlacementStrategy:kind: PersistentVolumeClaimapiVersion: v1metadata:annotations:placement_strategy: "postgres-volume-affinity"name: postgres-pvcspec:storageClassName: postgres-storage-classaccessModes:- ReadWriteOnceresources:requests:storage: 2Gi -
Save and apply your PVC:
kubectl apply -f yourPVC.yaml
After applying the VolumePlacementStrategy, StorageClass, and PVC, Portworx places the volumes according to the rules defined in the strategy. These rules are also followed during volume restores.
For information on the CRD Reference for VolumePlacementStrategies, see VolumePlacementStrategy CRD reference.
KubeVirt VMs and CSI clone
Starting with Portworx Enterprise 3.5.0, KubeVirt RWX Block VMs with only a single cloned root disk provisioned using cloneStrategy: csi-clone and no additional non-cloned data disks do not honor user-defined VPS rules at volume creation time. In this configuration, Portworx applies clone co-location behavior, placing the cloned volume replicas on the same nodes as the source volume.
To allow VPS rules to be honored, use one of the following approaches:
-
Switch to copy-based cloning: Configure the StorageClass or StorageProfile to use copy-based cloning instead of CSI clone.
Annotate the StorageClass:
oc annotate sc <storageClassName> cdi.kubevirt.io/clone-strategy=copy --overwriteOr patch the StorageProfile:
oc patch storageprofile <storage-profile-name> --type=merge -p '{"spec":{"cloneStrategy":"copy"}}' -
Add a blank data disk: Add at least one blank non-cloned data disk to the VM using the same StorageClass. With a non-cloned volume present, Portworx applies VPS rules during provisioning. Add the following entry to
spec.dataVolumeTemplatesin the VM spec:- metadata:name: <data-disk-name>annotations:cdi.kubevirt.io/storage.usePopulator: "false"spec:source:blank: {}storage:accessModes:- ReadWriteManyresources:requests:storage: <size>GistorageClassName: <storageClassName>volumeMode: Block
For more details, see Manage Shared Block Device (RWX Block) for KubeVirt VMs.