Hey all....quick question on PVC host path. I have a PV set up to use a hostpath . . .

Ryebridge:
Hey all…quick question on PVC host path. I have a PV set up to use a hostpath of “/data” and the PVC is bound but when I use it in my POD where I create a text file as well as sending data via the log simulator but I don’t see the file or logs in the “/data” directory of the worker node ? I know we don’t have to create a PVC (if using hostpath) and we can just specify a volume in the POD using “hostype” (which works fine) but shouldn’t the PVC hostpath do this same thing and write to my “/data” dir ? The PVC is bound so not sure where the problem is. @unnivkn any clue about this one please ?

spec:
containers:

  • name: events
    image: kodekloud/event-simulator
    env:

    • name: LOG_HANDLERS
      value: file
      volumeMounts:
    • name: workdir
      mountPath: “/log”
  • name: hello2
    image: busybox
    command: [‘sh’, ‘-c’, ‘echo hello from busy2; ifconfig -a > work-dir2/config.text; sleep 3600’]
    volumeMounts:

    • name: workdir
      mountPath: “/work-dir2”

volumes:

  • name: workdir
    persistentVolumeClaim:
    claimName: hellopvc

apiVersion: v1
kind: PersistentVolume
metadata:
name: hellopv
labels:
type: local
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: “/data”


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: hellopvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi

unnivkn:
Hi @Ryebridge which lab is this ?

Ryebridge:
Hey man, it’s an updated version of this :slightly_smiling_face: https://kodekloud.com/topic/practice-test-persistent-volume-claims-2/

Ryebridge:
I just added a side car with another command and instead of using the hostpath PV in the POD, I used a PV with a hosttype and PVC

Ryebridge:
@Lisenet Any idea about this one pls ? It’s a variation from the lab, can’t seem to get it to work when consuming as a PVC hostpath. Expectation is to find both the log and the output of that command on one of the worker nodes. It works fine without PVC.

unnivkn:
Hi @Ryebridge please try this:

Ryebridge:
Thank you so much @unnivkn ! I changed it a little to this:

spec:
containers:

  • name: my-busybox-container
    image: busybox
    command: [‘sh’, ‘-c’, ‘echo “hello from busy2” > /log/echobusy2.text; ifconfig -a >> /log/ifconfig.text; sleep 3600’]
    volumeMounts:
    • mountPath: /log
      name: log-volume
      volumes:
  • name: log-volume
    persistentVolumeClaim:
    claimName: hellopvc
    ~
    …so wound up with:

[email protected]:/data# ls -lrt
total 12
-rw-r–r-- 1 root root 17 Mar 6 18:32 echobusy2.text
-rw-r–r-- 1 root root 860 Mar 6 18:32 ifconfig.text
-rw-r–r-- 1 root root 19 Mar 6 18:34 worker1.text

…then jump to worker1 data dir, create a new file, log into busybox /log dir and I see all three :slightly_smiling_face: This is a cool example:

[email protected]:~$ k exec my-busybox -it – sh
/ # cd log
/log # ls
echobusy2.text ifconfig.text worker1.text

Lisenet:
Sorry, I didn’t see it until now.

Ryebridge:
No worries @Lisenet I’m a little unclear about mount sharing. Say for example I have a POD with one sidecar and an init:

apiVersion: v1
kind: Pod
metadata:
labels:
run: hello
name: hello
spec:
containers:

  • name: events
    image: kodekloud/event-simulator
    env:

    • name: LOG_HANDLERS
      value: file
      volumeMounts:
    • name: workdir
      mountPath: “/logevents”
  • name: hello2
    image: busybox
    command: [‘sh’, ‘-c’, ‘echo “hello from busy2” > /logbusy/echobusy2.text; ifconfig -a >> /logbusy/ifconfig.text; sleep 3600’]
    volumeMounts:

    • name: workdir
      mountPath: “/logbusy”

initContainers:

  • name: init
    image: busybox
    command: [‘sh’, ‘-c’, ‘echo “hello from init” > /loginit/init.text; sleep 6’]
    volumeMounts:
    • name: workdir
      mountPath: "/loginit

volumes:

  • name: workdir
    persistentVolumeClaim:
    claimName: hellopvc

If I ssh into the POD, I can see all of the files are created in the “logevents” folder, I was expecting them to land in their respective folders, don’t understand why it picked “logevents” and not one of the other mountpaths ? Is this because it always takes the first POD container and one is sidecar and other is init ? Adding @unnivkn in case (sorry for spam, last few answers would really help) !

unnivkn:
Hi @Ryebridge please create a simple pod with busybox + sleep 100 & login and see what are all mountpoints you can see in it. I believe you need to have respective mount points in the busybox image, else you have to create a custom busybox image with all the container mountpoints you mentioned above. Hope this helps.

Ryebridge:
Thanks @unnivkn Was playing with this and for the first container (kodekloud/event-simulator) it creates a directory if it doesn’t exist and places all 4 files in there if it’s configured like this:

containers:

  • name: events
    image: kodekloud/event-simulator
    env:

    • name: LOG_HANDLERS
      value: file
      volumeMounts:
    • name: workdir
      mountPath: “/johnny”
  • name: hello2
    image: busybox
    command: [‘sh’, ‘-c’, ‘echo “hello from busy2” > /log/echobusy2.text; ifconfig -a >> /log/ifconfig.text; sleep 100’]
    volumeMounts:

    • name: workdir
      mountPath: “/log”

initContainers:

  • name: init
    image: busybox
    command: [‘sh’, ‘-c’, ‘echo “hello from init” > /log/init.text; sleep 10’]
    volumeMounts:
    • name: workdir
      mountPath: “/log”

…so it seems regardless of mountpath name for the others, it will always use the mount path from the first one. The files are also on the /data dir on the worker nodes !
image.png

Lisenet:
@Ryebridge correct me if I’m wrong, but you seem to think that by specifying the mountPath you’re telling the pod to select a directory that is on a PV? That is simply not the case. The mountPath is a path on a pod where you want to mount the PV. It can be anything, e.g. /my/path/is/this , and it would still mount the PV as a volume on it. You can have multiple pods mounting the same volume all on different local paths, these paths are merely mountpoints where a pod can access the PV. E.g. /mnt or /log or /mnt/data/log would all mount the PV as a disk. If you’re familiar with Linux mounts, think of a /dev/sda1 , you can mount it on /mnt or you can mount it on /var/log, the content of the disk won’t change. All files that you write to the disk (or the PV in K8s case) will be stored in the root / of the volume. This is precisely what you’re seeing. Regardless of where you mount the volume on a pod, your files end up in the root / of the volume, e.g. /app.log , /echobusy.text, /ifconfig.text, init.text. I hope this clarifies things for you.

Lisenet:
If you want to create a folder on the volume, then you can do it by creating a directory within the mountPath, e.g. if you mount the volume as /log, then running mkdir /log/log would actually create a /log folder on the volume.

Ryebridge:
Thanks @Lisenet. The POD is using a PVC with hostpath set to the /data on the worker node. All of the files wound up in the “Johnny” folder of the POD when it was created, I was expecting that we’d see the other directories created in the pod. With regard to /data path on node01, files also ended up here too. If on the other hand I change the log path for second and init container, only the kodecloud logs are generated.