您可以在Terraform中使用YAML定义Kubernetes Services/Pod吗? [英] Can you define Kubernetes Services / Pods using YAML in Terraform?

查看:227
本文介绍了您可以在Terraform中使用YAML定义Kubernetes Services/Pod吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Kubernetes提供程序来描述服务/吊舱在Terraform中.

I am using the Kubernetes Provider to describe services/pods in Terraform.

使用Hashicorp配置语言来定义 kubernetes_pod kubernetes_service 资源,因为Kubernetes文档描述了所有内容在YAML中,这意味着您需要将其转换为HCL.

It can get confusing using the Hashicorp Configuration Language to define kubernetes_pod or kubernetes_service resources because the Kubernetes documentation describes everything in YAML which it means you need to translate it into HCL.

是否可以将Pod定义为YAML并将其与kubernetes_podkubernetes_service资源用作模板?

Is it possible to define pods as YAML and use them with kubernetes_pod and kubernetes_service resources as templates?

推荐答案

虽然Terraform通常使用HCL,但这是JSON的超集(非常类似于YAML本身),因此

While Terraform normally uses HCL, this is a superset of JSON (much like YAML itself) so can also read JSON.

一个可能的选择是获取您已经拥有的YAML示例,并将其转换为JSON,然后在这些示例上使用Terraform.

One possible option would be to take the YAML examples you already have and convert them into JSON and then use Terraform on those.

不幸的是,这不太可能起作用,因为关键字对于Terraform期望的方式可能会有所不同,因此您需要编写一些内容来将输入的YAML基本转换为Terraform资源JSON.在这一点上,可能值得将HCL输出添加到转换中,因此,如果您打算保留Terraform配置,而不仅仅是一次转换和应用配置,则输出的Terraform配置更易读.

Unfortunately, that's unlikely to work because keywords are likely to be different for how Terraform is expecting things so you'd need to write something to do some basic translation of the input YAML to a Terraform resource JSON. At this point, it'd probably be worth just adding HCL output to the conversion so your outputted Terraform config is more readable if you ever intend to keep the Terraform config around instead of just one shot converting and applying the config.

以这种方式进行操作的好处是,您可以使用kubectl或其他工具运行可重用的Kubernetes配置,但具有Terraform生命周期管理的能力,能够计划更改和与非Kubernetes集成基础架构的一部分(例如设置实例以在其上运行Kubernetes集群).

The benefit of doing things this way would be that you have a reusable Kubernetes config that could be ran using kubectl or other tools but gives you the power of Terraform's lifecycle management, being able to plan changes and integration with non Kubernetes parts of your infrastructure (such as setting up instances to run the Kubernetes cluster on).

我并没有使用太多,但是我相信 Kops 将允许您保留pod/服务在典型的Kubernetes YAML文件中进行配置,但随后可以使用Terraform来管理配置,甚至允许您输出Terraform配置,以便可以在Kops本身之外运行它.

I've not used it much but I believe Kops will allow you to keep pod/service config in typical Kubernetes YAML files but can then use Terraform to manage the configuration and even allows you to output the Terraform configuration so you can run it outside of Kops itself.

这篇关于您可以在Terraform中使用YAML定义Kubernetes Services/Pod吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆