<p>Let's say I have a <code>StatefulSet</code> definition</p>

<pre><code>apiVersion: v1
kind: StatefulSet
metadata:
  name: web
spec:
  ...
  volumeClaimTemplates:
  — metadata:
      name: www
    spec:
      resources:
        requests:
          storage: 1Gi
</code></pre>

<p>This will create me a <code>PersistentVolumeClaim</code> (PVC) with a <code>PersistentVolume</code> (PV) of 1 GiB for each pod.</p>

<p>How can I write something like this</p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: www
spec:
  ...
  resources:
    requests:
      storage: 1Gi
...
</code></pre>

<p>and connect it with the <code>StatefulSet</code> in a way that it still creates a PVC and PV for each pod?</p>

LEQADA
<p>I am guessing that in your question you are using statfulset example from <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#components" rel="nofollow noreferrer">this website</a> so I will follow its naming convention.</p>

<p>The solution I am about to present you was tested by myself and it seems to work.</p>

<p>In <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#statefulsetspec-v1-apps" rel="nofollow noreferrer">k8s api reference</a> you can find the folllowing definition:</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 this means that as long as you have volumeclaim with specific name, staefulset will use it without creating a new one. This means that you can create some pv/pvc manually and statefulset will use them.</p>

<p>All you need to do is to correctly name your pvcs. How is this name supposed to look like?
Here is the first part:</p>

<pre><code>volumeClaimTemplates:
  - metadata:
      name: www        &lt;-here is the first part
</code></pre>

<p>and the second part is a pod name.</p>

<p>(Have a look at this Stack question on <a href="https://stackoverflow.com/questions/46442238/can-i-rely-on-volumeclaimtemplates-naming-convention">can-i-rely-on-volumeclaimtemplates-naming-convention</a>.)</p>

<p>These two parts combined together create a name of pvc (separated with dash) e.g. </p>

<pre><code>www-web-0        &lt;- this is how you are supposed to name one of your pvcs
│   └ second part (pod name)
└ first part
</code></pre>

<p>If you already have (automatically provisioned) PVCs, use</p>

<pre><code>kubectl get pvc &lt;pvcname&gt; -oyaml &gt; pvcname.yaml
kubectl get pv  &lt;pvname&gt;  -oyaml &gt; pvname.yaml
</code></pre>

<p>to save its specification to disk. Then you can run:</p>

<pre><code>kubectl apply -f pvcname.yaml
kubectl apply -f pvname.yaml
</code></pre>

<p>to apply pvc/pv configuration. Remember that some yaml files may require slight modifications before running <code>kubectl apply</code>.</p>
