如何从掌舵文件创建配置映射? [英] How to create a config map from a file in helm?

查看:53
本文介绍了如何从掌舵文件创建配置映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a container running in a pod which requires a single config file. The deployment/service etc are deployed using helm, and ideally I'd like to use the same method to setup the config, and if possible, I'd like to use helm's templating engine to template the config file.

我遇到了这个: 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包含以下内容:

and my-config-map.yaml contains the following:

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

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

Note that it does not appear to have any data in it. However if I create it from the command line using this command:

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

我明白了,它似乎包含数据:

I get this, which does appear to contain the data:

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 .

Background

This tutorial and question is quite old (Asked 1 year, 4 months ago). The currently used version of Helm is 3. It does not require Tiller.

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

As you stated in your question, you have to use Glob and put files into proper directories. I have tested this on my GKE cluster with Helm3.

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

解决方案-步骤

1.创建测试图

Solution - Steps

1. Create test-chart

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

I have created it in my /home/user directory

$ 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.在 templates 目录

3. Create ConfigMap file in templates directory

$ cd ~/test-chart/templates

创建 configmap yaml,如下所示.

Create configmap yaml like below.

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

4.安装图表/使用-空运行

4. Install chart / use --dry-run

转到主目录〜/test-chart ,其中包含以下文件

Go to main directory ~/test-chart, where you have files like below

charts  Chart.yaml  configmap  templates  values.yaml

现在,您可以 helm install 图表或使用-干运行选项,以检查配置YAML的外观.

Now you can helm install chart or use --dry-run option before, to check how configuration YAMLs would looks like.

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 并提供配置文件的正确路径.

Conclusion

File which will be base for configmap must be in the directory, which is on the same level as template directory and Chart.yaml. You also have to use Files.Glob and provide proper path to configuration file.

这篇关于如何从掌舵文件创建配置映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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