<p>I want to setup a pre-defined PostgreSQL cluster in a bare meta kubernetes 1.7 with <a href="https://kubernetes.io/docs/concepts/storage/volumes/#local" rel="nofollow noreferrer">local PV</a> 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).</p>
<p>However I'm noticed that there's a kind of naming convention between the volumeClaimTemplates and PersistentVolumeClaim.
For example</p>
<pre><code>apiVersion: apps/v1beta1 
kind: StatefulSet
  metadata:   
     name: postgres
  volumeClaimTemplates:
  - metadata:
      name: pgvolume
</code></pre>
<p>The created pvc are <code>pgvolume-postgres-0</code>, <code>pgvolume-postgres-1</code>, <code>pgvolume-postgres-2</code> .</p>
<p>With some <a href="https://docs.openshift.org/3.11/install_config/persistent_storage/selector_label_binding.html" rel="nofollow noreferrer">tricky</a>, 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.</p>
<p>I finish my test successfully but I still have this question. Can I rely on volumeClaimTemplates naming convention? Is this an undocumented feature?</p>

Gong Yi
<p>Based on the statefulset <a href="https://v1-7.docs.kubernetes.io/docs/api-reference/v1.7/#statefulset-v1beta1-apps" rel="noreferrer">API reference</a> </p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>So I guess you can rely on it. </p>

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

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

<p>Please refer to <a href="http://blog.kubernetes.io/2017/03/dynamic-provisioning-and-storage-classes-kubernetes.html" rel="noreferrer">Dynamic Provisioning and Storage Classes in Kubernetes</a> for more details.</p>
