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/v1beta2
kind: VolumePlacementStrategy
metadata:
name: postgres-volume-affinity
spec:
volumeAffinity:
- matchExpressions:
- key: app
operator: In
values:
- 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/v1
kind: StorageClass
metadata:
name: postgres-storage-class
provisioner: pxd.portworx.com
parameters:
placement_strategy: "postgres-volume-affinity" -
Save and apply your StorageClass:
kubectl apply -f storageclass_volumestrategy.yaml -
Create a PVC that uses the
storageClass:kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgres-pvc
spec:
storageClassName: postgres-storage-class
accessModes:
- ReadWriteOnce
resources:
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: PersistentVolumeClaim
apiVersion: v1
metadata:
annotations:
placement_strategy: "postgres-volume-affinity"
name: postgres-pvc
spec:
storageClassName: postgres-storage-class
accessModes:
- ReadWriteOnce
resources:
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.