[英]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
:要具体回答原始问题,哪些标签包含
Azure
和AKS
,以及提问者要求在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.