Terraform vs Kubernetes - Everything You Need To Know

Terraform

Terraform is an open source tool built and managed by HashiCorp. Terraform allows you to use code to define, provision, and maintain infrastructure, a framework known as infrastructure as code. Terraform enables you to develop, automate, and version any infrastructure-regardless of where it’s deployed-safely and efficiently.

Why Terraform

Terraform is the go-to tool when you need to repeatedly provision resources for deploying large systems. Terraform allows you to create a high-level infrastructure description that it then uses to generate and execute plans to match any desired end state, all from a single workflow. The tool can also quickly reprovision infrastructure configuration changes when required.

Kubernetes

Kubernetes, sometimes abbreviated as K8s, is an industry-leading, open source container orchestration tool for running and maintaining production services and workloads. Developed by Google, in 2014 it was donated to the Cloud Native Computing Foundation and open sourced. Kubernetes manages containerized applications by keeping track of their states.

Why Kubernetes

Kubernetes introduces speed and visibility to the process of building, testing, and releasing/deploying your applications, and is the most popular and widely supported container orchestration tool. It keeps your container workloads running and scaled, and monitors performance to ensure that your application is highly available.

Working with Terraform

With Terraform, you can define low-level resources like networks and servers, as well as high-level resources such as DNS and SaaS, in human-readable configuration files written in the HashiCorp Configuration Language (HCL). Any infrastructure created from these configuration files can also be versioned, reused, and shared.

Terraform Workflow

Terraform follows a specific workflow while creating, updating, or destroying resources.

  • Write: In this stage, you define any resource you wish to create using code, just as you would write any other project. A resource can be as simple as a virtual machine within a virtual private cloud. Below is a simple Terraform configuration file in the write stage.
# Create repository
$ git init test-infra && cd test-infra

Initialized empty Git repository in /.../test-infra/.git/

# Write initial config
$ vim test.tf

# Initialize Terraform
$ terraform init

Initializing provider plugins...
# ...
Terraform has been successfully initialized!
  • Plan: In this stage, Terraform generates an execution object using the terraform plan command. The object shows the resources that will be created, versioned, or scaled down.
  • Apply: When the terraform apply command is run, Terraform executes the actions scripted in the configuration plan to create a reproducible infrastructure.

Common Use Cases

You can use Terraform to perform many tasks, but these are some of the most common.

Tracking Infrastructure

Terraform keeps track of your deployed resources using a state file, which serves as a point of reference when changes are to be made in the resources. Using a state file, Terraform can determine what changes to your infrastructure are necessary to match any desired end state.

Multi-Cloud Deployment

Terraform is a cloud-agnostic platform. You can use a configuration file written in HCL to manage infrastructure resources hosted in multiple cloud providers. It also handles cross-cloud dependencies.

Managing Multi-Tier Applications

Multi-tier applications have a workflow that separates functionalities by specifying a single logic to execute for every tier. Each tier is defined as a collection of resources, and Terraform automatically takes care of the dependencies required between each tier. To ensure consistency between the tiers, dependencies and plug-ins are needed, which can be challenging to install manually.

Software-Defined Networking

Terraform can interact with Software-Defined Networks (SDNs) to automatically configure the network according to the needs of its applications. This lets you move from a ticket-based workflow to an automated one, reducing deployment times.

Working with Kubernetes

As Kubernetes offers you the means to orchestrate containerized applications, it comes with a lot of flexibility and portability. As a developer, you can choose the operating system, container runtime, CI/CD tools, cloud platforms, among other things. It also allows you the choice of self hosting or using a hosting platform.

Kubernetes Components

Though Kubernetes is a complex system, it can be broken down into several basic components.

  • Cluster: A group of worker machines (nodes) that run containerized applications.
  • Node: An instance of a virtual machine or a physical machine that can run a pod.
  • Pod: The smallest unit of a computing resource that can be managed by Kubernetes. A pod can run a single instance of a containerized application.
  • Controller: A mechanism to watch the state of a cluster or any Kubernetes resource.

Common Use Cases

You can use Kubernetes for many common tasks. Some of the most frequent use cases are discussed in this section.

Orchestrating Containers Across Several Hosts

Because it’s platform independent, Kubernetes can host your container apps across multiple physical machines or cloud instances. It also maintains a failover watch layer to ensure high availability.

Managing Compute Resources

It’s often less expensive to run a single dedicated Kubernetes cluster than it is to run several separate servers. Additionally, it’s easier to manage such a cluster than to manage different hosts running multiple servers.

Running CI/CD Platforms

Kubernetes has become a major part of CI/CD methodologies, and is the preferred option for running CI/CD platforms such as Jenkins, Spinnaker, and Drone. As long as the CI/CD platforms can be packaged in a container, Kubernetes can run them.

Storage Orchestration

Kubernetes supports dynamic storage volume provisioning. You can attach storage systems from any platform of your choice, including local storage, cloud storage, and network storage.

Service Discovery and Load Balancing

Kubernetes exposes a container cluster service to other platforms through a cluster’s DNS endpoint. If network traffic to a cluster is high, Kubernetes can load balance and distribute the traffic to keep the deployed instances stable and available.

Differences and Similarities of Terraform and Kubernetes

In this section, you’re going to learn about some of the similarities and differences between these two tools. It’s important to note that this isn’t an attempt to determine which tool is “better,” since they serve very different purposes. Kubernetes is a container tool, while Terraform is an infrastructure tool. The difference in application and implementation creates the opportunity for developers to leverage both tools, resulting in a highly resilient system.

Leveraging both Terraform and Kubernetes

Provisioning Kubernetes clusters is a complex task, especially when multiple cloud providers are involved. A highly cloud-independent tool like Terraform comes in handy in such situations. Although you could use kubectl for full lifecycle management of your Kubernetes resources as described in YAML files, orchestration with Terraform offers some benefits. Through a descriptive, low-code tool such as Terraform, application configurations, deployment pipelines, and even scaling options in Kubernetes are all made easy, regardless of the platforms involved. Since Kubernetes is also highly declarative, Terraform and Kubernetes really complement each other. Terraform can be used to manage Kubernetes infrastructure, helping you to orchestrate your applications and run them at scale. This alleviates some of the challenges of running Kubernetes, including problems like detectingconfiguration drift, that is, planned or unplanned changes.

Similarities

Though they’re used for very different tasks, there are some major similarities between the two tools.

Agentless Operation

Neither Terraform nor Kubernetes needs additional agents installed in order to function correctly. Platforms like Google Cloud, AWS, and Azure take care of installing, managing, and releasing agent software into parts of the resources you need to provision.

Declarative Approach

Terraform templates are declarative HCL files that define infrastructure as a desired end state. Likewise, Kubernetes templates are written as declarative YAML files, and the end product is a declarative deployment object.

Version Control

Both tools integrate seamlessly with version control systems such as GitHub, providing a single viewpoint to see the status and impact of any change made in the configurations.

Cross-Platform Operations

You can run Kubernetes and Terraform on on-premises bare metal, OpenStack, or even public clouds. Both tools leverage APIs from multiple environments, avoiding vendor lock-in issues.

State Management

Terraform and Kubernetes store state and cluster data about your infrastructure from the configurations you define. You can tie resources to your configurations from the state data and create, update, or push changes to manage your infrastructure.

Managing Clusters

Kubernetes has a number of built-in tools, such as kubectl, which support different ways of managing Kubernetes cluster objects. However, it can be challenging for organizations to manage these objects because of the level of expertise Kubernetes requires. Managed Kubernetes options, such as third-party tools like kubestack, can help meet this challenge.

Differences

For all their similarities, the tools also have some major differences. Terraform focuses on codifying resources that your applications rely on, while Kubernetes is used for automating container tasks.

Configuration Language

Terraform defines declarative objects through Hashicorp Configuration Language, or HCL. With an HCL file, you can create resources that will run across multiple cloud platforms. Kubernetes, on the other hand, defines declarative objects as YAML or JSON files that illustrate how to define and manage Kubernetes objects. YAML is preferred to JSON in writing configuration files because it is more user-friendly, but for this purpose, they may be used interchangeably.

Resource Creation

The Terraform CLI is a command line interface to Terraform, and supports subcommands and switches such as terraform plan and terraform apply. Terraform uses the CLI to run commands that execute and manage declarative configurations to create any desired resources.

Conclusion

In this article, you have learned about two tools, Terraform and Kubernetes, and examined the similarities and differences between the tools. You’ve seen that Terraform uses a single framework to automate resources declaratively in various cloud platforms, while Kubernetes steps in to orchestrate resource management, deployments, and load balancing for your container environments. You’ve also learned how these two automation tools can work harmoniously, allowing you to use Terraform for creating and deploying Kubernetes resources.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Loft Labs

Loft Labs

>> www.loft.sh << Build Your Internal Kubernetes Platform With Virtual Clusters, Namespace Self-Service & Secure Multi-Tenancy