<p>I'm starting out with K8s and I'm stuck at setting up mongo db in replica set mode with local persistent volume. I'm using StorageClass, PersistentVolume and PersistentVolumeClaim.</p>
<pre><code>vincenzocalia@vincenzos-MacBook-Air server-node % kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS    REASON   AGE
mongo-pv   1Gi        RWO            Retain           Available           mongo-storage            24m
</code></pre>
<p>but when inspect the pod I get</p>
<pre><code>  Type     Reason            Age   From               Message
  ----     ------            ----  ----               -------
  Warning  FailedScheduling  2m    default-scheduler  0/1 nodes are available: 1 node(s) didn't find available persistent volumes to bind. preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling.
</code></pre>
<p>This post answer <a href="https://stackoverflow.com/a/70069138/2704032">https://stackoverflow.com/a/70069138/2704032</a> confirmed my suspect that I might be using the wrong label..
So I had a look at the PV and I see that as I've set nodeAffinity as</p>
<pre><code>nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernets.io/hostname
              operator: In
              values:
                - docker-desktop

</code></pre>
<p>it's looking for</p>
<pre><code>Node Affinity:     
  Required Terms:  
    Term 0:        kubernets.io/hostname in [docker-desktop]
</code></pre>
<p>I checked  nodes with <code>kubectl get nodes --show-labels</code>
and it does have that label as the output shows</p>
<pre><code>NAME             STATUS   ROLES           AGE    VERSION   LABELS
docker-desktop   Ready    control-plane   7d9h   v1.24.1   beta.kubernetes.io/arch=arm64,beta.kubernetes.io/os=linux,kubernetes.io/arch=arm64,kubernetes.io/hostname=docker-desktop,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=


</code></pre>
<p>I tried using volumeClaimTemplate in the StatefulSet as</p>
<pre><code>volumeClaimTemplates:
    - metadata:
        name: mongo-vctemplate
      spec:
        storageClassName: mongo-storage
        accessModes: 
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi  
</code></pre>
<p>but it didn't make a difference..I also tried to specify the pic in the pv with the claimRef parameter but still the insidious error comes up at pod creation..</p>
<p>What else can I check or do I need to setup?
Many thanks as usual
Here are my yaml files</p>
<h1>StorageClass</h1>
<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: mongo-storage
provisioner: kubernetes.io/no-provisioner
# volumeBindingMode: Immediate
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Retain
</code></pre>
<h1>PV</h1>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
  labels:
    type: local
spec:
  capacity:
    storage: 1Gi
  # persistentVolumeReclaimPolicy: Retain # prod
  persistentVolumeReclaimPolicy: Delete # local tests
  storageClassName: mongo-storage
  # claimRef:
  #   name: mongo-pvc
  accessModes:
    - ReadWriteOnce
  # volumeMode: Filesystem #default if omitted
  # hostPath:
  #   path: /mnt/data
  local:
    path: /mnt/disk/vol1
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernets.io/hostname
              operator: In
              values:
                - docker-desktop


</code></pre>
<h1>PVC</h1>
<pre><code>piVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-pvc
spec:
  storageClassName: mongo-storage
  # volumeName: mongo-pv # this will make it unbundable???
  resources:
    requests:
      storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
</code></pre>
<h1>StatefulSet</h1>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo-statefulset
spec:
  selector:
    matchLabels:
      app: mongo-pod # has to match .spec.template.metadata.labels
  serviceName: mongo-clusterip-service
  replicas: 1 # 3 
  template:
    metadata:
      labels:
        app: mongo-pod # has to match .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo-container
          image: mongo
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-pv-cont
              mountPath: /data/db #/mnt/data
      volumes:
        - name: mongo-pv-cont    
          persistentVolumeClaim:
            claimName: mongo-pvc
</code></pre>

<p>It is a typo in <code>kubernets.io/hostname</code>. It should be <code>kubernetes.io/hostname</code> in the pv definition.</p>
<p>similar to this one:
<a href="https://stackoverflow.com/questions/52183750/error-while-using-local-persistent-volumes-in-statefulset-pod">Error while using local persistent volumes in statefulset pod</a></p>
