【问题标题】:External deployment configuration with terraform and helm带有 terraform 和 helm 的外部部署配置
【发布时间】:2020-06-03 12:04:26
【问题描述】:

我对 terraform 很陌生,所以我的问题可能很幼稚,但我仍然不知道如何解决它。
我有一个使用 helm_release (https://www.terraform.io/docs/providers/helm/r/release.html) 的 terraform 脚本,问题是我在单独的 git 存储库中有部署配置。假设:https://project.git/configuration.
我想要实现的是能够添加所有文件,例如https://project.git/configuration/dev(用于开发环境部署)配置映射。
terraform模块的结构是:

+project
+-helm
+--templates
+---configmap.yaml
+---deployment.yaml
+---ingress.yaml
+---service.yaml
+--chart.yaml
+--values.yaml
+-helm.tf
+-other tf files

我需要将配置库中的所有文件放在下面

+project
+-helm
+--configuration

只有这样我才能使用它:

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

在我的 configmap.yaml 中,helm 要求将这些文件放在图表目录中。

【问题讨论】:

  • 我也热衷于制作 https://project.git/configuration 一个 terraform 模块。所以我可以将它用作子模块。但是问题还是一样,如何让这些文件在 project/helm/configuration 中可用

标签: terraform kubernetes-helm


【解决方案1】:

您只能对实际与图表正确捆绑的文件使用 Helm .Files 机制,而不是您尝试注入的外部配置。

如果您打算使用 Terraform 来管理所有内容,一种选择是让 Terraform 创建一个包含您需要的文件内容的 kubernetes_config_map,并将对该 ${kubernetes_config_map.with_the_files.name} 的引用传递给 Helm 图表。

如果您想主要使用 Helm,但将文件传递到外部,则可以将它们作为值传递。如果你有 Helm values.yaml 语法:

configFiles:
  test.yaml: |
    key: value
    key2: value2

您可以将其发送到 ConfigMap 中。您可以遍历 configFiles,或使用简单记录的 Helm toYaml 扩展来渲染整个内容。

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "mychart.name" . }}
data:
{{ .Values.configFiles | toYaml | indent 2 }}

完成此操作后,您可以通过 helm install --set 选项或等效的 Terraform helm_release set_string 设置传递这些:

resource "helm_release" "chart" {
  name = "chart"
  chart = "..."
  set_string {
    name = "configFiles.test\\.yaml"
    value = "${file("${path.module}/test.yaml")}"
  }
}

【讨论】:

  • 非常感谢。这看起来很棒。我对我的解决方案进行了过度设计,但你的方法看起来更容易,我肯定会尝试的。我目前所做的是我将配置存储库设为最小的 terraform 模块(将环境名称作为输入并返回部署配置目录的完整路径,例如 ${path.module}/dev)在主 terraform 中,我只是在复制使用本地 exec(在 null_resource 中)从那里的所有文件到 helm 所在的目录并调用 helm_release。有效,但恕我直言,闻起来很糟糕。
【解决方案2】:

经过多次尝试...我提出了最简单的解决方案。我使用了.gitmodules (https://git-scm.com/book/en/v2/Git-Tools-Submodules)。在项目 git clone 期间,配置可作为 ./project/helm/configuration 中的子模块使用。不需要地形技巧。唯一必须做的,有点隐含的是,必须设置特定的 git 设置以在克隆期间强制下载子模块(默认目录为空)。

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2020-11-24
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多