递归地从文件创建配置映射

dex*_*305 7 kubernetes configmap

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

  • 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. 
Run Code Online (Sandbox Code Playgroud)

按预期创建的配置映射无法表达嵌套的目录结构。

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

Vas*_*pov 7

不幸的是,目前不支持在 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
Run Code Online (Sandbox Code Playgroud)


Vin*_*t J 5

一种可自动化的解决方法:tar 文件,在 /tmp 中映射 tar configmap 卷文件,并在容器开始时将其解压。

创建焦油:

tar -cvf conf-d.tar ./conf.d
kubectl create configmap conf-d --from-file=conf-d.tar
rm conf-d.tar
Run Code Online (Sandbox Code Playgroud)

在 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
Run Code Online (Sandbox Code Playgroud)


aem*_*aem 5

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

\n

目录结构:

\n
test\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 init.sh\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 some.sh\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 thing.sh\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 README\n
Run Code Online (Sandbox Code Playgroud)\n

Helm 配置图模板:

\n
test\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 init.sh\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 some.sh\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 thing.sh\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 README\n
Run Code Online (Sandbox Code Playgroud)\n

结果:

\n
apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: my-configmap\ndata:\n  {{- $files := .Files }}\n  {{- range $path, $_ := .Files.Glob "test/**" }}\n  {{ $path | replace "/" "." }}: |\n{{ $files.Get $path | indent 4 }}\n  {{- end }}\n
Run Code Online (Sandbox Code Playgroud)\n

经头盔测试3.7.1

\n