Search code examples
azurekubernetespersistent-volumeskubernetes-pvc

Can I link a K8s deployment to PVCs by label or selector instead of name?


I have a deployment in Kubernetes. In this deployment, I can specify a persistent volume claim as follows:

  volumes:
  - name: my-volume
    persistentVolumeClaim:
      claimName: my-claim

I have a disk (an Azure disk to be precise) with lots of preprocessed data, which I can expose in Kubernetes as PVC with name my-claim. In the next step I link it to the deployment as shown above. The problem with this approach is, that I cannot scale the deployment to more than one pod.

How can I scale this setup? I tried to duplicate the disk and create it as second PVC with a different name. This worked, but now I don't see a way to tell the Kubernetes deployment, that each pod should mount one of these two PVCs.

I hope there is an option to mark both PVCs with a common label and then link my deployment to this label instead of the PVC name. Is something like this out there or is my approach completely wrong?

Thanks!


Solution

  • I have a disk (an Azure disk to be precise) with lots of preprocessed data, which I can expose in Kubernetes as PVC with name my-claim. In the next step I link it to the deployment as shown above. The problem with this approach is, that I cannot scale the deployment to more than one pod.

    Here, you use a PersistentVolumeClaim with Access Mode ReadWriteOnce (that's the only option for Azure Disks, see access mode link)

    How can I scale this setup? I tried to duplicate the disk and create it as second PVC with a different name. This worked, but now I don't see a way to tell the Kubernetes deployment, that each pod should mount one of these two PVCs.

    Here, it sounds like you want a volume with access mode ReadOnlyMany - so you need to consider a storage system that support this access mode.

    I tried to duplicate the disk and create it as second PVC with a different name. This worked, but now I don't see a way to tell the Kubernetes deployment, that each pod should mount one of these two PVCs.

    This does not work for a Deployment because the template for each pod is identical. But you can do this with StatefulSet, declaring your PVC with volumeClaimTemplates - then the PVC for each pod has in unique, well known identity.

    Example part of StatefulSet:

      volumeClaimTemplates:
      - metadata:
          name: my-pvc
        spec:
          accessModes: [ "ReadWriteOnce" ]
          storageClassName: "my-storage-class"
          resources:
            requests:
              storage: 1Gi
    

    Then if you have two replicas of you StatefulSet, they will you a PVC named my-pvc-0 and my-pvc-1 where the number is called "ordinal". The volumeClaimTemplate only creates a new PVC if it does not exists, so if you have created PVCs with the correct names - the existing will be used.

    Alternative Storage Solutions

    An alternative storage solution to Azure Disk is Azure Files. Azure Files support access mode ReadWriteOnce, ReadOnlyMany and ReadWriteMany. See Dynamically create and use a persistent volume with Azure Files in Azure Kubernetes Service (AKS).

    There may also be other storage alternatives that better fit your application.