Introduction to Deploying Applications on EKS

Amazon Elastic Kubernetes Service (EKS) is a managed Kubernetes service that makes deploying, managing, and scaling containerized applications easier. With the power of Kubernetes and AWS, EKS provides scalability, high availability, and security, allowing you to focus on application development rather than infrastructure management.

In this guide, we’ll walk you through deploying your application on EKS, from preparing it for containerization to testing the deployment.

Preparing Your Application for Containerization

It needs to be containerized before you can deploy an application to EKS. Containerization involves packaging your application, its dependencies, and the runtime environment into a single container. Docker is the most widely used tool for creating containers, making it ideal for this task.

Critical Steps for Preparing:

  1. Review Application Dependencies: Ensure all required libraries and software are listed in the configuration.
  2. Dockerize the Application: Create a Dockerfile in the root directory of your project.
  3. Optimize the Dockerfile: Use multi-stage builds to minimize the image size and improve performance.

Sample Dockerfile:

# Use an official Python runtime as a parent image

FROM python:3.8-slim

# Set the working directory

WORKDIR /app

# Copy the current directory contents into the container at /app

ADD . /app

# Install any needed packages specified in requirements.txt

RUN pip install –no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container

EXPOSE 80

# Define environment variable

ENV NAME World

# Run app.py when the container launches

CMD [“python”, “app.py”]

Creating a Custom Container Image

Once you have a Dockerfile, you can build and push your custom image to Amazon Elastic Container Registry (ECR), a secure, scalable container image repository.

Steps to Create and Push a Custom Image:

  1. Build the Image:
    docker build -t my-app .
  2. Tag the Image for ECR:
    docker tag my-app:latest <account-id>.dkr.ecr.<region>.amazonaws.com/my-app:latest
  3. Push the Image to ECR:
    docker push <account-id>.dkr.ecr.<region>.amazonaws.com/my-app:latest

Setting Up the EKS Cluster

With your application containerized, setting up an EKS cluster is next.

Steps to Set Up EKS:

  1. Create an EKS Cluster Using eksctl:
    eksctl create cluster –name my-cluster –region <region> –nodegroup-name my-nodes –node-type t3.medium –nodes 3
  2. Verify the Cluster is Active:
    kubectl get svc

Deploying the Application Using Kubernetes Manifests

Kubernetes uses manifest files to define the state of your application. These manifests are written in YAML and describe various aspects of your deployment, including Pods, Services, and ConfigMaps.

Sample Deployment Manifest:

apiVersion: apps/v1

kind: Deployment

metadata:

  name: my-app

spec:

  replicas: 2

  selector:

    matchLabels:

      app: my-app

  template:

    metadata:

      labels:

        app: my-app

    spec:

      containers:

      – name: my-app

        image: <account-id>.dkr.ecr.<region>.amazonaws.com/my-app:latest

        ports:

        – containerPort: 80

To deploy the application:

kubectl apply -f deployment.yaml

Exposing the Application with a Load Balancer

To make your application accessible externally, you must expose it using a Kubernetes service backed by an AWS load balancer.

Service Manifest with Load Balancer:

apiVersion: v1

kind: Service

metadata:

  name: my-app-service

spec:

  selector:

    app: my-app

  ports:

    – protocol: TCP

      port: 80

      targetPort: 80

  type: LoadBalancer

Deploy the service:

kubectl apply -f service.yaml

This command will create your application’s AWS Elastic Load Balancer (ELB).

Testing and Verifying the Deployment

Once the deployment and service are up, you can test your application by obtaining the load balancer’s external IP.

Steps to Test:

  1. Get the Load Balancer URL:
    kubectl get svc my-app-service
  2. Access the Application: Use the external IP or DNS name of the Load Balancer to verify that your application is running as expected.

You can now access your deployed application through the browser or send HTTP requests to the Load Balancer’s address.

Conclusion

Deploying applications on EKS offers a scalable, secure, and flexible solution for modern containerized applications. This step-by-step guide should provide a clear roadmap for deploying your applications using EKS and Kubernetes, leveraging AWS services for efficient management and scaling.

References

Deploy an application to Amazon EKS

Deploy application containers to Amazon EKS with AWS App2Container