How to mount pod container path which is having data (I need this data)to my local host path in kubernetes

5/7/2020

I have pod (kind:job) which is having some code build files under "/usr/src/app" and these files I need in my local k8s host.

But when I am trying to do as per below yamls, I am not able to see any data in mounted host path which is actually exists in pod container ("/usr/src/app"). I think mounting is overwriting/hide that data. Please help me to get in my local k8s host.

My files are :-

apiVersion: batch/v1
kind: Job
metadata:
  name: wf
spec:
  template:
    spec:
      containers:
      - name: wf
        image: 12345678.dkr.ecr.ap-south-1.amazonaws.com/eks:ws
        volumeMounts:
          - name: wf-persistent-storage
            mountPath: /usr/src/app    # my data is in (/usr/src/app)
      volumes:
        - name: wf-persistent-storage
          # pointer to the configuration of HOW we want the mount to be implemented
          persistentVolumeClaim:
            claimName: wf-test-pvc
      restartPolicy: Never
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wf-test-pvc
spec:
  storageClassName: mylocalstorage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local
spec:
  storageClassName: mylocalstorage
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/root/mnt/"
    type: DirectoryOrCreate
-- me25
docker
kubernetes
node.js

1 Answer

5/7/2020

Don't mount your /usr/src/app. As it will get overwritten by the contents of the PVC. In your case the pvc is empty initially so all the files will be deleted.

Try with the below code, where you will be mounting /tmp on the pvc and using command the files will be copied to the pvc.

apiVersion: batch/v1
kind: Job
metadata:
  name: wf
spec:
  template:
    spec:
      containers:
      - name: wf
        image: 12345678.dkr.ecr.ap-south-1.amazonaws.com/eks:ws
        command:
        - bash 
        - -c
        - cp -R /usr/src/app/* /tmp/ 
        volumeMounts:
          - name: wf-persistent-storage
            mountPath: /opt    # my data is in (/usr/src/app)
      volumes:
        - name: wf-persistent-storage
          # pointer to the configuration of HOW we want the mount to be implemented
          persistentVolumeClaim:
            claimName: wf-test-pvc
      restartPolicy: Never
-- Mandeep Singh
Source: StackOverflow