Surya.dev
Published on

Day 07: Deploy a Web Application in Kubernetes with Terraform

Authors

Introduction

Hey folks, Welcome to this #100daysofcloud. In this blog, i will first provision an EKS cluster with Terraform. Then, i will deploy an HTML5 web-based Pac-Man game that you can play in your browser to your EKS cluster with Terraform. Sounds like fun!

deploy-a-web-application-in-kubernetes-with-terraform

Note: LAB-Diagram Building

Lab practice

In a browser, log in to the AWS Management Console using the credentials provided. Make sure you're using the us-east-1 Region.

In your terminal, log in to the server using the credentials provided:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

Deploy Your EKS Cluster with Terraform

Create an IAM Access Key and Configure the AWS CLI

  1. In the AWS Management Console, select IAM from the list of Services.

  2. On the IAM dashboard, under IAM resources, click Users.

  3. In the list of users, click cloud_user.

  4. Click the Security credentials tab.

  5. Scroll down to the Access keys section and click Create access key.

  6. For Step 1, select Command Line Interface (CLI), check the I understand the above recommendations and want to proceed to create an access key checkbox, and click Next.

  7. For Step 2, in the Description tag value field, enter Terraform_Access_Key and click Create access key.

  8. Copy the Access key and Secret access key provided, and capture them in a safe and easily accessible location.

Note: You will need these in the next step to configure the CLI to connect to your AWS instance. You may want to capture this information in a text editor or other note-taking program for safe keeping.

  1. In your terminal, configure the AWS CLI to connect to your AWS instance:

    aws configure
    
  2. When prompted for the AWS Access Key ID, enter the Access key you copied from the AWS console.

  3. When prompted for the AWS Secret Access Key, enter the Secret access key you copied from the AWS console.

  4. When prompted, press Enter to accept the Default region name and Default output format.

Deploy the EKS Cluster with Terraform

Download the EKS Terraform configuration provided for the lab:

wget https://github.com/pluralsight-cloud/content-deploying-and-managing-a-web-application-in-kubernetes-with-terraform/raw/main/eks.zip Once completed, list the files to ensure that it has downloaded:

ls
# You should see the eks.zip file.

Unzip the file:

unzip eks.zip

List the files:

ls

You should now also see the eks directory.

Change into the eks directory:

cd eks

List the files in the directory:

ls

You should see the eks-cluster.tf, main.tf, outputs.tf, terraform.tf, variables.tf, and vpc.tf configuration files.

Initialize the working directory:

terraform init

Validate the configuration:

terraform validate

Apply the configuration and deploy the EKS cluster:

terraform apply

When prompted, enter yes to confirm.

Note: It can take between 10 and 15 minutes to deploy the cluster. Ensure that the deployment process completes successfully before moving on.

Once deployed, configure the Kubernetes CLI to use the cluster's context:

aws eks --region $(terraform output -raw region) update-kubeconfig --name $(terraform output -raw cluster_name)

Confirm that the cluster is up and running:

kubectl cluster-info

You should see that the Kubernetes control plane and CoreDNS are running as expected.

Complete the Terraform Configuration

Change into the main directory:

cd ../

Download the Pac-Man Terraform configuration provided for the lab:

wget https://github.com/pluralsight-cloud/content-deploying-and-managing-a-web-application-in-kubernetes-with-terraform/raw/main/pac-man.zip

Once completed, list the files to ensure that it has downloaded:

ls

You should now also see the pac-man.zip file.

Unzip the file:

unzip pac-man.zip

List the files:

ls

You should now also see the pac-man directory.

Change into the pac-man directory:

cd pac-man/

List the files in the directory:

ls

You should see the modules directory and the pac-man.tf configuration file.

Edit the Pac-Man configuration deployment:

vim modules/pac-man/pac-man-deployment.tf

Note: Press i to enter insert mode in Vim.

For the container spec, update the image argument with the Docker image provided for the lab:

image = "docker.io/jessehoch/pacman-nodejs-app:latest"

Press Escape and enter :wq! to save and exit the file.

List the files in the modules directory:

ls modules/

You should see the mongo and pac-man directories.

Edit the main Terraform configuration file:

vim pac-man.tf

Note: When copying and pasting code into Vim, first enter :set paste to avoid adding unnecessary spaces and hashes. Then, press i to enter insert mode and paste your content.

Update the file to include these two modules and pass the pac-man namespace to them:

module "mongo" {
  source = "./modules/mongo"
  kubernetes_namespace = "pac-man"
}

module "pac-man" {
  source = "./modules/pac-man"
  kubernetes_namespace = "pac-man"
  depends_on = [module.mongo]
}

Press Escape and enter :wq! to save and exit the file.

Check the configuration for any formatting issues:

terraform fmt

Initialize the working directory:

terraform init

Validate the configuration:

terraform validate

Deploy the Pac-Man Web Application with Terraform

Apply the configuration and deploy the web application:

terraform apply

When prompted, enter yes to confirm.

Once deployed, confirm that the web application resources were deployed in the pac-man namespace and are available:

kubectl -n pac-man get all

You should see that the mongo and pac-man pods, services, deployments, and replicas are all available and running as expected.

For the pac-man service, copy the external IP address provided in the EXTERNAL-IP column.

In a new browser tab or window, paste the external IP address and hit Enter.

When the Pac-Man web application launches, click to play as instructed, and play the game to confirm that it is working as expected.

Note: It can take some time for the application to function, even if its resources have been deployed. If the Pac-Man web application does not launch immediately, wait a few minutes for the deployment to complete.

Scale the Kubernetes Web Application

Back in the terminal, edit the MongoDB deployment configuration file:

vim modules/mongo/mongo-deployment.tf

For the replicas spec, update the replicas value to 2.

Enter :wq! to save and exit the file.

Edit the Pac-Man deployment configuration file:

vim modules/pac-man/pac-man-deployment.tf

For the replicas spec, update the replicas value to 3.

Enter :wq! to save and exit the file.

Apply the updates to the configuration:

terraform apply

When prompted, enter yes to confirm.

Confirm that the resources were updated:

kubectl -n pac-man get all

You should see that the mongo and pac-man pods, deployments, and replicas have all been scaled up to 2 and 3, respectively.

In the browser, refresh the Pac-Man web application and confirm that it is still working as expected.

Back in the terminal, edit the MongoDB and Pac-Man deployment configuration files again, this time changing the replicas back to 1.

Apply the updates to the configuration:

terraform apply

When prompted, enter yes to confirm.

Confirm that the resources were updated:

kubectl -n pac-man get all

You should see that the mongo and pac-man pods, deployments, and replicas have all been scaled back down to 1 each.

In the browser, refresh the Pac-Man web application again and confirm that it is still working as expected.