Gopinath Rajee
Gopinath Rajee

Reputation: 450

MountPath is not listing the files

All,

I deployed the below yaml file and it got deployed. I'm in the process of learning about Volume, VolumeMount and MountPath.

I see the containerdrive folder specified in the mountpath in the container (I did a kubectl exec -it podname) and in the containerdrive I was hoping to see the files in the folder "/home/rgn/kubernetes/scripts" specified in the Path of HostPath. But that is not the case and infact the containerdrive is empty.

Looks like my understanding of Volume, VolumeMount and MountPath is wrong. What am I missing? Can someone point me in the right direction?

Thanks, rgn

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.15.4
        ports:
        - containerPort: 80
        volumeMounts:
          - mountPath: /containerdrive
            name: test-volume
      volumes:
      - name: test-volume
        hostPath:
      # directory location on host
          path: /home/rgn/kubernetes/scripts
      # this field is optional
      #    type: DirectoryOrCrate

Upvotes: 1

Views: 2253

Answers (1)

Nick
Nick

Reputation: 1948

Looks like my understanding of Volume, VolumeMount and MountPath is wrong. What am I missing? Can someone point me in the right direction?

     hostPath:
   # directory location on host
       path: /home/rgn/kubernetes/scripts

The hostPath volume mounts a file or directory from the Kubernetes (k8s) host node's filesystem into your Pod. This is not something that most Pods will need, but it offers a powerful escape hatch for some applications. This path refers to the path on k8s Node that runs your container nginx.

To answer your specific case: please ensure that directory /home/rgn/kubernetes/scripts exists on your k8s node.

I have just ran the deployment from question on my own gke cluster (2 nodes)

$kubectl get nodes 
NAME                                         
gke-ubuntu-mnnv   
gke-ubuntu-nw9v

$ kubectl apply -f deployment.yaml


$ kubectl get pods -o wide 
NAME                               READY   STATUS    RESTARTS   AGE    NODE                                         
nginx-deployment-ddbfb785d-fggpq   1/1     Running   0          19s    gke-ubuntu-nw9v


$ kubectl exec nginx-deployment-ddbfb785d-fggpq -- ls -la containerdrive
total 8
drwxr-xr-x 2 root root 4096 Jun 22 11:54 .
drwxr-xr-x 1 root root 4096 Jun 22 11:55 ..
-rw-r--r-- 1 root root    0 Jun 22 11:54 gke-ubuntu-nw9v-file

$ kubectl describe deployments nginx-deployment 
...
    Mounts:
      /containerdrive from test-volume (rw)
  Volumes:
   test-volume:
    Type:          HostPath (bare host directory volume)
    Path:          /tmp/knp
    HostPathType:  
...

# and on k8s node itself:

$ ls -lah /tmp/knp/
total 8.0K
drwxr-xr-x  2 root root 4.0K Jun 22 11:54 .
drwxrwxrwt 10 root root 4.0K Jun 22 12:08 ..
-rw-r--r--  1 root root    0 Jun 22 11:54 gke-ubuntu-nw9v-file

I hope that output helps to see the whole picture clearly. :)

Additionally, as David Maze mentioned, it might be a good idea to compile all the scripts you rely on into the container itself.

Upvotes: 1

Related Questions