Kubernetes Orchestration: Deploying Python Applications at Scale
Kubernetes automates deployment, scaling, and management of containerized applications. It’s essential for production environments requiring high availability and scalability.
Kubernetes Fundamentals
Core Concepts
- Pod: Smallest deployable unit, contains one or more containers
- Deployment: Manages replicas of pods
- Service: Exposes pods to network
- ConfigMap: Configuration data
- Secret: Sensitive data
- Namespace: Virtual cluster
Installation
# Install kubectl
# macOS: brew install kubectl
# Ubuntu: sudo apt-get install kubectl
# Windows: choco install kubernetes-cli
# Verify installation
kubectl version --client
# Install Minikube for local development
brew install minikube
minikube start
Kubernetes Manifests
Pod Definition
apiVersion: v1
kind: Pod
metadata:
name: python-app-pod
labels:
app: python-app
spec:
containers:
- name: app
image: my-python-app:1.0
ports:
- containerPort: 8000
env:
- name: ENVIRONMENT
value: "production"
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-app
labels:
app: python-app
spec:
replicas: 3
selector:
matchLabels:
app: python-app
template:
metadata:
labels:
app: python-app
spec:
containers:
- name: app
image: my-python-app:1.0
ports:
- containerPort: 8000
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secret
key: url
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: log_level
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /health
port: 8000
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8000
initialDelaySeconds: 5
periodSeconds: 5
Service
apiVersion: v1
kind: Service
metadata:
name: python-app-service
spec:
type: LoadBalancer
selector:
app: python-app
ports:
- protocol: TCP
port: 80
targetPort: 8000
ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
log_level: "INFO"
debug_mode: "false"
app_config.yaml: |
database:
pool_size: 10
cache:
ttl: 3600
Secret
apiVersion: v1
kind: Secret
metadata:
name: db-secret
type: Opaque
stringData:
url: "postgresql://user:password@db:5432/mydb"
username: "user"
password: "password"
kubectl Commands
Deployment Management
# Apply manifest
kubectl apply -f deployment.yaml
# View deployments
kubectl get deployments
kubectl get deployment python-app -o yaml
# Describe deployment
kubectl describe deployment python-app
# Update deployment
kubectl set image deployment/python-app app=my-python-app:2.0
# Scale deployment
kubectl scale deployment python-app --replicas=5
# Rollout history
kubectl rollout history deployment/python-app
# Rollback to previous version
kubectl rollout undo deployment/python-app
# Delete deployment
kubectl delete deployment python-app
Pod Management
# List pods
kubectl get pods
kubectl get pods -o wide
# Describe pod
kubectl describe pod python-app-pod
# View pod logs
kubectl logs python-app-pod
kubectl logs -f python-app-pod # Follow logs
# Execute command in pod
kubectl exec -it python-app-pod -- /bin/bash
# Port forward
kubectl port-forward python-app-pod 8000:8000
# Copy files
kubectl cp python-app-pod:/app/file.txt ./file.txt
Service Management
# List services
kubectl get services
# Describe service
kubectl describe service python-app-service
# Get service endpoints
kubectl get endpoints python-app-service
# Delete service
kubectl delete service python-app-service
Configuration Management
# Create ConfigMap from file
kubectl create configmap app-config --from-file=config.yaml
# Create Secret from file
kubectl create secret generic db-secret --from-file=db-password.txt
# View ConfigMap
kubectl get configmap app-config -o yaml
# Edit ConfigMap
kubectl edit configmap app-config
# Delete ConfigMap
kubectl delete configmap app-config
Advanced Kubernetes Features
StatefulSet for Stateful Applications
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
spec:
serviceName: postgres
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:15
ports:
- containerPort: 5432
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: postgres-storage
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi
Horizontal Pod Autoscaler
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: python-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: python-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
Ingress for HTTP Routing
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: python-app-ingress
spec:
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: python-app-service
port:
number: 80
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: python-api-service
port:
number: 8000
Python Kubernetes Client
from kubernetes import client, config, watch
import os
# Load cluster config
config.load_incluster_config() # In-cluster
# or
config.load_kube_config() # Local development
# Create API client
v1 = client.CoreV1Api()
apps_v1 = client.AppsV1Api()
# List pods
pods = v1.list_namespaced_pod(namespace="default")
for pod in pods.items:
print(f"Pod: {pod.metadata.name}")
# Create pod
pod_manifest = {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {"name": "python-app-pod"},
"spec": {
"containers": [{
"name": "app",
"image": "my-python-app:1.0",
"ports": [{"containerPort": 8000}]
}]
}
}
v1.create_namespaced_pod(namespace="default", body=pod_manifest)
# Get pod logs
logs = v1.read_namespaced_pod_log(
name="python-app-pod",
namespace="default"
)
print(logs)
# Watch pod events
w = watch.Watch()
for event in w.stream(v1.list_namespaced_pod, namespace="default"):
print(f"Event: {event['type']} {event['object'].metadata.name}")
Deployment Strategies
Rolling Update
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-app
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
# ... rest of spec
Blue-Green Deployment
# Deploy blue version
kubectl apply -f deployment-blue.yaml
# Deploy green version
kubectl apply -f deployment-green.yaml
# Switch traffic to green
kubectl patch service python-app-service -p '{"spec":{"selector":{"version":"green"}}}'
# Delete blue version
kubectl delete deployment python-app-blue
Best Practices
- Resource requests/limits: Always define CPU and memory requests
- Health checks: Implement liveness and readiness probes
- Logging: Use centralized logging (ELK, Splunk)
- Monitoring: Monitor metrics with Prometheus/Grafana
- Security: Use RBAC, network policies, and secrets
- Namespaces: Organize resources with namespaces
- GitOps: Use version control for manifests
Common Pitfalls
Bad Practice:
# Don't: No resource limits
spec:
containers:
- name: app
image: my-app:1.0
# Don't: No health checks
# (no livenessProbe or readinessProbe)
# Don't: Single replica
replicas: 1
Good Practice:
# Do: Define resource limits
spec:
containers:
- name: app
image: my-app:1.0
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
# Do: Include health checks
livenessProbe:
httpGet:
path: /health
port: 8000
# Do: Multiple replicas
replicas: 3
Conclusion
Kubernetes provides powerful container orchestration for production Python applications. Master deployments, services, and scaling to build resilient, scalable systems. Start with Minikube locally, then deploy to cloud providers like AWS EKS, Google GKE, or Azure AKS.
Comments