0

I am trying to mount a specific file from a folder containing multiple files & folders of a kubernetes pod into the host machine's path as a backup.

I am using PersistentVolumeClaim as a volume and am able to mount the complete folder instead of that specific file. And this folder is accessible via the host machine's path.

Following are my deployment, PVC and PV files.

Deployment file:

containers:
- name: katana-db
  image: postgres:10.4
  imagePullPolicy: IfNotPresent
  envFrom:
  - configMapRef:
      name: war-config
  volumeMounts:
  - mountPath: /var/lib/postgresql/data
    name: katana-db
volumes:
- name: katana-db
  persistentVolumeClaim:
    claimName: katana-db-volume-claim

Persistent Volume Claim:

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: katana-db-volume-claim
  namespace: {{ $.Values.namespace }}
  creationTimestamp: null
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  volumeName: "katana-db-volume"
---

Persistent Volume:

---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: katana-db-volume
  namespace: {{ $.Values.namespace }}
spec:
  storageClassName: manual
  capacity:
    storage: 2Gi
  persistentVolumeReclaimPolicy: Retain
  accessModes:
  - ReadWriteMany
  nfs:
    path: /mnt/k8sMount/data/local-persist/data/warrior/katana-db
    server: 167.xx.xx.xx
---

I want to mount the file "/var/lib/postgresql/data/config.ini" instead of the complete folder "/var/lib/postgresql/data" that I mentioned in the deployment file so that I can have a backup of this file in my host machine.

I found "hostpath" volume but it seems to be unfit for production as suggested by kubernetes forums.

Please let me know the volume that I can use to mount a specific file from a folder within a Pod so that I can access it via the host machine.

Bhavani Prasad
  • 1,079
  • 1
  • 9
  • 26
  • Possible duplicate of https://stackoverflow.com/questions/33415913/whats-the-best-way-to-share-mount-one-file-into-a-pod – vulpxn Apr 14 '20 at 18:47
  • Does this answer your question? [What's the best way to share/mount one file into a pod?](https://stackoverflow.com/questions/33415913/whats-the-best-way-to-share-mount-one-file-into-a-pod) – acid_fuji Apr 15 '20 at 07:09
  • The mounted file (config.ini) is going to be updated in the Pod so I need the updated file to be available in the host machine. Please let me know the changes that I need to add, to make the file available in the host machine – Bhavani Prasad Apr 15 '20 at 08:24
  • To understand correctly, you want to mount the file into specific location and avoid overwriting the folders/files that are already there? – acid_fuji Apr 15 '20 at 12:35
  • I want to mount a file from a specific location in the host machine into a pod's location. The pod will overwrite this file and I need the updated file to be available from the host machine path. – Bhavani Prasad Apr 15 '20 at 16:58
  • The application overwrites a specific file (config.ini) present in Pod's folder containing multiple files. I just want to backup this file (config.ini) from the host machine. – Bhavani Prasad Apr 15 '20 at 17:09
  • Having a mounted file is different than having a copy of that file. What is your desired state here? Does it has to be on the host? Can it be mounted on the NFS server? – acid_fuji Apr 16 '20 at 10:55
  • I need the copy of the file to be available on host. Yes it can be mounted on NFS and am already using nfs in the PV that I mentioned in the query but not sure how I can do it for a file alone – Bhavani Prasad Apr 16 '20 at 16:25
  • I think I understand it now, I will prepare an answer for you – acid_fuji Apr 17 '20 at 08:06

1 Answers1

0

If your goal is to have the file mounted to the host file system than the hostPath is the the way to but the main drawback of using it is that when your pod will be terminated it might be scheduled into different node. In that case the data would be lost. You could workaround this with setting appropriate label to the node if that is so important.

To mount a file instead of directory you can use subPath like in this example:

    volumeMounts:
    - name: test
      mountPath: /var/lib/postgresql/data/config.ini
      subPath: config.ini

You need to use mountPath with directory and filename and subPath field with file name as well.

Instead of hostPath you could use your NFS server and if you must have it on the host you can mount the nfs into it. With this you can be sure that the contents of this volume are preserved.

And then just mount the server like in this example:

    volumeMounts:
    - name: test
      mountPath: /var/lib/postgresql/data/config.ini
      subPath: config.ini

  volumes:
    - name: nfs-volume
      nfs: 
        server: nfs.example.com # Change this to your NFS server
        path: /data # Change it to something relevant for your case

Let me know if it helps.

acid_fuji
  • 6,287
  • 7
  • 22