Deploying a Pac-Man Game on AWS EKS using Terraform. Game On ! (Prometheus + Grafana)
Tags: aws, kubernetes, terraform, devops
Published: 2023-06-06 14:23:36


## 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 -**

![](https://cdn-images-1.medium.com/max/2000/1*YodIWaUSwfJywo4bXYEseg.png)
>  **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](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) Installed on AWS EC2 bastion host.

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

* [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/) 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](https://github.com/Kunal-Shah107/Deploying-and-Managing-a-Pac-Man-Game-on-AWS-EKS-using-Terraform.git)

* 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.

![](https://cdn-images-1.medium.com/max/2000/1*bMw71E-L6R0nmjjZViUX0Q.png)

![](https://cdn-images-1.medium.com/max/3074/1*937mVjeLorfpcu7ECzz3sQ.jpeg)

**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.

![](https://cdn-images-1.medium.com/max/2508/1*bRQeFHTGGa8n052kNYWNAg.png)

**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**

![](https://cdn-images-1.medium.com/max/2000/1*bKTtxVcaD5yk-8gIwjZ_Bg.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*W6rQE42RMuyQxTW3L6R86Q.jpeg)

**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.

![](https://cdn-images-1.medium.com/max/2658/1*uIyRpfVbZFaiJXH7_UQTag.png)

![](https://cdn-images-1.medium.com/max/2000/1*YU_SaM7UDNMZQvylbdNnPQ.png)

**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.

![](https://cdn-images-1.medium.com/max/2000/1*J87iPeWJV8qZ2I79UOsXOg.png)

**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.

![](https://cdn-images-1.medium.com/max/2000/1*M0kCgMT3WAvmHDKTLf2AvQ.png)

![](https://cdn-images-1.medium.com/max/2000/1*JLqqeCwVT_2om1mw4H8MCA.png)

**STEP 7** : **OBSERVABILITY — Prometheus**

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

![](https://cdn-images-1.medium.com/max/2000/1*o518PJWM3rXQBeHPtcxh0g.png)

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

* change **mongo-sc** default class to **false.**

![](https://cdn-images-1.medium.com/max/2000/1*zaoz_ClVO-ETYGCiCmwz3g.png)

![](https://cdn-images-1.medium.com/max/2000/1*-4Tn6sRNaM3tTR4ejCEXwA.png)

* Create a new Namespace — **kubectl create ns monitoring**

* **Install Prometheus** — *helm 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**

![](https://cdn-images-1.medium.com/max/2054/1*tQ3WAeWH1RwNlDvSLA67fQ.png)

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

**STEP 8** : **OBSERVABILITY — Grafana**

* Install Grafana by creating & updating **[grafana.yml](https://github.com/Kunal-Shah107/Deploying-and-Managing-a-Pac-Man-Game-on-AWS-EKS-using-Terraform/blob/main/grafana.yml)**

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

![](https://cdn-images-1.medium.com/max/2000/1*Jtprqir3SypD_gf7lhc2yA.png)

* *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

![](https://cdn-images-1.medium.com/max/2000/1*9YUFLyhJvbadXSKumO0v-A.png)

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

![](https://cdn-images-1.medium.com/max/2000/1*pX2xMXMOrxIPcj4Nxv3JHA.png)

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

![](https://cdn-images-1.medium.com/max/2000/1*lWDW7GNV20mBxLkM_IxUqw.png)

* 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.

![](https://cdn-images-1.medium.com/max/3272/1*sm7o8FB3OccbSm4OcH3SNA.png)

![](https://cdn-images-1.medium.com/max/2000/1*XGxPpTsU_E-iEmZaUtAIFw.png)

**STEP 9** : Grafana Dashboard Setup

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

![](https://cdn-images-1.medium.com/max/2000/1*5pE114_CfNogQ_2a6WfROA.png)

* 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](https://grafana.com/dashboards/1621) or [#315](https://grafana.com/dashboards/315) or [#1860](https://grafana.com/dashboards/1860) or [#741](https://grafana.com/dashboards/741) or [#747](https://grafana.com/dashboards/747) or [#1471](https://grafana.com/dashboards/1471) or [#455](https://grafana.com/dashboards/455)

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

![](https://cdn-images-1.medium.com/max/2000/1*jxi3C-uw_48JpSerz8eKtg.png)

![](https://cdn-images-1.medium.com/max/2000/1*Xc_0QGahVAgZFBo-MiQgeQ.png)

* Final Grafana Observability Dashboard is ready !!

![](https://cdn-images-1.medium.com/max/3818/1*ffgrn0uMNMmPnaFUz9dUSw.png)

**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.

![](https://cdn-images-1.medium.com/max/3168/1*XSVHeCXZmWPoGQ07UoqDDg.png)

*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](https://acloudguy.in/)*