Automated CI/CD Deployment of a Stock Prediction App with Docker, Kubernetes, and ArgoCD (GitOps-Based)

Project Overview
This project demonstrates the automated CI/CD deployment of a web-based stock price prediction app. The application is built using FastAPI and leverages yfinance
to fetch recent stock data, processes the data with a MinMaxScaler for normalization, and simulates stock price predictions using a dummy LSTM-like function. The app is containerized with Docker and deployed using Kubernetes with ArgoCD for GitOps-based continuous deployment.
Tech Stack
Backend Framework: FastAPI
Data Source: Yahoo Finance (
yfinance
)Containerization: Docker
Deployment: Kubernetes
CI/CD Tool: ArgoCD
Version Control: Git & GitHub
Application Structure
The app consists of the following key components:
app.py
: Contains the FastAPI logic for the stock price prediction UI and prediction functionality.requirements.txt
: Lists the required Python packages.Dockerfile
: Builds the Docker image for the app.stock-deploy.yml
: Kubernetes manifest for deploying the app to a Kubernetes cluster.
Main Functionalities
Accepts a stock ticker (e.g., AAPL, GOOGL).
Downloads the last 7 days of stock prices at 5-minute intervals using the
yfinance
API.Normalizes the data using MinMaxScaler and simulates predictions with a dummy LSTM-like function.
Displays the predicted stock price (note: this is a simulated prediction, not real-time accurate).
Provides a clean, responsive UI for the user to interact with.
Docker Workflow
To build and deploy the app using Docker, follow these steps:
docker build -t stock-predictor-app .
docker tag stock-predictor-app harshithagm478/stock-predictor-app
docker push harshithagm478/stock-predictor-app
Dockerfile
Dockerfile
FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
Kubernetes & ArgoCD
- Create a Kubernetes Namespace:
kubectl create namespace stock-project
- Apply Deployment:
kubectl apply -f stock-deploy.yml
ArgoCD for GitOps Deployment:
Ensure the ArgoCD application is configured with the correct repository path.
Ensure the app name in ArgoCD is lowercase.
Resolve any sync errors by fixing YAML files with proper indentation.
Kubernetes Deployment YAML (stock-deploy.yml
)
apiVersion: apps/v1
kind: Deployment
metadata:
name: stock-project
namespace: stock-project
labels:
app: stock-project
spec:
replicas: 1
selector:
matchLabels:
app: stock-project
template:
metadata:
labels:
app: stock-project
spec:
containers:
- name: stock-predictor-app
image: harshithagm478/stock-predictor-app
resources: {}
Kubernetes Service YAML (stock-service.yml
)
apiVersion: v1
kind: Service
metadata:
name: stock-service
namespace: stock-project
spec:
type: NodePort
selector:
app: stock-project
ports:
- port: 80
targetPort: 8000
nodePort: 30036
Common Issues Faced & Fixes
Namespace Missing:
Solution: Ensure that the Kubernetes namespace is created first.kubectl create namespace stock-project
Invalid Metadata Name:
Solution: Ensure that themetadata.name
field is in lowercase, as Kubernetes requires resource names to be lowercase.App Path Does Not Exist in ArgoCD:
Solution: Point ArgoCD to the correct directory where your Kubernetes manifests are located.YAML Syntax Errors:
Solution: Ensure proper indentation and the correct use of YAML keys. Always validate your YAML files using linters.Missing
:
or Incorrect Namespace:
Solution: Double-check the syntax and namespace in your manifest files. This is a common issue that can break the deployment.
By following these steps, the stock prediction app is deployed in an automated and scalable way with Docker, Kubernetes, and ArgoCD. The application is continuously deployed and managed via GitOps, ensuring that updates to the codebase are automatically reflected in the production environment.
Subscribe to my newsletter
Read articles from Harshitha G M directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
