Deploying a 2048 Game on AWS EKS with Load Balancing: A Step-by-Step Guide

Deploying a 2048 Game on AWS EKS with Load Balancing: A Step-by-Step Guide

Kubernetes (K8s) and Amazon Elastic Kubernetes Service (EKS) offer a powerful platform for scalable and reliable containerized applications. In this tutorial, we'll walk through the process of deploying a 2048 game on EKS, leveraging the AWS Load Balancer Controller for efficient traffic distribution.

Why AWS EKS & Load Balancing?

  • Scalability: EKS allows seamless scaling of your application to handle varying workloads.

  • High Availability: Load balancing ensures your application remains accessible even during node failures or maintenance.

  • Cost-Effectiveness: With Fargate, you only pay for the resources your application uses, avoiding the need to manage worker nodes.

Prerequisites

  • An AWS account with sufficient permissions to create EKS clusters and manage IAM resources.

  • eksctl and kubectl command-line tools installed and configured.

  • Basic familiarity with Kubernetes concepts like namespaces, deployments, services, and ingresses.

Steps

  1. Create EKS Cluster:

     eksctl create cluster --name demo-cluster-1 --region us-east-1 --fargate
    

    This creates an EKS cluster named "demo-cluster-1" in the us-east-1 region using Fargate.

  2. Create Fargate Profile and Namespace:

     eksctl create fargateprofile \
         --cluster demo-cluster-1 \
         --region us-east-1 \
         --name alb-sample-app \
         --namespace game-2048
    

    This prepares your cluster to run the application in a serverless Fargate environment and creates a dedicated namespace (game-2048) for it.

  3. Deploy the Application:

     kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.5.4/docs/examples/2048/2048_full.yaml
    

    This file defines the Deployment, Service, and Ingress resources for the 2048 game application.

    We have created the Deployment, Service, and Ingress resources for the 2048 game application.

    Here is the complete file for your understanding

     ---
     apiVersion: v1
     kind: Namespace
     metadata:
       name: game-2048
     ---
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       namespace: game-2048
       name: deployment-2048
     spec:
       selector:
         matchLabels:
           app.kubernetes.io/name: app-2048
       replicas: 5
       template:
         metadata:
           labels:
             app.kubernetes.io/name: app-2048
         spec:
           containers:
           - image: public.ecr.aws/l6m2t8p7/docker-2048:latest
             imagePullPolicy: Always
             name: app-2048
             ports:
             - containerPort: 80
     ---
     apiVersion: v1
     kind: Service
     metadata:
       namespace: game-2048
       name: service-2048
     spec:
       ports:
         - port: 80
           targetPort: 80
           protocol: TCP
       type: NodePort
       selector:
         app.kubernetes.io/name: app-2048
     ---
     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
       namespace: game-2048
       name: ingress-2048
       annotations:
         alb.ingress.kubernetes.io/scheme: internet-facing
         alb.ingress.kubernetes.io/target-type: ip
     spec:
       ingressClassName: alb
       rules:
         - http:
             paths:
             - path: /
               pathType: Prefix
               backend:
                 service:
                   name: service-2048
                   port:
                     number: 80
    
  4. Configure OIDC and IAM:

    • Associate an IAM OIDC provider with your cluster:

        eksctl utils associate-iam-oidc-provider --cluster demo-cluster-1 --approve
      
    • Create an IAM policy for the AWS Load Balancer Controller: Bash

        curl -O https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.5.4/docs/install/iam_policy.json
        aws iam create-policy \
            --policy-name AWSLoadBalancerControllerIAMPolicy \
            --policy-document file://iam_policy.json
      

    • Create an IAM role and service account for the controller: Bash

        eksctl create iamserviceaccount \
            --cluster=demo-cluster-1 \
            --namespace=kube-system \
            --name=aws-load-balancer-controller-1 \
            --role-name AmazonEKSLoadBalancerControllerRole \
            --attach-policy-arn=arn:aws:iam::YOUR_ACCOUNT_ID:policy/AWSLoadBalancerControllerIAMPolicy \
            --approve
      

  5. Deploy AWS Load Balancer Controller:

    • Add the Helm repository and install the controller:

        helm repo add eks https://aws.github.io/eks-charts
        helm repo update eks
        helm install aws-load-balancer-controller eks/aws-load-balancer-controller -n kube-system \
            --set clusterName=demo-cluster-1 \
            --set serviceAccount.name=aws-load-balancer-controller-1 \
            --set region=us-east-1 \
            --set vpcId=YOUR_VPC_ID \
            --set serviceAccount.annotations."eks\.amazonaws\.com/role-arn"=YOUR_IAM_ROLE_ARN
      

  6. Verify and Access the Application:

     kubectl get ingress -n game-2048
    

    Note the load balancer DNS name and open it in your web browser to play the 2048 game!

Congratulations you have successfully deployed an game on the Kubernetes cluster.

Key Points:

  • This guide simplifies the process of deploying and load balancing applications on EKS Fargate.

  • Be sure to replace placeholders like YOUR_ACCOUNT_ID, YOUR_VPC_ID, and YOUR_IAM_ROLE_ARN with your actual values.