<p>I have a <code>kubernetes</code> cluster and I have a simple deployment for <code>mongodb</code> with <code>NFS</code> persistent volume set. It works fine, but since resources like databases are <code>stateful</code> I thought of using <code>Statefulset</code> for the <code>mongodb</code>, but now the problem is, when I go through the documentation, statefulset has <code>volumeClaimTemplates</code> instead of <code>volumes</code> (in deployments).</p>
<p>But now the problem comes.</p>
<p>in a <code>deployment</code> do it like this:</p>
<p><code>PersistentVolume</code> -&gt; <code>PersistentVolumeClaim</code> -&gt; <code>Deployment</code></p>
<p>But how can we do this in <code>Statefulset</code> ?</p>
<p>Is it like:</p>
<p><code>volumeClaimTemplates</code> -&gt; <code>StatefulSet</code></p>
<p>How can I set a <code>PersistentVolume</code> for the <code>volumeClaimTemplates</code>. If we don't use <code>PersistentVolume</code> for  <code>StatefulSet</code>, how does it create he volume and WHERE does it create the volumes? Is in <code>host</code> machines (i.e. kubernetes worker nodes)?</p>
<p>Because I have a separate <code>NFS</code> provisioner I am using for the <code>mongodb</code> deployment (with replicasset=1), how can I use the same setup with <code>StatefulSet</code> ?</p>
<p>Here's the my <code>mongo-deployment.yaml</code> -&gt; which I am going to transform into a statefulset as shown in the second code snippet (<code>mongo-stateful.yaml</code>)</p>
<ol>
<li><code>mongo-deployment.yaml</code></li>
</ol>
<pre><code>&lt;omitted&gt;
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    name: mynfs # name can be anything
spec:
  storageClassName: manual # same storage class as pvc
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: &lt;nfs-server-ip&gt;
    path: &quot;/srv/nfs/mydata&quot; 
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany #  must be the same as PersistentVolume
  resources:
    requests:
      storage: 1Gi          
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-deployment    
  labels:
    name: mongodb
spec:
  selector:
    matchLabels:
      app: mongodb
  replicas: 1
  template:
    metadata:
      labels: 
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo
        ports:
        -  containerPort: 27017
        ... # omitted some parts for easy reading
        volumeMounts:
        - name: data  
          mountPath: /data/db
      volumes: 
        - name: data
          persistentVolumeClaim: 
            claimName: task-pv-claim    
</code></pre>
<ol start="2">
<li><code>mongo-stateful.yaml</code></li>
</ol>
<pre><code>---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    name: mynfs # name can be anything
spec:
  storageClassName: manual # same storage class as pvc
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    server: &lt;nfs-server-ip&gt;
    path: &quot;/srv/nfs/mydata&quot; 
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb-statefulset
spec:
  selector:
    matchLabels:
      name: mongodb-statefulset
  serviceName: mongodb-statefulset
  replicas: 2
  template:
    metadata:
      labels:
        name: mongodb-statefulset
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mongodb
        image: mongo:3.6.4
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: db-data
          mountPath: /data/db
  volumeClaimTemplates:
  - metadata:
      name: db-data
    spec:
      accessModes: [ &quot;ReadWriteOnce&quot; ]
      storageClassName: &quot;manual&quot;
      resources:
        requests:
          storage: 2Gi

</code></pre>
<p>But this is not working (<code>mongo-stateful.yaml</code>) pods are in <code>pending</code> state as when I describe it shows:</p>
<blockquote>
<p>default-scheduler  0/3 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 2 pod has unbound immediate PersistentVolumeClaims</p>
</blockquote>
<p>PS: Deployment works fine without any errors, problem is with Statefulset</p>
<p>Can someone please help me, how to write a statefulset with volumes?</p>

Jananath Banuka
<p>If your storage class does not support dynamic volume provisionning, <strong>you have to manually create PVs and associated PVCs</strong>, using yaml files, then the volumeClaimTemplates will allow to link existing PVCs with your statefulset's pods.</p>
<p>Here is a working example: <a href="https://github.com/k8s-school/k8s-school/blob/master/examples/MONGODB-install.sh" rel="nofollow noreferrer">https://github.com/k8s-school/k8s-school/blob/master/examples/MONGODB-install.sh</a></p>
<p>You should:</p>
<ul>
<li>run it locally on <a href="https://kind.sigs.k8s.io/" rel="nofollow noreferrer">https://kind.sigs.k8s.io/</a>, which support dynamic volume provisionning, so here PVCs and PVs will be created automatically</li>
<li>export PVs and PVCs yaml files</li>
<li>use these yaml file as template to create your PVs and PVCs for your NFS backend.</li>
</ul>
<p>Here is what you will get on Kind:</p>
<pre class="lang-sh prettyprint-override"><code>$ ./MONGODB-install.sh               
+ kubectl apply -f 13-12-mongo-configmap.yaml
configmap/mongo-init created
+ kubectl apply -f 13-11-mongo-service.yaml
service/mongo created
+ kubectl apply -f 13-14-mongo-pvc.yaml
statefulset.apps/mongo created
$ kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
mongo-0   2/2     Running   0          8m38s
mongo-1   2/2     Running   0          5m58s
mongo-2   2/2     Running   0          5m45s
$ kubectl get pvc
NAME               STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
database-mongo-0   Bound    pvc-05247511-096e-4af5-8944-17e0d8222512   1Gi        RWO            standard       8m42s
database-mongo-1   Bound    pvc-f53c35a4-6fc0-4b18-b5fc-d7646815c0dd   1Gi        RWO            standard       6m2s
database-mongo-2   Bound    pvc-2a711892-eeee-4481-94b7-6b46bf5b76a7   1Gi        RWO            standard       5m49s
$ kubectl get pv 
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                      STORAGECLASS   REASON   AGE
pvc-05247511-096e-4af5-8944-17e0d8222512   1Gi        RWO            Delete           Bound    default/database-mongo-0   standard                8m40s
pvc-2a711892-eeee-4481-94b7-6b46bf5b76a7   1Gi        RWO            Delete           Bound    default/database-mongo-2   standard                5m47s
pvc-f53c35a4-6fc0-4b18-b5fc-d7646815c0dd   1Gi        RWO            Delete           Bound    default/database-mongo-1   standard                6m1s
</code></pre>
<p>And a dump of a PVC (generated here by <code>volumeClaimTemplate</code> because odf kind dynamic volume provisionning):</p>
<pre><code>$ kubectl get pvc database-mongo-0 -o yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    pv.kubernetes.io/bind-completed: &quot;yes&quot;
    pv.kubernetes.io/bound-by-controller: &quot;yes&quot;
    volume.beta.kubernetes.io/storage-provisioner: rancher.io/local-path
    volume.kubernetes.io/selected-node: kind-worker2
  creationTimestamp: &quot;2020-10-16T15:05:20Z&quot;
  finalizers:
  - kubernetes.io/pvc-protection
  labels:
    app: mongo
  managedFields:
    ...
  name: database-mongo-0
  namespace: default
  resourceVersion: &quot;2259&quot;
  selfLink: /api/v1/namespaces/default/persistentvolumeclaims/database-mongo-0
  uid: 05247511-096e-4af5-8944-17e0d8222512
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: standard
  volumeMode: Filesystem
  volumeName: pvc-05247511-096e-4af5-8944-17e0d8222512
status:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  phase: Bound
</code></pre>
<p>And the related PV:</p>
<pre><code>kubectl get pv pvc-05247511-096e-4af5-8944-17e0d8222512 -o yaml     
apiVersion: v1
kind: PersistentVolume
metadata:
  annotations:
    pv.kubernetes.io/provisioned-by: rancher.io/local-path
  creationTimestamp: &quot;2020-10-16T15:05:23Z&quot;
  finalizers:
  - kubernetes.io/pv-protection
  managedFields:
    ...
  name: pvc-05247511-096e-4af5-8944-17e0d8222512
  resourceVersion: &quot;2256&quot;
  selfLink: /api/v1/persistentvolumes/pvc-05247511-096e-4af5-8944-17e0d8222512
  uid: 3d1e894e-0924-411a-8378-338e48ba4a28
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: database-mongo-0
    namespace: default
    resourceVersion: &quot;2238&quot;
    uid: 05247511-096e-4af5-8944-17e0d8222512
  hostPath:
    path: /var/local-path-provisioner/pvc-05247511-096e-4af5-8944-17e0d8222512_default_database-mongo-0
    type: DirectoryOrCreate
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - kind-worker2
  persistentVolumeReclaimPolicy: Delete
  storageClassName: standard
  volumeMode: Filesystem
status:
  phase: Bound
</code></pre>
