9

我在两个目录中有多个配置文件。例如,

  • conf.d/parentconf1.conf
  • con.d/node1/child1.conf
  • conf.d/node2/child2.conf

我需要使用ConfigMap.

尝试使用

kubectl create configmap --from-file=./conf.d --from-file=./conf.d/node1/child1.conf --from-file=./conf.d/node2/child2.conf. 

正如预期的那样,创建的配置映射无法表达嵌套的目录结构。

是否可以从文件夹递归地创建 ConfigMap 并且仍然以 ConfigMap 的键条目的名称保留文件夹结构 - 因为目的是将这些 ConfigMap 挂载到 pod 中?

4

3 回答 3

8

不幸的是,目前不支持在 configmap 中反映目录结构。解决方法是像这样表达目录层次结构:

apiVersion: v1
kind: ConfigMap
metadata:
   name: testconfig
data:
  file1: |
    This is file1
  file2: |
    This is file2 in subdir directory
---
apiVersion: v1
kind: Pod
metadata:
  name: testpod
spec:
  restartPolicy: Never
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh","-c", "sleep 1000" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: testconfig
        items:
        - key: file2
          path: subdir/file2
        - key: file1
          path: file1
于 2019-04-22T07:11:28.087 回答
7

一种可自动化的解决方法:tar 文件,将 tar configmap 卷文件映射到 /tmp,然后在容器启动时解压缩它。

创建焦油:

tar -cvf conf-d.tar ./conf.d
kubectl create configmap conf-d --from-file=conf-d.tar
rm conf-d.tar

并在您的 pod.yml 中,在您的命令之前或默认图像命令之前添加 tar -xf:

    command: [ "/bin/sh","-c", "tar -xf /tmp/conf-d.tar -C /etc/ && sleep 1000" ]
    volumeMounts:
      - mountPath: /tmp/conf-d.tar
        name: nginx-config-volume
        subPath: conf-d.tar
于 2020-01-09T18:29:25.393 回答
0

在为 Helm 图表编写模板时,内置工具可用于创建配置映射或包含目录中所有文件的机密。

目录结构:

test
├── bar
│   └── init.sh
├── foo
│   ├── some.sh
│   └── thing.sh
└── README

Helm 配置图模板:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  {{- $files := .Files }}
  {{- range $path, $_ := .Files.Glob "test/**" }}
  {{ $path | replace "/" "." }}: |
{{ $files.Get $path | indent 4 }}
  {{- end }}

结果:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  test.bar.init.sh: |
    echo foo
  test.foo.some.sh: |
    echo foo
  test.foo.thing.sh: |
    echo foo
  test.README: |
    # My title

用 helm 测试3.7.1

于 2021-11-10T22:46:28.270 回答