<p>I have deployed (<em>using Helm</em>) some services in the <strong>K8s cluster</strong> hosted by <strong>Docker Desktop</strong> (<em>MacOS</em>). One of the &quot;services&quot; is MongoDB, for which I'm trying to set up a PersistedVolume, so that the actual data will be retained in a MacOS local directory between cluster (re)installs (or MongoDB pod replacements). Everything &quot;works&quot; per se, but the MongoDB container process keeps setting up its local directory /data/db, as if nothing is really setup in terms of Persistent Volumes. I've been pulling my hair for a while now and thought an extra set of eyes might spot whats wrong or missing.</p>
<p>I have several other resources deployed, e.g a small Micronaut based backend service which exposes an API to read from the MongoDB instance. All of that works just fine.</p>
<p>Here are the descriptors involved for MongoDB:</p>
<p><strong>PersistentVolumeClaim</strong>:</p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: persons-mongodb-pvc
  namespace: fo
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
</code></pre>
<p><strong>Deployment</strong>:</p>
<pre><code>kind: Deployment
metadata:
  name: fo-persons-mongodb
  namespace: fo
  labels:
    app: fo-persons-mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: fo-persons-mongodb
  template:
    metadata:
      labels:
        app: fo-persons-mongodb
    spec:
      volumes:
        - name: fo-persons-mongodb-volume-pvc
          persistentVolumeClaim:
            claimName: persons-mongodb-pvc
      containers:
        - name: fo-persons-mongodb
          image: mongo
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: fo-persons-mongodb-volume-pvc
              mountPath: &quot;/data/db&quot;
</code></pre>
<p><strong>Service:</strong></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: fo-persons-mongodb
  namespace: fo
spec:
  type: ClusterIP
  selector:
    app: fo-persons-mongodb
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017
</code></pre>
<p><strong>StorageClass:</strong></p>
<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
</code></pre>
<p><strong>PersistentVolume:</strong></p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: fo-persons-mongodb-volume
  labels:
    type: local
spec:
  storageClassName: local-storage
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /Users/mike/kubernetes/fo/persons/mongodb
</code></pre>

Pandion
<p>Alright! I got it working. Seems I'd made two mistakes. Below are the updated descriptors for the PersistentVolumeClaim and PersistentVolume:</p>
<p><strong>Error #1:</strong> <em>Not setting the storageClassName in the spec of the PersistentVolumeClaim:</em></p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: persons-mongodb-pvc
  namespace: fo
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  storageClassName: local-storage
</code></pre>
<p><strong>Error #2:</strong> <em>Not setting the node affinity and not using local.path instead of hostPath, both in the PersistentVolume:</em></p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: fo-persons-mongodb-volume
  labels:
    type: local
spec:
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  local:
    path: /Users/mike/kubernetes/fo/persons/mongodb
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: kubernetes.io/hostname
              operator: In
              values:
                - docker-desktop
</code></pre>
