如何使用configmap在Kubernetes中挂载整个目录?

22

我希望能够在 /etc/configs 目录下挂载未知数量的配置文件。

我使用以下命令将一些文件添加到 ConfigMap 中:

kubectl create configmap etc-configs --from-file=/tmp/etc-config

文件数量和文件名永远无法确定,我想重新创建 ConfigMap 并在同步间隔后更新 Kubernetes 容器中的文件夹。

我已经尝试过挂载,但是我无法这样做,文件夹始终为空,但是 ConfigMap 中有数据。

bofh$ kubectl describe configmap etc-configs
Name:         etc-configs
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
file1.conf:
----
{
 ... trunkated ...
}
file2.conf:
----
{
 ... trunkated ...
}
file3.conf:
----
{
 ... trunkated ...
}

Events:  <none>

我在容器的volumeMounts中使用了这个。

- name: etc-configs
  mountPath: /etc/configs

这是卷:

- name: etc-configs
  configMap:
    name: etc-configs

我可以挂载单独的项目,但无法挂载整个目录。

有什么建议可以解决这个问题吗?

3个回答

17

您可以将ConfigMap作为特殊卷挂载到容器中。

在这种情况下,挂载文件夹将会显示每个键作为挂载文件夹中的文件,并且文件的内容将具有映射值。

引用自Kubernetes文档:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
...
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config

这似乎是这个问题的一个有效答案。 - Kostanos

5

我现在感觉很愚蠢。

抱歉,是我的错。

Docker容器没有启动,所以我使用docker run -it --entrypoint='/bin/bash'手动启动它,但是我无法从configMap中看到任何文件。

这行不通,因为Docker在Kubernetes启动之前不知道我的部署情况。

Docker镜像失败了,而Kubernetes配置一直都是正确的。

我调试错误了。


谢谢,我被接受的答案搞糊涂了,我以为不可能将多个文件从configMap挂载到一个目录中。在我的情况下,我必须删除subPath。 - HereHere

4

根据您的配置,您将挂载在您的configmap中列出的每个文件。

如果您需要挂载文件夹中的所有文件,则不应使用configmap,而应该使用persistenceVolume和persistenceVolumeClaims:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-jenkins
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/pv-jenkins"

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-jenkins
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ""
  resources:
    requests:
      storage: 50Gi

在你的deployment.yml文件中:
 volumeMounts:
        - name: jenkins-persistent-storage
          mountPath: /data

  volumes:
        - name: jenkins-persistent-storage
          persistentVolumeClaim:
            claimName: pv-claim-jenkins

您也可以使用以下内容:

kubectl create configmap my-config --from-file=/etc/configs

创建包含该文件夹中所有文件的配置映射。

希望这能帮到您。


2
嗨,但我想挂载配置映射中的每个单独文件,因为我们正在从第三方系统填充配置映射。 - Johan Ryberg
你的意思是在configMan里面列出N个文件和一个文件夹,然后期望它按原样挂载该文件夹吗? - Nicola Ben
是的!我不需要文件夹,我对一个扁平的结构很满意,直接将文件放在/etc/configs下。 - Johan Ryberg
感谢@Nicola提供的所有帮助。我已发布了我的“解决方案”。Kubernetes一直在工作,但我的容器失败了,而且我的故障排除方法是错误的。我会给你的答案投票,因为你真的很努力地帮助我。 - Johan Ryberg
@NicolaBenaglia 在你的例子中,PersistentVolume 有什么用途? - pb100
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接