Skip to main content
Version: 3.5

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

  1. Create a YAML file containing the following common fields. All VolumePlacementStrategy CRDs use these fields:

    • apiVersion as portworx.io/v1beta2
    • kind as VolumePlacementStrategy
    • metadata.name with 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
  2. Save and apply your spec with the kubectl apply command:

    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.

  1. Create a StorageClass named storageclass_volumestrategy.yaml.
    This must include a reference to the VolumePlacementStrategy you created in the Create a VolumePlacementStrategy custom resource step above by using the placement_strategy parameter 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"
  2. Save and apply your StorageClass:

    kubectl apply -f storageclass_volumestrategy.yaml
  3. 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
  4. 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.

  1. Create a PVC which references the VolumePlacementStrategy you created in the Create a VolumePlacementStrategy custom resource step above by specifying placement_strategy as an annotation with the name of your VolumePlacementStrategy:

    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
  2. 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.