<p>I am trying to configure hostPath as the volume in kubernetes. I have logged into VM server, from where I usually use kubernetes commands such as kubectl.</p>

<p>Below is the pod yaml:</p>

<pre><code>apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: helloworldanilhostpath
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: helloworldanilhostpath
    spec:
      volumes:
        - name: task-pv-storage
          hostPath:
            path: /home/openapianil/samplePV
            type: Directory
      containers:
      - name: helloworldv1
        image: ***/helloworldv1:v1
        ports:
        - containerPort: 9123
        volumeMounts:
         - name: task-pv-storage
           mountPath: /mnt/sample
</code></pre>

<p>In VM server, I have created "/home/openapianil/samplePV" folder and I have a file present in it. It has a sample.txt file.</p>

<p>once I try to create this deployment. it does not happen with error -<br>
  Warning  FailedMount            28s (x7 over 59s)  kubelet, aks-nodepool1-39499429-1  MountVolume.SetUp failed for volume "task-pv-storage" : hostPath type check failed: /home/openapianil/samplePV is not a directory.</p>

<p>Can anyone please help me in understanding the problem here.</p>

<p><a href="https://kubernetes.io/docs/concepts/storage/volumes/#hostpath" rel="noreferrer"><code>hostPath</code></a> type volumes refer to directories on the Node (VM/machine) where your Pod is scheduled for running (<code>aks-nodepool1-39499429-1</code> in this case). So you'd need to create this directory at least on that Node. </p>

<p>To make sure your Pod is consistently scheduled on that specific Node you need to set <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#nodeselector" rel="noreferrer"><code>spec.nodeSelector</code></a> in the PodTemplate:</p>

<pre><code>apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: helloworldanilhostpath
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: helloworldanilhostpath
    spec:
      nodeSelector:
        kubernetes.io/hostname: aks-nodepool1-39499429-1
      volumes:
        - name: task-pv-storage
          hostPath:
            path: /home/openapianil/samplePV
            type: Directory
      containers:
      - name: helloworldv1
        image: ***/helloworldv1:v1
        ports:
        - containerPort: 9123
        volumeMounts:
         - name: task-pv-storage
           mountPath: /mnt/sample
</code></pre>

<p><strong>In most cases it's a bad idea to use this type of volume; there are some special use cases, but chance are yours is not one them!</strong></p>

<p>If you need local storage for some reason then a slightly better solution is to use <a href="https://kubernetes.io/docs/concepts/storage/volumes/#local" rel="noreferrer"><code>local</code></a> PersistentVolumes.</p>
