What is Kubernetes and Why Do You Need It?
When you run containers in production, you face problems: How do you automatically restart failed containers? How do you scale from 2 to 20 instances during a traffic spike? How do you roll out a new version with zero downtime? Kubernetes solves all of this.
What Kubernetes Gives You
- Self-healing — automatic restart of failed containers
- Auto-scaling — scale pods based on CPU/memory metrics
- Rolling deployments — zero-downtime updates
- Service discovery & load balancing — built-in
- Secret & config management — keep credentials out of code
Core Concepts You Must Understand
Pod: The smallest deployable unit in K8s. A Pod wraps one or more containers that share networking and storage.
Deployment: Manages a set of identical Pods. Handles rolling updates and rollbacks automatically.
Service: Provides a stable network endpoint (IP + DNS name) to a set of Pods — even as Pods come and go.
Namespace: Virtual cluster within a cluster. Use them to isolate environments (dev, staging, prod) on the same cluster.
ConfigMap / Secret: Inject configuration and credentials into Pods without hardcoding them.
Your First Deployment (Real YAML)
Let's deploy a simple Node.js web app. Create deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: production
labels:
app: myapp
spec:
replicas: 3 # Run 3 instances
selector:
matchLabels:
app: myapp
strategy:
type: RollingUpdate # Zero-downtime updates
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: yourregistry/myapp:v1.2.0
ports:
- containerPort: 3000
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
readinessProbe: # Only send traffic when ready
httpGet:
path: /health
port: 3000
initialDelaySeconds: 10
periodSeconds: 5
livenessProbe: # Restart if unhealthy
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
env:
- name: NODE_ENV
value: production
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: myapp-secrets
key: db-password
Expose Your App with a Service
apiVersion: v1
kind: Service
metadata:
name: myapp-service
namespace: production
spec:
selector:
app: myapp # Routes to Pods with this label
ports:
- port: 80
targetPort: 3000
type: ClusterIP # Internal only; use LoadBalancer for external
resources.requests and resources.limits on every container. Without them, a single misbehaving Pod can starve the entire node of CPU/memory.Apply & Verify Your Deployment
# Apply the deployment
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
# Check deployment status
kubectl rollout status deployment/myapp -n production
# See your pods running
kubectl get pods -n production -l app=myapp
# View logs from a pod
kubectl logs -f deployment/myapp -n production
# Check resource usage
kubectl top pods -n production
Rolling Update with Zero Downtime
# Update the image version
kubectl set image deployment/myapp \
myapp=yourregistry/myapp:v1.3.0 \
-n production
# Watch the rollout
kubectl rollout status deployment/myapp -n production
# Rollback if something goes wrong
kubectl rollout undo deployment/myapp -n production
Auto-Scaling with HPA
Horizontal Pod Autoscaler (HPA) automatically adds/removes pods based on metrics:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: myapp-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
Common Beginner Mistakes
- No resource limits — always set CPU and memory limits
- Missing health probes — without readiness probes, traffic hits starting containers
- Using latest tag — always use specific image versions for reproducibility
- Storing secrets in ConfigMaps — use Secrets (or External Secrets Operator) for sensitive data
- Running as root — set
securityContext.runAsNonRoot: true