文章目录
- 前言
- 一、什么是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-nginxname: test5-nginxnamespace: middleware
spec:replicas: 1selector:matchLabels:app: test5-nginxtemplate:metadata:labels:app: test5-nginxspec:containers:- image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stablename: nginxvolumeMounts:- name: test5subPath: usernamemountPath: /etc/nginx/usernamevolumes:- name: test5configMap:items:- key: usernamepath: usernamename: 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-1image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stablevolumeMounts:- mountPath: /tmp/nginx # 容器1的挂载目录name: subpath-volsubPath: nginxtest1 # 宿主机volume的子目录1- name: subpath-container-2image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/nginx:stable-alpinevolumeMounts:- mountPath: /etc/nginx/nginxtest2 # 容器2的挂载目录name: subpath-volsubPath: nginxtest2 # 宿主机volume的子目录2 volumes:- name: subpath-volpersistentVolumeClaim: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