Can I rely on volumeClaimTemplates naming convention?

Gong Yi picture Gong Yi · Sep 27, 2017 · Viewed 14.3k times · Source

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?

Answer

Jimmy Lu picture Jimmy Lu · Oct 22, 2017

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.