How do I run "ls" and then print the output to the console?

8/7/2020

I am testing a connection between a persistent volume and a kubernetes pod by running busybox, but am getting "can't open" "no such file or directory. In order to do further testing, I tried running

echo ls /mntpoint/filename

This is obviously not the correct command. I have tried a few other iterations - too many to list here.

I want to run ls of the mountpoint and print to the console. How do I do this?

EDIT

My code was closest to Rohit's suggestion (below), so I made the following edits, but the code still does not work. Please help.

Persistent Volume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: data
  labels:
    type: local
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: "/mnt/data"
  storageClassName: test

Persistent Volume Claim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: data
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: test

Pod

apiVersion: v1
kind: Pod
metadata:
  name: persistent-volume
spec:
  containers:
    - name: busybox
      command: ['tail', '-f', '/dev/null']
      image: busybox
      volumeMounts:
        - name: data
          mountPath: "/data"
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: data

EDIT 2

So, after taking a day off, I came back to my (still running) pod and the command (ls) worked. It works as expected on any directory (e.g. "ls /" or "ls /data").

My current interpretation is that I did not wait long enough before running the command - although that does not seem to explain it since I had been monitoring with "kubectl describe pod <pod>." Also I have run the same test several times with short latency between the "apply" and "exec" commands and the behavior has been consistent today.

I am going to continue to keep playing with this, but I think the current problem has been solved. Than you!

-- user3877654
busybox
kubernetes
persistent-volumes

2 Answers

8/7/2020

We cannot directly access volume mount on the POD without create a claim. You are missing some steps here.

  1. Create a Persistent Volume. I think you have done this par.
  2. Create Persistent Volume Claim. This will bind your claim to your volume.
  3. Attach your Persistent Volume Claim to the pound.

Once these steps are done you can access the files from your volume in the pod.

Go through the link for detailed information and steps.

-- Rohit
Source: StackOverflow

8/8/2020

Steps you need to follow when dealing with volumes and Kubernetes resources:

  1. Create a Persistent volume.
  2. Create a Persistent volume claim and make sure the state is bound.
  3. Once the PV and PVC are bounded, try to use the PV from a pod/deployment through PVC.
  4. Check the logs of the pod/deployment. You might see the entry of command execution.

Reference: https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/

Hope this helps, please try to elaborate more and paste the logs of above-mentioned steps.

-- Venkata Sai Hitesh
Source: StackOverflow