<p>I'm trying to mount mongo <code>/data</code> directory on to a NFS volume in my kubernetes master machine for persisting mongo data. I see the volume is mounted successfully but I can see only <code>configdb</code> and <code>db</code> dirs but not their subdirectories. And I see the data is not even persisting in the volume. when I <code>kubectl describe &lt;my_pv&gt;</code> it shows <code>NFS (an NFS mount that lasts the lifetime of a pod)</code> </p>

<p>Why is that so?</p>

<p>I see in kubernetes docs stating that:</p>

<blockquote>
  <p>An nfs volume allows an existing NFS (Network File System) share to be
  mounted into your pod. Unlike emptyDir, which is erased when a Pod is
  removed, the contents of an nfs volume are preserved and the volume is
  merely unmounted. This means that an NFS volume can be pre-populated
  with data, and that data can be “handed off” between pods. NFS can be
  mounted by multiple writers simultaneously.</p>
</blockquote>

<p>I'm using kubernetes version 1.8.3.</p>

<p>mongo-deployment.yml:</p>

<pre><code>apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: mongo
  labels:
    name: mongo
    app: mongo
spec:
  replicas: 3
  selector:
    matchLabels:
      name: mongo
      app: mongo
  template:
    metadata:
      name: mongo
      labels:
        name: mongo
        app: mongo
    spec:
      containers:
        - name: mongo
          image: mongo:3.4.9
          ports:
            - name: mongo
              containerPort: 27017
              protocol: TCP
          volumeMounts:
            - name: mongovol
              mountPath: "/data"
      volumes:
      - name: mongovol
        persistentVolumeClaim:
          claimName: mongo-pvc
</code></pre>

<p>mongo-pv.yml:</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
  labels:
    type: NFS
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: "/mongodata"
    server: 172.20.33.81
</code></pre>

<p>mongo-pvc.yml:</p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 3Gi
  storageClassName: slow
  selector:
    matchLabels:
      type: NFS
</code></pre>

<p>The way I mounted my nfs share on my kubernetes master machine:</p>

<pre><code>1) apt-get install nfs-kernel-server
2) mkdir /mongodata
3) chown nobody:nogroup -R /mongodata
4) vi /etc/exports
5) added the line "/mongodata *(rw,sync,all_squash,no_subtree_check)"
6) exportfs -ra
7) service nfs-kernel-server restart
8) showmount -e ----&gt; shows the share
</code></pre>

<p>I logged into the bash of my pod and I see the directory is mounted correctly but data is not persisting in my nfs server (kubernetes master machine). </p>

<p>Please help me see what I am doing wrong here.</p>

<p>It's possible that pods don't have permission to create files and directories. You can <code>exec</code> to your pod and try to <code>touch</code> a file in NFS share if you get permission error you can ease up permission on file system and <code>exports</code> file to allow write access.</p>

<p>It's possible to specify <code>GID</code> in PV object to avoid permission denied issues.
<a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#access-control" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#access-control</a></p>
