The mountPath should be absolute in Kubernetes, is it? The mountPath should be absolute in Kubernetes, is it? kubernetes kubernetes

The mountPath should be absolute in Kubernetes, is it?


I believe the source of your confusion is that you are running a single node Cluster, like Minikube.

  • The process chain (roughly summarized) is:

Kubectl > Kube-API(master) > Kubelet agent (node) > Pod creation (as specified on the yaml manifest).

In a single node cluster all these agents are on the same computer, that's why the files in /var/www/html/kubernetes/code were mounted to the pod.

  • I'll clarify it with this example:
    • You have a cluster with 3 nodes.
    • You manage your nodes remotely, with kubectl from your notebook.

When you use hostPath the files must exist on the node, not on your notebook, because it's not the kubectl on your computer that will trigger the creation of the pod and mount the files/directories.

This is the job of the kubelet agent of the node that will create the pod and apply it's manifest. This is why you need to specify the full path of the file/dir you want to mount.


According to PersistentVolumes documentation:

Kubernetes supports hostPath for development and testing on a single-node cluster. A hostPath PersistentVolume uses a file or directory on the Node to emulate network-attached storage.

In a production cluster, you would not use hostPath. Instead a cluster administrator would provision a network resource like a Google Compute Engine persistent disk, an NFS share, or an Amazon Elastic Block Store volume. Cluster administrators can also use StorageClasses to set up dynamic provisioning.

Watch out when using hostPath type, because:

  • Pods with identical configuration (such as created from a podTemplate) may behave differently on different nodes due to different files on the nodes.
  • when Kubernetes adds resource-aware scheduling, as is planned, it will not be able to account for resources used by a hostPath.
  • the files or directories created on the underlying hosts are only writable by root. You either need to run your process as root in a privileged Container or modify the file permissions on the host to be able to write to a hostPath volume

If you have any question let me know in the comments.