mountPath property in the Volume Kubernetes

11/1/2021

I am learning about Volumes in the Kubernetes.
I understood the concept of Volume and types of volume.
But, I am confused about the mouthPath property. Following my YAML file:

apiVersion: v1 
kind: Pod 
metadata:
  name: nginx-alpine-volume
spec:   

containers: 
  - name: nginx 
    image: nginx:alpine 
    volumeMounts: 
      - name: html 
        mountPath: /usr/share/nginx/html
        readOnly: true 
    resources:
  - name: html-updater 
    image: alpine 
    command: ["/bin/sh", "-c"] 
    args: 
      - while true; do date >> /mohit/index.html;sleep 10; done 
    resources:
    volumeMounts: 
      - name: html 
        mountPath: /mohit
  volumes: 
  - name: html 
    emptyDir: {}

Question: What is the use of the mountPath property. Here, I am using one pod with two containers. Both containers have different mounPath values.

Update:

enter image description here

-- KiddoDeveloper
docker
kubernetes
volume

1 Answer

11/1/2021

Consider the mount path as the directory where you are attaching or mounting the files or system

While your actual volume is emptyDir

What basically the idea is there to both container have different mount path

as both containers need to use different folders

While as your volume is single name html so locally from volume both container pointing or using the different folders

both containers manage the different files at their mounting point (or folder)

so mount path is a point or directly where your container will be managing files.

Empty dir : https://kubernetes.io/docs/concepts/storage/volumes/#emptydir

Read more at : https://kubernetes.io/docs/tasks/access-application-cluster/communicate-containers-same-pod-shared-volume/

if you see this example: https://github.com/harsh4870/Kubernetes-wordpress-php-fpm-nginx/blob/master/wordpress-deployment.yaml

it has the same two containers with mount path and emptydir volume

what i am doing is attaching the Nginx configuration file, to the container so Nginx will use that configuration file which i am mounting from outside to the container.

My config file stored inside configmap or secret.

-- Harsh Manvar
Source: StackOverflow