From 3e0f7409e65a7c0f68188c190f762c8c64638586 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 18:33:16 +0530 Subject: [PATCH 01/13] Create test.txt --- test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.txt diff --git a/test.txt b/test.txt new file mode 100644 index 00000000..45b983be --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +hi From 610dbf2ed227b9bab408df54f5ee828c49630d94 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 18:39:00 +0530 Subject: [PATCH 02/13] Create main.py --- fastapi-service/main.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 fastapi-service/main.py diff --git a/fastapi-service/main.py b/fastapi-service/main.py new file mode 100644 index 00000000..9628872d --- /dev/null +++ b/fastapi-service/main.py @@ -0,0 +1,7 @@ +from fastapi import FastAPI + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"message": "Hello from FastAPI in Kubernetes!"} From 25eed5288ef73381ade2f4947cffb712fa8c9550 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 18:39:46 +0530 Subject: [PATCH 03/13] Create Dockerfile --- fastapi-service/Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 fastapi-service/Dockerfile diff --git a/fastapi-service/Dockerfile b/fastapi-service/Dockerfile new file mode 100644 index 00000000..0f531652 --- /dev/null +++ b/fastapi-service/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.9-slim +WORKDIR /app +COPY . /app +RUN pip install fastapi uvicorn +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] From 9e9ab26cd119e150f7c6c2486779a2d9614c6625 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 18:54:02 +0530 Subject: [PATCH 04/13] Create deployment.yaml --- fastapi-service/deployment.yaml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 fastapi-service/deployment.yaml diff --git a/fastapi-service/deployment.yaml b/fastapi-service/deployment.yaml new file mode 100644 index 00000000..a1a484e5 --- /dev/null +++ b/fastapi-service/deployment.yaml @@ -0,0 +1,20 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: fastapi-service +spec: + replicas: 2 + selector: + matchLabels: + app: fastapi-service + template: + metadata: + labels: + app: fastapi-service + spec: + containers: + - name: fastapi-service + image: 503147168047.dkr.ecr.ap-south-1.amazonaws.com/fastapi-service:latest # Updated with ECR details + ports: + - containerPort: 8000 + imagePullPolicy: Always # Ensures the latest image is pulled on deployment From f22fa677a75d83391903eba5c5052c8e8af5580f Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 18:54:34 +0530 Subject: [PATCH 05/13] Create service.yaml --- fastapi-service/service.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 fastapi-service/service.yaml diff --git a/fastapi-service/service.yaml b/fastapi-service/service.yaml new file mode 100644 index 00000000..4896bdbf --- /dev/null +++ b/fastapi-service/service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: fastapi-service +spec: + selector: + app: fastapi-service + ports: + - protocol: TCP + port: 80 + targetPort: 8000 + type: LoadBalancer # Exposes service externally From 756f36f2dae5bcc88b7a61e8a480466bf6a7926b Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:27:51 +0530 Subject: [PATCH 06/13] Create cicd.yml --- .github/workflows/cicd.yml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/cicd.yml diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 00000000..3a49108a --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,38 @@ +name: Build and Push Docker Image to ECR + +on: + push: + branches: + - main # Runs when code is pushed to the main branch + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Set Up AWS CLI + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Login to Amazon ECR + run: | + aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }} + + - name: Build and Tag Docker Image + run: | + IMAGE_TAG=$(git rev-parse --short HEAD) + docker build -t ${{ secrets.ECR_REPO }} . + docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG + docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest + + - name: Push Docker Image to ECR + run: | + IMAGE_TAG=$(git rev-parse --short HEAD) + docker push ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG + docker push ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest From 8e1b21cf222e4b8a745fa1ce25df778b39ef5b11 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:54:01 +0530 Subject: [PATCH 07/13] Update cicd.yml --- .github/workflows/cicd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 3a49108a..aef75c0f 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -27,7 +27,7 @@ jobs: - name: Build and Tag Docker Image run: | IMAGE_TAG=$(git rev-parse --short HEAD) - docker build -t ${{ secrets.ECR_REPO }} . + docker build -t ${{ secrets.ECR_REPO }} -f fastapi-service/Dockerfile fastapi-service/ docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest From e10925d111c65757a1e3137cdf55f8dba6d8a12c Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:56:53 +0530 Subject: [PATCH 08/13] Update cicd.yml --- .github/workflows/cicd.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index aef75c0f..af6228a8 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -27,7 +27,8 @@ jobs: - name: Build and Tag Docker Image run: | IMAGE_TAG=$(git rev-parse --short HEAD) - docker build -t ${{ secrets.ECR_REPO }} -f fastapi-service/Dockerfile fastapi-service/ + cd fastapi-service # Change directory before building + docker build -t ${{ secrets.ECR_REPO }} -f Dockerfile . docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest From 37b787ec3be42571748c4e05591508cb202bc500 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:59:13 +0530 Subject: [PATCH 09/13] Update cicd.yml --- .github/workflows/cicd.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index af6228a8..aef75c0f 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -27,8 +27,7 @@ jobs: - name: Build and Tag Docker Image run: | IMAGE_TAG=$(git rev-parse --short HEAD) - cd fastapi-service # Change directory before building - docker build -t ${{ secrets.ECR_REPO }} -f Dockerfile . + docker build -t ${{ secrets.ECR_REPO }} -f fastapi-service/Dockerfile fastapi-service/ docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest From 2461c783f115899d90d258da639651b661df1b63 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 23:01:15 +0530 Subject: [PATCH 10/13] Create main.py --- main.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 00000000..3c7e3763 --- /dev/null +++ b/main.py @@ -0,0 +1,19 @@ +from fastapi import FastAPI + +# Create a FastAPI instance +app = FastAPI() + +# Define a root endpoint +@app.get("/") +def read_root(): + return {"message": "Hello from FastAPI running on Kubernetes!"} + +# Example endpoint with a parameter +@app.get("/items/{item_id}") +def read_item(item_id: int, q: str = None): + return {"item_id": item_id, "query": q} + +# Health check endpoint (useful for Kubernetes readiness and liveness probes) +@app.get("/health") +def health_check(): + return {"status": "healthy"} From 1284b42334c314428d0893cb1bc085dee0b84921 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 23:02:03 +0530 Subject: [PATCH 11/13] Create requirements.txt --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..97dc7cd8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi +uvicorn From 6d6977706444520bdec1fca8b8734b65c787009d Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 23:02:41 +0530 Subject: [PATCH 12/13] Create Dockerfile --- Dockerfile | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..99f7ee23 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use a lightweight Python image +FROM python:3.9-slim + +# Set the working directory +WORKDIR /app + +# Copy only the necessary files first +COPY requirements.txt . + +# Install dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the FastAPI application files +COPY . . + +# Expose the application port +EXPOSE 8000 + +# Run the FastAPI application using Uvicorn +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] From 4076f009f804439b27db8d909a4170bac7234a05 Mon Sep 17 00:00:00 2001 From: maddy00o7 <149822847+maddy00o7@users.noreply.github.com> Date: Thu, 13 Feb 2025 23:04:35 +0530 Subject: [PATCH 13/13] Update cicd.yml --- .github/workflows/cicd.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index aef75c0f..0a33508f 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -27,9 +27,9 @@ jobs: - name: Build and Tag Docker Image run: | IMAGE_TAG=$(git rev-parse --short HEAD) - docker build -t ${{ secrets.ECR_REPO }} -f fastapi-service/Dockerfile fastapi-service/ - docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG - docker tag ${{ secrets.ECR_REPO }}:latest ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest + docker build -t ${{ secrets.ECR_REPO }}:$IMAGE_TAG -f fastapi-service/Dockerfile fastapi-service/ + docker tag ${{ secrets.ECR_REPO }}:$IMAGE_TAG ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:$IMAGE_TAG + docker tag ${{ secrets.ECR_REPO }}:$IMAGE_TAG ${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPO }}:latest - name: Push Docker Image to ECR run: |