Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,058 changes: 1,058 additions & 0 deletions Dashboard.json

Large diffs are not rendered by default.

182 changes: 181 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,181 @@
# docker-fastapi-test
# docker-fastapi-test



Application:-
clone the below repositry

Repo URL:- https://github.com/jayan/docker-fastapi-test

To clone the these use
```bash
git clone https://github.com/jayan/docker-fastapi-test
```

Dockerized the application

```bash
nano dockerfile
```

```bash
# Dockerfile

# pull the official docker image
FROM python:3.11.1-slim

# set work directory
WORKDIR /app

# set env variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
```

create docker-compose.yml file

```bash
nano docker-compose.yml
```

```bash
version: '3.8'

services:
web:
build: .
command: uvicorn app.main:app --host 0.0.0.0
volumes:
- .:/app
ports:
- 8008:8000

prometheus:
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"

grafana:
image: grafana/grafana
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD: admin
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana
depends_on:
- prometheus

volumes:
grafana-data:
```

create prometheus.yml

```bash
nano prometheus.yml
```

```bash
global:
scrape_interval: 15s

scrape_configs:
- job_name: "api"
metrics_path: "/metrics"
static_configs:
- targets: ["13.232.27.191:8008"]

```

create a jenkins file

```bash
nano jenkinsfile
```

```bash
pipeline {
agent any

environment {
GIT_REPO = 'https://github.com/jayan/docker-fastapi-test.git'
}

stages {
stage('Checkout') {
steps {
// Checkout the code from GitHub
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
userRemoteConfigs: [[url: "${env.GIT_REPO}"]]
])
}
}
stage('Deploy') {
steps {
// Run docker compose up --build -d
sh 'docker compose up --build -d'
}
}
}
}
```

# upload it into the git hub using these commands

```bash
git add .
git commit -m "commit"
git push origin main
```
#install jenkins and docker using jenkins.sh and docker.sh

After configuring, create a job to run the Jenkinsfile in Jenkins and check whether the FastAPI application is up and running.


![6114142690867264183](https://github.com/user-attachments/assets/9dcc15a0-e577-4e2a-adb6-bde66f394ec5)

![6114142690867264183](https://github.com/user-attachments/assets/a0f55a14-e709-4b53-a9b1-7ea016d0daad)

These is my Fast api application
```bash
http://13.232.27.191:8008/docs
```

![6114142690867264184](https://github.com/user-attachments/assets/ba0cdbd7-6096-4f8f-862d-dae8085e7654)

monitoring tools Prometheus and Grafana
```bash
http://13.232.27.191:9090/
http://13.232.27.191:3000/
```

![6114142690867264182](https://github.com/user-attachments/assets/e67b7142-cab1-472b-9846-c745f85b88f9)


![6114142690867264185](https://github.com/user-attachments/assets/2fc6a74c-cb01-4235-aa5f-b7a1ec7f2bbe)


#Once the application runs successfully, make sure to destroy containers and recreate another one and check if previous data is still present.

These is the data Before destroying the previous containers


![6114142690867264188 (1)](https://github.com/user-attachments/assets/87df6d2e-f51f-4bbb-8513-2e48fd7e1df7)

After destrying the containers i recreated the containers these is the output

![6114142690867264189](https://github.com/user-attachments/assets/d860eeb1-1c6d-4900-a2e8-8846e4b3cb5f)

Previous data was still existed
68 changes: 68 additions & 0 deletions Terraform/ec2.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#provider
provider "aws" {
region = "us-east-1" # Change to your desired region
}

# security group
resource "aws_security_group" "web_sg" {
name_prefix = "web-sg-"

ingress {
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 9090
to_port = 9090
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 8000
to_port = 8000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 8008
to_port = 8008
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "web-sg"
}
}

#EC2 instance
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
key_name = "webserver"

security_groups = [aws_security_group.web_sg.name]

tags = {
Name = "web-server-instance"
}
}
4 changes: 3 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import List
from fastapi import FastAPI

from prometheus_fastapi_instrumentator import Instrumentator
from app import services
from app.schema import UserIn, BaseResponse, UserListOut

app = FastAPI()

# Initialize Prometheus Instrumentator
instrumentator = Instrumentator().instrument(app).expose(app)

@app.get("/")
async def index():
Expand Down
34 changes: 34 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3.8'

services:
web:
build: .
command: uvicorn app.main:app --host 0.0.0.0
volumes:
- .:/app
ports:
- 8008:8000

prometheus:
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"

grafana:
image: grafana/grafana
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD: admin
ports:
- "3000:3000"
volumes:
- grafana-data:/var/lib/grafana
depends_on:
- prometheus

volumes:
grafana-data:


17 changes: 17 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Dockerfile

# pull the official docker image
FROM python:3.11.1-slim

# set work directory
WORKDIR /app

# set env variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
16 changes: 16 additions & 0 deletions install/docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
# Add Docker's official GPG key:
sudo apt-get update -y
sudo apt-get install ca-certificates curl -y
sudo install -m 0755 -d /etc/apt/keyrings -y
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
9 changes: 9 additions & 0 deletions install/jenkins.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
#install jenkins
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y
27 changes: 27 additions & 0 deletions jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pipeline {
agent any

environment {
GIT_REPO = 'https://github.com/jayan/docker-fastapi-test.git'
}

stages {
stage('Checkout') {
steps {
// Checkout the code from GitHub
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
userRemoteConfigs: [[url: "${env.GIT_REPO}"]]
])
}
}
stage('Deploy') {
steps {
// Run docker compose up --build -d
sh 'docker compose up --build -d'
}
}
}
}

10 changes: 10 additions & 0 deletions prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
global:
scrape_interval: 15s

scrape_configs:
- job_name: "api"
metrics_path: "/metrics"
static_configs:
- targets: ["13.232.27.191:8008"]


Binary file modified requirements.txt
Binary file not shown.