Comparing Terraform and Kubernetes CRD for Infrastructure as Code
Terraform & Kubernetes comparison
Kubernetes has emerged as the de facto container orchestration platform. Its flexibility, scalability, and robust ecosystem make it an ideal choice for managing complex microservices architectures. Alongside Kubernetes, Infrastructure as Code (IaC) has revolutionized the way we manage infrastructure resources, providing a more reliable, reproducible, and version-controlled approach to deployments.
Terraform is a popular IaC framework with support for most cloud providers. Kubernetes also provides a mechanism to extend itself to manage custom infrastructure (resources) via Custom Resource Definitions (CRD). The wide adoption of Kubernetes as a computing platform leads to the question of using CRDs as an IaC tool. In this post, we compare both these tools and identify the use cases fit for them.
Developer Experience
Terraform
HCL (HashiCorp Configuration Language) is the configuration syntax used by Terraform to define and manage infrastructure resources. You can extend the framework by writing modules to manage custom resources. You can choose the backend to allow Terraform to store the state. I faced some problems when my dev environment became out of sync with the actual resources, but eventually found my way.
Kubernetes CRD
The resources would be expressed in YAML. For functionality like loops, you would need a templating engine like Jinja. If your services are already deployed on Kubernetes, this is a good option for managing cloud infrastructure the same way. This is also a good option for custom resources. E.g if you wish to manage custom Kafka installation, you can use Strimzi.
One more advantage of using CRDs, is that they provide a RESTFul interface to manage your custom resources. This enables more ways to manage your infrastructure.
Adoption & support
Terraform
Terraform is quite mature, so most cloud providers already have terraform modules. You can check out the list here. The codebase seems pretty active & there is a lot of support on Stackoverflow.
Kubernetes CRD
Kubernetes is itself a very mature project, but the operator (CRD) implementation might not be very mature. There is an open-source project Crossplane, that provides CRDs for all cloud providers that support Terraform. AWS & Azure have open source providers (CRD). Adoption & support wise Terraform is more prevalent, but Kubernetes CRDs have a good enough ecosystem to make it useful. If your organization is already a Kubernetes shop, it is worth considering using CRDs.
If your org is heavily invested in Kubernetes, CRDs may integrate seamlessly, while Terraform offers broader support for diverse infrastructures. Assessing the complexity of managed resources & technical skillset can guide your decision.