【发布时间】:2021-09-27 03:27:02
【问题描述】:
我想通过 Terraform 模板化我的 Kubernetes 清单,并从 local-exec 配置器执行 kubectl apply。
一切似乎都很完美,直到我意识到资源(服务和部署)实际上并没有被terraform apply 破坏。
清单以这种方式模板化和应用:
data "template_file" "service_template" {
count = length(var.services)
template = file("${path.module}/templates/${lookup(var.services[count.index], "name")}.tpl")
vars = {
cluster_name = var.cluster_name
tag = lookup(var.services[count.index], "tag")
}
}
resource "local_file" "template" {
count = length(var.services)
content = data.template_file.service_template[count.index].rendered
filename = "${path.module}/deployments/${lookup(var.services[count.index], "name")}.yaml"
}
resource "null_resource" "apply" {
count = length(var.services)
provisioner "local-exec" {
command = "kubectl apply -f ${path.module}/deployments/${lookup(var.services[count.index], "name")}.yaml --kubeconfig config_file_path"
}
}
我想生成清单,以便在需要时能够轻松地手动管理我的服务/部署。
是否有人像这样部署清单并在销毁后设法保持干净状态?
或者唯一的解决方案是使用 kubernetes_service 和 kubernetes_deployment 资源?
编辑:
我尝试使用将 on_destroy 值分配给 when 的 local-exec 配置程序,但遇到了引用错误:
Destroy-time provisioners and their connection configurations may only
reference attributes of the related resource, via 'self', 'count.index', or
'each.key'.
References to other resources during the destroy phase can cause dependency
cycles and interact poorly with create_before_destroy.
【问题讨论】:
-
你见过registry.terraform.io/providers/hashicorp/kubernetes-alpha/…吗?这将为您提供管理清单文件生命周期的更好选择。
-
谢谢,我没看到,它可以满足我的需要!我也复制了这个有趣的链接:hashicorp.com/blog/…
-
@ydaetskcoR 这种管理清单的方式的一个问题是,鉴于清单将在 Terraform 中硬编码为 HCL,我看不到如何动态部署服务。
标签: kubernetes terraform amazon-eks