DEV Community

Cover image for Deploying a Pac-Man Game on AWS EKS using Terraform. Game On ! (Prometheus + Grafana)
Kunal Shah for AWS Community Builders

Posted on • Edited on

Deploying a Pac-Man Game on AWS EKS using Terraform. Game On ! (Prometheus + Grafana)

Deploying a Pac-Man Game on AWS EKS using Terraform. Game On !

AWS Cloud Hands on Lab Practice Series

Gobbling Dots in the Cloud on AWS EKS using Terraform.

Project Overview —

We explore into the world of AWS EKS by deploying an web application i.e., Pac-man game. Our goal is to build AWS EKS cluster, deploy, manage & scale the application using terraform. Pac-man game runs using Node.js and uses MongoDB as the backend database.

SOLUTIONS ARCHITECTURE OVERVIEW -

First Let’s understand the real world use case -

  1. High Traffic Handling: AWS EKS allows for efficient load balancing and auto-scaling capabilities, enabling the web application to handle high traffic without compromising performance. With managed services like Elastic Load Balancer (ELB) and Auto Scaling, incoming traffic can be distributed across multiple pods within the Kubernetes cluster.

  2. Fault Tolerance: By deploying the web application on AWS EKS, fault tolerance can be achieved through the inherent capabilities of Kubernetes. Kubernetes automatically monitors the health of pods and restarts or replaces failed instances to maintain application availability. Additionally, AWS provides managed services like Elastic Block Store (EBS) for persistent storage, which enhances data durability and minimizes the risk of data loss.

  3. High Availability: With AWS EKS, the web application can achieve high availability through the deployment of multiple pods across multiple availability zones (AZs). Kubernetes ensures that the desired number of pods are always running, and in the event of a failure, it automatically replaces or reschedules pods to maintain application availability. Combined with AWS Auto Scaling, the cluster can dynamically adjust the number of pods based on demand.

  4. Scalability: AWS EKS enables horizontal scalability, allowing the web application to handle increased traffic and workload. Kubernetes provides seamless scaling by adding or removing pods based on predefined metrics or custom policies. AWS Auto Scaling integrates with Kubernetes, dynamically adjusting the cluster size to meet demand, ensuring optimal resource utilization, and reducing costs during low traffic periods.

  5. Orchestration: Kubernetes serves as a powerful orchestration tool, simplifying the management of containerized applications. With AWS EKS, managing the deployment, scaling, and monitoring of the web application becomes more efficient. Kubernetes offers features such as declarative configuration, service discovery, rolling updates, and integration with other ecosystem tools, allowing for seamless application management.

Overall, Deploying a web application on AWS EKS provides significant advantages in handling high traffic, achieving fault tolerance, ensuring high availability, enabling scalability, and simplifying orchestration. By leveraging the robust features of AWS EKS and Kubernetes, businesses can confidently deploy their applications, adapt to changing demands, and deliver a reliable and scalable user experience.

PREREQUISITE —

  • AWS Account with Admin Access.

  • EC2 Instance (Bastion Host) with IAM Role attached having Admin permissions.

  • Terraform Installed on AWS EC2 bastion host.

  • AWS CLI Configured on AWS EC2 Bastion host with access key & secret key.

  • kubectl Installed on AWS EC2 bastion host.

AWS Services Usage —

AWS EKS, EC2, ELB, IAM, VPC, CLI, Security Groups

STEP BY STEP GUIDE -

STEP 1 :

  • Deploy the EKS Cluster with Terraform.

  • git clone Github Repo

  • Change into the eks directory: cd eks

  • ls now & we 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

  • Now apply the configuration and deploy the EKS cluster: terraform apply -auto-approve

  • Note: It can take between 15 and 20 minutes to deploy the eks cluster.

STEP 2 :

  • Once aws eks cluster is 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)

  • kubectl cluster-info (To confirm that the cluster is active)

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

STEP 3 :

  • Complete the Terraform Configuration by change into main directory

  • cd ../pac-man/

  • Docker Image used for this lab is “docker.io/jessehoch/pacman-nodejs-app:latest”

  • terraform init

  • terraform validate

  • terraform apply -auto-approve

STEP 4 :

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

  • kubectl -n pac-man get all

  • We 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 DNS record 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.

STEP 5 :

  • Scale the AWS EKS Pac-Man Application.

  • Edit the MongoDB deployment configuration file:

  • vi modules/mongo/mongo-deployment.tf

  • For the replicas spec, update the replicas value to 2. save & exit.

  • Edit the Pac-Man deployment configuration file:

  • vi modules/pac-man/pac-man-deployment.tf

  • For the replicas spec, update the replicas value to 3. save & exit.

  • 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

  • We 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.

STEP 6 :

  • 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

  • Confirm that the resources were updated: kubectl -n pac-man get all

  • We 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 and confirm that it is still working as expected.

STEP 7 : OBSERVABILITY — Prometheus

  • Give EC2 Full access to IAM role attached to Node Group 1 & 2 Instances.

  • Now change the default Storage Class on cluster — kubectl edit sc

  • change mongo-sc default class to false.

  • Create a new Namespace — kubectl create ns monitoring

  • Install Prometheushelm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm upgrade -i prometheus prometheus-community/prometheus \
    — namespace monitoring \
    — set alertmanager.persistentVolume.storageClass=”gp2",server.persistentVolume.storageClass=”gp2"

  • Check services in monitoring namespace.

  • kubectl get services -n monitoring

  • Note the cluster IP details & Port details of prometheus-server.

STEP 8 : OBSERVABILITY — Grafana

  • Install Grafana by creating & updating grafana.yml

  • Add cluster IP details & Port details of prometheus-server in url.

  • helm repo add grafana https://grafana.github.io/helm-charts
    helm install grafana grafana/grafana — namespace monitoring — set persistence.enabled=true — set service.type=LoadBalancer — values=grafana.yml

  • Check all pods in all namespaces

  • Now hit the ELB URL at port 80 to access Grafana & enter credentials.

  • After accessing the Grafana UI click on Data sources found in the bottom left hand please see image below.

  • On the next screen select add data source then select Prometheus.

  • We will input the url http://clusterIP:80 for you prometheus-server service.

  • We can then click save and test at the bottom to confirm the data source works.

STEP 9 : Grafana Dashboard Setup

  • We can then navigate to the left of the screen > hover over the new icon & from dropdown select import.

  • In the import via grafana.com text box input the number 3119.

  • Optional Tip : You can different number as per your use case & needs some important dashboard numbers are : #1621 or #315 or #1860 or #741 or #747 or #1471 or #455

  • Select Prometheus and click load and you will have your dashboard as shown below.

  • Final Grafana Observability Dashboard is ready !!

STEP 10 :

  • Decommission the resources: terraform destroy

  • NOTE — You need to decommission from both folders pac-man & eks.

  • When prompted, enter yes to confirm.

  • IMP NOTE — This DEMO/POC might incur some charges if kept active for long time. So please make sure to clean up the environment once done.

Congrats ! We have successfully completed lab for Deploying a Pac-Man Game on AWS EKS using Terraform.

I am Kunal Shah, AWS Certified Solutions Architect, helping clients to achieve optimal solutions on the Cloud. Cloud Enabler by choice, DevOps Practitioner having 7+ Years of overall experience in the IT industry.

I love to talk about Cloud Technology, DevOps, Digital Transformation, Analytics, Infrastructure, Dev Tools, Operational efficiency, Serverless, Cost Optimization, Cloud Networking & Security.

aws #community #builders #devops #terraform #eks #ec2 #kubernetes #scaling #infrastructure #webapplication #acloudguy

You can reach out to me @ acloudguy.in

Top comments (0)