当前位置: 首页 > article >正文

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


文章目录

  • 前言
  • 一、什么是subPath
  • 二、subPath使用场景
  • 三、场景一示例
    • 1.资源准备
    • 2.使用subPath字段
  • 四、场景二示例
    • 1.资源准备
    • 2.测试


前言

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

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


一、什么是subPath

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

在这里插入图片描述

二、subPath使用场景

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

三、场景一示例

避免目录下的内容被覆盖
在这里插入图片描述

1.资源准备

configMap资源

[root@k8s-master YamlTest]# cat test_cfg.yml
username: "ops"
password: "123"

deployment资源

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字段

[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.资源准备

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.测试

[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


http://www.kler.cn/a/580137.html

相关文章:

  • Unity开发——CanvasGroup组件介绍和应用
  • Netty基础—1.网络编程基础一
  • Python助力区块链数据可视化:从数据到洞见
  • vue项目纯前端把PDF转成图片并下载
  • [250310] Mistral 发布世界领先的文档理解 API:Mistral OCR | 谷歌利用 AI 保护自然的三种新方式
  • 三星首款三折叠手机被曝外屏6.49英寸:折叠屏领域的新突破
  • DOM容器
  • 刷题记录(LeetCode 78 子集)
  • preact组件案例的使用
  • 常见HTTP 状态码及意义
  • Vue脚手架基础
  • 【Servlet】深入解析 Servlet 启动过程 —— 原理分析、代码实战及在 JDK 和 Spring 中的应用
  • Unity ES3保存类的问题
  • 单元测试、系统测试和集成测试知识总结
  • javaEE初阶————多线程进阶(2)
  • 信息安全访问控制、抗攻击技术、安全体系和评估(高软42)
  • RabbitMQ配置消息转换器
  • 【Linux】初识线程
  • 面试:hive的优化, 4个by, 存储过程和自定义函数
  • 【Java学习】泛型