ConfigMap 安装在持久卷声明上

Lor*_*reV 4 kubernetes kubernetes-helm

在我的部署中,我想将持久卷声明与配置映射安装结合使用。例如,我想要以下内容:

volumeMounts:
    - name: py-js-storage
        mountPath: /home/python
    - name: my-config
        mountPath: /home/python/my-config.properties
        subPath: my-config.properties
        readOnly: true
...
    volumes:
    - name: py-storage
    {{- if .Values.py.persistence.enabled }}
        persistentVolumeClaim:
        claimName: python-storage
    {{- else }}
        emptyDir: {}
    {{- end }}
Run Code Online (Sandbox Code Playgroud)

这是一条可能且可行的方法吗?有没有更好的方法来处理这种情况?

Mr.*_*ler 7

由于您没有给出您的用例,我的答案将基于是否可能。事实上:是的,确实如此。

我假设您希望从configMap已经包含其他文件的安装点安装文件,并且您的使用方法subPath是正确的!

当需要在同一路径上挂载不同的卷时,需要指定subPath,否则原目录的内容将被隐藏

换句话说,如果您想保留这两个文件(来自挂载点来自 configMap),则必须使用subPath.

为了说明这一点,我使用下面的部署代码进行了测试。/mnt我在那里挂载了包含在我的 pod 中调用的文件和来自我的 configmap 的filesystem-file.txt文件的hostPath :/mnt/configmap-file.txttest-pd-plus-cfgmap

注意:我使用的是 Kubernetes 1.18.1

配置图:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-pd-plus-cfgmap
data:
  file-from-cfgmap: file data
Run Code Online (Sandbox Code Playgroud)

部署:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-pv
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test-pv
  template:
    metadata:
      labels:
        app: test-pv
    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - mountPath: /mnt
          name: task-pv-storage
        - mountPath: /mnt/configmap-file.txt
          subPath: configmap-file.txt
          name: task-cm-file
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: task-pv-claim
        - name: task-cm-file
          configMap:
            name: test-pd-plus-cfgmap
Run Code Online (Sandbox Code Playgroud)

部署完成后,您可以在/mntpod 中看到以下内容:

$ kubectl exec test-pv-5bcb54bd46-q2xwm -- ls /mnt
configmap-file.txt
filesystem-file.txt
Run Code Online (Sandbox Code Playgroud)

你可以用同样的讨论来检查这个 github问题。

在这里您可以阅读有关卷的更多信息subPath