HomeRoadmapsTerraform Day 10
Kubernetes & GitOps Track Β· Day 10 Β· 20 min

Workload Management in HCL
DevOps IaC Part 2 β€” Day 10

Orchestrate K8s Deployments, Services, ConfigMaps, and Pod health checks natively using the HashiCorp Kubernetes provider

πŸ› οΈ Tooling: Terraform 1.5+, HashiCorp Kubernetes Provider & kubectl🎭 Role: Kubernetes & Cloud Infrastructure Quality Architect
KubernetesHCL WorkloadsDeploymentsServicesConfigMapsHealth Checks
About Today's 20-Minute Lab

While raw YAML is traditional in Kubernetes, managing workloads via HashiCorp's official Kubernetes Provider in Terraform provides compile-time schema checking, dynamic variable injection, state tracking, and unified lifecycle management across cloud infrastructure and containers. Today you will orchestrate a complete microservice tier: Namespace, ConfigMap, Deployment with liveness/readiness probes, and Service, all cleanly expressed in declarative HCL.

🎬
Day 10 Video Walkthrough: Kubernetes Workload Management via HashiCorp ProviderComing Soon

An interactive 20 min video walkthrough for Day 10 is currently in production. Follow the step-by-step interactive playbook below!

πŸ“¦Sample Codebase & Working Solution (Day 10)Available Now
View on GitHub

The official reference codebase for Day 10 is hosted in the repository. Clone the repository and navigate to day-10-k8s-workloads to run the working solution.

Git Clone β€” Working Solution
git clone https://github.com/777abhi/devops-terraform-roadmap.git
cd devops-terraform-roadmap/day-10-k8s-workloads
1

Why HCL for Kubernetes Workloads?

Compare Terraform HCL workloads against raw YAML templates and Helm charts.

HCL vs Raw YAML Comparison Matrix

Evaluate the architectural advantages of expressing cluster objects in Terraform.

hcl-vs-yaml-matrix.txt
Feature               Raw YAML Manifests        Terraform HCL
──────────────────────────────────────────────────────────────────
Schema Validation     Runtime (kubectl apply)   Plan-time (terraform plan)
Dynamic Variables     External (sed/envsubst)   Native (var.environment)
Resource Dependencies Implicit / Manual wait    Automatic DAG Graph
Drift Detection       Manual diff               terraform plan -detailed-exitcode
Unified State         Separate etcd state       Synchronized in S3/DynamoDB
2

HCL Workload Definition: Deployment & Service

Write declarative HCL to provision a resilient 3-replica web workload with health checks.

Declare Kubernetes Provider Connection

Point the provider to the local Kind kubeconfig context.

HCL β€” versions.tf
# versions.tf
terraform {
  required_version = ">= 1.5.0"
  required_providers {
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.30.0"
    }
  }
}

provider "kubernetes" {
  config_path    = "~/.kube/config"
  config_context = "kind-devops-lab-cluster"
}
Define Namespace, ConfigMap & Deployment in main.tf

Author the complete application workload with resource limits and health probes.

HCL β€” main.tf
# main.tf
resource "kubernetes_namespace" "lab" {
  metadata {
    name = "devops-lab"
    labels = {
      environment = "sandbox"
    }
  }
}

resource "kubernetes_config_map" "app_config" {
  metadata {
    name      = "webapp-config"
    namespace = kubernetes_namespace.lab.metadata[0].name
  }

  data = {
    APP_NAME = "OrderService"
    ENV_TIER = "Development"
  }
}

resource "kubernetes_deployment" "webapp" {
  metadata {
    name      = "webapp-deployment"
    namespace = kubernetes_namespace.lab.metadata[0].name
    labels = {
      app = "webapp"
    }
  }

  spec {
    replicas = 3

    selector {
      match_labels = {
        app = "webapp"
      }
    }

    template {
      metadata {
        labels = {
          app = "webapp"
        }
      }

      spec {
        container {
          name  = "nginx"
          image = "nginx:1.25-alpine"

          resources {
            limits = {
              cpu    = "250m"
              memory = "128Mi"
            }
            requests = {
              cpu    = "100m"
              memory = "64Mi"
            }
          }

          port {
            container_port = 80
          }

          liveness_probe {
            http_get {
              path = "/"
              port = 80
            }
            initial_delay_seconds = 5
            period_seconds        = 10
          }

          readiness_probe {
            http_get {
              path = "/"
              port = 80
            }
            initial_delay_seconds = 3
            period_seconds        = 5
          }
        }
      }
    }
  }
}

resource "kubernetes_service" "webapp_svc" {
  metadata {
    name      = "webapp-service"
    namespace = kubernetes_namespace.lab.metadata[0].name
  }

  spec {
    selector = {
      app = kubernetes_deployment.webapp.metadata[0].labels.app
    }

    port {
      port        = 80
      target_port = 80
      node_port   = 30080
    }

    type = "NodePort"
  }
}
3

Execution, Scaling & Health Probe Auditing

Apply the workload and audit pod readiness and endpoints with kubectl.

Apply Workload via Terraform

Initialize the Kubernetes provider and provision all resources.

Terminal β€” Provision K8s Workloads
cd /Users/abhinavsharma/Downloads/devops-terraform-roadmap/day-10-k8s-workloads
terraform init
terraform apply -auto-approve
Verify Pod Health and Service Endpoints

Confirm that all 3 replicas are Running and probes are healthy.

Terminal β€” Verify Health & Endpoints
kubectl get pods -n devops-lab -o wide
kubectl get svc -n devops-lab webapp-service
curl -I http://localhost:8080
4

Production Best Practices & Zero-Downtime Rollouts

Learn zero-downtime rolling update configurations and pod disruption budgets.

🏭Zero-Downtime Rolling Update Architecture

In production deployments, prevent service drops during deployment rollouts by configuring strategy:

HCL β€” Zero-Downtime Rolling Update Strategy
strategy {
  type = "RollingUpdate"
  rolling_update {
    max_surge       = "25%"
    max_unavailable = "0"   # Guarantees no capacity drops during rollout
  }
}
Day 10 Quality Gate & Outcomes Checklist
  • Understood the benefits of Terraform HCL for Kubernetes workloads over raw unvalidated YAML.
  • Configured the official HashiCorp Kubernetes provider with local Kind cluster context.
  • Declared Namespace, ConfigMap, Deployment with 3 replicas, and NodePort Service.
  • Instrumented liveness and readiness HTTP health probes directly in HCL.
  • Verified pod distribution and tested zero-downtime rolling update strategies.