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:

  1. Base Image: Using python:3.12-slim as our foundation

  2. Layer Caching: Optimizing build times with proper layer ordering

  3. Port Exposure: Making port 4000 accessible

  4. 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:

  1. Docker Image Building

  2. Image Push to Docker Hub

  3. Helm Chart Creation/Update

  4. Kubernetes Deployment

  5. 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:

  1. Docker installed and running

  2. Minikube or local Kubernetes cluster

  3. Helm CLI tool

  4. kubectl configured

  5. Docker Hub account

Running the Demo

  1. Clone and Navigate:

     cd Demo_K8s_Deploy
    
  2. Update Docker Username: Edit autodeploy.sh and replace MentionYourDockerHubUsername with your actual Docker Hub username.

  3. Make Script Executable:

     chmod +x autodeploy.sh
    
  4. Run the Automation:

     ./autodeploy.sh
    
  5. Access Your Application: Open http://127.0.0.1:4000 in your browser.

What Happens Behind the Scenes

  1. Docker Build: Creates a containerized version of your Python app

  2. Registry Push: Uploads the image to Docker Hub for distribution

  3. Helm Chart Generation: Creates Kubernetes manifests

  4. Kubernetes Deployment: Schedules and runs your application

  5. 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

  1. Image Tagging: Using semantic versioning (v1)

  2. Resource Cleanup: Removing previous deployments

  3. Health Checks: Liveness and readiness probes

  4. Port Configuration: Consistent port mapping across layers

  5. Error Handling: Graceful cleanup and status checking

Extending the Demo

This project can be extended in several ways:

  1. Multi-Environment Deployment: Different values for dev/staging/prod

  2. Database Integration: Add persistent storage with PersistentVolumes

  3. Load Balancing: Implement Ingress controllers

  4. Monitoring: Add Prometheus and Grafana

  5. 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

  1. Explore Kubernetes Concepts: Learn about pods, services, deployments, and more

  2. Master Helm: Understand chart templating and value management

  3. Implement CI/CD: Integrate with GitHub Actions or Jenkins

  4. Add Monitoring: Implement observability with Prometheus and Grafana

  5. 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!

0
Subscribe to my newsletter

Read articles from Rajeswaran Devarajan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajeswaran Devarajan
Rajeswaran Devarajan