繁体   English   中英

如何从 helm 中的文件创建配置映射?

[英]How to create a config map from a file in helm?

我有一个在 pod 中运行的容器,它需要一个配置文件。 部署/服务等是使用 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>

我到底做错了什么?

背景

这个教程和问题已经很老了(问 1 年,4 个月前)。 当前使用的Helm版本是3 它不需要Tiller

正如您在问题中所述,您必须使用Glob并将文件放入正确的目录中。 我已经在我的GKE集群上用Helm3测试过这个。

$ helm version
version.BuildInfo{Version:"v3.2.1"

解决方案 - 步骤

1. 创建test-chart

我已经在我的/home/user目录中创建了它

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

2.创建目录,您将在其中放置/下载带有配置的文件

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

我使用了Kubernetes docs 中的示例。


$ 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

3.在templates目录下创建ConfigMap文件

$ cd ~/test-chart/templates

像下面这样创建configmap yaml。

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
data:
  {{- (.Files.Glob "configmap/*").AsConfig | nindent 2 }}

4.安装chart/使用--dry-run

转到主目录~/test-chart ,其中有如下文件

charts  Chart.yaml  configmap  templates  values.yaml

现在您可以在之前使用helm install chart 或使用--dry-run选项来检查配置 YAML 的样子。

5. 输出

$ 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处于同一级别。 您还必须使用Files.Glob并提供配置文件的正确路径。

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

尝试移动模板下的 config 文件夹:

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

或改变

(.Files.Glob "config/*") 

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

另外,请记住, tpl函数是在 Helm 2.5 中引入的。

希望有帮助!

对于它的价值,您正在缩进 2,但在粘贴的代码中已经缩进了 2。 所以,有效地缩进 4。你想要:

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

我测试,这会工作

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM