Dev Ops Assignment Steps
Dev Ops Assignment Steps
Steps followed:
# 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
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
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
Steps followed:
Pre-requisites - Minikube and kubectl installed.
1. Start Minikube
minikube start
apiVersion: v1
kind: ConfigMap
metadata:
name: config-2023mt03601
data:
app_version: "1.0"
app_title: "Welcome to Kowshalyaa's FastAPI Application"
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
apiVersion: v1
kind: Service
metadata:
name: fastapi-service
spec:
selector:
app: fastapi
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: NodePort
Steps followed:
Pre-requisites - Prometheus Helm added.
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']
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]