What You’ll Learn

What You’ll Need

img.png

Core Terraform Commands and Workflow

img.png

Now that we’ve covered the fundamentals of how we get started with Terraform, let’s leverage it to:

  1. Deploy some NGINX nodes on top of our Kubernetes cluster.
  2. Add a NodePort service and scale our deployment of NGINX.
  3. Destroy all resources.

Simple enough? Let’s get rollin'!

Before we start, let’s make sure we have cloned our project 👉🏽 This GitHub repository

git clone https://github.com/CiscoLearning/deploy-terraform-kubernetes.git
  1. Navigate to the directory deploy-terraform-kubernetes.

In this working directory, there are three relevant files (for now):

  1. Head over into terraform.tfvars. Here’s where we need to update our variables with your own Kubernetes cluster information:
host                   = "REPLACE THIS"
client_certificate     = "REPLACE THIS"
client_key             = "REPLACE THIS"
cluster_ca_certificate = "REPLACE THIS"

Gather the variable information from our Kubernetes clusters, open a Terminal, and execute:

kubectl config view --minify --flatten --context=kind-my-kubernetes-cluster

This will return the information we need to update our Terraform var file. The output should look something like this:

img.png

  1. Let’s update our terraform.tfvars file with the information gathered above. Copy the information from your Terminal output and paste in place of “REPLACE THIS”. Here’s how it maps:

Your terraform.tfvars file should now look similar to this:

img.png

At this point, we are ready to deploy our nodes using Terraform! Do you remember the Terraform flow?

In your Terminal, execute the following in order:

  1. Initialize your working directory:
terraform init
  1. Plan your resources deployment to your Kubernetes cluster:
terraform plan
  1. Deploy your resources to your Kubernetes cluster; confirm the changes by typing “yes” and Enter:
terraform apply

🎉 We just deployed our first node service on top of our cluster! 🎉

Terraform is stateful, which means it knows exactly what is happening with our infrastructure at a given point—which also means we can update our infrastructure as needed. Pretty cool, right?

Let’s make our NGINX deployment available to the outside world by deploying a NodePort service and updating our configuration.

⚠️ Before you proceed any further, rename the file to kubernetes_service.tf by removing the trailing .hold from the filename.

Add the service!

  1. Check out what the new plan looks like before deployment:
terraform plan
  1. Deploy your resources to your Kubernetes cluster; confirm the changes by typing “yes” and Enter:
terraform apply

What if our NGINX deployment is seeing high utilization that’s degrading the UX of our application, and we need to scale up the node? It’s not an issue; Terraform has you covered.

Let’s Scale Up the Node

  1. Check out and edit the kubernetes_resources.tf file.
  2. Modify the replicas from 2 to 4:
spec {
    replicas = 4			 # Change this value from 2 to 4
    selector {
      match_labels = {
        App = "longlivethebat"
      }
    }
  1. Apply your changes and confirm that the replicas were changed from 2 to 4, then type “yes.” Enter a value line to confirm the apply:
terraform apply
  1. Verify your deployment is now using 4 pods:
kubectl get deployments

As Andrea Bocelli might say, it’s “Time to Say Goodbye” to your cluster!

⚠️ If you’d like to keep your deployment running for further learning, skip this step.

  1. Destroy your resources, including your NGINX deployment and NodePort service:
terraform destroy
  1. On the Enter a value line, type “yes” to confirm the destroy. The destroy takes a moment to complete.
  2. Delete your cluster:
kind delete cluster --name my-kubernetes-cluster
  1. Verify the cluster was deleted:
kind get clusters

Congratulations! You’ve successfully completed this lab!

Learn More