Introduction:

Microservices have gained immense popularity in recent years due to their ability to provide scalability, flexibility, and resilience to modern software applications. Amazon Web Services (AWS) offers a powerful managed service called Elastic Kubernetes Service (EKS) for container orchestration, making it an ideal choice for deploying and managing microservices. In this blog, we will walk you through the process of deploying a Python-based microservice application on AWS 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.

Prerequisites:

Before we dive into the deployment process, there are a few prerequisites you should have in place:

  1. An AWS account: You need an AWS account to use Elastic Kubernetes Service (EKS).
  2. AWS Command Line Interface (CLI): Install the AWS CLI on your local machine and configure it with your AWS account credentials.
  3. kubectl: You’ll need the Kubernetes command-line tool kubectl to interact with your EKS cluster.
  4. Docker: Ensure you have Docker installed on your local machine for building container images.
  5. Python: A basic understanding of Python is required for developing and configuring the microservices.
  6. A Python-based microservice application: You should have a Python application with Flask or FastAPI, which are popular web frameworks for building microservices.
  7. Install Helm: Helm, a Kubernetes package manager, simplifies the deployment of applications on Kubernetes clusters. We’ll walk you through the installation steps.
  8. Database Setup: As our microservice application relies on both PostgreSQL and MongoDB, ensure that these databases are set up and ready to go.

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.(T2.medium).

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/Mithun-Kalarikal/microservices-python-app.git
cd cd microservices-python-app

we have to install Terraform, kubectl, AWS CLI, python3, helm, postgres, mongoDb and docker on ec2 instance.

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.

terraform init
terraform validate
terraform plan
terraform apply –auto-approve

It will take 5 to 10 minutes to provision.

It will create a cluster and node group with an ec2 instance.

Node group ec2 instance

Now update the Kubernetes configuration(added new context)

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

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

kubectl get all

kubectl get pv

Now go 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/mobaxtream 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, Use your mail for values

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’);

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 and Come out of helm charts

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 .

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

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.

Come back to Putty/mobaxtream 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>

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

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

It will download an MP3 file

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

If this post was helpful, please do follow and click the clap đź‘Ź button below to show your support.

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

If this post was helpful, please do follow and click the clap đź‘Ź button below to show your support.

Leave a Reply

Your email address will not be published. Required fields are marked *