Deploying a Kubernetes Cluster on Amazon EKS and Running a Flask App

Deploying a Kubernetes Cluster on Amazon EKS and Running a Flask App

Amazon EKS (Elastic Kubernetes Service) is a fully managed service that allows you to run Kubernetes on AWS without needing to manage your own control plane. In this guide, we'll walk through setting up an EKS cluster, deploying a simple NGINX pod, and finally deploying a Flask application using a two-tier architecture.

Prerequisites

Before we begin, make sure you have:

  1. AWS account with appropriate permissions (AdministratorAccess).

  2. A new EC2 instance in the us-west-2 region.

Step 1: Create an Admin User on the EC2 Instance

  1. Launch a new EC2 instance in us-west-2.

  2. Create a user with AdministratorAccess on the instance to manage AWS resources.

Step 2: Install AWS CLI on Your EC2 Instance

AWS CLI is necessary to manage AWS resources from the terminal.

  1. Update the system and install unzip:

     sudo apt install unzip
    

  2. Download and install AWS CLI

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

  1. Configure AWS CLI with your credentials:
aws configure

  1. Test the AWS CLI installation by listing users:
aws iam list-users

Step 3: Install eksctl

eksctl is a simple CLI tool for creating and managing EKS clusters.

https://eksctl.io/installation/

curl -sL "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

Step 4: Install kubectl

kubectl is a command-line tool used to interact with your Kubernetes cluster.

https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

  1. Download and install kubectl:
   curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

Optionally validate the kubectl binary: Download the kubectl checksum file:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"

Validate the kubectl binary against the checksum file:

echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check

Install kubectl

sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Step 5: Create an EKS Cluster

Use eksctl to create an EKS cluster with minimal node configuration.

  1. Create the EKS cluster:
eksctl create cluster --name tws-cluster --region us-west-2 --node-type t2.micro --nodes-min 2 --nodes-max 3

if you need to check what is going on in the backend check cloudformation

Verify the cluster using kubectl:

kubectl get nodes

Check the available namespaces:

kubectl get namespace
kubectl get pods -n kube-system

Update the kubeconfig:

aws eks update-kubeconfig --name tws-cluster --region us-west-2

  • aws eks update-kubeconfig: The AWS CLI command to update your kubeconfig file for EKS.

  • --name tws-cluster: Specifies the name of your EKS cluster, in this case, tws-cluster.

  • --region us-west-2: Specifies the AWS region where your EKS cluster is running.

After running this command:

  1. Your local ~/.kube/config file will be updated with information to connect to the EKS cluster.

  2. You can check the available nodes or resources in your cluster using kubectl:

Step 7: Deploy a Flask App

We will now deploy a Flask application using a two-tier architecture with MySQL as the database.

  1. Clone the Flask app repository:
https://github.com/LondheShubham153/two-tier-flask-app.git

create a namespace or apply if from the namespace.yaml file

kubectl apply -f namespace.yaml

Apply the MySQL manifest files:

kubectl apply -f mysql-deployment.yaml -f mysql-pv.yaml -f mysql-pvc.yaml -f mysql-secret.yaml -f mysql-service.yaml

kubectl get all -n two-tier-ns

Apply the Flask app manifest files:

kubectl apply -f flask-deployment.yaml -f flask-service.yaml

kubectl get all -n two-tier-ns

now all the pods are running

copy the external ip and past on the browser

Step 8: Clean Up Resources

  1. Delete the Flask app and all resources:
kubectl delete -f .

Finally, delete the EKS cluster:

eksctl delete cluster --name tws-cluster --region us-west-2

Conclusion

You've successfully created and managed an EKS cluster, deployed a two-tier Flask app, and cleaned up your environment. This guide is a great starting point for working with Amazon EKS and understanding the basics of Kubernetes deployment on AWS.

Feel free to ask any questions or share your thoughts in the comments below!