variables.tf
: Declares variables used to parameterize the configurationmain.tf
: Describes the infrastructure to be provisioned declaratively, making frequent references to a variety of providersoutput.tf
: Declares output values that are displayed upon instantiation of the declared infrastructure and can then be referenced by other toolingterraform init
: Initializes the working directory containing the *.tf files, including the creation of a state file, and downloads off any referenced providersterraform plan
: Creates and displays an execution plan based on the infrastructure declared in the *.tf filesterraform apply
: Executes the plan based on the infrastructure declared in the *.tf files and updates the state as pieces of the declaration are instantiatedterraform destroy
: Deletes all the infrastructure declared in the *.tf files and updates the state accordinglyNow that we’ve covered the fundamentals of how we get started with Terraform, let’s leverage it to:
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
deploy-terraform-kubernetes
.In this working directory, there are three relevant files (for now):
kubernetes_resources.tf
: This file contains the declared resources, two NGINX pods.kubernetes.tf
: This file contains our main Terraform configuration file.terraform.tfvars
: This file contains all our variables that we need to define for Terraform to successfully connect to our Kubernetes instance.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:
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:host
:: server
client_certificate
:: client-certificate-data
client_key
:: client-key-data
cluster_ca_certificate
:: certificate-authority-data
Your terraform.tfvars
file should now look similar to this:
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:
terraform init
terraform plan
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.
kubernetes_service.tf.hold
.⚠️ Before you proceed any further, rename the file to kubernetes_service.tf
by removing the trailing .hold
from the filename.
kubernetes_service.tf
: This file contains the declaration of our NodePort
service.terraform plan
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.
kubernetes_resources.tf
file.2
to 4
:spec {
replicas = 4 # Change this value from 2 to 4
selector {
match_labels = {
App = "longlivethebat"
}
}
terraform apply
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.
terraform destroy
kind delete cluster --name my-kubernetes-cluster
kind get clusters
Congratulations! You’ve successfully completed this lab!