Articles

How to Manage Kubernetes using Terraform?

by Krishan Kumar SEO Expert & Content Marketer
Terraform Kubernetes Deployment

Kubernetes continues to work to make a difference in the tech world with its portability and flexibility in container orchestration. It has led to a growing need for Kubernetes to be connected, configured, and managed with other tools and resources. Also, it has led to the development of new automation products, infrastructure, and features to meet this growing need.

 

Terraform is one tool that offers the most effective way to configure Kubernetes. It is a popular and commonly used infrastructure-as-code product that includes a connector to Kubernetes, aptly called Kubernetes provider.

 

Terraform can create templates and provision infrastructure, making it stand out from other similar tools. Templates are created by specifying the resources you will need to deploy your infrastructure. With terraform, you describe the details of your infrastructure-as-code and terraform subsequently handles the provisioning.

 

Terraform lets you script your infrastructure. However, Ansible is a configuration management tool that automates the configuration of software and systems in your infrastructure.

 

Let's look at Terraform as a tool to provision Kubernetes clusters.

 

  1. Terraform allows you to keep Kubernetes cluster definitions within your code.
  2. It uses the same declarative syntax to provide the lower underlying infrastructure.
  3. Terraform allows you to modify Kubernetes clusters using variables.
  4. It includes a dry-run function for modifying Kubernetes clusters before applying any changes.
  5. Terraform's ability to use the same configuration language when provisioning Kubernetes or deploying applications into it is a significant advantage.
  6. Terraform is a single command that can create, update, or delete resources and pods without looking at APIs.
  7. Terraform recognizes the interdependence of resources and modularizes the infrastructure within the code.
  8. Terraform reduces product delivery times and aids in disaster recovery.

 

After we have discussed the benefits of Terraform to Kubernetes, we will move on to learn more about its workings.

 

Important to know that Terraform Kubernetes provider doesn't build or deploy Kubernetes Clusters. It requires the Kubernetes Cluster to run before using the cluster. We'll look at an example to show you how Terraform Kubernetes provider’s work.

 

Terraform Kubernetes Provider: A Use Case

 

Terraform Kubernetes providers are used to interacting with Terraform Kubernetes providers. To do this, create a namespace, deploy the application to a pod and then expose the pod to users as a service. For these things, your Kubernetes cluster must be up and running. Below are the steps:

Step 1: Configure Our Kubernetes Provider

 

Configuration is the first step in Terraform Kubernetes deployment. We will create a configuration file at http://kube/config to do this. Next, we will need to include the Terraform instance's config. Use the following code block to do this.

 

provider "kubernetes" {

   host = "https://0.0.0.0"

}

 

Step 2: Install the Pod

 

Terraform provides a pod where Kubernetes manages containers. A pod is usually one or more containers. They are scheduled on cluster nodes according to the available memory.

We'll be using Terraform to build our pod and then expose port 80 for the users.

 

resource "kubernetes_pod" "example" {

  metadata {

    name = "example-test"

    labels {

      App = "example"

    }

  }

 

  spec {

    container {

      image = "example/http-echo:0.1.0"

      name  = "example-test"

 

      port {

        container_port = 80

      }

    }

  }

}

 

Step 3: Expose the Pod with A Service

 

The Terraform configuration also includes an output that prints out the load balancer's IP address, making it easier for the operator. This output is in addition to indicating the Terraform service function.

 

resource "kubernetes_service" "example" {

  metadata {

    name = "example-test"

  }

  spec {

    selector {

      App = "${kubernetes_pod.example.metadata.0.labels.App}"

    }

    port {

      port        = 80

      target_port = 80

    }

    type = "LoadBalancer"

}

}

 

output "load_balancer_ip" {

  value = "${kubernetes_service.example.load_balancer_ingress.0.ip}"

}

 

Step 4: Verify that the application is working.

 

After having done all necessary configurations, we must verify that the application is running. You can check that the application runs by using curl in the terminal.

 

$ curl -s $(terraform output load_balancer_ip)

 

Open your preferred browser, and type the IP address. If everything works as it should, you will see your welcome page.

 

Container instances pass configurations using config_map. It is not a good way of protecting sensitive information. Terraform Kubernetes provides a tool called Kubernetes_secrets for sensitive information such as passwords in container instances. However, you don't want to make them available to the entire cluster.

 

Kubernetes_secrets creates an anonymous resource by default and make it available to all pods in the namespace.

 

Example Of Kubernetes_secret

 

resource "kubernetes_secret" "example" {

  metadata {

    name = "example-test"

  }

 

  data = {

    username = "admin"

    password = "GuessPassword"

  }

 

  type = "kubernetes.io/basic-auth"

}

 

You can import the secrets by running this command in your terminal.

 

$ terraform import kubernetes_secret.example default/my-secret

 

 

Terraform directly provides Storage and Persistent volumes. It is a good idea to provision Storage Class with Terraform (to all volumes) while Kubernetes manages it. It's crucial for control and scalability.

 

Conclusion

 

Terraform makes it simple to manage Kubernetes resources and clusters efficiently. It allows organizations to work with infrastructure-as-code, management of cloud platforms, and also the opportunity to create modules for self-service infrastructure. The Terraform Kubernetes provider provides all the tools required to manage Kubernetes clusters within an environment.

Sponsor Ads


About Krishan Kumar Innovator   SEO Expert & Content Marketer

18 connections, 1 recommendations, 92 honor points.
Joined APSense since, January 16th, 2020, From New York, United States.

Created on Jun 16th 2022 02:18. Viewed 215 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.