Birla Institute of Technology & Science, Pilani
Work Integrated Learning Programmes Division
First Semester 2024-2025
Devops for Cloud (S1-24_CCZG507) Course Assignment
Name : Kowshalyaa Kumaraguru
Roll No: 2023mt03601
Task 1: Create the Backend Application using FastAPI
Steps followed:
1. Created and moved to directory using the commands below.
mkdir app-2023mt03601
cd app-2023mt03601
2. Created Virtual Env and installed required libraries.
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn
3. Created the python application using FastAPI - main.py.
from fastapi import FastAPI
import os
# Create the FastAPI app instance
app = FastAPI()
# Retrieve app_version and app_title from environment variables, with default values if not set
app_version = os.getenv("APP_VERSION", "1.0")
app_title = os.getenv("APP_TITLE", "Welcome to Kowshalyaa's FastAPI Application")
@app.get("/get_info")
def get_info():
return {"app_version": app_version, "app_title": app_title}
4. Set Environment variables.
export APP_VERSION=1.0
export APP_TITLE="Welcome to Kowshalyaa's FastAPI Application"
5. Run the FastAPI application locally using Uvicorn
uvicorn main:app --reload
Screenshots, incl. Output:
Task 2: Dockerize the Backend Application
Steps followed:
Pre-requisites - Docker installed.
1. Create Dockerfile.
# Step 1: Use a base image with Python 3
FROM python:3.9-slim
# Step 2: Set the working directory inside the container
WORKDIR /app
# Step 3: Copy the requirements file into the container
COPY requirements.txt .
# Step 4: Install dependencies from the requirements file
RUN pip install --no-cache-dir -r requirements.txt
# Step 5: Copy the entire application into the container
COPY . .
# Step 6: Expose port 8000 to allow access from the host machine
EXPOSE 8000
# Step 7: Define the command to run the FastAPI app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
2. Create requirements.txt.
fastapi
uvicorn
3. Build Docker Image.
docker build -t img-2023mt03601 .
4. Verify Docker Image creation using below command.
docker images
Screenshots, incl. Output:
Task 3: Run the Docker Container
Steps followed:
1. We have created a Docker image in the previous step, running it using below command.
docker run -d --name cnr-2023mt03601 -p 8000:8000 img-2023mt03601
Screenshots, incl. Output:
[Stopping the container and checking for output - unable to connect as expected]
Task 4: Deploy the Docker Image to a Kubernetes Cluster
Steps followed:
Pre-requisites - Minikube and kubectl installed.
1. Start Minikube
minikube start
2. Create a ConfigMap file and apply it.
vi config-2023mt03601.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: config-2023mt03601
data:
app_version: "1.0"
app_title: "Welcome to Kowshalyaa's FastAPI Application"
kubectl apply -f config-2023mt03601.yaml
3. Create a Deployment YAML file and apply it.
vi deployment-2023mt03601.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: fastapi-deployment
spec:
replicas: 2
selector:
matchLabels:
app: fastapi
template:
metadata:
labels:
app: fastapi
spec:
containers:
- name: fastapi-app
image: img-2023mt03601 # Docker image
ports:
- containerPort: 8000
envFrom:
- configMapRef:
name: config-2023mt03601 # Reference to the ConfigMap
created earlier
kubectl apply -f deployment-2023mt03601.yaml
4. Expose the Deployment using Service.
vi service-2023mt03601.yaml
apiVersion: v1
kind: Service
metadata:
name: fastapi-service
spec:
selector:
app: fastapi
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: NodePort
kubectl apply -f service-2023mt03601.yaml
5. Use the following commands to verify.
kubectl get deployments
kubectl get pods
kubectl get services
Screenshots, incl. Output:
Issue observed - Failed to pull image.
Workaround - Added image to Dockerhub, and pulled image as kowshalyaa/img-2023mt03601
[We can see pods running in the second next screenshot.]
Task 5: Configure Networking with a Load Balancer in the Kubernetes Cluster
Steps followed:
1. Edit service YAML to change type to LoadBalancer.
apiVersion: v1
kind: Service
metadata:
name: fastapi-service
spec:
selector:
app: fastapi
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: LoadBalancer # This is the key change!
2. Edit the service.
kubectl expose deployment fastapi-deployment --type=LoadBalancer
--name=fastapi-service
Screenshots, incl. Output:
Task 6: Configure Prometheus for Metrics Collection
Steps followed:
Pre-requisites - Prometheus Helm added.
1. Modify main.py to expose metrics.
from prometheus_fastapi_instrumentator import Instrumentator
instrumentator = Instrumentator()
@app.on_event("startup")
def on_startup():
instrumentator.instrument(app).expose(app)
2. Add Prometheus Scrape Config.
scrape_configs:
- job_name: 'fastapi-app'
static_configs:
- targets: ['<fastapi-service-name>:8001']
kubectl apply -f prometheus-config.yaml
Issue Observed - Version Compatibility conflict when installing requirements. Fixed Prometheus
Version.
FastAPI shown as targets.
[/metrics shows requests_total, cpu_usage etc.]
[requests_total metric on Prometheus UI over time]