Modern microservices and cloud-native applications rely on containerization for consistent, reproducible environments. While Docker packages code, runtimes, and dependencies into lightweight images, Kubernetes (K8s) provides the enterprise orchestration layer required to scale, self-heal, load balance, and roll out zero-downtime updates across hundreds of servers.
This tutorial provides a hands-on foundation in containerization, multi-stage Dockerfiles, core Kubernetes objects, production YAML manifests, and practical kubectl workflows.
1. Docker Fundamentals & Multi-Stage Builds
Unlike traditional Virtual Machines that virtualize the underlying hardware via a Hypervisor (running full guest OS instances), Docker containers share the host Linux kernel. This makes containers start in milliseconds while consuming a fraction of the memory footprint.
Production Multi-Stage Dockerfile Best Practice:
Multi-stage builds allow you to compile your application inside a heavy build container and copy only the compiled binary or runtime assets into a tiny, secure base image (like Alpine or Distroless):
# Stage 1: Build & Dependency Resolution
FROM python:3.12-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt
# Stage 2: Minimal Production Runtime
FROM python:3.12-alpine
WORKDIR /app
# Copy dependencies from builder stage
COPY --from=builder /root/.local /root/.local
COPY ./src ./src
ENV PATH=/root/.local/bin:$PATH
EXPOSE 8080
CMD ["python", "src/main.py"]
2. Kubernetes Architecture: Control Plane vs Worker Nodes
A Kubernetes cluster is divided into two primary logical planes:
- Control Plane: Manages the overall cluster state.
kube-apiserver: The frontend REST API gateway.etcd: Highly available key-value store containing cluster state.kube-scheduler: Assigns newly created Pods to healthy Worker Nodes.kube-controller-manager: Runs controller loops (e.g., NodeController, ReplicaSetController).
- Worker Nodes: Execute the containerized application workloads.
kubelet: Agent ensuring containers are running in Pods as defined.kube-proxy: Maintains network routing rules across worker nodes.Container Runtime: (e.g., containerd, CRI-O) to pull images and run containers.
Ready to Master Enterprise Kubernetes?
Get our complete 200-page Practical Kubernetes Study Guide with hands-on labs, Helm chart tutorials, Ingress TLS setups, and production manifests.
🛒 Download Full Kubernetes PDF Guide ($5 USD) →3. Core Kubernetes Objects & Production YAML Manifests
In Kubernetes, we declare desired infrastructure states using declarative YAML manifests. Below is a complete production manifest featuring a Deployment with health checks (Liveness & Readiness probes) and a ClusterIP Service:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-api-deployment
labels:
app: web-api
spec:
replicas: 3
selector:
matchLabels:
app: web-api
template:
metadata:
labels:
app: web-api
spec:
containers:
- name: api-container
image: ithubofficial/web-api:v1.2.0
ports:
- containerPort: 8080
resources:
limits:
memory: "512Mi"
cpu: "500m"
requests:
memory: "256Mi"
cpu: "250m"
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: web-api-service
spec:
type: ClusterIP
selector:
app: web-api
ports:
- port: 80
targetPort: 8080
4. Essential Kubectl Command Toolkit
DevOps and Cloud Engineers interact with clusters daily through the kubectl CLI:
# 1. Apply declarative manifests
$ kubectl apply -f deployment.yaml
# 2. Inspect running Pods with node placements & IPs
$ kubectl get pods -o wide
# 3. Stream real-time container application logs
$ kubectl logs -f deployment/web-api-deployment
# 4. Open an interactive shell inside a container
$ kubectl exec -it pod-name -- /bin/sh
# 5. Perform zero-downtime rolling update & rollback
$ kubectl set image deployment/web-api-deployment api-container=ithubofficial/web-api:v2.0.0
$ kubectl rollout status deployment/web-api-deployment
$ kubectl rollout undo deployment/web-api-deployment
Summary & Production Cloud Strategy
When running Kubernetes on managed cloud providers (AWS EKS, Google GKE, Azure AKS), always enforce Resource Limits (CPU & RAM), configure Pod Disruption Budgets (PDBs), and implement Horizontal Pod Autoscalers (HPA) to scale dynamically with incoming user traffic.