有些场景下我们需要在多个POD中使用同一个volume,这种情况在就不能直接直接写在同一个目录下,而需要使用该目录下针对每个POD的子目录,这个时候就可以使用subPath。
同时如果configMap/Secret挂载在容器的路径是会覆盖原有路径的下所有文件,这种场景在使用subPath也可以很好的解决该问题。
我们来看下用法吧。
我们先创建一套测试的pv/pvc
cat test-storage.yaml kind: PersistentVolume apiVersion: v1 metadata: name: test-pv labels: release: stable spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle hostPath: path: /tmp/test type: DirectoryOrCreate --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: test-pvc namespace: default spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 1Gi --- apiVersion: v1 kind: Pod metadata: name: test-pod namespace: default spec: containers: - name: test-pod command: ["bash", "-c"] args: ["while true; do sleep 30; done;"] image: ubuntu imagePullPolicy: IfNotPresent volumeMounts: - mountPath: /data/tool/ name: volst subPath: subpath1 - name: test-pod2 command: ["bash", "-c"] args: ["while true; do sleep 30; done;"] image: ubuntu imagePullPolicy: IfNotPresent volumeMounts: - mountPath: /data/tool/ name: volst subPath: subpath2 volumes: - name: volst persistentVolumeClaim: claimName: test-pvc
然后进行创建
kubectl apply -f test-storage.yaml
POD被创建以后我们进入POD中的test-pod容器,查看/data/tool路径创建一个sub1_file的文件,然后接着退出进入test-pod2容器,也创建一个sub2_file文件
然后我们直接登录挂载的存储的节点上,查看/tmp/test目录
虽然它们都是挂载的的/tmp/test目录,但是通过subPath又创建了一个子目录,每个容器都是使用/tmp/test目录下的自己所定义的子目录。
同时使用subPath挂载configMap/Secret文件时不会覆盖原有/tmp目录下的文件和目录。
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://sulao.cn/post/991
相关推荐
- k8s中harbor-database-0日志报Permissions should be u=rwx (0700)的处理方法
- k8s使用helm部署harbor镜像仓库并使用nodeport方式暴露
- k8s集群部署prometheus/node-exporter/dcgm-exporter
- k8s中calico匹配多种网络接口名字的方法
- ubuntu22.04使用containerd部署k8s集群
- k8s集群kube-proxy从iptables模式切换到ipvs模式
- k8s级联删除(删除deployment却删除不了rs和pod)异常问题的处理办法
- k8s中修改containerd存储目录并将数据迁移到新磁盘目录
- k8s网络cni插件calico的安装和网络模式切换
- k8s中pod使用RDMA网卡的方法(待验证)
评论列表