What is Kubernetes?
Kubernetes (often abbreviated as K8s) is an open-source platform for automating the deployment, scaling, and management of containerized applications. Originally developed by Google and now maintained by the Cloud Native Computing Foundation (CNCF).
Think of Kubernetes as a conductor for an orchestra. Each musician (container) knows how to play their instrument, but the conductor (Kubernetes) ensures everyone plays together harmoniously, at the right time, and adapts if someone misses a beat.
Why Do We Need Kubernetes?
Docker lets you create containers, but what happens when you need to:
- Run hundreds or thousands of containers?
- Automatically restart crashed containers?
- Scale up during high traffic and down during low traffic?
- Deploy new versions without downtime?
- Distribute containers across multiple servers?
- Balance traffic between containers?
Kubernetes handles all of this automatically. It's the difference between manually managing each container versus having an intelligent system do it for you.
Kubernetes Architecture
┌─────────────────────────────────────────────────────────┐
│ KUBERNETES CLUSTER │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ CONTROL PLANE (Master) │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌────────┐ │ │
│ │ │API Server│ │Scheduler │ │Controller│ │ etcd │ │ │
│ │ │ │ │ │ │ Manager │ │ │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └────────┘ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ┌────────────────────────┴────────────────────────────┐ │
│ │ WORKER NODES │ │
│ │ ┌─────────────────┐ ┌─────────────────┐ │ │
│ │ │ Node 1 │ │ Node 2 │ │ │
│ │ │ ┌─────┐ ┌─────┐ │ │ ┌─────┐ ┌─────┐ │ │ │
│ │ │ │Pod 1│ │Pod 2│ │ │ │Pod 3│ │Pod 4│ │ │ │
│ │ │ └─────┘ └─────┘ │ │ └─────┘ └─────┘ │ │ │
│ │ │ kubelet │ │ kubelet │ │ │
│ │ └─────────────────┘ └─────────────────┘ │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
Control Plane (Master Node)
- API Server: The front door to Kubernetes. All commands go through here.
- Scheduler: Decides which node should run each pod.
- Controller Manager: Ensures the cluster state matches desired state.
- etcd: Key-value store holding all cluster data.
Worker Nodes
- Kubelet: Agent that ensures containers are running in pods.
- Container Runtime: Software that runs containers (Docker, containerd).
- Kube-proxy: Handles network rules for pod communication.
Key Kubernetes Concepts
Pod
The smallest deployable unit in Kubernetes. A pod can contain one or more containers that share storage and network resources.
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-python-app
labels:
app: web
spec:
containers:
- name: python-app
image: my-python-app:1.0
ports:
- containerPort: 5000
Deployment
Manages pod replicas and handles updates. You usually create Deployments, not individual Pods.
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-app-deployment
spec:
replicas: 3 # Run 3 copies of the app
selector:
matchLabels:
app: python-app
template:
metadata:
labels:
app: python-app
spec:
containers:
- name: python-app
image: my-python-app:1.0
ports:
- containerPort: 5000
resources:
limits:
memory: "256Mi"
cpu: "500m"
Service
Exposes pods to network traffic. Provides stable IP and DNS name even as pods change.
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: python-app-service
spec:
selector:
app: python-app
ports:
- port: 80 # Service port
targetPort: 5000 # Pod port
type: LoadBalancer # Exposes externally
ConfigMap and Secrets
Store configuration data and sensitive information separately from containers.
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DATABASE_HOST: "postgres-service"
LOG_LEVEL: "INFO"
---
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
DATABASE_PASSWORD: cGFzc3dvcmQxMjM= # base64 encoded
Essential kubectl Commands
# Cluster Information
kubectl cluster-info
kubectl get nodes
# Working with Pods
kubectl get pods
kubectl get pods -o wide # More details
kubectl describe pod
kubectl logs
kubectl exec -it -- bash
# Working with Deployments
kubectl get deployments
kubectl create -f deployment.yaml
kubectl apply -f deployment.yaml # Create or update
kubectl delete deployment
kubectl scale deployment --replicas=5
# Working with Services
kubectl get services
kubectl expose deployment --port=80 --type=LoadBalancer
# Debugging
kubectl describe
kubectl logs -f # Follow logs
kubectl get events --sort-by='.lastTimestamp'
# Common shortcuts
kubectl get all # See pods, services, deployments
kubectl delete -f # Delete resources from file
Deploying a Python App to Kubernetes
Complete example of deploying a Python application:
Step 1: Create Dockerfile
# Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]
Step 2: Build and Push Image
# Build image
docker build -t myregistry/python-app:1.0 .
# Push to registry
docker push myregistry/python-app:1.0
Step 3: Create Kubernetes Manifests
# k8s/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-app
spec:
replicas: 3
selector:
matchLabels:
app: python-app
template:
metadata:
labels:
app: python-app
spec:
containers:
- name: python-app
image: myregistry/python-app:1.0
ports:
- containerPort: 5000
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: app-secrets
readinessProbe:
httpGet:
path: /health
port: 5000
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /health
port: 5000
initialDelaySeconds: 15
periodSeconds: 20
---
# k8s/service.yaml
apiVersion: v1
kind: Service
metadata:
name: python-app-service
spec:
selector:
app: python-app
ports:
- port: 80
targetPort: 5000
type: LoadBalancer
Step 4: Deploy to Kubernetes
# Apply all manifests
kubectl apply -f k8s/
# Check status
kubectl get pods
kubectl get services
# Watch pods come up
kubectl get pods -w
Scaling and Updates
Manual Scaling
# Scale to 5 replicas
kubectl scale deployment python-app --replicas=5
# Scale down to 2
kubectl scale deployment python-app --replicas=2
Horizontal Pod Autoscaler
# Autoscale based on CPU usage
kubectl autoscale deployment python-app --min=2 --max=10 --cpu-percent=80
# Or with YAML
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: 80
Rolling Updates
# Update image (triggers rolling update)
kubectl set image deployment/python-app python-app=myregistry/python-app:2.0
# Check rollout status
kubectl rollout status deployment/python-app
# View rollout history
kubectl rollout history deployment/python-app
# Rollback to previous version
kubectl rollout undo deployment/python-app
Local Development with Kubernetes
Options for running Kubernetes locally:
Minikube
# Install minikube (see minikube.sigs.k8s.io)
# Start cluster
minikube start
# Access dashboard
minikube dashboard
# Get service URL
minikube service python-app-service --url
Docker Desktop
Enable Kubernetes in Docker Desktop settings. Simple and works well on Mac/Windows.
Kind (Kubernetes in Docker)
# Install kind
# Create cluster
kind create cluster
# Delete cluster
kind delete cluster
Kubernetes Best Practices
- Use Deployments: Don't create bare Pods. Let Deployments manage them.
- Set resource limits: Always define CPU and memory limits.
- Use health checks: Implement liveness and readiness probes.
- Use namespaces: Organize resources by environment or team.
- Version your images: Never use :latest in production.
- Use ConfigMaps and Secrets: Externalize configuration.
- Implement monitoring: Use Prometheus and Grafana.
- Store manifests in Git: Practice GitOps for infrastructure.
Master Kubernetes with Expert Mentorship
Our Full Stack Python program covers container orchestration with Kubernetes. Learn to deploy, scale, and manage Python applications in production with personalized guidance.
Explore Full Stack Python Program