首页 > 解决方案 > 递归地从文件创建配置映射

问题描述

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

我需要使用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 中?

标签: kubernetesconfigmap

解决方案


不幸的是,目前不支持在 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

推荐阅读