K8S中的mountPath和subPath使用详解

 更新时间:2024年12月20日 08:31:38   作者:Blue summer  
这篇文章主要介绍了K8S中的mountPath和subPath使用的相关资料,通过示例展示了如何使用mountPath和subPath来挂载ConfigMap到Pod中,并解决了服务无法启动的问题,需要的朋友可以参考下

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

参考文档:

  • https://stackoverflow.com/questions/65399714/what-is-the-difference-between-subpath-and-mountpath-in-kubernetes

总结 

到此这篇关于K8S中的mountPath和subPath使用详解的文章就介绍到这了,更多相关K8S的mountPath和subPath内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • kubernetes k8s常用问题排查方法

    kubernetes k8s常用问题排查方法

    新手学习K8s最大的难度感觉是在起步动手实践的时候,Pod没有正常启动起来,或者运行了一段时间Pod自己崩溃了。是什么问题导致了它没运行起来,或是什么因素导致了它的崩溃,本文来学习总结几个使用 K8s时常见的错误现象以及排查这些现象背后问题的方法
    2022-06-06
  • podman容器工具的具体使用

    podman容器工具的具体使用

    本文主要介绍了podman容器工具的具体使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 再分享6个可无限激活阿里云盘邀请码,不信你不能激活阿里云盘

    再分享6个可无限激活阿里云盘邀请码,不信你不能激活阿里云盘

    这篇文章主要分享6个可无限激活的阿里云盘邀请码,不信你不能激活阿里云盘,需要的朋友可以参考下
    2020-11-11
  • RFO SIG之openEuler AWS AMI 制作详解

    RFO SIG之openEuler AWS AMI 制作详解

    这篇文章主要为大家介绍了RFO SIG之openEuler AWS AMI 制作详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • 如何在Centos中搭建 K8s 1.23 集群

    如何在Centos中搭建 K8s 1.23 集群

    文章详细介绍了在CentOS上搭建Kubernetes 1.23集群的步骤,包括准备环境、安装Kubernetes软件包、上传离线镜像、初始化集群、添加节点、安装网络插件以及测试验证,感兴趣的朋友一起看看吧
    2025-03-03
  • Google Kubernetes Engine 集群实战详解

    Google Kubernetes Engine 集群实战详解

    这篇文章主要为大家介绍了Google Kubernetes Engine 集群实战详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • K8s实战教程之容器和 Pods资源分配问题

    K8s实战教程之容器和 Pods资源分配问题

    这篇文章主要介绍了K8s实战教程之容器和 Pods资源分配,本篇文章通过配置集群中运行的容器的 CPU 请求和限制,你可以有效利用集群上可用的 CPU 资源,通过将 Pod CPU 请求保持在较低水平,可以使 Pod 更有机会被调度,需要的朋友可以参考下
    2022-07-07
  • Kubernetes中使用临时容器进行故障排查的方法

    Kubernetes中使用临时容器进行故障排查的方法

    在使用Kubernetes时,用户常常会遇到一些错误和迷惑,下面这篇文章主要给大家介绍了关于Kubernetes中使用临时容器进行故障排查的相关资料,需要的朋友可以参考下
    2022-03-03
  • kubenetes集群版本升级方式

    kubenetes集群版本升级方式

    本文详细介绍了使用kubeadm和二进制方式搭建及升级Kubernetes集群的方法,介绍了版本控制、升级步骤、备份ETCD数据、升级各节点组件等关键操作,并提供了操作示例和注意事项,帮助理解和实施Kubernetes集群的搭建和升级过程
    2024-09-09
  • Rancher通过界面管理K8s平台的图文步骤详解

    Rancher通过界面管理K8s平台的图文步骤详解

    这篇文章主要为大家介绍了Rancher通过界面管理K8s平台通过详细的图文进行步骤讲解,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03

最新评论