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.254.204.64
---
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.
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.