Check KodeKloud’s Terraform Challenge #1!

Note - if you really must see the solutions directly, find them on my GitHub page; okbobm!

What’s the scenario? In this challenge we will deploy several Kubernetes resources using terraform.

Challenge1

What are the general requirements for this challenge?

  1. Verify Terraform is installed on ControlPlane, if not install.
  2. Configure kubernetes file
  3. Create deployment (i.e. frontend)
  4. Create service (i.e. webapp-service)
  5. Deploy terraform

Step 1: Verify Terraform is installed on ControlPlan, if not install

Requirements:

  • Terraform version: 1.1.5 installed on controlplane?

Solution:

  • Is Terraform installed?
    • Try to find terraform on the controlplane
      • terraform –version
      • which terraform
  • Install directory
    • Run update and install unzip library
      • apt update
      • apt install unzip
    • Download terraform, unzip, and move files
      • wget https://releases.hashicorp.com/terraform/1.1.5/terraform_1.1.5_linux_amd64.zip
      • unzip terraform_1.1.5_linux_amd64.zip
      • mv terraform /usr/local/bin/
    • Verify install and version successfull
      • terraform –version
      • which terraform

Step 2: Configure kubernetes file

Requirements:

  • Configure terraform and provider settings within provider.tf file with following specifications:
  • Configure terraform to use hashicorp/kubernetes provider.
  • Specify the provider’s local name: kubernetes
  • Provider version: 2.11.0
  • Configure kubernetes provider with path to your kubeconfig file: /root/.kube/config

Solution:

  • Create provider.tf (in terraform_challenge directory)
    • Create the provider.tf file based on the Terraform Kubernetes provider example, modifying for parameters in the above requirements.
  • Initialize the file
    • terraform init

Step 3: Create frontend (i.e. frontend)

Requirements:

  • Create a terraform resource frontend for kubernetes deployment with following specs:
    • Deployment Name: frontend
    • Deployment Labels = name: frontend
    • Replicas: 4
    • Pod Labels = name: webapp
    • Image: kodekloud/webapp-color:v1

Solution:

  • Create the frontend.tf file based on the Terraform Kubernetes-Deployment example, modifying for parameters in the above requirements.
  • Note the selector spec>selector portion was challenging to get this correct!

Step 4: Create service (i.e. webapp-service)

Requirements:

  • Create a terraform resource webapp-service for kubernetes service with following specs:
    • Service name: webapp-service
    • Service Type: NodePort
    • Port: 8080
    • NodePort: 30080

Solution:

  • Create the web-service.tf file based on the Terraform Kubernetes-Service example, modifying for parameters in the above requirements.

Step 5: Deploy

  • Run commands to apply the plan to deploy resources
    • terraform plan
    • terraform apply

Finally - completed!

You should see everything in green as in the diagram below:

Challenge1_completed