Managing one Docker container is easy. Managing hundreds across multiple servers? Thatβs a nightmare β unless you have an orchestra conductor.
This is where Kubernetes (K8s) comes in. Itβs the operating system for the cloud.
Introduction
- What Kubernetes actually solves (The βWhyβ)
- The full architecture: Control Plane vs. Worker Nodes
- Key objects: Pods, Deployments, Services
- How to write real YAML manifests
- 10 essential
kubectlcommands - A hands-on project: Deploy your first app
Imagine a massive orchestra with 100 musicians (Containers).
- Docker is the individual musician. They know how to play their instrument.
- Kubernetes is the Conductor.
The Conductor doesnβt play an instrument. Instead, they:
- Tell musicians when to start/stop (Scheduling)
- Replace a musician if they faint (Self-healing)
- Ensure the volume is balanced (Scaling)
Without the Conductor, you just have noise. With Kubernetes, you have a symphony.
Kubernetes Architecture
A Kubernetes cluster is split into two parts: The Brain (Control Plane) and The Muscle (Worker Nodes).
The Control Plane Components
| Component | Nickname | What It Does |
|---|---|---|
| API Server | The Front Desk | Everything talks here. kubectl sends commands here. |
| Scheduler | The Placement Agency | Decides which node runs which Pod. |
| Etcd | The Distributed Notebook | Stores the entire cluster state (replicated, fault-tolerant). |
| Controller Manager | The Watchdog | Continuously checks: βIs the current state = desired state?β |
The Worker Node Components
| Component | What It Does |
|---|---|
| Kubelet | The site manager. Ensures containers are running as instructed. |
| Kube-proxy | The network traffic cop. Routes traffic to the right Pod. |
| Container Runtime | The engine (e.g., containerd). Actually runs the containers. |
Key Concepts: The βK8s Objectsβ
1. Pod β The Atom
The smallest deployable unit. Usually contains one container.
Pods die. They can be killed by the scheduler, OOMKilled, or evicted. Never rely on a single Pod staying alive. Thatβs what Deployments are for.
2. Deployment β The Blueprint
Defines how many copies (replicas) of a Pod you want. Deployments self-heal β if a Pod dies, the Deployment restarts it.
# my-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3 # Run 3 copies at all times
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
3. Service β The Stable Phone Number
Pods get new IP addresses every time they restart. A Service gives them a stable address.
# my-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-nginx-svc
spec:
selector:
app: my-nginx # Routes to Pods with this label
ports:
- port: 80
targetPort: 80
type: LoadBalancer # Exposes externally (or NodePort for local dev)
- ClusterIP (default): Only accessible inside the cluster.
- NodePort: Exposes on each nodeβs IP + a static port (30000β32767).
- LoadBalancer: Provisions a cloud load balancer (AWS/GCP/Azure).
Setting Up: Minikube (Local Kubernetes)
Before running kubectl, you need a cluster. Minikube runs a single-node cluster on your laptop.
Start a local Kubernetes cluster (downloads K8s and starts a VM/container)
minikube start --driver=docker- β’--driver=docker uses Docker as the backend (no VM needed)
- β’First run takes 2-5 minutes to download
- β’minikube status to check if it's running
Check that your local cluster is running
minikube\ntype: Control Plane\nhost: Running\nkubelet: Running\napiserver: Running\nkubeconfig: Configured
Hands-On: Essential kubectl Commands
kubectl is your command-line remote control for the entire cluster.
1. Get Nodes
List all nodes in the cluster and their status
NAME STATUS ROLES AGE VERSION\nminikube Ready control-plane 5m v1.292. Apply a YAML Manifest
Create or update resources from a YAML file
deployment.apps/my-nginx created- β’The primary way to deploy in Kubernetes
- β’-f can point to a file OR a directory
- β’Re-run to update β Kubernetes diffs the change
3. Get All Resources
List all Pods, Services, Deployments, and ReplicaSets in the current namespace
- β’-n kube-system to see the system namespace
- β’--all-namespaces (-A) to see everything cluster-wide
4. Get Pods
List all running Pods and their status
NAME READY STATUS RESTARTS AGE\nmy-nginx-6d5f9c4b89-x4kzp 1/1 Running 0 1m- β’-o wide shows node IP and node name
- β’--watch (-w) streams live updates
5. Describe a Resource
Show detailed information about a Pod including events and error messages
kubectl describe [resource] [name]kubectl describe pod my-nginx-6d5f9c4b89-x4kzp- β’This is your #1 debugging tool
- β’The 'Events' section at the bottom shows WHY a pod failed
- β’Also works: kubectl describe deployment my-nginx
6. View Logs
Print the logs from a container in a Pod
kubectl logs my-nginx-6d5f9c4b89-x4kzp- β’-f = follow (live stream)
- β’--previous = logs from the crashed previous container
- β’-c [container-name] if the Pod has multiple containers
7. Execute a Command Inside a Pod
Open an interactive shell inside a running container
kubectl exec -it my-nginx-6d5f9c4b89-x4kzp -- /bin/sh- β’-it = interactive terminal
- β’Use /bin/bash if the container has bash
- β’Exit with 'exit' or Ctrl+D
8. Scale a Deployment
Change the number of running Pod replicas instantly
deployment.apps/my-nginx scaled- β’Scale to 0 to stop all Pods without deleting the Deployment
- β’Kubernetes adds/removes Pods within seconds
9. Port Forward (Local Access)
Forward a local port to a port on a Pod (for testing)
kubectl port-forward pod/my-nginx-6d5f9c4b89-x4kzp 8080:80- β’Open http://localhost:8080 to access the Pod directly
- β’Also works with services: kubectl port-forward svc/my-nginx-svc 8080:80
- β’Press Ctrl+C to stop forwarding
10. Delete a Resource
Delete all resources defined in a YAML file
deployment.apps/my-nginx deleted- β’Preferred over kubectl delete deployment my-nginx (matches what you created)
- β’Kubernetes gracefully stops Pods before deleting
Troubleshooting: Common Pod Statuses
| Status | Meaning | What To Do |
|---|---|---|
| Pending | No node has enough CPU/RAM | Check kubectl describe pod for resource errors |
| CrashLoopBackOff | App crashes immediately on start | kubectl logs --previous [pod] to see the crash reason |
| ImagePullBackOff | Canβt download the container image | Check image name spelling and registry access |
| OOMKilled | Container exceeded its memory limit | Increase resources.limits.memory in your YAML |
| Terminating (stuck) | Pod wonβt delete cleanly | kubectl delete pod [name] --force --grace-period=0 |
Hands-On Challenge: Deploy Your First App
Deploy a 3-replica Nginx app and access it from your browser.
Step 1: Start minikube
minikube start --driver=dockerStep 2: Create your deployment YAML (save as nginx-deploy.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
spec:
replicas: 3
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80Step 3: Apply it
kubectl apply -f nginx-deploy.yamlStep 4: Verify 3 Pods are Running
kubectl get podsStep 5: Access it via port-forward
kubectl port-forward deployment/nginx-app 8080:80Open http://localhost:8080 β you should see the Nginx welcome page!
Step 6: Scale to 5 replicas
kubectl scale deployment nginx-app --replicas=5
kubectl get pods --watchQuiz: Test Your Knowledge
Test Your Knowledge
Take a quick 4-question quiz to check your understanding.
Key Takeaways
- Kubernetes orchestrates containers β itβs the Conductor for your entire application.
- Control Plane manages state; Worker Nodes run workloads.
- Pods are ephemeral; Deployments keep them alive and at the right replica count.
- Services give Pods stable, load-balanced networking.
- kubectl apply -f is how you deploy; kubectl describe + kubectl logs is how you debug.
The Conductor (Kubernetes) ensures your application keeps playing, no matter what happens to the musicians. And you can now direct that orchestra with kubectl!
Next Steps
- Docker Compose β Multi-container apps for development
- AWS vs Azure vs GCP β Where to run your cluster in production
Found this helpful? Explore more in the Containers Hub!