Docker & kubernetes (k8s) to deploy a python

Posted by William(王明高) Blog on March 9, 2025
Here's an integration guide for Python, Docker, and Kubernetes (k8s) to deploy a Python application:

1. Python Application

Start with a simple Flask application (app.py):

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello from Python in Kubernetes!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

Requirements: Create a requirements.txt file:

flask==3.0.0

2. Docker Containerization

Create a Dockerfile to package the app:

# Use a lightweight Python base image
FROM python:3.11-alpine

# Set working directory
WORKDIR /app

# Copy requirements and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Expose port 5000 (Flask default)
EXPOSE 5000

# Run the application
CMD ["python", "app.py"]

Build the Docker Image:

docker build -t python-app:1.0 .

Run Locally:

docker run -p 5000:5000 python-app:1.0

3. Kubernetes Deployment

Create Kubernetes manifests:

Deployment (deployment.yaml)

Manages the app’s pods and scaling.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-app
spec:
  replicas: 3  # Number of pods
  selector:
    matchLabels:
      app: python-app
  template:
    metadata:
      labels:
        app: python-app
    spec:
      containers:
      - name: python-app
        image: python-app:1.0  # Use your Docker image
        ports:
        - containerPort: 5000
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "200m"

Service (service.yaml)

Exposes the app to external traffic.

apiVersion: v1
kind: Service
metadata:
  name: python-app-service
spec:
  type: LoadBalancer  # Use NodePort for local clusters
  selector:
    app: python-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 5000

Deploy to Kubernetes:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

Check Deployment:

kubectl get pods
kubectl get services

Key Concepts

  • Docker: Containerizes the app for consistency across environments.
  • Kubernetes: Orchestrates containers, handles scaling, self-healing, and load balancing.
  • Pod: Smallest deployable unit in Kubernetes (1+ containers).
  • Deployment: Manages pod replicas and updates.
  • Service: Provides network access to pods.

Best Practices

  1. Image Optimization Use small base images (e.g., python:alpine).
  2. Health Checks Add liveness/readiness probes to your Kubernetes deployment:

    livenessProbe:
      httpGet:
        path: /
        port: 5000
      initialDelaySeconds: 5
      periodSeconds: 10
    
  3. Environment Variables Use ConfigMap or Secrets for configuration:

    env:
    - name: FLASK_ENV
      value: "production"
    
  4. CI/CD Pipeline Automate building and deploying with tools like GitHub Actions, Jenkins, or ArgoCD.

Local Testing with Minikube

  1. Start Minikube:

    minikube start
    
  2. Load Docker image into Minikube:

    minikube image load python-app:1.0
    
  3. Access the app:

    minikube service python-app-service
    

This workflow allows you to develop a Python app locally, package it with Docker, and deploy it at scale using Kubernetes.