Deploying a 2048 Game App with AWS EKS (Elastic Kubernetes Service)

Deploying a 2048 Game App with AWS EKS (Elastic Kubernetes Service)

Amazon Web Services (AWS) Elastic Kubernetes Service (EKS) offers a managed Kubernetes solution that simplifies running Kubernetes on AWS without installing and operating your own Kubernetes control plane or nodes. This guide will walk you through deploying the 2048 game application using AWS EKS, providing detailed steps and commands to achieve a successful deployment. By the end of this guide, you will clearly understand AWS EKS, its integration with other AWS services, and how to deploy and manage applications using Kubernetes on AWS.

What is AWS EKS?

AWS Elastic Kubernetes Service (EKS) is a managed service that allows you to run Kubernetes on AWS without the need to manage the Kubernetes control plane. Key features of AWS EKS include:

  • Managed Kubernetes Control Plane: EKS manages the availability and scalability of the Kubernetes control plane nodes, responsible for scheduling containers and managing the desired state.

  • Integration with AWS Services: EKS integrates with AWS services like IAM, VPC, CloudTrail, and more to provide a scalable and secure environment.

  • Security and Compliance: EKS supports RBAC, compliance certifications, and integration with AWS security services for data encryption.

  • Scalability: EKS supports scaling of worker nodes and integrates with AWS Auto Scaling.

  • High Availability: EKS runs the Kubernetes management infrastructure across multiple AWS Availability Zones.

  • Support for Hybrid Deployments: EKS allows running Kubernetes clusters that span both AWS and on-premises resources.

Setting Up Your Environment

Before deploying the application, you need to set up your environment with the necessary tools:

Tools Required:

a. kubectl: Command line tool for working with Kubernetes clusters. Refer: https://docs.aws.amazon.com/emr/latest/EMR-on-EKS-DevelopmentGuide/setting-up-eksctl.html

b. eksctl: Command line tool for working with EKS clusters. Refer: https://docs.aws.amazon.com/emr/latest/EMR-on-EKS-DevelopmentGuide/setting-up-eksctl.html

c. AWS CLI: Command line tool for working with AWS services, including EKS. Refer: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

Creating an EKS Cluster

On your AWS Console, head over to Services and search for Elastic Kubernetes Service.

We will be using the command line tool i.e. eksctl to create the EKS cluster as it requires a lot of configurations when it comes to creating a cluster through the UI and it's a better practice to minimize the overhead as much as possible.

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

What Happens When You Run This Command:

  • EKS Cluster Creation: Initiates the creation of an EKS cluster named "demo-cluster" in the "us-east-1" region.

  • Fargate Integration: Configures the cluster to use AWS Fargate for running Kubernetes pods.

  • Subnets and VPC: Automatically creates a new VPC with necessary subnets and network configurations.

  • IAM Roles and Policies: Creates the necessary IAM roles and policies for the EKS cluster and Fargate.

Once the EKS cluster is created, you can view various configurations, resources, and worker nodes running inside the cluster through the AWS Management Console.

The EKS Cluster is successfully created!

For kubectl to communicate with the Kubernetes cluster that we just created, we need the kubeconfig file for that.

aws eks update-kubeconfig --name democluster --region us-east-1

Deploying the 2048 Application

Step 1: Create a Fargate Profile

First, create a Fargate profile for the namespace where the 2048 game will run.

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

Step 2: Deploy the Application

Create the namespace, deployment, service, and ingress resources using a single YAML file.

kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.5.4/docs/examples/2048/2048_full.yaml
---
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

YAML File Breakdown:

  • Namespace: Creates a namespace called game-2048.

  • Deployment: Defines a deployment named deployment-2048 with 5 replicas, using the Docker image public.ecr.aws/l6m2t8p7/docker-2048:latest.

  • Service: Defines a NodePort service named service-2048 mapping incoming traffic on port 80.

  • Ingress: Defines an Ingress resource named ingress-2048 with annotations for the AWS ALB Ingress Controller, directing traffic to the service-2048 service.

There is no external IP attached to the service which means only those inside the VPC can access the application, but we intend to make it available to the outside world and that’s why we have created the Ingress resource.

After all the resources are successfully created, we need to install the Ingress controller because, without it, the Ingress resource is of no use.

The above picture also conveys that there is no Ingress Controller since there is no address attached to Ingress

Step 3: Install the Ingress Controller

Prerequisite: Associate IAM OIDC Provider

Associate the IAM OIDC provider with the EKS cluster.

eksctl utils associate-iam-oidc-provider --cluster demo-cluster --approve

**Associating an IAM OIDC provider allows Kubernetes service accounts in your cluster to use AWS Identity and Access Management (IAM) roles for pod-level permissions.

Step 3.1: Create IAM Policy

ALB controller that we will now be installing very soon is a pod that will communicate with the Application Load Balancer Service of AWS. For doing that necessary permissions must be attached to the service role. Download the IAM policy JSON file.

curl -O https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.5.4/docs/install/iam_policy.json

Create an IAM policy using the downloaded JSON file.

 aws iam create-policy \
 --policy-name AWSLoadBalancerControllerIAMPolicy \
 --policy-document file://iam_policy.json

Step 3.2: Create an IAM Service Role

Create an IAM service account and attach the IAM policy.

eksctl create iamserviceaccount --cluster=demo-cluster --namespace=kube-system --name=aws-load-balancer-controller --role-name AmazonEKSLoadBalancerControllerRole --attach-policy-arn=arn:aws:iam::<your-aws-account-id>:policy/AWSLoadBalancerControllerIAMPolicy --approve

Step 3.3: Install the ALB Ingress Controller

Download and install Helm. Refer: https://helm.sh/docs/intro/install/

Add the EKS Helm repository and install the ALB Ingress 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 --set serviceAccount.create=false --set serviceAccount.name=aws-load-balancer-controller --set region=us-east-1 --set vpcId=<your-vpc-id>

Step 4: Verify the Deployment

Verify the deployment of the ALB Ingress Controller.

kubectl get deployment -n kube-system aws-load-balancer-controller
kubectl get pods -n kube-system

The deployment is successful!

Step 5: Access the Application

Retrieve the external endpoint for the 2048 application.

kubectl get ingress -n game-2048

Copy the address and paste it into your browser to access the 2048 game application.

We have successfully deployed our 2048 Game Application!

Conclusion

By following this comprehensive guide, you have successfully deployed the 2048 game application using AWS EKS. This deployment leverages the power of AWS-managed services to provide a scalable, secure, and highly available Kubernetes environment. With the EKS cluster in place, you can now explore further customization, monitoring, and scaling options to suit your application needs. Happy deploying!

Follow this GitHub Repo: https://github.com/nishankkoul/2048-Game-EKS/