如何从 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 文档中的示例。


$ 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. 在目录中创建ConfigMaptemplates文件

$ cd ~/test-chart/templates

创建configmapyaml,如下所示。

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

4. 安装图表/使用--dry-run

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

charts  Chart.yaml  configmap  templates  values.yaml

现在,您可以先helm install图表或使用 --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并提供正确的配置文件路径。

我测试,这将起作用

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

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

尝试在模板下移动配置文件夹:

/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 }}

最新更新