Simple Kubernetes Infrastructure with Nginx Image using Terraform

Simple Kubernetes Infrastructure with Nginx Image using Terraform

Introduction

We are going to explore how to play with Kubernetes infrastructure using Terraform. Kubernetes automates operational tasks of container management and includes built-in commands for deploying applications, rolling out changes to your applications, scaling your applications up and down to fit changing needs, monitoring your applications, and more

Prerequisites

Before we start, ensure that you have the following prerequisites:

  • A minikube, kubectl, is installed on your local machine

  • Terraform installed on your local machine

  • A basic understanding of Kubernetes concepts

Step 1: Set up your local Kubernetes environment

To test the deployment of your application on your local machine we need to install and start the services of minikube and kubectl.

Start your minikube:

minikube start --driver=docker

Step 2: Create a Terraform configuration file

Create a new directory for your Terraform files and create a file named kube.tf. In this file, write the Terraform configuration code to set up your Kubernetes cluster.

Create a directory and main.tf file in it:

mkdir kube-terraform
cd kube-terraform
touch main.tf

kube.tf

terraform {
  required_providers {
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = ">= 2.23.0"
    }
  }
}
provider "kubernetes" {
  config_path = "~/.kube/config"
}
resource "kubernetes_namespace" "nginx" {
  metadata {
    name = "nginx"
  }
}
resource "kubernetes_deployment" "nginx" {
  metadata {
    name      = "my-nginx-ns"
    namespace = kubernetes_namespace.nginx.metadata.0.name
  }
  spec {
    replicas = 2
    selector {
      match_labels = {
        app = "my-nginx-app"
      }
    }
    template {
      metadata {
        labels = {
          app = "my-nginx-app"
        }
      }
      spec {
        container {
          image = "nginx"
          name  = "nginx-container"
          port {
            container_port = 80
          }
        }
      }
    }
  }
}
resource "kubernetes_service" "nginx" {
  metadata {
    name      = "nginx"
    namespace = kubernetes_namespace.nginx.metadata.0.name
  }
  spec {
    selector = {
      app = kubernetes_deployment.nginx.spec.0.template.0.metadata.0.labels.app
    }
    type = "NodePort"
    port {
      node_port   = 31010
      port        = 80
      target_port = 80
    }
  }
}

Step 3: Initialize and apply the Terraform configuration

Now, navigate to the directory where your kube.tf file is located. Run the following command to initialize the Terraform provider and download the necessary plugins:

terraform init

Once the initialization is complete, you can apply the Terraform configuration by running the following command:

terraform apply

Terraform will now inspect your configuration and provision the defined Kubernetes resources in your cluster.

Step 4: Verify the deployment

After Terraform finishes provisioning your Kubernetes resources, you need to verify the deployment by using the Kubernetes tool kubectl . Check if the pods and services are up and running as expected.

Run the following commands:

kubectl get ns

kubectl get all -n nginx

kubectl port-forward -n nginx svc/nginx 31010

Check Nginx deployment by opening its IP address in a web browser.

Step 5: Clean up

Once you are done with your Kubernetes infrastructure, remember to clean up the resources to avoid unnecessary costs. Run the following command to destroy the resources provisioned by Terraform:

terraform destroy

Terraform will remove all the resources that were provisioned by the configuration file.

Conclusion

In this blog post, we explored how to create a simple Kubernetes infrastructure with an Nginx container using Terraform. By leveraging Terraform's power to provision and manage resources, you can simplify the deployment process and focus more on building and scaling your applications.

Resources

Kubernetes Provider by Terraform

Did you find this article valuable?

Support Jatin Chourasia by becoming a sponsor. Any amount is appreciated!