Can I rely on volumeClaimTemplates naming convention?

18,432

Based on the statefulset API reference

volumeClaimTemplates is a list of claims that pods are allowed to reference. The StatefulSet controller is responsible for mapping network identities to claims in a way that maintains the identity of a pod. Every claim in this list must have at least one matching (by name) volumeMount in one container in the template. A claim in this list takes precedence over any volumes in the template, with the same name.

So I guess you can rely on it.

Moreover, you can define a storage class to leverage dynamic provisioning of persistent volumes, so you won't have to create them manually.

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

Please refer to Dynamic Provisioning and Storage Classes in Kubernetes for more details.

Share:
18,432

Related videos on Youtube

Gong Yi
Author by

Gong Yi

Updated on June 04, 2022

Comments

  • Gong Yi
    Gong Yi almost 2 years

    I want to setup a pre-defined PostgreSQL cluster in a bare meta kubernetes 1.7 with local PV enable. I have three work nodes. I create local PV on each node and deploy the stateful set successfully (with some complex script to setup Postgres replication).

    However I'm noticed that there's a kind of naming convention between the volumeClaimTemplates and PersistentVolumeClaim. For example

    apiVersion: apps/v1beta1 
    kind: StatefulSet
      metadata:   
         name: postgres
      volumeClaimTemplates:
      - metadata:
          name: pgvolume
    

    The created pvc are pgvolume-postgres-0, pgvolume-postgres-1, pgvolume-postgres-2 .

    With some tricky, I manually create PVC and bind to the target PV by selector. I test the stateful set again. It seems the stateful set is very happy to use these PVC.

    I finish my test successfully but I still have this question. Can I rely on volumeClaimTemplates naming convention? Is this an undocumented feature?

  • JulioHM
    JulioHM almost 5 years
    That's fine and well for dynamic storage provisioning, but what happens when you need to manually provision PVs and assign them to those PVCs. That is quite troubling in my experience. Is there a way to sequentially define which PV names will be used in that PVC template? Otherwise, manual PV provisioning becomes quite a task trying to carefully match .spec.volumeName as each SatetefulSet instance is created only after the previous one is Ready.