Kubernetes) subdirectories not mounted when creating pod

8/27/2021

I am working with kubernetes. I want to mount the local(host) volume in the container.

But it doesn't mount the subdirectories inside the directory.

This is my job.yaml file.

apiVersion: v1
kind: Pod
metadata:
    name: ts-engine
spec:
    containers:
    - image: ts_engine:latest
      name: ts-engine
      imagePullPolicy: Never
      command: ["/bin/bash", "-c","cd workspace && ls && echo '1234'&& cd .. && ls"]
      volumeMounts:
      - mountPath: /workspace
        name: workspace
    volumes:
    - name: workspace
      hostPath:
        path: /workspace
        type: Directory

The logs for this pod are like this:

1234
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
workspace

It shows that the "workspace" directory is empty but actually it has some directories in "workspace".

Is there any idea for this situation?

-- GiwoongLee
kubernetes
kubernetes-pod

1 Answer

8/27/2021

The output of your "ls" command shows that you are not actually working in the directory "workspace". You are at the top level (/), and are listing the directory not its contents. In other words, your cd command isn't doing what you think.

To really be able to answer this, we would need to know more about your setup, but here is my best guess:

  • You are probably running Minikube, or some other local kubernetes cluster.
  • This runs it its own virtual machine, which does not have access to the directories on your local machine by default
  • As @David Maze noted, hostPath: mounts a directory from the local node, so this is only really usable if you are running kubernetes locally in development
  • Your Kubernetes cluster is probably in an isolated virtual machine, so the mount fails
-- Alejandro703
Source: StackOverflow