Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubernetes mountPath vs hostPath

Tags:

kubernetes

I am trying to deploy an app to kubernetes cluster and I want to store data in Persistent Volume. However, I am very confused about two parameters in the setup. Can someone explains what is the different between volumes.hostPath and volumeMounts.mountPath? I read some documentations online but it does not help me to understand.

volumeMounts:
  - mountPath: /var/lib/mysql

volumes:
  hostPath:
    path: /k8s

If my setup is as above, is the volume going to be mounted at /k8s/var/lib/mysql?

like image 560
jiashenC Avatar asked Jun 29 '18 18:06

jiashenC


People also ask

What is Kubernetes Mountpath?

The mount path is always the destination inside the Pod a volume gets mounted to. I think the documentation is pretty clear on what hostPath does: A hostPath volume mounts a file or directory from the host node's filesystem into your Pod.

What is Kubernetes hostPath?

A Kubernetes hostpath is one of the volumes supported by Kubernetes. It is used to mount a file or directory from the host node's file system into our pod. It does not require most pods. However, it is instrumental in testing or development scenarios and provides a powerful escape for some applications.

Can a pod have multiple volumes?

Kubernetes supports many types of volumes. A Pod can use any number of volume types simultaneously. Ephemeral volume types have a lifetime of a pod, but persistent volumes exist beyond the lifetime of a pod.


2 Answers

The mount path is always the destination inside the Pod a volume gets mounted to.

I think the documentation is pretty clear on what hostPath does:

A hostPath volume mounts a file or directory from the 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.

For example, some uses for a hostPath are:

- running a Container that needs access to Docker internals; use a hostPath of /var/lib/docker
- running cAdvisor in a Container; use a hostPath of /sys
- allowing a Pod to specify whether a given hostPath should exist prior to the Pod running, whether it should be created, and what it should exist as

So your example does not what you think it does. It would mount the node's /k8s directory into the Pod at /var/lib/mysql.

This should be done only if you fully understand the implications!

like image 168
matthias krull Avatar answered Oct 06 '22 14:10

matthias krull


Host path: The directory in your node.
Mount path: The directory in your pod.

Your setup will mount the node's directory(/k8s) into the pod's directory(at /var/lib/mysql)

like image 34
AATHITH RAJENDRAN Avatar answered Oct 06 '22 15:10

AATHITH RAJENDRAN