如何在 Terraform 中使用 yaml 文件?

How to use yaml file in Terraform?

我在单独的 yaml 文件中有 kubernetes 配置。我想在 运行 terraform 时使用那个 yaml 文件,我可以这样做吗?如果是,那么如何。

据我所知,这已经讨论了很长时间,但尚未实施:https://github.com/terraform-providers/terraform-provider-kubernetes/issues/141

如果有帮助,我经常使用这个工具将 YAML 文件转换为 terraform 规范。这是相当可靠的。 https://github.com/sl1pm4t/k2tf

根据我的经验,Terraform 支持 Kubernetes 提供程序,但该提供程序中的所有内容都是独立的,例如部署、pod、服务等。它不提供从配置加载所有内容的方法文件。

因此,要从配置文件进行部署,我建议您将 kubectl apply -f config_file 放在 null_resource 中。而且删除所有已经部署的东西也很简单mull_resource,你只需要使用Terraform命令terraform destroy,它会删除所有通过Terraform文件部署的资源。[=14] =]

可以,但是您必须使用第 3 部分 kubernetes 提供程序

# Retrieve an access token as the Terraform runner
data "google_client_config" "provider" {}

# Same parameters as kubernetes provider
data "google_container_cluster" "my-cluster" {
  name      = "my-cluster"
  location  = "europe-west4-a"
}

provider "kubectl" {
  load_config_file       = false
  host                   = "https://${google_container_cluster.my-cluster.endpoint}"
  cluster_ca_certificate = "${base64decode(google_container_cluster.my-cluster.master_auth.0.cluster_ca_certificate)}"
  token = data.google_client_config.provider.access_token
}

data "kubectl_filename_list" "manifests" {
    pattern = "./manifests/*.yml"
}

resource "kubectl_manifest" "test" {
    count     = length(data.kubectl_filename_list.manifests.matches)
    yaml_body = file(element(data.kubectl_filename_list.manifests.matches, count.index))
}

Link 提供者 https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs

类似问题 How To Run kubectl apply commands in terraform

具体回答原问题,哪些标签包含AzureAKS,以及提问者要求在terraform中使用纯kubernetes yaml manifests -我用过 gavinbunney/kubectl:

terraform {
  required_providers {

    kubectl = {
      source  = "gavinbunney/kubectl"
      version = "1.14.0"
    }
  }
}

provider "kubectl" {
  load_config_file = false
  host = azurerm_kubernetes_cluster.REDACTED.kube_config.0.host
  cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.REDACTED.kube_config.0.cluster_ca_certificate)
  token = yamldecode(azurerm_kubernetes_cluster.REDACTED.kube_config_raw).users[0].user.token
}

可以在 original topic in Github

中找到更多信息