如何从掌舵文件创建配置映射?

时间:2019-07-26 11:39:43

标签: kubernetes google-cloud-platform google-kubernetes-engine kubernetes-helm

我有一个运行在容器中的容器,该容器需要一个配置文件。部署/服务等是使用helm进行部署的,理想情况下,我想使用相同的方法来设置配置,如果可能的话,我想使用helm的模板引擎对配置文件进行模板化。

我遇到了这个: https://www.nclouds.com/blog/simplify-kubernetes-deployments-helm-part-3-creating-configmaps-secrets/

我具有以下文件结构:

/chart
  /templates
    my-config-map.yaml
  /config
    application.config

和my-config-map.yaml包含以下内容:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  labels:
    app: {{ template "app.prefix" . }}
data:
  {{ (tpl (.Files.Glob "config/*").AsConfig . ) | indent 2 }}

当我运行此命令时:

kubectl get configmaps my-config -n my-namespace -o yaml

我得到:

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2019-07-26T11:11:05Z
  labels:
    app: my-app
  name: my-config
  namespace: my-namespace
  resourceVersion: "2697856"
  selfLink: <selflink>
  uid: 0fe63ba8-af96-11e9-a73e-42010af00273

请注意,它似乎没有任何数据。但是,如果我使用以下命令从命令行创建它:

kubectl --namespace my-namespace create configmap my-config --from-file=application.conf

我明白了,它确实包含数据:

apiVersion: v1
data:
  application.conf: |-
    conf {
      ...
kind: ConfigMap
metadata:
  creationTimestamp: 2019-07-26T11:00:59Z
  name: my-config
  namespace: my-namespace
  resourceVersion: "2695174"
  selfLink: <selflink>

我到底在做什么错?

4 个答案:

答案 0 :(得分:0)

我的猜测是Helm正在“模板”文件夹下寻找“配置”。

尝试将config文件夹移到模板下:

/chart
  /templates
    my-config-map.yaml
    /config
      application.config

或更改

(.Files.Glob "config/*") 

(.Files.Glob "../config/*")

此外,请记住,Helm 2.5中引入了 tpl 功能。

希望有帮助!

答案 1 :(得分:0)

对于它的价值,您缩进了2,但是在粘贴的代码中已经缩进了2。因此,有效缩进4。您需要:

data:
{{ (tpl (.Files.Glob "config/*").AsConfig . ) | indent 2 }}

答案 2 :(得分:0)

我测试,这会起作用

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  labels:
    app: {{ template "app.prefix" . }}
data:
  application.conf: |-
{{ .Files.Get "config/application.config" | nindent 4 }}

答案 3 :(得分:0)

背景

本教程和问题非常古老(要求1年零4个月前)。 Helm当前使用的版本是3。它不需要Tiller

如您在问题中所述,您必须使用Glob并将文件放入正确的目录中。我已经在GKE的{​​{1}}集群上对此进行了测试。

Helm3

解决方案-步骤

1。创建$ helm version version.BuildInfo{Version:"v3.2.1"

我已经在我的test-chart目录中创建了它

/home/user

2。创建目录,您将在其中放置/下载配置文件

$ helm create test-chart
Creating test-chart
$ ls
postgress test-chart

我使用了Kubernetes docs中的示例。

$ cd test-chart/
$ ls
charts  Chart.yaml  templates  values.yaml
$ mkdir configmap
$ ls
charts  Chart.yaml  configmap  templates  values.yaml
$ cd configmap/

3。在 $ wget https://kubernetes.io/examples/configmap/ui.properties ... ui.properties 100%[===========================================================================================>] 83 --.-KB/s in 0s 2020-12-14 15:14:14 (687 KB/s) - ‘ui.properties’ saved [83/83] ... user@cloudshell:~/test-chart/configmap (project)$ cat ui.properties color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice 目录中创建ConfigMap文件

templates

按如下所示创建$ cd ~/test-chart/templates yaml。

configmap

4。安装图表/使用apiVersion: v1 kind: ConfigMap metadata: name: test-config data: {{- (.Files.Glob "configmap/*").AsConfig | nindent 2 }}

转到主目录--dry-run,该目录中包含以下文件

~/test-chart

现在,您可以charts Chart.yaml configmap templates values.yaml 绘制图表或使用--dry-run选项,以检查配置YAML的外观。

5。输出

helm install

结论

将作为$ helm install test . --dry-run NAME: test LAST DEPLOYED: Mon Dec 14 15:24:38 2020 NAMESPACE: default STATUS: pending-install REVISION: 1 HOOKS: --- # Source: test-chart/templates/tests/test-connection.yaml ... --- # Source: test-chart/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: test-config data: ui.properties: | color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice --- # Source: test-chart/templates/service.yaml 基础的文件必须位于目录中,该目录与configmap目录和template处于同一级别。您还必须使用Chart.yaml并提供配置文件的正确路径。

相关问题