从文件递归创建配置映射

时间:2019-04-22 06:34:15

标签: kubernetes configmap

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

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

我需要使用ConfigMap将这些配置文件以相同的目录结构安装到kubernetes pod。

尝试使用

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中?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

一种自动解决方法:将文件压缩,在/ tmp中映射tar configmap卷文件,然后在容器的开头将其解压缩。

创建tar:

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
相关问题