简体   繁体   English

如何在Terraform中使用yaml文件?

[英]How to use yaml file in Terraform?

I have kube.netes configuration in a seperate yaml file.我在单独的 yaml 文件中有 kube.netes 配置。 I want to use that yaml file while running terraform can I do it?我想在运行 terraform 时使用那个 yaml 文件,我可以这样做吗? If yes, then how.如果是,那么如何。

Yes you can, but you have to use 3rd-part kubernetes provider是的,您可以,但您必须使用第 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 to provider https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs链接到供应商https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs

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

To answer to the original question specifically, which tags contain Azure and AKS , as well as the asker is requesting to use pure kube.netes yaml manifests in terraform - I've used gavinbunney/kubectl :要具体回答原始问题,哪些标签包含AzureAKS ,以及提问者要求在terraform中使用纯kube.netes 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
}

More info could be found in the original topic in Github更多信息可以在 Github 的原始主题中找到

As far as I know this has been talked about for quite some time but as of yet hasn't been implemented: https://github.com/terraform-providers/terraform-provider-kubernetes/issues/141据我所知,这已经讨论了很长一段时间,但尚未实施: https://github.com/terraform-providers/terraform-provider-kubernetes/issues/141

If it helps, I often use this tool to convert YAML files to terraform specification.如果有帮助,我经常使用此工具将 YAML 文件转换为 terraform 规范。 It is quite reliable.这是相当可靠的。 https://github.com/sl1pm4t/k2tf https://github.com/sl1pm4t/k2tf

According to my experience, Terraform supports the Kubernetes provider, but all the things in that provider are separate, such as deployment, pod, service and etc. It does not provide a way to load all the things from a configuration file.根据我的经验,Terraform 支持 Kubernetes 提供程序,但该提供程序中的所有内容都是独立的,例如部署、pod、服务等。它不提供从配置文件加载所有内容的方法。

So, to deploy from the configuration file, I recommend you put the kubectl apply -f config_file in the null_resource .因此,要从配置文件进行部署,我建议您将kubectl apply -f config_file放在null_resource中。 And it's also simple to delete all the things that have deployed with multiple mull_resource, you just need to use the Terraform command terraform destroy , it will remove all the resources that deployed through the Terraform file.而且删除所有部署了多个mull_resource的东西也很简单,你只需要使用Terraform命令terraform destroy ,它将删除所有通过Z303E96F80576360DAEBZ0C7B文件部署的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform - 如何从 YAML 文件创建组和用户 - Terraform - How to create groups and users from YAML file 如何在 ingress 中使用 secrets in.yaml 文件 - how use secrets in .yaml file in ingress Terraform CDKTF:如何在现有的 terraform 项目中使用/导入 CDKTF 代码? - Terraform CDKTF: How to use/import CDKTF code in existing terraform project? 如何在地形中使用'depends_on'和'for_each'? - How to use 'depends_on' with 'for_each' in terraform? 如何部署特定的 tf 文件 (Terraform) - How to deploy a specific tf file (Terraform) 如何将 Terraform 中的逻辑用于 Azure? - How can i use logic in Terraform for Azure? 如何有条件地使用 Terraform 中的现有资源? - How to conditionally use existing resource in Terraform? Kustomize 如何找到部署的 yaml 文件? - How Kustomize finds the deployment's yaml file? 您如何使用 terraform 应用程序的 output 作为另一个 terraform 变量的输入? - How do you use the output of a terraform apply as input into another terraform variable? 用 403 锁定我的 terraform state 文件; 如何识别执行terraform init的账户? - Locked out of my terraform state file with 403; How to identify the account performing terraform init?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM