This project demonstrates how to run a Python-based Flask web application that connects to a MySQL database using Docker, Kubernetes (Minikube), and Helm for deployment.
- Python Flask app that connects to a MySQL database.
- Automatically creates a
test_databaseif it doesn't exist. - Helm chart for deploying the app to Kubernetes.
To run this project locally or in a Kubernetes cluster, you will need the following:
.
├── Dockerfile
├── README.md
├── app.py
├── docker-compose.yml
├── mysql-deployment.yaml
├── namespace.yaml
├── persistent-volume.yaml
├── python-app-deployment.yaml
├── python-mysql-app
│ ├── Chart.yaml
│ ├── templates
│ │ ├── deployment.yaml
│ │ ├── service.yaml
│ │ └── tests
│ │ └── test-connection.yaml
│ └── values.yaml
└── storage-class.yaml
git clone https://github.com/your-username/python-flask-mysql-app.git
cd python-flask-mysql-appIf you want to run this app locally using Docker, use the following steps:
-
Build the Docker images and run the containers:
docker-compose up --build
-
Access the App:
Once the containers are running, open your browser and visit
http://localhost:8080. You should see the message:Hello, world! Connected to database: test_database
To deploy the app on a Kubernetes cluster using Minikube:
-
Start Minikube:
minikube start
-
Build Docker Images Inside Minikube:
Switch Docker environment to Minikube:
eval $(minikube docker-env) docker build -t rohithbathini97/python-flask-app:latest .
-
Deploy Using Helm:
-
First, install Helm (if not already installed):
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash -
Install the Helm chart:
helm upgrade --install python-mysql-app ./python-mysql-app --namespace python-mysql-app --create-namespace
-
-
Access the App:
Use Minikube to get the service URL:
minikube service python-mysql-app -n python-mysql-app
This will open the app in your browser. You should see the same output:
Hello, world! Connected to database: test_database
When you're done, you can clean up your Kubernetes resources and stop Minikube:
helm uninstall python-mysql-app -n python-mysql-app
minikube stopThis project uses GitHub Actions to automate the CI/CD pipeline. The following secrets are required for GitHub Actions to work:
DOCKER_USERNAME: Your Docker Hub username.DOCKER_PASSWORD: Your Docker Hub password or access token.KUBECONFIG_DATA: Base64-encoded kubeconfig for accessing your Kubernetes cluster.
- The CI/CD pipeline automatically builds the Docker image and pushes it to Docker Hub.
- The Helm chart is used to deploy the application to a Kubernetes cluster.
- On each commit or pull request, the pipeline will run and update the deployment.
- Fork this repository.
- Set the required secrets (
DOCKER_USERNAME,DOCKER_PASSWORD,KUBECONFIG_DATA) in your GitHub repository settings under Settings > Secrets. - Push any changes to the repository, and the GitHub Actions workflow will automatically trigger.
- Liveness and Readiness Probes: Configured in the deployment YAML to ensure that the application is healthy.
- Rolling Update Strategy: Ensures minimal downtime during deployment updates.