top of page
Writer's picturesandeepseeram

Using Terraform to Deploy to Kubernetes

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.


Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.


By using both Terraform and Kubernetes, we can parametrize application deployment, manage deployment configuration, and track differences in configuration and versioning.


Terraform uses provider file to interface with various API’s and exposing the resources. A provider is versioned and included as part of a configuration file.


For Example:


Terraform Initialization is the first step,

Ø terraform init

This command will initialize terraform in the current working directory and creates the local state directory or connects to the remote state store and installs any required providers, from the above example of the provider file, it will install the specified Kubernetes provider.




Initializing terraform created .terraform directory for local state management.

State file include the current state of resources deployed.


Terraform Planning: You can write what you want to build in code and check how it’s going to build.

Ø terraform plan

Terraform plan helps you dry-run of changes and displays the differences as per the state file.


You can see how many resources are going to be added, changed and destroyed.



It should output two changes and create a binary file called mydeployment.tfplan


Terraform Graph

Terraform uses graph theory to create and modify resources in the correct order, in some cases we can also set dependencies. For. Ex.: To create a Kubernetes POD A, Dependency to create a storage volume A first.

Ø terraform graph


Generates a digraph layout of your deployment which can be visually represented in graphviz tool.




Once we verify our terraform plan and graph, we can proceed to deploy the resources using terraform apply



In this example, we deployed a nginx web server on kubernetes cluster.





187 views

Recent Posts

See All

Comments


Commenting has been turned off.
bottom of page