Project 3 - Deploying a Python-Based Microservice Application on AWS EKS

Project 3 - Deploying a Python-Based Microservice Application on AWS EKS

In today’s fast-paced world of software development, deploying scalable and reliable microservice applications is essential. Microservices allow for the efficient management of various components of a complex application, ensuring flexibility and robustness. To help you navigate this journey, we’ve prepared a comprehensive step-by-step guide on deploying a Python-based microservice application on AWS Elastic Kubernetes Service (EKS).

This application is composed of four pivotal microservices: the auth-server, converter-module, database-server (leveraging PostgreSQL and MongoDB), and the notification-server. By the end of this guide, you'll have a fully operational microservices-based application up and running on AWS EKS.

All credits To Nasiullha Chaudhari

YouTube video: https://youtu.be/jHlRqQzqB_Y?si=oVjnBLWD5JEs_dFW

Prerequisites:

Before we dive into the deployment process, make sure you’ve completed the following prerequisites:

  1. Create an AWS Account: If you’re new to AWS, it’s essential to have an AWS account to access the services and resources required for this deployment. Don’t worry; we’ll guide you through the process.

  2. Install Helm: Helm, a Kubernetes package manager, simplifies the deployment of applications on Kubernetes clusters. We’ll walk you through the installation steps.

  3. Python Installation: Ensure that Python is installed on your system. You can download it directly from the official Python website if needed.

  4. AWS CLI: The AWS Command Line Interface (CLI) is your command-line interface to AWS services. We’ll help you set it up for seamless interaction with AWS resources.

  5. kubectl Installation: Kubectl is the command-line tool for interacting with Kubernetes clusters. Installing it is crucial for managing your EKS cluster.

  6. Database Setup: As our microservice application relies on both PostgreSQL and MongoDB, ensure that these databases are set up and ready to go.

With these prerequisites in place, you’ll be well-prepared to embark on your journey to deploy a Python-based microservice application on AWS EKS. This guide will take you through each step, providing clear instructions and best practices to ensure your success.

Launch an Ec2 Instance

To launch an AWS EC2 instance with Ubuntu 22.04 using the AWS Management Console, sign in to your AWS account, access the EC2 dashboard, and click “Launch Instances.” In “Step 1,” select “Ubuntu 22.04” as the AMI, and in “Step 2,” choose “t2.medium” as the instance type. Configure the instance details, storage, tags, and security group settings according to your requirements. Review the settings, create or select a key pair for secure access, and launch the instance. Once launched, you can connect to it via SSH using the associated key pair.

Create an IAM ROLE

Navigate to AWS CONSOLE

Click the “Search” field.

Type “IAM enter

Click “Roles”

Click “Create role”

Click “AWS service”

Click “Choose a service or use case”

Click “EC2”

Click “Next”

Click the “Search” field.

Add permissions policies Administrator Access

Click Next

Click the “Role name” field, Add the name

Click “Create role” (JUST SAMPLE IMAGE BELOW ONE)

Click “EC2”

Go to the instance and add this role to the Ec2 instance.

Select instance → Actions → Security → Modify IAM role

Add a newly created Role and click on Update IAM role.

Now connect to your Ec2 instance using Putty or Mobaxtrem.

Clone this repo

https://github.com/Aj7Ay/microservices-python-app.git

Change into the microservices directory

cd microservices-python-app

Provide the executable permissions to the file

sudo chmod +x script.sh
#run this script
./script.sh

This script does the following:

It sets up your system with the necessary tools and services for your environment.

Check versions once the script completes running.

# Check Versions
terraform -version
kubectl version --client --short
aws --version
helm version --short
python3 --version
postgres --version
mongod --version
docker version

Now go inside Eks-terraform to provision Eks cluster with terraform files

cd Eks-terraform

Now you can see terraform files and make sure to change your region and S3 bucket name in the backend file.

Now initialize

terraform init

Validate the code

terraform validate

Let’s see plan

terraform plan

Let’s apply to provision

terraform apply --auto-approve

It will take 10 minutes to provision

It will create a cluster and node group with one ec2 instance

Node group ec2 instance

Now come outside of the Eks-terraform directory.

cd ..

Now update the Kubernetes configuration

aws eks update-kubeconfig --name <cluster-name> --region <region>

Let’s see the nodes

kubectl get nodes

If you provide ls, you will see Helm_charts

ls -a

Let’s go inside the Helm_charts

cd Helm_charts
ls

Now go inside MongoDB and apply the k8s files

cd MongoDB
ls
helm install mongo .   #to apply all deployment and service files

Now provide the command

kubectl get all

Now see Persistent volume

kubectl get pv

Now come back

cd ..
cd Postgres
sudo vi init.sql

Change your mail here

Apply the Kubernetes files using Helm

helm install postgres .

Let’s see the pods and deployments

kubectl get all

Let’s add some ports for our node group ec2 instance

Go to the Node group ec2 instance and select the security group of the node group

Add these rules for it

Now copy the public IP of the Node group ec2 instance

Go back to Putty and paste the following command with updated details

mongosh mongodb://<username>:<pwd>@<nodeip>:30005/mp3s?authSource=admin
#username use nasi
#pwd nasi1234    #if you want to update them go to mongo secrets.yml file and update
#nodeip #use your node ec2 instance ip

Now you are connected to MongoDB. come out of it.

exit

Connect to the Postgres database and copy all the queries from the “init.sql” file.

psql 'postgres://<username>:<pwd>@<nodeip>:30003/authdb'
#username nasi
#pwd cnd2023
#nodeip node group ec2 public ip

Now it’s connected to psql

Now add the init.sql file

CREATE TABLE auth_user (
    id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    email VARCHAR (255) NOT NULL,
    password VARCHAR (255) NOT NULL
);

--Add Username and Password for Admin User
-- INSERT INTO auth_user (email, password) VALUES ('thomasfookins007helby@gmail.com', '123456');
INSERT INTO auth_user (email, password) VALUES ('<User your mail>', '123456');

Use your mail for values

Now provide \d to see data tables

Now provide the below command to see the authentication user

SELECT * from auth_user;

You will get your email and password.

Now provide an exit to come out of psql

exit

Now change the directory into RabbitMq to deploy

#if you are at Postgres directory use 
cd ..
cd RabbitMQ
ls
helm install rabbit .

Now see the deployments and pods

kubectl get all

Now copy the public IP of the node group Ec2 instance

<Node-ec2-public-ip:30004>

You will get this page Just login

username is guest 
password is guest

After login, you will see this page

Now click on Queues

Click on Add a new Queue

Type as classic

Name mp3 → Add queue.

Now click again Add a new queue for Video

Type as classic → Name as video → Add queue

You will see queues like this.

Now go back to Putty

Come out of helm charts

Microservices

cd ../..
#if you are at microservices-python-app
cd src
ls      #these are the microservices

Now Go inside each microservice, and you will find a Dockerfile
you can create your docker images for each microservice and use it in the deployment file also.

First Go inside auth-service

cd auth-service
ls
cd manifest #directory
kubectl apply -f .

Now let’s see whether it created pods or not

kubectl get all

Now come back to another Microservice and do the same process

cd ../..
cd gateway-service
cd manifest
kubectl apply -f .

Check

kubectl get all

Now come back to another microservice and do the same process

cd ../..
cd converter-service
cd manifest
kubectl apply -f .

Check for deployments and service

kubectl get all

GMAIL PASSWORD

Now let’s create a Password for Gmail to get Notifications

Open your Gmail account in the browser and click on your profile top right.

Click on Manage your Google account

Now click on Security

Two-step verification should be enabled
If not, enable it

Now click on the search bar and search for the APP

Click on App passwords

It will ask for your Gmail password, provide and login

Now for the app name, you can use any name → create

You will get a Password, copy it and save it for later use.

One time watchable

Come back to Putty and update the secret.yaml file for notification service microservice

cd ../..
cd notification-service
cd manifest
sudo vi secret.yaml
#change your mail and password that Generated.

Now apply the manifest files

kubectl apply -f .

Check whether it created deployments or not.

kubectl get all

Now come to the assets directory

cd /home/ubuntu/microservice-python-app
cd assets
ls
curl -X POST http://nodeIP:30002/login -u <email>:<password>

Copy the token and paste it inside a base 64 decoder you will get this

change JWT Token and node ec2 IP.

curl -X POST -F 'file=@./video.mp4' -H 'Authorization: Bearer <JWT Token>' http://nodeIP:30002/upload

It will send an ID to the mail

Let’s check the mail

Copy that ID and paste it inside the below command with the JWT token and fid

curl --output video.mp3 -X GET -H 'Authorization: Bearer ' "http://nodeIP:30002/download?fid="
#change Bearer with JWT token
#nodeIp
#fid token at end

It will download an MP3 file

You can see it’s created mp3 file and you can play it.

aws s3 cp your-file s3://your-bucket/your-prefix/

You can copy it s3 bucket and download and you can listen to it.

Destroy

Now go inside Eks-terraform directory

To delete eks cluster

terraform destroy --auto-approve

It will take 10 minutes to delete the cluster

Once that is done
Remove your Ec2 instance and Iam role.

Did you find this article valuable?

Support CloudOpsAcademy - Prashanth Katkam by becoming a sponsor. Any amount is appreciated!