Kubernetes-如何定义使用Yaml中的文件构建的ConfigMap? [英] Kubernetes - How to define ConfigMap built using a file in a yaml?

查看:884
本文介绍了Kubernetes-如何定义使用Yaml中的文件构建的ConfigMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在通过执行以下操作从文件config.json创建一个configmap:

At present I am creating a configmap from the file config.json by executing:

kubectl create configmap jksconfig --from-file=config.json

我希望在部署过程中<创建>创建 ,并尝试执行以下操作:

I would want the ConfigMap to be created as part of the deployment and tried to do this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |-
    {{ .Files.Get "config.json" | indent 4 }}

但似乎不起作用.应该怎么做才能进入configmap.yaml以便创建相同的configmap?

But doesn't seem to work. What should be going into configmap.yaml so that the same configmap is created?

-更新---

当我执行舵机空运行时:

when I do a helm install dry run:

# Source: mychartv2/templates/jks-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: jksconfig
data:
  config.json: |

注意:我正在使用minikube作为我的kubernetes集群

Note: I am using minikube as my kubernetes cluster

推荐答案

您的config.json文件应位于您的 mychart/目录中,而不应位于 mychart/templates

Your config.json file should be inside your mychart/ directory, not inside mychart/templates

图表模板指南

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  config.json: |-
{{ .Files.Get "config.json" | indent 4}}

config.json

{
    "val": "key"
}

helm install --dry-run --debug mychart

[debug] Created tunnel using local port: '52091'     

[debug] SERVER: "127.0.0.1:52091"                    

...           

NAME:   dining-saola                                 
REVISION: 1                                          
RELEASED: Fri Nov 23 15:06:17 2018                   
CHART: mychart-0.1.0                                 
USER-SUPPLIED VALUES:                                
{}                                                   

...

---                                                  
# Source: mychart/templates/configmap.yaml           
apiVersion: v1                                       
kind: ConfigMap                                      
metadata:                                            
  name: dining-saola-configmap                       
data:                                                
  config.json: |-                                    
    {                                                
        "val": "key"                                 
    }     

但是我希望它从values.yaml中获取config.json文件中的值.有可能吗?

But I want it the values in the config.json file to be taken from values.yaml. Is that possible?

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  config.json: |-
    {
{{- range $key, $val := .Values.json }}
{{ $key | quote | indent 6}}: {{ $val | quote }}
{{- end}}
    }

values.yaml

json:
  key1: val1
  key2: val2
  key3: val3

helm install --dry-run --debug mychart

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mangy-hare-configmap
data:
  config.json: |-
    {
      "key1": "val1"
      "key2": "val2"
      "key3": "val3"
    }

这篇关于Kubernetes-如何定义使用Yaml中的文件构建的ConfigMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆