Home > Software engineering >  How does Kubernetes and Terraform work seamlessly together and what role do they each undertake?
How does Kubernetes and Terraform work seamlessly together and what role do they each undertake?

Time:05-09

I am a bit confused about the individual roles of Kubernetes and Terraform when using them both on a project.

Until very recently, I had a very clear understanding of both their purposes and everything made sense to me. But, then I heard in one of Nana's videos on Terraform, that Terraform was also very advanced in orchestration and I got confused.

Here's my current understanding of both these tools:

  • Kubernetes: Orchestration software that controls many docker containers working together seamlessly. Kubernetes makes sure that new containers are deployed based on the desired infrastructure defined in configuration files (written with the help of a tool like Terraform, as IaC).

  • Terraform: Tool for provisioning, configuring, and managing infrastructure as IaC.

So, when we say that Terraform is a good tool for orchestration, do we mean that it's a good tool for orchestrating infrastructure states or docker containers as well?

I hope someone can clear that out for me!

CodePudding user response:

Terraform is an infrastructure as code tool, which allows declaring a resources and their desired state as code and it will determine the plan and execute it to take the infrastructure to a desired state. Terraform needs providers for managing these resources using respective API's. Once resources are provisioned terraform cannot take care of keeping the desired state unless you again run terraform plan to determine if resources are in desired state or not and apply the script to take it to desired state.

On the other hand, kubernetes is expert is orchestrating container workloads which takes care of keeping the workloads in desired state throughout the life cycle of resources. It continuously monitors cluster and make changes to keep desired state of workloads.

Major difference between both is kubernetes is an container orchestration platform which manage desired state of container workloads and many other features. Whereas terraform is a tool which helps you write, provision and maintain the state of resources as a code. It uses provider API's to create resources to match the desired state by identifying difference between current state and desired state of resources.

Both terraform and kubernetes can be used together. There are kubernetes providers for terraform which can help you define desired state of your cluster resources. Once you apply the terraform state, kubernetes takes care of maintaining the desired state.

Kubernetes is very specific to container workload orchestration whereas terraform can be used to work with any resource state management like provisioning cloud resources, server resources or anything that provides terraform provider to manage resources.

Simple example I can think of to better understand the difference is, You can use terraform with docker provider to declare that you want to create a container and once you apply that state container will spin up, but if you delete the container it won't get recreated automatically unless you again run terraform plan which will determine the difference from desired state and apply will recreate the container. To solve this specific problem of maintaining a desired state of container workloads kubernetes orchestration helps. Kubernetes has much more features and flexibility than just container orchestration but this is the core idea of container orchestration.

I hope that helps you understand the difference. In case my understanding is wrong please correct me.

CodePudding user response:

Both the tools are different with their defined usages in any project

Terrraform [IAC tool]: Provision the infrastructure in the cloud or onprem. It can be pushed to some extent to create the Docker host and run Docker containers and its limits ends there.

Kubernetes [container orchestration tool]: while kubernetes is a container orchestration tool, which takes care of the containers,pod networking, loadbalance it, expose it to the outside world via services , ingress etc.

Hope this helps. Cheers

  • Related