深入解析K8s VolumeMounts中的subPath字段及其应用


文章目录


前言

在Kubernetes中,挂载存储卷是容器化应用的常见需求。然而当我们将整个卷挂载到容器中的某个目录时,可能会覆盖目标目录中已有的文件,尤其是在共享目录或有多个应用访问同一卷的场景下。为了避免这种情况,subPath字段应运而生,它允许精确指定要挂载的子目录或文件,从而避免覆盖目录中其他重要数据。

在这篇文章中,我们将深入探讨subPath字段的使用方法,展示如何通过这一功能实现精准挂载,确保不干扰或覆盖目录中的其他文件。通过合理利用subPath,你可以在K8s中灵活管理存储,避免数据冲突,提升系统的可维护性和安全性。


一、什么是subPath

shell 复制代码
subPath是kubernetes中Pod资源volumeMounts字段的挂载选项。
subPath所定义的路径,指的是卷(Volume)内的子路径,用于将卷内subPath所对应的目录或文件,挂载到容器的挂载点,卷内subPath不存在时自动创建。
不指定此参数时,默认是将卷的根路径进行挂载

二、subPath使用场景

yaml 复制代码
避免覆盖:
	如果挂载路径是一个已存在的目录,则目录下的内容不会被覆盖。
	直接将configMap/Secret挂载在容器的路径,会覆盖掉容器路径下原有的文件,
		使用subpath选定configMap/Secret的指定的key-value挂载在容器中,则不会覆盖掉原目录下的其他文件
yaml 复制代码
文件隔离:
	pod中含有多个容器共用用一个volume,不同容器日志路径挂载的到不同的子目录,
		而不是根路径(Subpath目录会在底层存储自动创建且权限为777,无需手动创建)

三、场景一示例

避免目录下的内容被覆盖

1.资源准备

configMap资源

shell 复制代码
[root@k8s-master YamlTest]# cat test_cfg.yml
username: "ops"
password: "123"

deployment资源

shell 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test5-nginx
  name: test5-nginx
  namespace: middleware
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test5-nginx
  template:
    metadata:
      labels:
        app: test5-nginx
    spec:
      containers:
      - image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stable
        name: nginx
        volumeMounts:
        - name: test5
          subPath: username
          mountPath: /etc/nginx/username
      volumes:
        - name: test5
          configMap:
            items:
            - key: username
              path: username
            name: test-cfg

默认情况下,该nginx镜像的/etc/nginx目录下存在以下文件

2.使用subPath字段

shell 复制代码
[root@k8s-master YamlTest]# kubectl create -f test_nginx.yml


由此可见,当使用subPath字段后,挂载到nginx目录下的configMap字段以文件的形式存在,并且未影响原目录下的文件及目录。切记以subpath方式挂载文件,文件内容不会随着configMap的更新而自动更新

注意事项(如下所示)

特别注意mountPath和subPath的写法, 最后的path要保持一致.

如mountPath是: /etc/nginx/username; subPath是: username.

mountPath不要漏写为: /etc/nginx

四、场景二示例

pod中含有多个容器共用用一个volume,即不同容器的路径挂载在存储卷volume的子路径

1.资源准备

shell 复制代码
apiVersion: v1
kind: Pod
metadata:
  name: pod-subpath-test
spec:
    containers:
    - name: subpath-container-1
      image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stable
      volumeMounts:
      - mountPath: /tmp/nginx            # 容器1的挂载目录
        name: subpath-vol
        subPath: nginxtest1                   # 宿主机volume的子目录1
    - name: subpath-container-2
      image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stable-alpine
      volumeMounts:
      - mountPath: /etc/nginx/nginxtest2             # 容器2的挂载目录
        name: subpath-vol
        subPath: nginxtest2                   # 宿主机volume的子目录2 
    volumes:
    - name: subpath-vol
      persistentVolumeClaim:
        claimName: test-subpath

2.测试

shell 复制代码
[root@k8s-master default-test-subpath-pvc-6bf62280-0cb5-4df9-a5c1-d5aa0e061725]# pwd
/export/nfs/default-test-subpath-pvc-6bf62280-0cb5-4df9-a5c1-d5aa0e061725
[root@k8s-master default-test-subpath-pvc-6bf62280-0cb5-4df9-a5c1-d5aa0e061725]# ls -l
total 0
drwxrwxrwx 2 root root 15 Mar 10 23:09 nginxtest1
drwxrwxrwx 4 root root 29 Mar 10 23:18 nginxtest2

[root@k8s-master default-test-subpath-pvc-6bf62280-0cb5-4df9-a5c1-d5aa0e061725]# cd nginxtest1/
[root@k8s-master nginxtest1]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 10 23:09 1

[root@k8s-master nginxtest2]# ll
total 0
drwxr-xr-x 2 root root 6 Mar 10 23:18 ops1

[root@k8s-master nginxtest2]# kubectl exec -it pod-subpath-test  bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "subpath-container-1" out of: subpath-container-1, subpath-container-2
root@pod-subpath-test:/# ls -l /tmp/nginx/
total 0
-rw-r--r-- 1 root root 0 Mar 10 15:09 1        ###在volume目录下创建该文件,容器中也看到了
root@pod-subpath-test:/# ls -l /etc/nginx/
total 24
drwxr-xr-x 1 root root   26 Mar 10 15:14 conf.d
-rw-r--r-- 1 root root 1007 May 28  2024 fastcgi_params
-rw-r--r-- 1 root root 5349 May 28  2024 mime.types
lrwxrwxrwx 1 root root   22 May 29  2024 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root  648 May 29  2024 nginx.conf
drwxr-xr-x 2 root root    6 Mar 10 15:17 ops1               ###在volume目录下创建该目录,容器中也看到了
-rw-r--r-- 1 root root  636 May 28  2024 scgi_params
-rw-r--r-- 1 root root  664 May 28  2024 uwsgi_params

相关推荐
行者Sun19891 小时前
【K8s】专题十六(3):Kubernetes 包管理工具之 Helm 语法
云原生·容器·kubernetes·helm
全是操作11 小时前
k8s scheduler源码阅读
云原生·容器·kubernetes
obboda13 小时前
Docker基础入门
运维·docker·云原生·容器·eureka
cooldream200913 小时前
Docker Desktop 安装与使用详解
运维·docker·容器
川石课堂软件测试15 小时前
涨薪技术|Kubernetes(k8s)之Service服务
功能测试·adb·docker·云原生·容器·kubernetes·单元测试
钰紫薇15 小时前
玩转K8S,轻松部署SpringBoot项目,实现hello K8s
运维·kubernetes
RedCong15 小时前
k8s之PodDisruptionBudget详解
云原生·容器·kubernetes
{⌐■_■}16 小时前
【Kubernets】Kubernetes 的基础知识,Pod是什么? 和容器的关系?多个容器如何在同一个 Pod 里协作?
云原生·容器·kubernetes
hxdcxy16 小时前
docker企业级事例部署phpmyadmin和MySQL
mysql·docker·容器
川石课堂软件测试16 小时前
涨薪技术|Kubernetes(k8s)之Service服务类型
mysql·nginx·docker·容器·贪心算法·kubernetes·tomcat