Automation has become one of the most important concepts in software development. Automating infrastructure speeds up configuration changes, eliminates the human error risk factor, and provides the necessary transparency for all the teams across the project.
This article will give an overview of two popular automation choices, Terraform and Kubernetes. It will also provide popular use cases for both tools and suggest ways to work together in the infrastructure as code (IaC) environment.
Terraform vs. Kubernetes: Definitions
Terraform and Kubernetes serve different purposes and deal with different levels of software automation.
Terraform is a tool for the safe and efficient management of infrastructure configuration. It gives users the framework for defining infrastructure and enables access to resources via resource providers. The providers cover various popular public cloud platforms, as well as Git hosting platforms and generic HTTP and FTP solutions.
Kubernetes is a container orchestration platform for the automation of deployment, scaling, and management of containerized apps. By managing groups of hosts organized in clusters, Kubernetes enables the running of distributed container systems without downtime.
Note: For more information about Kubernetes, read our comprehensive guide - What is Kubernetes? Container Orchestration Overview.
How Do They Work?
Infrastructure as Code (IaC) is the main concept necessary to understand how Terraform works. The tool features HCL (HashiCorp Configuration Language), a declarative configuration language used to define infrastructure resources.
Regardless of whether they use one or multiple infrastructure providers, Terraform users can utilize HCL to describe their complete infrastructure in the form of code. Terraform achieves this through "providers" - plugins designed to communicate with the cloud and SaaS providers.
Terraform Kubernetes providers help interact with Kubernetes supported resources. While it is possible to manage Kubernetes resources with tools such as kubectl, Terraform enables unifying the workflow and provides full lifecycle management.
Note: Search for providers in Terraform Registry. For example, the phoenixNAP provider is used to connect to PNAP supported resources.
Terraform features commands such as:
terraform init- Initializes a directory that contains Terraform configuration files.
terraform plan- Creates an execution plan that consists of reading the current state of objects to make sure they are up-to-date, compares the current state of the system with the previous state, and proposes changes in objects necessary to match the declared configuration.
terraform apply- Executes the proposed plan.
terraform destroy- Removes objects managed by a certain configuration.
Kubernetes works with clusters - groups of machines, called nodes, which are combined to facilitate the running of containerized applications.
One Kubernetes cluster consists of:
- Pods - The container groups that work together.
- Services - Groups of pods with the same function.
- Replication Controllers - Frameworks for pod replica management.
The structure of the Kubernetes system has two important parts:
- The worker node contains the containerized application and the tools necessary to manage the node as part of the K8s cluster. Each cluster has at least one worker node listening to the API for assignments.
- The control plane contains head nodes that run tools for managing the cluster.
Kubernetes works by processing YAML manifest files designed to declare a desired configuration of the system. To communicate with the Kubernetes API server, users employ a command-line tool such as kubectl.
YAML files feature simple, declarative syntax. An example of a file declaring a Kubernetes deployment would be:
apiVersion: apps/v1 kind: Deployment metadata: name: k8s-demo namespace: default spec: replicas: 3 selector: matchLabels: k8s: web template: metadata: labels: k8s: web spec: containers: - name: k8s-testapp image: testapp:1.0
The file above specifies the deployment called k8s-demo containing three replicas of the pod with the testapp:1.0 image. The file is saved on the system and then applied using a command-line tool, which effectively produces a Kubernetes deployment.
Terraform: Pros and Cons
- Allows multi-cloud deployments with many different resources.
- Helps avoid downtime.
- Facilitates recording, tracking, and managing changes.
- Features declarative syntax.
- Readable and comprehensive documentation.
- It does not fully support GKE (Google Kubernetes Engine).
- It does not feature error handling.
- No rollback - if the need arises, the user must destroy the managed object and re-apply it.
- New releases often have bugs.
Kubernetes: Pros and Cons
- Resource friendly - enables horizontal scaling of infrastructure.
- It helps avoid infrastructure lock-ins.
- Features declarative syntax.
- Automates the healing process by monitoring replicas and making sure the system is always healthy.
- Leading, Google-backed container management tool, with comprehensive documentation.
- Difficult to master.
- Enables only infrastructure orchestration.
- Introducing K8s to an organization may require significant workflow adjustment.
How to Choose?
Since it has been designed with the IaC concept in mind, Terraform is a good choice for organizations aiming to codify their application infrastructure, especially ones which need to manage their infrastructure across multiple public and private clouds. This is an example in which Terraform and Kubernetes can complement each other since Kubernetes is a useful tool for achieving application portability.
Another way in which Terraform can be used to complement Kubernetes is through the Kubernetes Terraform Provider. The provider manages the K8s API server and detects resource configuration changes - something Kubernetes itself has not been designed to do.
Just like Terraform, Kubernetes supports the IaC paradigm. Using Kubernetes in the IaC context can be beneficial when you want to standardize your cluster configuration. Kubernetes is also the best choice for projects that strive to minimize resource usage by introducing horizontal scaling.
By reading this article, you should have a better understanding of Terraform and Kubernetes - the way they work, as well as their good and bad sides. If you are interested in more Kubernetes-related comparisons, read the articles in which we compare K8s with Docker Swarm, OpenStack, Mesos, and OpenShift.