How to Set Up an EKS Cluster and Deploy a Three-Tier Application

vansh bhardwajvansh bhardwaj
6 min read

What is EKS?

Amazon Elastic Kubernetes Service (EKS) is a managed Kubernetes service provided by AWS. EKS handles the complexity of setting up and managing a Kubernetes cluster, allowing developers to focus on deploying and scaling their applications.
It automatically manages the Kubernetes control plane, offers high availability, and integrates with other AWS services like IAM and VPC.

What is a Three-Tier Application?

A three-tier application architecture divides an application into three main layers:

  1. Backend (Application Layer): This layer handles the application’s business logic, processing data, and responding to client requests. For this blog, we’ll use Django .

  2. Frontend (Presentation Layer): The frontend interacts with users, presenting data and capturing input. We will use React for this layer.

  3. Database (Data Layer): This layer manages the application’s data and ensures persistence. For this, we’ll use MySQL.

In this blog, we’ll deploy these layers on Amazon EKS, using Django for the backend, React for the frontend, and MySQL as the database.

What Are We Doing Here?

We are going to set up an EKS cluster and deploy:

  • Backend (Django): A simple Django application.

  • Frontend (React): A React-based user interface.

  • Database (MySQL): A MySQL database.

For this project, I am using a pre-built project named Projectmanager hosted on GitHub. You can use your own 3-tier application.

Let’s dive into the implementation. 🚀🚀🚀

Step-by-Step Guide

Step 1: Spin base EC2 Instance and Install Required Tools

First, create an EC2 instance on AWS and SSH into it. Install the following tools:

  • Docker: To containerize the application.

  • AWS CLI: To interact with AWS services.

  • kubectl: To manage Kubernetes clusters.

  • eksctl: A simple CLI tool for creating EKS clusters.

How to Install eksctl:

Run the following commands to install eksctl:

$ curl -sSL "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar -xz -C /tmp

$ sudo mv /tmp/eksctl /usr/local/bin

$ eksctl version

Step 2: Configure AWS CLI

Install the AWS CLI using:

$ sudo apt-get install awscli

Next, configure AWS CLI with your credentials:

  1. Create access keys by going to the AWS Management Console, navigating to your account settings
    Security Credentials > Create access keys (use root user)
    Once access keys are created download csv file.

  2. Run the aws configure command and provide the access key and secret key.

$ aws configure

Step 3: Create an EKS Cluster

Now that we have the required tools, let’s create the EKS cluster, use the command:

$ eksctl create cluster --name projectmanager --region us-east-1 --node-type t2.large --nodes-min 2 --nodes-max 3

Let’s examine each part of the command:

eksctl create cluster*:* base command to create a new Kubernetes cluster on Amazon EKS.

— name projectmanager: Specifies the name of the cluster. I’m calling it projectmanager

— region us-east-1: Defines the AWS region where the cluster will be created.

— node-type t2.large: Sets the type of EC2 instances to be used as worker nodes in the cluster. Use t2.large as it will have 8 GB memory and 2 vCPUs

— nodes-min 2: Specifies the minimum number of nodes in the cluster’s autoscaling group. So our cluster will always maintain at least 2 nodes.

— nodes-max 3: Specifies the maximum number of nodes in the cluster’s autoscaling group. The cluster can scale up to a maximum of 3 nodes based on workload demands.

You can choose them according to your own requirements.

Verify the creation of the cluster:

$ kubectl get nodes

Step 4: Get the Code

Clone the project from your GitHub repository:

$ git clone https://github.com/vansh1999/projectmanager.git

$ cd projectmanager

Step 5: Containerize and Build Image

We will use Docker to containerize our application.

Create a Dockerfile for the backend(Django) first

$ vim Dockerfile

Dockerfile

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Now, build and push the Docker image to DockerHub:

$ docker build -t vansh1999/projectmanager:latest .

$ docker login
(provide your DockerHub username and password , you can also use Amazon Elastic Container Registry (ECR))

$ docker push vansh1999/projectmanager:latest

Step 6: Create Kubernetes Manifests

Create Kubernetes manifests for the deployment and service of the backend(Django).

Deployment Manifest (deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: projectmanager
spec:
  replicas: 3
  selector:
    matchLabels:
      app: projectmanager
  template:
    metadata:
      labels:
        app: projectmanager
    spec:
      containers:
      - name: projectmanager
        image: vansh1999/projectmanager:latest
        ports:
        - containerPort: 8000
        env:
        - name: DJANGO_SETTINGS_MODULE
          value: "projectmanager.settings"

Service Manifest (service.yaml):

apiVersion: v1
kind: Service
metadata:
  name: projectmanager-service
spec:
  selector:
    app: projectmanager
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8000
  type: LoadBalancer

Why a Load Balancer?

We need a LoadBalancer to expose the application to the external world (Internet). Load balancer distributes incoming traffic across multiple pods, ensuring high availability and scalability.

Step 7: Deploy Your App to EKS

Apply the Kubernetes manifests to deploy the app:

$ kubectl apply -f deployment.yaml

$ kubectl apply -f service.yaml

Verify the deployment and ensure all pods are running:

$ kubectl get pods

Check the service:

$ kubectl get svc

Note the EXTERNAL-IP of the projectmanager-service. This is the public URL of your app.

Step 8: Deploy the Frontend (React) and Database (MySQL)

  1. First, containerize the React frontend and write a Dockerfile for same

  2. Then, Create Kubernetes manifests for the frontend deployment and service, similar to the backend.

You can use the MySQL container to deploy the database. Here’s the deployment YAML for MySQL:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "password"
        ports:
        - containerPort: 3306

Create a service for MySQL to expose the database:

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306
  type: ClusterIP

Step 9: Access Your App

Access your app by using the EXTERNAL-IP:

http://<EXTERNAL-IP>

Conclusion

In this article, we walked through the entire process of setting up an EKS cluster and deploying a three-tier application consisting of a backend (Django), frontend (React), and a MySQL database. By containerizing each component, creating the appropriate Kubernetes manifests, and deploying the app to EKS, you can leverage the scalability and reliability of Kubernetes for your cloud-native applications.

Now, you have a fully functional three-tier application running on AWS EKS. You can scale, manage, and monitor your app efficiently using Kubernetes.

GitHub Repository : projectmanager

About Me

Hi, I’m Vansh. I’m Building stuff on the web. Exploring Cloud and DevOps. Passionate about creating and scaling solutions. Let’s connect on Twitter: [heyyvanshh].

0
Subscribe to my newsletter

Read articles from vansh bhardwaj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

vansh bhardwaj
vansh bhardwaj