From Zero to Kubernetes: A Complete DevOps Journey with Python, Docker, and Helm


Introduction
Welcome to my first blog post on Hashnode !!
As an SRE and DevOps enthusiast, I'm excited to share a practical demonstration of modern application deployment practices for buddies who are new to Kubernetes and DevOps skills. In this post, we'll explore a complete workflow that takes a simple Python Flask application deployment on Kubernetes using Docker and Helm using automation via shell scripting.
This project demonstrates several key DevOps concepts:
Containerization with Docker
Image management and distribution
Infrastructure as Code with Helm
Kubernetes deployment automation
CI/CD pipeline simulation
Project Overview
Our demo application is a simple Python Flask web service that returns a greeting message. While the application itself is basic, the deployment infrastructure showcases enterprise-grade practices that you'd use in real-world scenarios.
Project Structure
You can download the files from the below GitHub link.
https://github.com/drajeswaran-sre/SRE/tree/main/Demo_Webapp_Deploy_K8s
Demo_K8s_Deploy/
├── app.py # Python Flask application
├── Dockerfile # Docker image definition
├── autodeploy.sh # Automation script
The Application Layer
Let's start with our Python Flask application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello, Thanks you for using this application!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4000)
This is a minimal Flask application that:
Listens on all network interfaces (
0.0.0.0
)Runs on port 4000
Returns a simple greeting message
Containerization with Docker
Docker allows us to package our application with all its dependencies into a portable container. Here's our Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.12-slim
# Set the working directory in the container
WORKDIR /myapp
# Copy the current directory contents into the container at /app
COPY . /myapp
# Install Flask
RUN pip install Flask
# Make port 4000 available
EXPOSE 4000
# Run the application
CMD ["python", "app.py"]
Key Docker Concepts Demonstrated:
Base Image: Using
python:3.12-slim
as our foundationLayer Caching: Optimizing build times with proper layer ordering
Port Exposure: Making port 4000 accessible
Command Execution: Running our Python application
The Automation Script
The heart of our deployment pipeline is the autodeploy.sh
script. This bash script automates the entire process from building to deployment:
Script Workflow:
Docker Image Building
Image Push to Docker Hub
Helm Chart Creation/Update
Kubernetes Deployment
Port Forwarding for Access
Let's break down the key sections:
1. Variable Configuration
DOCKER_USERNAME="MentionYourDockerHubUsername"
IMAGE_NAME="webapp"
TAG="v1"
CHART_NAME="chart-app"
RELEASE_NAME="release-app"
2. Docker Operations
# Build the Docker Image
docker build -t $DOCKER_USERNAME/$IMAGE_NAME:$TAG .
# Push to Docker Hub
docker login
docker push $DOCKER_USERNAME/$IMAGE_NAME:$TAG
3. Helm Chart Management
The script creates a Helm chart if it doesn't exist and customizes it for our application:
image:
repository: $DOCKER_USERNAME/$IMAGE_NAME
tag: $TAG
pullPolicy: Always
service:
enabled: true
port: 4000
type: ClusterIP
4. Kubernetes Deployment
# Clean up previous releases
helm uninstall "$RELEASE_NAME"
# Deploy new release
helm install $RELEASE_NAME ./$CHART_NAME
5. Application Access
# Port forwarding for local access
kubectl port-forward pod/"$POD_NAME" "$PORT":"$PORT"
Key DevOps Concepts Demonstrated
1. Infrastructure as Code (IaC)
Helm charts define our Kubernetes resources declaratively
Version-controlled deployment configurations
Reproducible infrastructure
2. Container Orchestration
Kubernetes manages pod lifecycle
Automatic scaling and failover capabilities
Service discovery and load balancing
3. CI/CD Pipeline Simulation
Automated build and deployment process
Image versioning and tagging
Rollback capabilities with Helm
4. Security Best Practices
Non-root container execution
Resource limits and requests
Health checks (liveness and readiness probes)
Prerequisites
Before running this demo, ensure you have:
Docker installed and running
Minikube or local Kubernetes cluster
Helm CLI tool
kubectl configured
Docker Hub account
Running the Demo
Clone and Navigate:
cd Demo_K8s_Deploy
Update Docker Username: Edit
autodeploy.sh
and replaceMentionYourDockerHubUsername
with your actual Docker Hub username.Make Script Executable:
chmod +x autodeploy.sh
Run the Automation:
./autodeploy.sh
Access Your Application: Open
http://127.0.0.1:4000
in your browser.
What Happens Behind the Scenes
Docker Build: Creates a containerized version of your Python app
Registry Push: Uploads the image to Docker Hub for distribution
Helm Chart Generation: Creates Kubernetes manifests
Kubernetes Deployment: Schedules and runs your application
Service Exposure: Makes your app accessible via port forwarding
Monitoring and Debugging
Check Pod Status:
kubectl get pods
kubectl describe pod <pod-name>
View Logs:
kubectl logs <pod-name>
Check Services:
kubectl get services
Helm Release Status:
helm list
helm status <release-name>
Best Practices Demonstrated
Image Tagging: Using semantic versioning (
v1
)Resource Cleanup: Removing previous deployments
Health Checks: Liveness and readiness probes
Port Configuration: Consistent port mapping across layers
Error Handling: Graceful cleanup and status checking
Extending the Demo
This project can be extended in several ways:
Multi-Environment Deployment: Different values for dev/staging/prod
Database Integration: Add persistent storage with PersistentVolumes
Load Balancing: Implement Ingress controllers
Monitoring: Add Prometheus and Grafana
Security: Implement RBAC and network policies
Common Issues and Solutions
1. Port Already in Use
# Kill existing port-forward processes
pkill -f "kubectl port-forward"
2. Docker Build Failures
Ensure Docker daemon is running
Check internet connectivity for base image pulls
Verify Dockerfile syntax
3. Helm Installation Issues
Check Kubernetes cluster status
Verify Helm is properly configured
Ensure sufficient cluster resources
4. Image Pull Errors
Verify Docker Hub credentials
Check image name and tag
Ensure image exists in registry
Conclusion
This demo project showcases the power of modern DevOps practices:
Automation reduces manual errors and deployment time
Containerization ensures consistency across environments
Kubernetes provides robust orchestration and scaling
Helm simplifies complex deployments with templating
The combination of these technologies creates a robust, scalable, and maintainable deployment pipeline that's essential for modern application development.
Next Steps
Explore Kubernetes Concepts: Learn about pods, services, deployments, and more
Master Helm: Understand chart templating and value management
Implement CI/CD: Integrate with GitHub Actions or Jenkins
Add Monitoring: Implement observability with Prometheus and Grafana
Security Hardening: Implement RBAC, network policies, and secrets management
Resources
Happy DevOps-ing! 🚀
Feel free to reach out if you have questions or want to discuss SRE, Kubernetes, or DevOps topics. Let's build robust, scalable systems together!
Subscribe to my newsletter
Read articles from Rajeswaran Devarajan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
