SoFunction
Updated on 2025-04-11

Detailed explanation of mountPath and subPath usage in K8S

1 mountPath

mountPath is the mount point of the file system inside the container. It defines the path to which external storage volumes (such as PersistentVolume, ConfigMap, Secret, etc.) are mounted inside the container. mountPath allows containers to access these mounted data or configurations.

2 subPath

subPath is a subpath under mountPath, which allows the container to mount a specific file or directory in the mounted data volume to a specified path in the container. This allows for more granular file system-level access control.

3 mountPath usage scenarios

For example, I need to create a nginx deployment, and I need to separate the custom configuration files and mount them into the pod as a configmap.

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  : |
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index   ;
        }

        error_page   500 502 503 504  /;
        location = / {
            root   /usr/share/nginx/html;
        }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/
      volumes:
      - name: nginx-config-volume
        configMap:
          name: nginx-config

After the deployment is completed, you can enter the pod and you can see the following file, which is mounted into the container through configmap.

kubectl exec -it nginx-deployment-5b4699b7dd-fh4qc -- /bin/sh
# cd /etc/nginx/
# ls

4 subPath usage scenarios

If I want to define /etc/nginx/ directly through configmap, if I still only use mountPath, there will be a problem.

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  : |
    user  nginx;
    worker_processes  auto;

    error_log  /var/log/nginx/ notice;
    pid        /var/run/;


    events {
        worker_connections  1024;
    }


    http {
        include       /etc/nginx/;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx//*.conf;
    }

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx
      volumes:
      - name: nginx-config-volume
        configMap:
          name: nginx-config

After creating the container, the service cannot get up and an error will be reported, because the /etc/nginx/ directory in the container will be overwritten by the configmap we mounted, so the files in the original /etc/nginx/ directory cannot be accessed by the pod, which means an error will be reported.

2024/03/25 06:56:58 [emerg] 1#1: open() "/etc/nginx/" failed (2: No such file or directory) in /etc/nginx/:14
nginx: [emerg] open() "/etc/nginx/" failed (2: No such file or directory) in /etc/nginx/:14

What if I change volumeMount to the following configuration,

      - name: nginx
        image: nginx
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/

At this time, the files in the /etc/nginx/ directory will not be affected, but there will still be an error, and even the container cannot be created.

Error: failed to create containerd task: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/var/lib/kubelet/pods/6755a0be-2f05-4edb-813b-ece2dcc2e8f1/volumes/~configmap/nginx-config-volume" to rootfs at "/etc/nginx/": mount /var/lib/kubelet/pods/6755a0be-2f05-4edb-813b-ece2dcc2e8f1/volumes/~configmap/nginx-config-volume:/etc/nginx/ (via /proc/self/fd/6), flags: 0x5001: not a directory: unknown

This is because the path /etc/nginx/ in the container already exists, and the default file in the image cannot be used as mountpath.

Of course, you can change it to another name, for example,

      - name: nginx
        image: nginx
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/

This way there will be no errors, but the effect is that a directory /etc/nginx// will be created in the container. There is a file in this directory, which is the same as we defined in /etc/nginx// at the beginning, but this is not what we want.

kubectl exec -it nginx-deployment-6bf5f55df8-f452d -- /bin/sh
# cd /etc/nginx//
# ls

At this time, we need to use subPath and modify volumeMount as follows.

      - name: nginx
        image: nginx
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/
          subPath: 

At this time, the service is started as we expected. The files in the container are as follows, and the content is the configuration defined in our configmap.

kubectl exec -it nginx-deployment-bb7d454c6-75bwz -- /bin/sh
# cd /etc/nginx/
# ls
	fastcgi_params	  modules    scgi_params  uwsgi_params

In similar scenarios, for example, if you need to define different configurations for different hosts in /etc/nginx//, we can create multiple configmaps and use subPath to mount them to the same directory.

      - name: nginx
        image: nginx
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx//
          subPath: 
        - name: my-nginx-config-volume
          mountPath: /etc/nginx//
          subPath: 

Reference documentation:

  • /questions/65399714/what-is-the-difference-between-subpath-and-mountpath-in-kubernetes

Summarize

This is the article about the detailed explanation of mountPath and subPath in K8S. For more information about mountPath and subPath content of K8S, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!