Kubernetes for Python Apps

Loading

Kubernetes (K8s) is an open-source container orchestration system that helps deploy, manage, and scale containerized applications. In this guide, we’ll learn how to deploy a Python application on Kubernetes.

What You Will Learn

Why use Kubernetes for Python applications
Writing a Dockerfile for a Python app
Creating Kubernetes YAML files
Deploying a Python app on a Kubernetes cluster
Scaling and managing applications with Kubernetes


1. Why Use Kubernetes for Python Applications?

Kubernetes automates deployment, scaling, and management of containerized applications.

Benefits

Scalability – Easily scale applications up or down
High Availability – Automatically restarts failed containers
Load Balancing – Distributes traffic efficiently
Self-Healing – Detects and replaces unhealthy containers


2. Setting Up Kubernetes for Python Applications

2.1 Prerequisites

✔ Install Docker and Kubernetes (kubectl, minikube or a cloud Kubernetes service)

Start Minikube (for local development):

minikube start

3. Writing a Python Application and Dockerizing It

3.1 Example Python App (app.py)

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
return "Hello, Kubernetes!"

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

3.2 Create a requirements.txt File

flask

3.3 Writing the Dockerfile

# Use Python as base image
FROM python:3.9

# Set working directory
WORKDIR /app

# Copy application files
COPY . .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Expose the application port
EXPOSE 5000

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

3.4 Build and Push the Docker Image

  1. Build the Image docker build -t my-python-app .
  2. Tag and Push to Docker Hub docker tag my-python-app username/my-python-app docker push username/my-python-app

4. Deploying to Kubernetes

We need three key components:
Deployment – Defines how the app runs and updates
Service – Exposes the application to the network
Ingress (optional) – Manages external access

4.1 Creating Deployment YAML (deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
name: python-app
spec:
replicas: 2
selector:
matchLabels:
app: python-app
template:
metadata:
labels:
app: python-app
spec:
containers:
- name: python-app
image: username/my-python-app
ports:
- containerPort: 5000

4.2 Creating a Service YAML (service.yaml)

apiVersion: v1
kind: Service
metadata:
name: python-app-service
spec:
selector:
app: python-app
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer

5. Deploying to Kubernetes

5.1 Apply the Deployment and Service

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

5.2 Verify the Deployment and Service

kubectl get pods
kubectl get services

6. Scaling the Application

6.1 Scale Up to 5 Replicas

kubectl scale deployment python-app --replicas=5

6.2 Verify Scaling

kubectl get pods

7. Managing Kubernetes Applications

7.1 View Logs

kubectl logs <pod-name>

7.2 Describe a Pod

kubectl describe pod <pod-name>

7.3 Delete Deployment and Service

kubectl delete -f deployment.yaml
kubectl delete -f service.yaml

Leave a Reply

Your email address will not be published. Required fields are marked *