Home > Net >  Kubernetes PersistentVolume on local machine, share data
Kubernetes PersistentVolume on local machine, share data

Time:10-24

I would like to spin up a Pod on my local machine. Inside the pod is a single container with a .jar file in it. That jar file can take in files, process then, and then output them. I would like to create a PersistentVolume and attach that to the Pod, so the container can accesss the files.

My Dockerfile:

 FROM openjdk:11

 WORKDIR /usr/local/dat

 COPY . .

 ENTRYPOINT ["java", "-jar", "./tool/DAT.jar"]

(Please note that the folder used inside the container is /usr/local/dat)

My PersistentVolume.yml file:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: dat-volume
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 150Mi
  storageClassName: hostpath
  hostPath:
    path: /home/zoltanvilaghy/WORK/ctp/shared

My PersistentVolumeClaim.yml file:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dat-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
  storageClassName: hostpath
  volumeName: dat-volume

My Pod.yml file:

apiVersion: v1
kind: Pod
metadata:
  name: dat-tool-pod
  labels:
    name: dat-tool-pod
spec:
  containers:
  - name: dat-tool
    image: dat_docker
    imagePullPolicy: Never
    args: ["-in", "/usr/local/dat/shared/input/Archive", "-out", "/usr/local/dat/shared/output/Archive2", "-da"]
    volumeMounts:
      - mountPath: /usr/local/dat/shared
        name: dat-volume
  restartPolicy: Never
  volumes:
    - name: dat-volume
      persistentVolumeClaim:
        claimName: dat-pvc
  

If all worked well, after attaching the PersistentVolume (and putting the Archive folder inside the shared/input folder), by giving the arguments to the jar file it would be able to process the files and output them to the shared/output folder.

Instead, I get an error saying that the folder cannot be found. Unfortunately, after the error the container exists, so I can't look around inside the container to check the file structure. Can somebody help me identify the problem?

Edit: Output of kubectl get sc, pvc, pv :

NAME                                             PROVISIONER          RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE



                                                                                                    

storageclass.storage.k8s.io/hostpath (default)   docker.io/hostpath   Delete          Immediate           false                  20d

                                                                                                                                                                                                                                                                                                                                              

NAME                            STATUS   VOLUME       CAPACITY   ACCESS MODES   STORAGECLASS   AGE

                                                                                                                                   

persistentvolumeclaim/dat-pvc   Bound    dat-volume   150Mi      RWO            hostpath       4m52s

                                                                                                                                                                                                                                                                                                                                                                              

NAME                          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM             STORAGECLASS   REASON   AGE

                                                                                                      

persistentvolume/dat-volume   150Mi      RWO            Retain           Bound    default/dat-pvc   hostpath                4m55s

CodePudding user response:

Assumed your sc/pvc/pv are all correct, here's how you can test:

apiVersion: v1
kind: Pod
metadata:
  name: dat-tool-pod
  labels:
    name: dat-tool-pod
spec:
  containers:
  - name: dat-tool
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["ash","-c","sleep 7200"]
    volumeMounts:
    - mountPath: /usr/local/dat/shared
      name: dat-volume
  restartPolicy: Never
  volumes:
  - name: dat-volume
    persistentVolumeClaim:
      claimName: dat-pvc

After the pod is created then you can kubectl exec -it dat-tool-pod -- ash and cd /usr/local/dat/shared. Here you can check the directory/files (incl. permission) to understand why your program complaint about missing directory/files.

CodePudding user response:

For anyone else experiencing this problem, here is what helped me find a solution:

https://github.com/docker/for-win/issues/7023

(And actually the link inside the first comment in this issue.)

So my setup was a Windows 10 machine, using WSL2 to run docker containers and kubernetes cluster on my machine. No matter where I put the folder I wanted to share with my Pod, it didn't appear inside the pod. So based on the link above, I created my folder in /mnt/wsl, called /mnt/wsl/shared.

Because supposedly, this /mnt/wsl folder is where the DockerDesktop will start to look for the folder that you want to share. I changed my PersistentVolume.yml to the following:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: dat-volume
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 150Mi
  storageClassName: hostpath
  hostPath:
    path: /run/desktop/mnt/host/wsl/shared

My understanding is that /run/desktop/mnt/host/wsl is the same as /mnt/wsl, and so I could finally pass files between my Pod and my machine.

  • Related