3 Basics of Kubernetes
The master reference guide for Kubernetes basics โ architecture, core objects, workflows, and the command cheat sheet you'll actually use.
This is the Master Reference Guide. I have condensed everything we covered into a visual flow, theory recap, and a command cheat sheet.
Bookmark this. It is your roadmap from "Zero" to "Engineer."
๐๏ธ 1. The Big Picture: Architecture
Before typing commands, you must visualize where they go.
The Theory:
- Cluster: The whole system (The Port).
- Control Plane: The Brain. It makes decisions (scheduling, detecting crashes).
- Worker Node: The Ship. It provides CPU/RAM to run your apps.
- Pod: The Container. The smallest unit. Your code lives here.
๐งฑ 2. The Core Objects (The "Lego Blocks")
Kubernetes uses specific objects to manage your software. Here is how they connect.
| Object | Analogy | Responsibility |
|---|---|---|
| Deployment | The Manager ๐ท | Ensures X copies of your app are always running. Restarts them if they crash. |
| Service | The Phone # ๐ | Provides a stable IP address so others can find the Pods (which move around). |
| Ingress | The Receptionist ๐๏ธ | Routes traffic from domain.com to the correct Service. |
| PVC | External Drive ๐พ | Persistent storage that survives when a Pod dies. |
| ConfigMap | Sticky Note ๐ | Non-sensitive settings (URLs, Colors). |
| Secret | The Safe ๐ | Sensitive settings (Passwords, API Keys). |
The "Magic Glue": Labels & Selectors
The Service finds the Pod using "stickers" (Labels).
- Pod has Label:
app: my-app - Service has Selector:
app: my-app
๐ 3. The "Zero to Hero" Workflow (The Flowchart)
This is the exact process we followed to deploy your custom Python app.
Step 1: Code & Build
- Write code (
app.py). - Write
Dockerfile. - Build Image:
docker build -t my-app:v1 .
Step 2: The Minikube Bridge (Crucial for Local Dev)
- Since Minikube is a separate VM, you must move the image inside.
- Command:
minikube image load my-app:v1
Step 3: Deploy (The "Order")
- Create Deployment (Pods).
- Create Service (Networking).
Step 4: Access (The "Tunnel")
- If using Ingress/LoadBalancer on Local, you need a tunnel.
- Command:
sudo -E minikube tunnel
๐ 4. The Ultimate Command Cheat Sheet
Copy-paste these into your notes. These are the commands you will use 90% of the time.
Setup & Cluster Management
minikube start # Start the cluster
minikube status # Check if it's running
minikube dashboard # Open the GUI
minikube stop # Pause (save battery)
minikube delete # Nuke everything (start fresh)
Inspection (The "Get" Commands)
kubectl get nodes # Check your servers
kubectl get pods # Check your containers
kubectl get svc # Check your Services (IPs)
kubectl get deploy # Check your Deployments
kubectl get ingress # Check your URLs/Rules
Debugging (When Things Break)
kubectl describe pod <name> # Detailed error report (Why is it pending?)
kubectl logs <name> # See the app's internal logs (Print statements)
kubectl exec -it <name> -- sh # Hack into the container (SSH-like)
Action (Making Changes)
kubectl apply -f file.yaml # Create/Update objects from a file
kubectl delete -f file.yaml # Delete objects defined in a file
kubectl delete pod <name> # Kill a pod (to test auto-restart)
๐ 5. Advanced Mechanics: Autoscaling (HPA)
We mastered this in the final module.
The Loop:
- Metrics Server acts as the speedometer.
- HPA reads the speed (CPU %).
- If speed > Target (e.g., 50%), HPA tells Deployment to add replicas.
- If speed drops, HPA tells Deployment to remove replicas.
The HPA Setup Command:
kubectl autoscale deployment <name> --cpu-percent=50 --min=1 --max=10
๐ 6. The "Golden Rules" of Kubernetes
To truly master this, remember these three principles:
- Declarative > Imperative: Don't tell Kubernetes how to do it ("start server, then start app"). Tell it what you want ("I want 3 replicas"). It figures out the rest.
- Pods are Cattle, not Pets: Don't give them names. Don't nurse them back to health. If one is sick, shoot it and get a new one. (This is why we use Deployments).
- Labels are Everything: If your Service isn't working, 99% of the time it's because the Label on the Pod doesn't match the Selector on the Service.
๐ Next Step for You
You have the foundation. To turn this into a Company Service:
- Cloud: Try doing this on a real cloud provider (AWS EKS, Google GKE, or DigitalOcean). The
LoadBalancerwill give you a real Public IP, so you won't needminikube tunnel. - CI/CD: Learn how to make GitHub automatically build your Docker image and deploy it to Kubernetes whenever you push code.
You have done incredible work today. You went from "No Knowledge" to "Autoscaling Traffic" in one session. Be proud! ๐