Learn Deployments, Scaling & ReplicaSets
Hands-On Lab Guide
In this lab, we'll deploy a full-stack MERN application to Kubernetes. The application consists of three microservices:
First, verify you can access the Kubernetes cluster:
kubectl get nodes
NAME STATUS ROLES AGE VERSION ip-10-1-1-10 Ready control-plane 1d v1.28.0 ip-10-1-1-20 Ready worker 1d v1.28.0
kubectl get pods -A
Clone the demo application repository:
git clone git@github.com:sasidatta/docker_demo.git
cd docker_demo/k8s
ssh-keygen and add it to your GitHub account.
Namespaces provide a virtual cluster within a physical cluster. They're useful for:
kubectl create namespace <your-name>
kubectl config set-context --current --namespace=<your-name>
--namespace with every command!
kubectl get namespaces
A Deployment is a higher-level abstraction that manages Pods through ReplicaSets. It provides:
kubectl apply -f frontend.yaml
kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE frontend 1/1 1 1 30s
kubectl apply -f backend.yaml
Scale your deployment dynamically:
kubectl scale deployment backend --replicas=2
kubectl get pods
NAME READY STATUS RESTARTS AGE backend-6b5f7b8889-abc12 1/1 Running 0 1m backend-6b5f7b8889-def34 1/1 Running 0 10s
A Pod is the smallest deployable unit in Kubernetes. Key concepts:
kubectl apply -f busybox.yaml
kubectl run nginx --image=nginx:latest --restart=Never
kubectl get pod busybox-pod -o yaml | grep -i restart
restartPolicy: Always restartCount: 1
Always - Always restart the container (default)OnFailure - Restart only if container exits with errorNever - Never restart the containerkubectl get pods -o wide
NAME READY STATUS IP NODE frontend-abc 1/1 Running 10.244.1.30 ip-10-1-1-20
kubectl delete pod busybox-pod nginx
| Feature | Pod | Deployment |
|---|---|---|
| Self-healing | ❌ No | ✅ Yes |
| Scaling | ❌ Manual | ✅ Automatic |
| Rolling updates | ❌ No | ✅ Yes |
| Use case | Testing, debugging | Production workloads |
ReplicaSets ensure a specified number of pod replicas are running at all times. They:
kubectl get rs
kubectl describe replicaset frontend-6b5f7b8889
app=frontend to identify which
pods they manage.
kubectl logs <pod-name>
kubectl describe pod <pod-name>
kubectl exec <pod-name> -- /bin/sh
kubectl exec -it <pod-name> -- /bin/sh
/bin/bash, use
/bin/sh instead!
On worker nodes, use crictl instead of docker:
crictl ps
crictl stop <container-id>
Cause: Cannot pull container image
Solution: Check image name, registry access, and credentials
Cause: Container keeps crashing
Solution: Check logs with kubectl logs
Cause: Cannot schedule pod (resources, node selector, etc.)
Solution: Use kubectl describe pod to see events
By default, all pods can communicate with each other. Use Network Policies to restrict traffic: