In this project we will use terraform to deploy a Kubernetes cluster and then deploy a HTML5 Web App that allows you to play PACMAN (A Kubernetes project created by Ivan Font) on your browser. We will also manage the Kubernetes resources we will be using such as Node.js frontend and Mongo db backend.
Why Terraform with Kubernetes?
Terraform gives you the following;
Comprehensive management of the entire lifecycle
Terraform doesn't only create resources, it can update and delete them, particularly, trapped resources without inspecting the API to identity those resources.
Interconnected relationships.
Terraform understands dependency relationships between resources. E.g. If you're using an EKS cluster it knows it need a specific VPC and subnet config. Terraform won't attempt to create the cluster if the VPC and subnets fail to create with the proper config which saves you time if there is an error.
A cohesive process
If you are already deploying apps to the cloud with terraform, Kubernetes cluster can fit into your workflow. you can also deploy apps in your cluster using terraform.
Prerequisites
Cloud account with AWS
AWS CLI
kubectl
Terraform
Package managers: Chocolatey for windows | Homebrew for MAC
Installing the Pre-req for windows
- Install AWS CLI
choco install aws-cli
- Install kubectl
choco install kubernetes-cli
- Login to AWS
aws configure
Step 1: Provisioning an EKS cluster with Terraform
We will be working with 6 files which you can download from the github link provided
Terraform.tf*- Sets the terraform version, provider version.*
Variables.tf*- Contains our region variable and controls where to create the EKS cluster*
VPC.tf*- Provisions a VPC, subnets and availability zones using the AWS VPC module*
Outputs.tf*- Defines the output values for our config*
EKS-cluster.tf*- Uses the AWS EKS module to provision the EKS cluster and other required resources which includes; auto scaling groups, security groups, IAM roles and IAM policies*
Main.tf*- Configures our providers as well creates a data set and a local variable*
We can now run our terraform innit
command in our terminal to initialize our terraform config which will pull up our provider plugins.
After we can run
terraform validate
to check our syntax to make sure it correct in our config.Next we deploy our eks with the
terraform apply
. The provisioning of the EKS cluster can take 10 minutes or more due to the amount of configs it needs to make.
Now that we've applied our config, we are now going to configure kubectl
aws eks --region $(terraform output -raw region) update-kubeconfig --name $(terraform output -raw cluster_name) --no-verify-ssl
.
We pull that region into our command via the terraform output command and tell it to output the region. It uses the output to update the local Kubernetes configuration file (kubeconfig
) to enable interaction with the specified EKS cluster. Additionally, it disables SSL certificate verification for communication with the Kubernetes API server.
To verify our cluster, we can use the command kube cluster-info
You can see our control plane and core DNS running
If we do a kubectl get node
, we can see the nodes that make up our eks cluster.
Step 2: Create a Kubernetes Web App
The two big components we will be deploying into our cluster are first our web server and then our database (frontend and backend).
Webserver using Node.js application deployed via docker image into Kubernetes.
Mongo DB database which allows the front end to connect and store info such as time, highscore, progress etc. allowing the frontend to display the info in realtime.
We will be running this app in an EKS cluster ---> Kubectl installed and configured so its pointing to the EKS cluster ----> and Terraform installed.
You can download the files in the github link provided
In our project's main directory, we have the following files;
To begin, we run our terraform innit
command.
Once its all good, we do terraform validate
to make sure its all good.
Next we deploy our web app by applying the config with terraform apply
.
We can now do a kubectl -n pac-man get all
you can see we've successfully deployed our pacman web app
You can see our pods, service and deployment created for our app. Everything showing available and in a ready state.
To view the app, we can grab the link from our pacman service.
Step 3: Managing resources
We can attempt to scale up our web app. We can add more nodes to it to support more traffic.
We want to add 3 pods to our front end and add 2 pods to our back end
In the pac-man deployment.tf file, scroll down to the spec and change the unit to 3. So we scale our front end up to 3 node. Save the changes
In our mongo deployment.tf file scroll down to the spec and change the unit to 2. So we scale our front end up to 2 node. Save the changes
Do
terraform apply
to redeploy and confirm changes
Now if we do a kubectl -n pac-man get all
you can see the changes.
Refresh the page and you should still have a fully functional game running. Apply the same method as above to scale it down. Don't forget to save and re apply Terraform apply
.
Summary: This project uses Terraform to deploy a Kubernetes cluster and a PACMAN web app. It explains the benefits of Terraform with Kubernetes, prerequisites, provisioning an EKS cluster, deploying web server and database, managing resources by scaling up/down, and playing the game.