Hosting local directory to Kubernetes Pod

5/16/2019

I have a single node Kubernetes cluster. I want the pod I make to have access to /mnt/galahad on my local computer (which is the host for the cluster).

Here is my Kubernetes config yaml:

apiVersion: v1
kind: Pod
metadata:
  name: galahad-test-distributor
  namespace: galahad-test
spec:
  volumes:
     - name: place-for-stuff
       hostPath:
        path: /mnt/galahad
  containers:
   - name: galahad-test-distributor
     image: vergilkilla/distributor:v9
     volumeMounts:
        - name: place-for-stuff
          mountPath: /mnt
    resources:
      limits:
        memory: "200Mi"
      requests:
        memory: "100Mi"

I start my pod like such:

kubectl apply -f ./create-distributor.yaml -n galahad-test

I get a terminal into my newly-made pod:

kubectl exec -it galahad-test-distributor -n galahad-test -- /bin/bash

I go to /mnt in my pod and it doesn't have anything from /mnt/galahad. I make a new file in the host /mnt/galahad folder - doesn't reflect in the pod. How do I achieve this functionality to have the host path files/etc. reflect in the pod? Is this possible in the somewhat straightforward way I am trying here (defining it per-pod definition without creating separate PersistentVolumes and PersistentVolumeRequests)?

-- PinkElephantsOnParade
kubernetes
persistent-volumes

1 Answer

5/16/2019

Your yaml file looks good.

Using this configuration:

apiVersion: v1
kind: Pod
metadata:
  name: galahad-test-distributor
  namespace: galahad-test
spec:
  volumes:
     - name: place-for-stuff
       hostPath:
        path: /mnt/galahad
  containers:
   - name: galahad-test-distributor
     image: busybox
     args: [/bin/sh, -c,
            'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1; done']
     volumeMounts:
        - name: place-for-stuff
          mountPath: /mnt
     resources:
       limits:
         memory: "200Mi"
       requests:
         memory: "100Mi"

I ran this and everything worked as expected:

>>> kubectl apply -f create-distributor.yaml # side node: you don't need 
                                             # to specify the namespace here
                                             # since it's inside the yaml file
pod/galahad-test-distributor created

>>> touch /mnt/galahad/file
>>> kubectl -n galahad-test exec galahad-test-distributor ls /mnt
file

Are you sure you are adding your files in the right place? For instance, if you are running your cluster inside a VM (e.g. minikube), make sure you are adding the files inside the VM, not on the machine hosting the VM.

-- Alassane Ndiaye
Source: StackOverflow