K8S的mountPath和subPath

1 mountPath

mountPath是容器内部文件系统的挂载点,它定义了容器内部将外部存储卷(如 PersistentVolume、ConfigMap、Secret 等)挂载到哪个路径下。通过 mountPath,容器可以访问这些挂载的数据或配置。

2 subPath

subPath 是 mountPath 下的子路径,它允许容器将挂载的数据卷中的特定文件或目录挂载到容器中的指定路径下。这样可以实现更加精细的文件系统级别的访问控制。

3 mountPath使用场景

比如我需要创建一个nginx deployment,需要将自定义的nginx.conf配置文件独立出来,作为一个configmap来挂载到pod中。

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

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            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/conf.d
      volumes:
      - name: nginx-config-volume
        configMap:
          name: nginx-config

部署完成后,你可以进入pod,可以看到如下文件,这就是通过configmap挂载到容器中。

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

4 subPath使用场景

如果我想直接通过configmap定义/etc/nginx/nginx.conf,这时候如果还是只使用mountPath,就会有问题了。

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

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


    events {
        worker_connections  1024;
    }


    http {
        include       /etc/nginx/mime.types;
        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/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.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

创建容器后,服务无法起来,会报错,因为此时容器中的/etc/nginx/目录会被我们挂载的configmap给覆盖,所以原先/etc/nginx/目录下的文件都无法被pod访问,也就报错了。

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

那如果我将volumeMount改为如下配置呢,

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

此时原来/etc/nginx/目录下的文件都不会受影响,但是依旧会报错,连容器都无法创建,

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/kubernetes.io~configmap/nginx-config-volume" to rootfs at "/etc/nginx/nginx.conf": mount /var/lib/kubelet/pods/6755a0be-2f05-4edb-813b-ece2dcc2e8f1/volumes/kubernetes.io~configmap/nginx-config-volume:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5001: not a directory: unknown

这是因为此时容器中/etc/nginx/nginx.conf这个路径已经是存在的,镜像中默认的文件,没法作为mountpath使用。

当然,你可以将nginx.conf换成其他名字,比如mynginx.conf,

      - name: nginx
        image: nginx
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/mynginx.conf

这样就不会报错,但是这样的效果是,容器中会创建一个目录/etc/nginx/mynginx.conf/,这个目录下有个文件nginx.conf,与最开始我们在/etc/nginx/conf.d/定义my-nginx.conf一样,但这并不是我们所要的。

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

这个时候,我们就需要使用subPath了,将volumeMount做如下修改,

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

这时服务就按照我们预期启动了,容器内文件如下,而且nginx.conf的内容就是我们configmap中定义的配置,

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

类似的场景,比如需要在/etc/nginx/conf.d/为不同的host定义不同的配置,我们就可以创建多个configmap,配合使用subPath来挂载到同一个目录下,

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

参考文档:

  1. https://stackoverflow.com/questions/65399714/what-is-the-difference-between-subpath-and-mountpath-in-kubernetes
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Kubernetes 中,你可以通过在 Deployment 或者 StatefulSet 中配置一个 "subPath" 来实现 subpath。 在 Deployment 中使用 subPath 的示例配置如下: ``` apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: template: spec: containers: - name: my-container volumeMounts: - name: my-volume mountPath: /path/to/mount subPath: my-subpath volumes: - name: my-volume configMap: name: my-configmap ``` 在 StatefulSet 中使用 subPath 的示例配置如下: ``` apiVersion: apps/v1 kind: StatefulSet metadata: name: my-statefulset spec: template: spec: containers: - name: my-container volumeMounts: - name: my-volume mountPath: /path/to/mount subPath: my-subpath volumes: - name: my-volume configMap: name: my-configmap ``` 其中,"subPath" 应该指向 configMap 中需要挂载的目录或文件的路径。 需要注意的是,这个只适用于 configMap 与 Secret 这两种 volume。 ### 回答2: K8sKubernetes)的Subpath是一种配置方式,用于将容器内的特定路径挂载到Pod的特定路径上。接下来我将用300字回答如何配置K8sSubpath。 首先,要使用Subpath,需要在Pod的配置文件中的“volumes”部分定义一个Volume,并指定其类型和相关配置。例如,可以定义一个ConfigMap类型的Volume,通过将容器内的文件或目录路径挂载到Pod的某个路径上,实现文件共享和持久化。 接下来,在同一个Pod配置文件中的“volumeMounts”部分,将定义的Volume挂载到容器内的特定路径上。同样需要指定路径和相关配置。例如,可以将Volume挂载到容器内的/mounted_path路径上。 最后,确保在Pod的配置文件中,将定义的Volume和对应的VolumeMounts以及容器的其他相关配置相连接。 以下为示例配置: ``` apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mycontainer image: myimage volumeMounts: - name: myvolume mountPath: /mounted_path readOnly: true # 是否设置只读 volumes: - name: myvolume configMap: name: myconfigmap ``` 在上述示例中,定义了一个名为myvolume的Volume,类型为ConfigMap,将myconfigmap映射到Pod中。然后,在容器mycontainer中,将myvolume挂载到/mounted_path路径上,并设置为只读。 通过这样配置,容器内的特定路径和Pod的特定路径就能进行映射,可以实现文件的共享和持久化,便于资源的管理和访问。 总结起来,使用Subpath配置,需要在Pod的配置文件中定义Volume和VolumeMounts,并确保将它们连接到容器的相关路径上。这样就能实现容器内某个路径的挂载到Pod的特定路径上,方便文件的管理和访问。 ### 回答3: 在Kubernetes中,SubPath用于将容器中的单个文件或目录挂载到容器中的特定路径上。在进行SubPath配置时,需要在容器的卷挂载配置中添加subPath字段,并指定宿主机的文件或目录路径。 首先,在Pod的配置文件中,定义一个Volume,指定它的类型和名称。例如,可以使用emptyDir作为Volume类型,名称为"myvolume": ```yaml ... volumes: - name: myvolume emptyDir: {} ... ``` 接下来,在容器的卷挂载配置中,使用上面定义的Volume,并添加subPath字段来指定容器内挂载点的路径。例如,将宿主机上的"/data/myfile.txt"文件挂载到容器内的"/app/data"目录下: ```yaml ... volumeMounts: - name: myvolume mountPath: /app/data subPath: myfile.txt ... ``` 完成上述配置后,当Pod启动时,"/data/myfile.txt"文件将会被挂载到容器内的"/app/data/myfile.txt"路径上。 需要注意的是,subPath只能用于将单个文件或目录挂载到容器中,无法将多个文件或目录挂载到同一个路径上。此外,修改subPath字段的值可能会导致Pod重新启动,以确保卷的正确挂载和文件的可访问性。 总结起来,通过在Pod的Volumes定义中创建一个Volume,并在容器的VolumeMounts中指定subPath字段,可以配置Kubernetes中的SubPath。这样可以将宿主机上的文件或目录挂载到容器中指定的路径上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值