16

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?

yishaiz
  • 2,433
  • 4
  • 28
  • 49
Gong Yi
  • 189
  • 1
  • 1
  • 5

1 Answers1

13

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.

Jimmy Lu
  • 348
  • 1
  • 9
  • 7
    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`. – JulioHM May 09 '19 at 11:01
  • @JulioHM https://docs.okd.io/3.11/install_config/persistent_storage/selector_label_binding.html add claimRef to your persistentVolume. It is tricky, I've spent several days to find it. – Max Lapshin Aug 07 '22 at 05:34